-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocktextedit.cpp
350 lines (283 loc) · 12.9 KB
/
blocktextedit.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Multi-page display code adapted from @dimkanovikov on GitHub
// https://github.com/dimkanovikov/PagesTextEdit
// ^ Comments translated using Google Translate with some changes
#include "blocktextedit.h"
#include "codesubblocksettings.h"
#include <QScreen>
#include <QApplication>
#include <QAbstractTextDocumentLayout>
#include <QScrollBar>
#include <QPainter>
#include <QTextFrame>
#include <QToolButton>
#include <QLayout>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionMatchIterator>
#include <QClipboard>
BlockTextEdit::BlockTextEdit(QWidget *parent) : QTextEdit(parent),
m_document(), m_codeHighlighter(this->document())
{
// Set up settings button for subblocks
m_settingsButton = new QPushButton(this);
m_settingsButton->setIcon(QIcon(":/icons/settings.png"));
m_settingsButton->setIconSize(QSize(26, 26));
m_settingsButton->setVisible(false);
// Prevents the document from automatically changing
aboutDocumentChanged();
connect(this, SIGNAL(textChanged()), this, SLOT(aboutDocumentChanged()));
// Update the document layout when it changes (overrides QTextEdit default reset)
connect(document()->documentLayout(), SIGNAL(update()), this, SLOT(aboutUpdateDocumentGeometry()));
// Custom setting of the scroll interval
connect(verticalScrollBar(), SIGNAL(rangeChanged(int,int)),
this, SLOT(aboutVerticalScrollRangeChanged(int,int)));
// Subblock buttons movement
connect(this, &QTextEdit::cursorPositionChanged, this, &BlockTextEdit::updateSubblockButtons);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &BlockTextEdit::updateSubblockButtons);
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &BlockTextEdit::updateSubblockButtons);
// Open settings menu
connect(m_settingsButton, &QPushButton::clicked, this, &BlockTextEdit::openSubblockSettings);
textCursor().movePosition(QTextCursor::Start);
}
qreal BlockTextEdit::inchesToPixels(qreal inches, bool _x) {
// Convert inches to pixel using screen size
QScreen *screen = QApplication::primaryScreen();
return inches * (_x ? screen->logicalDotsPerInchX() : screen->logicalDotsPerInchY());
}
void BlockTextEdit::setPixelPageSize(QSizeF _pageSize, bool _inches) {
if(_inches) {
_pageSize = QSizeF(inchesToPixels(_pageSize.width()), inchesToPixels(_pageSize.height(), false));
}
m_pixelPageSize = _pageSize;
// Repaint after change
repaint();
}
void BlockTextEdit::setPixelPageMargins(QMarginsF _pageMargins, bool _inches) {
if(_inches) {
_pageMargins = QMarginsF(inchesToPixels(_pageMargins.left()), inchesToPixels(_pageMargins.top(), false),
inchesToPixels(_pageMargins.right()), inchesToPixels(_pageMargins.bottom(), false));
}
m_pixelPageMargins = _pageMargins;
// Repaint after change
repaint();
}
void BlockTextEdit::setPageBreakGap(qreal _pageBreakGap, bool _inches) {
if(_inches) {
_pageBreakGap = inchesToPixels(_pageBreakGap, false);
}
m_pageBreakGap = _pageBreakGap;
// Repaint after change
repaint();
}
void BlockTextEdit::paintEvent(QPaintEvent* _event) {
// Make sure the scroll bar range is correct before painting to user
updateVerticalScrollRange();
// Paints page break lines
paintPagesView();
QTextEdit::paintEvent(_event);
}
void BlockTextEdit::resizeEvent(QResizeEvent* _event) {
// Updates viewport and scroll bar range when the window is resized
updateViewportMargins();
updateVerticalScrollRange();
updateSubblockButtons();
QTextEdit::resizeEvent(_event);
}
void BlockTextEdit::keyPressEvent(QKeyEvent* _event) {
// Override default action because formatting gets reset after consecutive newlines
if(_event->key() == Qt::Key_Return || _event->key() == Qt::Key_Enter) {
textCursor().insertBlock();
} else if(_event->key() == Qt::Key_Tab && textCursor().currentFrame()->frameFormat().property(1) == 0) {
// Insert 4 spaces instead of a tab in code subblocks
textCursor().insertText(" ");
} else {
QTextEdit::keyPressEvent(_event);
}
}
void BlockTextEdit::updateViewportMargins() {
// Declare Margins
QMargins viewportMargins;
// Get page width and height
int pageWidth = m_pixelPageSize.width();
int pageHeight = m_pixelPageSize.height() + m_pageBreakGap;
// Calculate padding for the viewport
const int DEFAULT_TOP_MARGIN = 0;
const int DEFAULT_BOTTOM_MARGIN = 0;
int leftMargin = 0;
int rightMargin = 0;
// If the editor's width is greater than the width of the document, add side padding
if (width() > pageWidth) {
const int BORDERS_WIDTH = 4;
// Account for vertical scroll bar width if visible
const int VERTICAL_SCROLLBAR_WIDTH =
verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0;
// The padding width of one side is half the editor width minus the page, scroll bar, and borders
leftMargin = rightMargin =
(width() - pageWidth - VERTICAL_SCROLLBAR_WIDTH - BORDERS_WIDTH) / 2;
}
// Top margin remains constant
int topMargin = DEFAULT_TOP_MARGIN;
// Calculate bottom margin
int bottomMargin = DEFAULT_BOTTOM_MARGIN;
int documentHeight = pageHeight * document()->pageCount();
// Check if document is taller than editor
if ((height() - documentHeight) > (DEFAULT_TOP_MARGIN + DEFAULT_BOTTOM_MARGIN)) {
const int BORDERS_HEIGHT = 2;
// Account for horizontal scroll bar height if visible
const int HORIZONTAL_SCROLLBAR_HEIGHT =
horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0;
// Calculate the amount of padding needed on the bottom
bottomMargin = height() - documentHeight - HORIZONTAL_SCROLLBAR_HEIGHT - DEFAULT_TOP_MARGIN - BORDERS_HEIGHT;
}
// Define the viewport margins
viewportMargins = QMargins(leftMargin, topMargin, rightMargin, bottomMargin);
setViewportMargins(viewportMargins);
// Update document geometry
aboutUpdateDocumentGeometry();
}
void BlockTextEdit::updateVerticalScrollRange() {
const int pageHeight = m_pixelPageSize.height();
const int documentHeight = (pageHeight + m_pageBreakGap) * document()->pageCount() - 1;
const int maximumValue = documentHeight - viewport()->height();
if (verticalScrollBar()->maximum() != maximumValue) {
verticalScrollBar()->setMaximum(maximumValue);
}
}
void BlockTextEdit::paintPagesView() {
// Draw page break lines
qreal pageWidth = m_pixelPageSize.width();
qreal pageHeight = m_pixelPageSize.height() + m_pageBreakGap;
QPainter p(viewport());
QPen spacePen(QColor(234, 234, 234), m_pageBreakGap);
QPen borderPen(palette().dark(), 1);
// Find the current height of top page on the screen
qreal curHeight = pageHeight - (verticalScrollBar()->value() % (int)pageHeight);
// Find the position of the right border
const int x = pageWidth + (width() % 2 == 0 ? 2 : 1);
// Horizontal offset if there is a horizontal scrollbar
const int horizontalDelta = horizontalScrollBar()->value();
// Draw the top border
if (curHeight - pageHeight >= 0) {
p.setPen(borderPen);
p.drawLine(0, curHeight - pageHeight, x, curHeight - pageHeight);
}
// Draw page breaks and borders for each page visible
while (curHeight <= height()) {
// Draw Page Break
p.setPen(spacePen);
p.drawLine(0, curHeight - (int)(m_pageBreakGap / 2), width(), curHeight - (int)(m_pageBreakGap / 2));
// Draw Page Borders
p.setPen(borderPen);
// Bottom of the page
p.drawLine(0, curHeight - (m_pageBreakGap), x, curHeight - (m_pageBreakGap));
// Top of the next page
p.drawLine(0, curHeight, x, curHeight);
// Left border
p.drawLine(0 - horizontalDelta, curHeight - pageHeight, 0 - horizontalDelta, curHeight - (m_pageBreakGap));
// Right border
p.drawLine(x - horizontalDelta, curHeight - pageHeight, x - horizontalDelta, curHeight - (m_pageBreakGap));
// Move to next page
curHeight += pageHeight;
}
// Draw the side borders of the last page when it doesn't fit the screen
if (curHeight >= height()) {
p.setPen(borderPen);
// Left border
p.drawLine(0 - horizontalDelta, curHeight-pageHeight, 0 - horizontalDelta, height());
// Right border
p.drawLine(x - horizontalDelta + 1, curHeight-pageHeight, x - horizontalDelta + 1, height());
}
}
void BlockTextEdit::aboutVerticalScrollRangeChanged(int _minimum, int _maximum) {
Q_UNUSED(_minimum)
updateViewportMargins();
int scrollValue = verticalScrollBar()->value();
// If the current scroll position is greater than the maximum value,
// so textedit updated the interval itself, apply our own correction function
if (scrollValue > _maximum) {
updateVerticalScrollRange();
}
}
void BlockTextEdit::aboutDocumentChanged() {
if (m_document != document()) {
m_document = document();
}
}
void BlockTextEdit::aboutUpdateDocumentGeometry()
{
// Determine the document size
QSizeF documentSize(width() - verticalScrollBar()->width(), -1);
int pageWidth = m_pixelPageSize.width();
int pageHeight = m_pixelPageSize.height() + m_pageBreakGap;
documentSize = QSizeF(pageWidth, pageHeight);
// Update document size
if (document()->pageSize() != documentSize) {
document()->setPageSize(documentSize);
}
// Clear document margins, uses root frame margins instead
if (document()->documentMargin() != 0) {
document()->setDocumentMargin(0);
}
QMarginsF rootFrameMargins = m_pixelPageMargins;
QTextFrameFormat rootFrameFormat = document()->rootFrame()->frameFormat();
if (rootFrameFormat.leftMargin() != rootFrameMargins.left()
|| rootFrameFormat.topMargin() != rootFrameMargins.top()
|| rootFrameFormat.rightMargin() != rootFrameMargins.right()
|| rootFrameFormat.bottomMargin() != rootFrameMargins.bottom() + m_pageBreakGap) {
rootFrameFormat.setLeftMargin(rootFrameMargins.left());
rootFrameFormat.setTopMargin(rootFrameMargins.top());
rootFrameFormat.setRightMargin(rootFrameMargins.right());
rootFrameFormat.setBottomMargin(rootFrameMargins.bottom() + m_pageBreakGap);
document()->rootFrame()->setFrameFormat(rootFrameFormat);
}
}
void BlockTextEdit::updateSubblockButtons() {
if(textCursor().currentFrame()->frameFormat().hasProperty(1)) {
switch(textCursor().currentFrame()->frameFormat().property(1).toInt()) {
case 0:
// Get the bounding rectangle of the frame
QRectF frameRect = document()->documentLayout()->frameBoundingRect(textCursor().currentFrame());
// Convert the frame-bounding rectangle coordinate system to the one of the central widget
QPointF frameRectTopRight = viewport()->mapTo(this, frameRect.topRight());;
frameRectTopRight.setY(frameRectTopRight.y() - verticalScrollBar()->value());
// If the top of the frame is partially off screen, move button downwards, lowest point is the bottom of the frame
if(frameRectTopRight.y() <= 0 && frameRectTopRight.y() >= -frameRect.height() + m_settingsButton->height()) {
frameRectTopRight.setY(0);
} else if(frameRectTopRight.y() <= 0 && frameRectTopRight.y() >= -frameRect.height()) {
frameRectTopRight.setY(frameRectTopRight.y() + frameRect.height() - m_settingsButton->height());
}
// Move the button
QRectF border = QRectF(frameRectTopRight, QSizeF(30, 30));
m_settingsButton->setVisible(true);
m_settingsButton->setGeometry(border.toRect());
border.moveRight(border.right() + 25);
break;
}
} else {
if(textCursor().currentFrame() == document()->rootFrame()) {
m_settingsButton->setVisible(false);
}
}
}
void BlockTextEdit::openSubblockSettings() {
if(textCursor().currentFrame()->frameFormat().property(1).isValid() &&
textCursor().currentFrame()->frameFormat().property(1).toInt() == 0) {
CodeSubblockSettings settings(textCursor().currentFrame());
settings.exec();
}
}
void BlockTextEdit::insertCodeBlock() {
// Set up subblock format
QTextFrameFormat format;
format.setBorder(1);
format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
format.setBackground(QBrush(QColor(230, 230, 230)));
// property 1 = type, 0 = code
format.setProperty(1, 0);
// Insert new frame
textCursor().insertFrame(format);
// Change font to monospace font
QTextCharFormat newFormat;
newFormat.setFont(QFont("Courier New", 9));
mergeCurrentCharFormat(newFormat);
}