QLineNumberArea.cpp
Go to the documentation of this file.
1 // QCodeEditor
2 #include <QLineNumberArea>
3 #include <QSyntaxStyle>
4 #include <QCodeEditor>
5 
6 // Qt
7 #include <QTextEdit>
8 #include <QPainter>
9 #include <QPaintEvent>
10 #include <QTextBlock>
11 #include <QScrollBar>
12 #include <QAbstractTextDocumentLayout>
13 
15  QWidget(parent),
16  m_syntaxStyle(nullptr),
17  m_codeEditParent(parent)
18 {
19 
20 }
21 
23 {
24  if (m_codeEditParent == nullptr)
25  {
26  return QWidget::sizeHint();
27  }
28 
29  // Calculating width
30  int digits = 1;
31  int max = qMax(1, m_codeEditParent->document()->blockCount());
32  while (max >= 10) {
33  max /= 10;
34  ++digits;
35  }
36 
37 #if QT_VERSION >= 0x050B00
38  int space = 13 + m_codeEditParent->fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
39 #else
40  int space = 13 + m_codeEditParent->fontMetrics().width(QLatin1Char('9')) * digits;
41 #endif
42 
43  return {space, 0};
44 }
45 
47 {
48  m_syntaxStyle = style;
49 }
50 
52 {
53  return m_syntaxStyle;
54 }
55 
56 void QLineNumberArea::paintEvent(QPaintEvent* event)
57 {
58  QPainter painter(this);
59 
60  // Clearing rect to update
61  painter.fillRect(
62  event->rect(),
63  m_syntaxStyle->getFormat("Text").background().color()
64  );
65 
66  auto blockNumber = m_codeEditParent->getFirstVisibleBlock();
67  auto block = m_codeEditParent->document()->findBlockByNumber(blockNumber);
68  auto top = (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).translated(0, -m_codeEditParent->verticalScrollBar()->value()).top();
69  auto bottom = top + (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).height();
70 
71  auto currentLine = m_syntaxStyle->getFormat("CurrentLineNumber").foreground().color();
72  auto otherLines = m_syntaxStyle->getFormat("LineNumber").foreground().color();
73 
74  painter.setFont(m_codeEditParent->font());
75 
76  while (block.isValid() && top <= event->rect().bottom())
77  {
78  if (block.isVisible() && bottom >= event->rect().top())
79  {
80  QString number = QString::number(blockNumber + 1);
81 
82  auto isCurrentLine = m_codeEditParent->textCursor().blockNumber() == blockNumber;
83  painter.setPen(isCurrentLine ? currentLine : otherLines);
84 
85  painter.drawText(
86  -5,
87  top,
88  sizeHint().width(),
89  m_codeEditParent->fontMetrics().height(),
90  Qt::AlignRight,
91  number
92  );
93  }
94 
95  block = block.next();
96  top = bottom;
97  bottom = top + (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).height();
98  ++blockNumber;
99  }
100 }
sol::stack::top
int top(lua_State *L)
Definition: sol.hpp:11684
QLineNumberArea::m_codeEditParent
QCodeEditor * m_codeEditParent
Definition: QLineNumberArea.hpp:53
QSyntaxStyle::getFormat
QTextCharFormat getFormat(QString name) const
Method for getting format for property name.
Definition: QSyntaxStyle.cpp:122
QLineNumberArea::m_syntaxStyle
QSyntaxStyle * m_syntaxStyle
Definition: QLineNumberArea.hpp:51
QLineNumberArea::setSyntaxStyle
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style object.
Definition: QLineNumberArea.cpp:46
QCodeEditor::getFirstVisibleBlock
int getFirstVisibleBlock()
Method for getting first visible block index.
Definition: QCodeEditor.cpp:378
QLineNumberArea::QLineNumberArea
QLineNumberArea(QCodeEditor *parent=nullptr)
Constructor.
Definition: QLineNumberArea.cpp:14
udp_client.int
int
Definition: udp_client.py:11
block
static void block(LexState *ls)
Definition: lparser.c:1293
QSyntaxStyle
Class, that describes Qt style parser for QCodeEditor.
Definition: QSyntaxStyle.hpp:13
QLineNumberArea::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QLineNumberArea.cpp:51
QLineNumberArea::paintEvent
void paintEvent(QPaintEvent *event) override
Definition: QLineNumberArea.cpp:56
nullptr
#define nullptr
Definition: backward.hpp:386
QCodeEditor
Class, that describes code editor.
Definition: QCodeEditor.hpp:15
QLineNumberArea::sizeHint
QSize sizeHint() const override
Overridden method for getting line number area size.
Definition: QLineNumberArea.cpp:22


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45