-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEditor.cpp
More file actions
70 lines (63 loc) · 2.87 KB
/
Copy pathCodeEditor.cpp
File metadata and controls
70 lines (63 loc) · 2.87 KB
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
#include "CodeEditor.h"
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
lineNumberArea = new LineNumberArea(this);
highlighter = new SyntaxHighlighter(document());
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
updateLineNumberAreaWidth(0);
QFont monoFont("Cascadia Code", 14);
monoFont.setStyleHint(QFont::Monospace);
setFont(monoFont);
setTabStopDistance(28);
setLineWrapMode(QPlainTextEdit::NoWrap);
}
int CodeEditor::lineNumberAreaWidth() {
int digits = 1, max = qMax(1, blockCount());
while (max >= 10) { max /= 10; ++digits; }
return 10 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * (digits + 1);
}
void CodeEditor::updateLineNumberAreaWidth(int) {
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) {
if (dy) lineNumberArea->scroll(0, dy);
else lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(0);
}
void CodeEditor::resizeEvent(QResizeEvent *e) {
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) {
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), currentTheme.lineNumberBg.isValid()
? currentTheme.lineNumberBg : QColor("#181825"));
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + qRound(blockBoundingRect(block).height());
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
painter.setPen(currentTheme.lineNumberFg.isValid()
? currentTheme.lineNumberFg : QColor("#585B70"));
painter.drawText(0, top, lineNumberArea->width()-4,
fontMetrics().height(), Qt::AlignRight,
QString::number(blockNumber + 1));
}
block = block.next();
top = bottom;
bottom = top + qRound(blockBoundingRect(block).height());
++blockNumber;
}
}
void CodeEditor::applyTheme(const Theme &theme) {
currentTheme = theme;
highlighter->applyTheme(theme);
setStyleSheet(QString(
"QPlainTextEdit{background-color:%1;color:%2;border:none;"
"selection-background-color:%3;"
"font-family:'Cascadia Code','Consolas',monospace;}"
).arg(theme.background.name(), theme.foreground.name(), theme.selectionBg.name()));
update();
}