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),
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 }
void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) override
Method for drawing frame.
QFramedTextAttribute(QObject *parent=nullptr)
Constructor.
void frame(QTextCursor cursor)
Method for creating frame in cursor selection.
#define nullptr
Definition: backward.hpp:386
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 ...
static int type()
Static method for getting framed text attribute type.
static void block(LexState *ls)
Definition: lparser.c:1293
Class, that describes Qt style parser for QCodeEditor.
QTextCharFormat getFormat(QString name) const
Method for getting format for property name.
void clear(QTextCursor cursor)
Method for clearing all frames with desired cursor.
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style for rendering.
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
Definition: color.h:583


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38