-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinglinemargin.cpp
More file actions
217 lines (192 loc) · 8.65 KB
/
winglinemargin.cpp
File metadata and controls
217 lines (192 loc) · 8.65 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/****************************************************************************
**
** Copyright (C) 2025-2028 WingSummer
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License version 3
** along with this program. If not, see <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "winglinemargin.h"
#include "wingcodeedit.h"
#include "wingsymbolcenter.h"
#include "wingsyntaxhighlighter.h"
#include <QPainter>
#include <QTextBlock>
QSize WingLineMargin::sizeHint() const {
return QSize(m_editor->lineMarginWidth(), 0);
}
void WingLineMargin::wheelEvent(QWheelEvent *e) { m_editor->wheelEvent(e); }
WingLineMargin::WingLineMargin(WingCodeEdit *editor)
: QWidget(editor), m_editor(editor), m_marginSelectStart(-1),
m_foldHoverLine(-1) {
setMouseTracking(true);
}
void WingLineMargin::paintEvent(QPaintEvent *paintEvent) {
if (!m_editor->showSymbolMark() && !m_editor->showLineNumbers() &&
!m_editor->showFolding())
return;
QPainter painter(this);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.fillRect(paintEvent->rect(), m_editor->m_lineMarginBg);
QTextBlock block = m_editor->firstVisibleBlock();
qreal top = m_editor->blockBoundingGeometry(block)
.translated(m_editor->contentOffset())
.top();
qreal bottom = top + m_editor->blockBoundingRect(block).height();
const QFontMetricsF metrics(font());
auto numWidth = metrics.boundingRect(QLatin1Char('0')).width();
const int foldPixmapWidth = m_editor->m_foldOpen.width() + 2;
const int symWidthOff =
m_editor->showSymbolMark() ? metrics.height() + 2 : 0;
const qreal numOffset = numWidth / 2.0 +
(m_editor->showFolding() ? foldPixmapWidth : 0) +
symWidthOff;
QTextCursor cursor = m_editor->textCursor();
while (block.isValid() && top <= paintEvent->rect().bottom()) {
if (block.isVisible()) {
if (m_editor->showSymbolMark()) {
auto symid = m_editor->highlighter()->symbolMarkID(block);
if (!symid.isEmpty()) {
auto sym =
WingSymbolCenter::instance().symbolFromName(symid);
if (!sym.isNull()) {
painter.drawPixmap(
QRect(3, top, metrics.height(), metrics.height()),
sym, sym.rect());
}
}
}
if (m_editor->showLineNumbers() &&
bottom >= paintEvent->rect().top()) {
const QString lineNum =
QString::number(block.blockNumber() + 1);
if (block.blockNumber() == cursor.blockNumber())
painter.setPen(m_editor->m_cursorLineNum);
else
painter.setPen(m_editor->m_lineMarginFg);
const QRectF numberRect(symWidthOff, top, width() - numOffset,
metrics.height());
painter.drawText(numberRect, Qt::AlignRight, lineNum);
}
if (m_editor->showFolding() &&
m_editor->m_highlighter->isFoldable(block)) {
const bool blockFolded = WingSyntaxHighlighter::isFolded(block);
if (block.blockNumber() == m_foldHoverLine) {
const int foldHighlightLeft =
width() - foldPixmapWidth -
(m_editor->showLineNumbers() ? 2 : 0);
QTextBlock endBlock =
m_editor->m_highlighter->findFoldEnd(block);
if (!endBlock.isValid())
endBlock = m_editor->document()->lastBlock();
const qreal foldHighlightBottom =
blockFolded ? bottom
: m_editor->blockBoundingGeometry(endBlock)
.translated(m_editor->contentOffset())
.bottom();
painter.fillRect(foldHighlightLeft, top, width(),
foldHighlightBottom - top,
m_editor->m_codeFoldingBg);
}
const QPixmap &foldPixmap =
blockFolded ? m_editor->m_foldClosed : m_editor->m_foldOpen;
painter.drawPixmap(
width() - foldPixmapWidth,
top + (metrics.height() - foldPixmap.height()) / 2,
foldPixmap);
}
}
block = block.next();
top = bottom;
bottom = top + m_editor->blockBoundingRect(block).height();
}
}
void WingLineMargin::mouseMoveEvent(QMouseEvent *e) {
const QPoint eventPos = e->pos();
QTextCursor lineCursor =
m_editor->cursorForPosition(QPoint(0, eventPos.y()));
const int foldPixmapWidth = m_editor->m_foldOpen.width() + 4;
m_foldHoverLine = -1;
if (m_editor->showFolding()) {
if (!m_editor->showLineNumbers() ||
eventPos.x() >= width() - foldPixmapWidth) {
QTextBlock block = lineCursor.block();
if (block.isValid() && m_editor->m_highlighter->isFoldable(block))
m_foldHoverLine = block.blockNumber();
}
update();
}
if ((e->buttons() & Qt::LeftButton) && m_marginSelectStart >= 0) {
const int linePosition = lineCursor.position();
lineCursor.setVisualNavigation(true);
lineCursor.setPosition(m_marginSelectStart);
if (linePosition >= m_marginSelectStart) {
lineCursor.setPosition(linePosition, QTextCursor::KeepAnchor);
lineCursor.movePosition(QTextCursor::NextBlock,
QTextCursor::KeepAnchor);
} else {
lineCursor.movePosition(QTextCursor::NextBlock);
lineCursor.setPosition(linePosition, QTextCursor::KeepAnchor);
}
m_editor->setTextCursor(lineCursor);
} else {
m_marginSelectStart = -1;
}
QWidget::mouseMoveEvent(e);
}
void WingLineMargin::mousePressEvent(QMouseEvent *e) {
m_marginSelectStart = -1;
if (e->button() == Qt::LeftButton) {
const QPoint eventPos = e->pos();
const int foldPixmapWidth = m_editor->m_foldOpen.width() + 4;
QTextCursor lineCursor =
m_editor->cursorForPosition(QPoint(0, eventPos.y()));
if (m_editor->showSymbolMark() &&
eventPos.x() < m_editor->realSymbolMarkSizeWithPadding()) {
emit symbolMarkLineMarginClicked(lineCursor.blockNumber() + 1);
} else if (m_editor->showLineNumbers() &&
(!m_editor->showFolding() ||
eventPos.x() < width() - foldPixmapWidth)) {
// Clicked in the number margin
lineCursor.setVisualNavigation(true);
lineCursor.movePosition(QTextCursor::StartOfBlock);
m_marginSelectStart = lineCursor.position();
lineCursor.movePosition(QTextCursor::NextBlock,
QTextCursor::KeepAnchor);
m_editor->setTextCursor(lineCursor);
} else if (m_editor->showFolding() &&
(!m_editor->showLineNumbers() ||
eventPos.x() >= width() - foldPixmapWidth)) {
// Clicked in the folding margin
QTextBlock block = lineCursor.block();
if (block.isValid() && m_editor->m_highlighter->isFoldable(block)) {
if (WingSyntaxHighlighter::isFolded(block))
m_editor->m_highlighter->unfoldBlock(block);
else
m_editor->m_highlighter->foldBlock(block);
m_editor->viewport()->update();
update();
m_editor->updateScrollBars();
// Move the editing cursor if it was in a folded block
QTextCursor cursor = m_editor->textCursor();
if (!cursor.block().isVisible()) {
cursor.setPosition(block.position());
m_editor->setTextCursor(cursor);
}
}
}
}
QWidget::mousePressEvent(e);
}
void WingLineMargin::leaveEvent(QEvent *e) {
m_foldHoverLine = -1;
update();
QWidget::leaveEvent(e);
}