forked from pbek/qmarkdowntextedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linenumberarea.h
124 lines (99 loc) · 3.14 KB
/
linenumberarea.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef LINENUMBERAREA_H
#define LINENUMBERAREA_H
#include <QDebug>
#include <QPainter>
#include <QScrollBar>
#include <QWidget>
#include "qmarkdowntextedit.h"
class LineNumArea final : public QWidget {
Q_OBJECT
public:
explicit LineNumArea(QMarkdownTextEdit *parent)
: QWidget(parent),
textEdit(parent)
{
Q_ASSERT(parent);
_currentLineColor = QColor(QStringLiteral("#eef067"));
_otherLinesColor = QColor(QStringLiteral("#a6a6a6"));
setHidden(true);
// We always use fixed font to avoid "width" issues
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
}
void setCurrentLineColor(QColor color) {
_currentLineColor = color;
}
void setOtherLineColor(QColor color) {
_otherLinesColor = std::move(color);
}
int lineNumAreaWidth() const
{
if (!enabled) {
return 0;
}
int digits = 2;
int max = std::max(1, textEdit->blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
#if QT_VERSION >= 0x050B00
int space = 13 + textEdit->fontMetrics().horizontalAdvance(u'9') * digits;
#else
int space = 13 + textEdit->fontMetrics().width(QLatin1Char('9')) * digits;
#endif
return space;
}
bool isLineNumAreaEnabled() const
{
return enabled;
}
void setLineNumAreaEnabled(bool e)
{
enabled = e;
setHidden(!e);
}
QSize sizeHint() const override
{
return {lineNumAreaWidth(), 0};
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
painter.fillRect(event->rect(), palette().color(QPalette::Active, QPalette::Window));
auto block = textEdit->firstVisibleBlock();
int blockNumber = block.blockNumber();
qreal top = textEdit->blockBoundingGeometry(block).translated(textEdit->contentOffset()).top();
// Maybe the top is not 0?
top += textEdit->viewportMargins().top();
qreal bottom = top;
const QPen currentLine = _currentLineColor;
const QPen otherLines = _otherLinesColor;
painter.setFont(font());
while (block.isValid() && top <= event->rect().bottom()) {
top = bottom;
bottom = top + textEdit->blockBoundingRect(block).height();
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
auto isCurrentLine = textEdit->textCursor().blockNumber() == blockNumber;
painter.setPen(isCurrentLine ? currentLine : otherLines);
painter.drawText(
-5,
top,
sizeHint().width(),
textEdit->fontMetrics().height(),
Qt::AlignRight,
number
);
}
block = block.next();
++blockNumber;
}
}
private:
bool enabled = false;
QMarkdownTextEdit *textEdit;
QColor _currentLineColor;
QColor _otherLinesColor;
};
#endif // LINENUMBERAREA_H