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
|
#include <QtGui>
#include "editor.h"
Editor::Editor() : QPlainTextEdit(),
folded(false)
{
document()->setDocumentLayout(new EditorLayout(document()));
setPlainText(
"int main(int argc, char **args)\n"
"{\n"
"\tqDebug() << \"Folding Code Example\";\n"
"\treturn 0;\n"
"}\n"
"\n"
"#include main.moc"
);
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateCursorPosition()));
}
void Editor::paintEvent(QPaintEvent *event)
{
if (folded) {
QTextBlock foldedBlock = document()->findBlockByNumber(1);
if (!foldedBlock.isValid() || !foldedBlock.isVisible())
return;
qreal top = blockBoundingGeometry(foldedBlock).translated(contentOffset()).top();
QTextLayout *layout = foldedBlock.layout();
QTextLine line = layout->lineAt(layout->lineCount()-1);
QRectF lineRect = line.naturalTextRect().translated(0, top);
lineRect.adjust(0, 0, -1, -1);
QRectF collapseRect(lineRect.right() + 12,
lineRect.top(),
fontMetrics().width(QLatin1String(" ...} ")),
lineRect.height());
QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing, true);
painter.translate(.5, .5);
painter.drawRoundedRect(collapseRect.adjusted(0, 0, 0, -1), 3, 3);
painter.translate(-.5, -.5);
painter.drawText(collapseRect, Qt::AlignCenter, "...}");
}
QPlainTextEdit::paintEvent(event);
}
QTextBlock Editor::foldedBlockAt(const QPoint &pos)
{
QTextBlock block = firstVisibleBlock();
qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
qreal bottom = top + blockBoundingRect(block).height();
int viewportHeight = viewport()->height();
while (block.isValid() && top <= viewportHeight) {
QTextBlock nextBlock = block.next();
if (block.isVisible() && bottom >= 0) {
if (nextBlock.isValid() && !nextBlock.isVisible()) {
QTextLayout *layout = block.layout();
QTextLine line = layout->lineAt(layout->lineCount()-1);
QRectF lineRect = line.naturalTextRect().translated(0, top);
lineRect.adjust(0, 0, -1, -1);
QRectF collapseRect(lineRect.right() + 12,
lineRect.top(),
fontMetrics().width(QLatin1String(" ...} ")),
lineRect.height());
if (collapseRect.contains(pos)) {
QTextBlock result = block;
return result;
} else {
block = nextBlock;
while (nextBlock.isValid() && !nextBlock.isVisible()) {
block = nextBlock;
nextBlock = block.next();
}
}
}
}
block = nextBlock;
top = bottom;
bottom = top + blockBoundingRect(block).height();
}
return QTextBlock();
}
void Editor::updateCursorPosition()
{
QTextCursor cursor = textCursor();
QTextBlock block = cursor.block();
if (!block.isVisible()) {
while (!block.isVisible() && block.previous().isValid()) {
block.setVisible(true);
block.setLineCount(qMax(1, block.layout()->lineCount()));
block = block.previous();
}
EditorLayout *layout = static_cast<EditorLayout *>(document()->documentLayout());
layout->requestUpdate();
layout->emitDocumentSizeChanged();
folded = false;
QPlainTextEdit::ensureCursorVisible();
}
}
void Editor::mousePressEvent(QMouseEvent *event)
{
if (folded) {
QTextBlock foldedBlock = foldedBlockAt(event->pos());
if (!foldedBlock.isValid()) {
QPlainTextEdit::mousePressEvent(event);
return;
}
} else {
QPlainTextEdit::mousePressEvent(event);
}
EditorLayout *layout = static_cast<EditorLayout *>(document()->documentLayout());
folded = !folded;
for (int i = 0; i < 3; ++i) {
QTextBlock block = document()->findBlockByNumber(i + 2);
block.setVisible(!folded);
block.setLineCount(folded ? 0 : qMax(1, block.layout()->lineCount()));
}
QTextCursor cursor = textCursor();
if (!cursor.block().isVisible()) {
cursor.setVisualNavigation(true);
cursor.movePosition(QTextCursor::Up);
setTextCursor(cursor);
}
layout->requestUpdate();
layout->emitDocumentSizeChanged();
}
|