-
Notifications
You must be signed in to change notification settings - Fork 15
/
PlainTextEdit.cpp
141 lines (111 loc) · 4.19 KB
/
PlainTextEdit.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
/****************************************************************************
** Deling Final Fantasy VIII Field Editor
** Copyright (C) 2009-2012 Arzel Jérôme <[email protected]>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "PlainTextEdit.h"
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
# define setTabStopDistance setTabStopWidth
#endif
#if (QT_VERSION < QT_VERSION_CHECK(5, 11, 0))
# define horizontalAdvance width
#endif
PlainTextEditPriv::PlainTextEditPriv(QWidget *parent) :
QPlainTextEdit(parent), _previousLineHovered(-1)
{
setMouseTracking(true);
_overlay = new PreviewWidget(this);
_overlay->hide();
}
void PlainTextEditPriv::mouseMoveEvent(QMouseEvent *event)
{
QTextBlock currentBlock = cursorForPosition(event->pos()).block();
if(_previousLineHovered != currentBlock.firstLineNumber()) {
_previousLineHovered = currentBlock.firstLineNumber();
int maxH = int(document()->documentLayout()->blockBoundingRect(currentBlock).height()),
i = 0;
for(; i < maxH; ++i) {
if (cursorForPosition(event->pos() + QPoint(0, i)).block() != currentBlock) {
break;
}
}
int margin = maxH / 3;
emit lineHovered(currentBlock.text(), QPoint(margin, event->pos().y() + i + margin));
}
QPlainTextEdit::mouseMoveEvent(event);
}
PlainTextEdit::PlainTextEdit(QWidget *parent) :
QWidget(parent)
{
_textEdit = new PlainTextEditPriv(parent);
_textEdit->viewport()->installEventFilter(this);
_textEdit->setTabStopDistance(30.0);
_textEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
_textEdit->setMouseTracking(true);
}
QSize PlainTextEdit::sizeHint() const
{
return QSize(_textEdit->fontMetrics()
.horizontalAdvance(QString::number(_textEdit->document()->blockCount()))
+ 4,
_textEdit->height());
}
PlainTextEditPriv *PlainTextEdit::textEdit() const
{
return _textEdit;
}
bool PlainTextEdit::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Paint) {
setFixedHeight(_textEdit->height());
setFixedWidth(_textEdit->fontMetrics().horizontalAdvance(QString::number(_textEdit->document()->blockCount())) + 4);
update();
}
return QObject::eventFilter(obj, event);
}
void PlainTextEdit::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
QFontMetrics fontMetrics = _textEdit->fontMetrics();
/* Draw background */
// int minWidth = fontMetrics.width(QString::number(_textEdit->document()->blockCount())) + 4;
// painter.fillRect(QRect(0, event->rect().top(), minWidth, height()), QColor(0xf0,0xf0,0xf0));
painter.setPen(QColor(0xa0,0xa0,0xa0));
/* Draw line numbers */
int line=_textEdit->verticalScrollBar()->value() + 1;
int y= _textEdit->frameWidth() + fontMetrics.ascent();
if(line == 1) {
y += _textEdit->document()->documentMargin();
}
QTextBlock currentBlock = _textEdit->textCursor().block();
QTextBlock block = _textEdit->document()->findBlockByNumber(line - 1);
while(block.isValid() && y <= height()) {
if(block == currentBlock) {
QFont font = painter.font();
font.setBold(true);
painter.setFont(font);
}
painter.drawText(width() - fontMetrics.horizontalAdvance(QString::number(line)) - 3, y, QString::number(line));
y += _textEdit->document()->documentLayout()->blockBoundingRect(block).height();
if(block == currentBlock) {
QFont font = painter.font();
font.setBold(false);
painter.setFont(font);
}
block = block.next();
line++;
}
}