-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownedit.cpp
165 lines (134 loc) · 4.28 KB
/
markdownedit.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "markdownedit.h"
MarkdownEdit::MarkdownEdit(QWidget *parent)
: QTextEdit(parent)
{
lineNumberArea = new LineNumberArea(this);
connect(this->document(), &QTextDocument::contentsChanged, this, &MarkdownEdit::updateLineNumberArea);
connect(this, &MarkdownEdit::cursorPositionChanged, this, &MarkdownEdit::highlightCurrentLine);
defaultFontSize = font().pointSize();
updateLineNumberAreaWidth();
highlighter = new MarkdownHighlighter(this->document());
}
MarkdownEdit::MarkdownEdit(const QString &text, QWidget *parent)
: QTextEdit(text, parent)
{
lineNumberArea = new LineNumberArea(this);
connect(this->document(), &QTextDocument::contentsChanged, this, &MarkdownEdit::updateLineNumberArea);
connect(this, &MarkdownEdit::cursorPositionChanged, this, &MarkdownEdit::highlightCurrentLine);
defaultFontSize = font().pointSize();
updateLineNumberAreaWidth();
}
void MarkdownEdit::resizeEvent(QResizeEvent *event)
{
QTextEdit::resizeEvent(event);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberArea->lineNumberAreaWidth(), cr.height()));
}
void MarkdownEdit::updateLineNumberAreaWidth()
{
setViewportMargins(lineNumberArea->lineNumberAreaWidth(), 0, 0, 0);
}
void MarkdownEdit::updateLineNumberArea()
{
QRect rect = viewport()->rect();
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
{
updateLineNumberAreaWidth();
}
}
void MarkdownEdit::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly())
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::blue).lighter(130);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
QTextCursor cursor = textCursor();
cursor.clearSelection(); // Clear any existing selection
selection.cursor = cursor; // Assign the cursor to the selection
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void MarkdownEdit::scrollContentsBy(int dx, int dy)
{
QTextEdit::scrollContentsBy(dx, dy);
lineNumberArea->scroll(0, dy);
}
void MarkdownEdit::resetFontSize()
{
QFont f = font();
f.setPointSize(defaultFontSize);
setFont(f);
lineNumberArea->update();
updateLineNumberAreaWidth();
}
void MarkdownEdit::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() & Qt::ControlModifier)
{
int numDegrees = event->angleDelta().y() / 8;
int numSteps = numDegrees / 15;
QFont f = font();
int newSize = f.pointSize() + numSteps;
if (newSize > 4)
{
f.setPointSize(newSize);
setFont(f);
lineNumberArea->update();
updateLineNumberAreaWidth();
}
event->accept();
return;
}
QTextEdit::wheelEvent(event);
}
void MarkdownEdit::keyReleaseEvent(QKeyEvent *event)
{
if (event->modifiers() & Qt::ControlModifier)
{
if (event->key() == Qt::Key_Plus || event->key() == Qt::Key_Equal)
{
QFont f = font();
int newSize = f.pointSize() + 1;
f.setPointSize(newSize);
setFont(f);
lineNumberArea->update();
updateLineNumberAreaWidth();
event->accept();
return;
}
if (event->key() == Qt::Key_Minus || event->key() == Qt::Key_Underscore)
{
QFont f = font();
int newSize = f.pointSize() - 1;
if (newSize > 4)
{
f.setPointSize(newSize);
setFont(f);
lineNumberArea->update();
updateLineNumberAreaWidth();
}
event->accept();
return;
}
}
QTextEdit::keyPressEvent(event);
}
void MarkdownEdit::focusInEvent(QFocusEvent *event)
{
QTextEdit::focusInEvent(event);
highlightCurrentLine();
}
void MarkdownEdit::focusOutEvent(QFocusEvent *event)
{
QTextEdit::focusOutEvent(event);
clearLineHighlighting();
}
void MarkdownEdit::clearLineHighlighting()
{
setExtraSelections(QList<QTextEdit::ExtraSelection>()); // Clear all extra selections
}