-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexteditor.cpp
More file actions
232 lines (164 loc) · 6.67 KB
/
texteditor.cpp
File metadata and controls
232 lines (164 loc) · 6.67 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "texteditor.h"
TextEditor::TextEditor(QWidget *parent, QWidget *mainWidget, TextAnalyzer *statistics, SpellChecker *spellchecker)
: QPlainTextEdit(parent)
{
this->mainWidget = mainWidget;
textwidth = 80;
limittextwidth = true;
this->statistics = statistics;
//this->setAttribute(Qt::WA_AcceptTouchEvents);
//this->grabGesture(Qt::PanGesture);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->verticalScrollBar()->setStyle(new QCommonStyle());
this->horizontalScrollBar()->setStyle(new QCommonStyle());
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
this->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
QTextOption option = this->document()->defaultTextOption();
option.setAlignment(Qt::AlignJustify);
this->document()->setDefaultTextOption(option);
highlighter = new MDHighlighter(this->document(), spellchecker);
highlighter->setDefaultFont(this->font());
connect(this->document(), &QTextDocument::contentsChange, this, &TextEditor::analyzeTextChange);
connect(this, &TextEditor::selectionChanged, this, &TextEditor::analyzeSelection);
}
/**
* @brief Sets the margins of the contents so that textwidth characters are shown.
* @param textwidth
* @return
*/
int TextEditor::setTextWidth(const int textwidth) {
int leftMargin = 0;
int rightMargin = 0;
if(limittextwidth) {
this->textwidth = textwidth;
if(textwidth < 10) {
this->textwidth = 10;
}
int globalWidth = this->mainWidget->width();
int globalX = this->mapToGlobal(QPoint(0, 0)).x();
int textwidthPixels = this->fontMetrics().horizontalAdvance(QString(this->textwidth, 'X'));
if(textwidthPixels < this->width()) {
int globalMargin = (globalWidth - textwidthPixels) / 2;
leftMargin = globalMargin - globalX;
if(leftMargin < 0) {
leftMargin = 0;
}
rightMargin = globalMargin - (globalWidth - globalX - this->width());
if(rightMargin < 0) {
rightMargin = 0;
}
if(this->width() < textwidthPixels + leftMargin + rightMargin) {
rightMargin = this->width() - textwidthPixels - leftMargin;
}
}
}
this->setViewportMargins(leftMargin, 0, rightMargin, 0);
return this->textwidth;
}
void TextEditor::limitTextWidth(const bool limittextwidth) {
this->limittextwidth = limittextwidth;
setTextWidth(textwidth);
}
void TextEditor::resizeEvent(QResizeEvent *event) {
this->QPlainTextEdit::resizeEvent(event);
this->setTextWidth(textwidth);
}
void TextEditor::setFont(const QFont &font) {
QFont fontCopy = font;
fontCopy.setPointSize(this->font().pointSize());
this->QWidget::setFont(fontCopy);
this->setTextWidth(textwidth);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
highlighter->setDefaultFont(fontCopy);
highlighter->rehighlight();
}
void TextEditor::setFontSize(const int fontsize) {
QFont font = this->font();
font.setPointSize(fontsize);
this->QWidget::setFont(font);
this->setTextWidth(textwidth);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
highlighter->setDefaultFont(font);
highlighter->rehighlight();
}
void TextEditor::findRequested(const QString &text) {
if(!this->find(text, QTextDocument::FindCaseSensitively)) {
this->moveCursor(QTextCursor::Start);
if(!this->find(text, QTextDocument::FindCaseSensitively)) {
QMessageBox::information(this, tr("Find Results"), text + tr(" not found."));
}
}
}
void TextEditor::replaceRequested(const QString &text, const QString &replacement) {
if(!(this->textCursor().hasSelection() && this->textCursor().selectedText() == text)) {
if(!this->find(text, QTextDocument::FindCaseSensitively)) {
this->moveCursor(QTextCursor::Start);
if(!this->find(text, QTextDocument::FindCaseSensitively)) {
QMessageBox::information(this, tr("Replace Results"), text + tr(" not found."));
return;
}
}
}
this->textCursor().insertText(replacement);
if(!this->find(text, QTextDocument::FindCaseSensitively)) {
this->moveCursor(QTextCursor::Start);
this->find(text, QTextDocument::FindCaseSensitively);
}
}
void TextEditor::replaceAllRequested(const QString &text, const QString &replacement) {
this->moveCursor(QTextCursor::Start);
this->textCursor().beginEditBlock();
while(this->find(text, QTextDocument::FindCaseSensitively)) {
this->textCursor().insertText(replacement);
}
this->textCursor().endEditBlock();
}
void TextEditor::scrollToBlock(const int blockNumber) {
int position = this->document()->findBlockByNumber(blockNumber).position();
if(blockNumber < 0) {
position = 0;
}
if(blockNumber >= this->blockCount()) {
position = this->document()->lastBlock().position();
}
this->moveCursor(QTextCursor::End);
QTextCursor cursor = this->textCursor();
cursor.setPosition(position);
this->setTextCursor(cursor);
this->ensureCursorVisible();
}
void TextEditor::analyzeWholeText() {
analyzeTextChange(0, 0, this->document()->characterCount());
}
void TextEditor::analyzeTextChange(const int position, const int charsRemoved, const int charsAdded) {
QTextBlock block = this->document()->findBlock(position);
int blockNumberDelta = this->document()->blockCount() - statistics->blockCount();
if(blockNumberDelta < 0) {
statistics->removeBlocks(block.blockNumber(), -blockNumberDelta);
} else {
statistics->addBlocks(block.blockNumber(), blockNumberDelta);
}
int lastChangedBlock = this->document()->findBlock(position + charsAdded).blockNumber();
if(lastChangedBlock < 0) {
lastChangedBlock = this->document()->blockCount() - 1;
}
for(int i = block.blockNumber(); i <= lastChangedBlock; ++i) {
statistics->updateBlock(i, block.text(), block.userState());
block = block.next();
}
if(!this->textCursor().hasSelection()) {
emit textAnalyzed(false);
}
}
void TextEditor::analyzeSelection() {
if(this->textCursor().hasSelection()) {
statistics->analyzeSelection(this->textCursor().selectedText());
emit textAnalyzed(true);
} else {
emit textAnalyzed(false);
}
}