QFramedTextAttribute.cpp
Go to the documentation of this file.
1 // QCodeEditor
2 #include <QFramedTextAttribute>
3 #include <QSyntaxStyle>
4 
5 // Qt
6 #include <QFontMetrics>
7 #include <QPainter>
8 #include <QDebug>
9 #include <QTextBlock>
10 
12 {
13  return QTextFormat::UserFormat + 1;
14 }
15 
17  QObject(parent),
18  m_style(nullptr)
19 {
20 
21 }
22 
24 {
25  m_style = style;
26 }
27 
29 {
30  return m_style;
31 }
32 
33 QSizeF QFramedTextAttribute::intrinsicSize(QTextDocument*, int, const QTextFormat&)
34 {
35  return {0, 0};
36 }
37 
38 void QFramedTextAttribute::drawObject(QPainter* painter,
39  const QRectF& rect,
40  QTextDocument*,
41  int,
42  const QTextFormat& format)
43 {
44  // Casting
45  auto textCharFormat = reinterpret_cast<const QTextCharFormat&>(format);
46 
47  // Getting font data
48  auto font = textCharFormat.font();
49  QFontMetrics metrics(font);
50 
51  // Getting required size
52  auto string = format.property(FramedString).toString();
53  auto stringSize = metrics.boundingRect(string).size();
54 
55  // Creating frame rect
56  QRectF drawRect(rect.topLeft(), stringSize);
57  drawRect.moveTop(rect.top() - stringSize.height());
58  drawRect.adjust(0, 4, 0, 4);
59 
60  // Drawing
61  painter->setPen(m_style->getFormat("Occurrences").background().color());
62  painter->setRenderHint(QPainter::Antialiasing);
63  painter->drawRoundedRect(drawRect, 4, 4);
64 }
65 
66 void QFramedTextAttribute::frame(QTextCursor cursor)
67 {
68  auto text = cursor.document()->findBlockByNumber(cursor.blockNumber()).text();
69 
70  QTextCharFormat format;
71  format.setObjectType(type());
72  format.setProperty(FramedString, cursor.selectedText());
73 
74  if (cursor.selectionEnd() > cursor.selectionStart())
75  {
76  cursor.setPosition(cursor.selectionStart());
77  }
78  else
79  {
80  cursor.setPosition(cursor.selectionEnd());
81  }
82 
83  cursor.insertText(
84  QString(QChar::ObjectReplacementCharacter),
85  format
86  );
87 }
88 
89 void QFramedTextAttribute::clear(QTextCursor cursor)
90 {
91  auto doc = cursor.document();
92 
93  for (auto blockIndex = 0;
94  blockIndex < doc->blockCount();
95  ++blockIndex)
96  {
97  auto block = doc->findBlockByNumber(blockIndex);
98 
99  auto formats = block.textFormats();
100  int offset = 0;
101 
102  for (auto& format : formats)
103  {
104  if (format.format.objectType() == type())
105  {
106  cursor.setPosition(block.position() + format.start - offset);
107  cursor.deleteChar();
108  ++offset;
109  }
110  }
111  }
112 }
QFramedTextAttribute::intrinsicSize
QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) override
Method for getting custom element (frame) size. Though frame symbol has no size, this method returns ...
Definition: QFramedTextAttribute.cpp:33
QFramedTextAttribute::setSyntaxStyle
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style for rendering.
Definition: QFramedTextAttribute.cpp:23
QFramedTextAttribute::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QFramedTextAttribute.cpp:28
QFramedTextAttribute::QFramedTextAttribute
QFramedTextAttribute(QObject *parent=nullptr)
Constructor.
Definition: QFramedTextAttribute.cpp:16
QFramedTextAttribute::frame
void frame(QTextCursor cursor)
Method for creating frame in cursor selection.
Definition: QFramedTextAttribute.cpp:66
QFramedTextAttribute::FramedString
@ FramedString
Definition: QFramedTextAttribute.hpp:23
QSyntaxStyle::getFormat
QTextCharFormat getFormat(QString name) const
Method for getting format for property name.
Definition: QSyntaxStyle.cpp:122
QFramedTextAttribute::drawObject
void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) override
Method for drawing frame.
Definition: QFramedTextAttribute.cpp:38
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
QFramedTextAttribute::type
static int type()
Static method for getting framed text attribute type.
Definition: QFramedTextAttribute.cpp:11
block
static void block(LexState *ls)
Definition: lparser.c:1293
QSyntaxStyle
Class, that describes Qt style parser for QCodeEditor.
Definition: QSyntaxStyle.hpp:13
QFramedTextAttribute::m_style
QSyntaxStyle * m_style
Definition: QFramedTextAttribute.hpp:86
nullptr
#define nullptr
Definition: backward.hpp:386
QFramedTextAttribute::clear
void clear(QTextCursor cursor)
Method for clearing all frames with desired cursor.
Definition: QFramedTextAttribute.cpp:89


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23