-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeeditor.cpp
executable file
·451 lines (357 loc) · 13.3 KB
/
codeeditor.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <QtWidgets>
#include <QFont>
#include "codeeditor.h"
#include <QFontMetrics>
#include "gatortext.h"
//new
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
//creates the updating line number area
lineNumberArea = new LineNumberArea(this);
//connects the changing line numbers to the line number editor
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
//sets the editor to update the window title whenever the editor is modified. Useful in that it shows if something is saved or unsavesd
connect(this->document(), &QTextDocument::contentsChanged, this, &CodeEditor::updateWindowTitleDocumentChanged);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
//sets editor default font and size. The other stuff is to make it look like an IDE
QFont font;
font.setFamily("Courier");
font.setStyleHint(QFont::Monospace);
font.setFixedPitch(true);
font.setPointSize(11);
//updates the default tab stop of the editor. Sets the width to 4. Otherwise tabs only equal one space by default
QFontMetrics metrics(font);
this->setTabStopWidth(tabStop * metrics.width(" "));
this->setFont(font);
//attempts to create the completer. If its unsuccessful display an error
if (!createCompleter()) {
QMessageBox errorBox;
errorBox.setText("Error. Unable to open wordlist. AutoComplete Disabled");
errorBox.exec();
}
//allows the editor to accept drop events for drag and drop
setAcceptDrops(true);
}
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void CodeEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> selections = extraSelections();
if (!isReadOnly()) {
for (int index = 0; index < selections.size(); index++) {
if (selections[index].format.background().color().name() == QColor(Qt::blue).lighter(160).name()) {
selections.removeAt(index);
}
}
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::blue).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
selections.append(selection);
}
setExtraSelections(selections);
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
bool CodeEditor::createCompleter() {
QFile file(":/cpp.txt");
file.open(QFile::ReadOnly);
if (file.isOpen()) {
while(!file.atEnd()) {
QString line = file.readLine();
masterList.append(line);
}
completer = new QCompleter(masterList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setWidget(this);
//connects the activated completer signal to the insert completion slot
QObject::connect(completer, SIGNAL(activated(QString)),
this, SLOT(insertCompletion(QString)));
file.close();
return true;
} else {
return false;
}
}
//where the majority of the work is done
void CodeEditor::keyPressEvent(QKeyEvent *event) {
//check the currently entered text and apply it to the completer
if (completer) {
QTextCursor cursor = textCursor();
cursor.select(QTextCursor::WordUnderCursor);
QString temp = cursor.selectedText();
if (temp.length() >= 2) {
completer->setCompletionPrefix(temp);
//the rect displays the contents of the completion window
//TODO. dress up the window a bit. maybe add some more information
QRect rect = cursorRect();
rect.setWidth(completer->popup()->sizeHintForColumn(0)
+ completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(rect);
}
}
//inserts the next paren
if (event->key() == Qt::Key_ParenLeft) {
QTextCursor tc = textCursor();
int oldPos = tc.position();
tc.insertText(")");
tc.setPosition(oldPos);
setTextCursor(tc);
parenAdded = true;
}
//skips the current paren if one is already added
if (event->key() == Qt::Key_ParenRight) {
if (parenAdded == true) {
QTextCursor tc = textCursor();
int pos = tc.position();
tc.setPosition(pos + 1);
setTextCursor(tc);
parenAdded = false;
event->ignore();
return;
}
}
//inserts the matching quote and skips the next one when the appropriate character is pressed
if (event->key() == Qt::Key_QuoteDbl) {
QTextCursor tc = textCursor();
int pos = tc.position();
if (dblQuoteAdded == false) {
tc.insertText("\"");
tc.setPosition(pos);
setTextCursor(tc);
dblQuoteAdded = true;
} else {
tc.setPosition(pos + 1);
setTextCursor(tc);
dblQuoteAdded = false;
event->ignore();
return;
}
}
//removes the completer when space is added
if (event->key() == Qt::Key_Space) {
//remove completer if space is pressed
completer->popup()->hide();
QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::WordLeft);
tc.select(QTextCursor::WordUnderCursor);
QString temp = tc.selectedText();
//dont add the word to the list if its in master or additional list
if (!checkForWordInList(temp)) {
masterList.insert(0, temp);
additionalItems.insert(0, temp);
updateCompleter();
}
}
//when the user hits enter used for adding indentation
if (event->key() == Qt::Key_Return) {
QTextCursor tc = textCursor();
QString line = tc.block().text();
QTextBlock nextBlock = document()->findBlockByNumber(tc.blockNumber() + 1);
qDebug()<< nextBlock.text();
int count = line.count("\t");
//if indentation is added.
if (count > 0) {
tc.insertText("\n");
if (nextBlock.text().contains("}")) {
tc.insertText("\t");
}
for (int i = 0; i < count; i++) {
tc.insertText("\t");
}
return;
//if no indentation is added check for the end brace
} else if (nextBlock.text().contains("}")) {
tc.insertText("\n");
tc.insertText("\t");
return;
}
}
if (event->key() == Qt::Key_BraceLeft) {
QTextCursor tc = textCursor();
int originalPosition = tc.position();
int tabCount = tc.block().text().count("\t");
QString prependString = "\n";
if (tabCount > 0) {
for (int i =0; i <tabCount; i++) {
prependString += "\t";
}
}
prependString += "}";
tc.insertText(prependString);
tc.setPosition(originalPosition);
setTextCursor(tc);
}
//passes the key to the original event handler to do normal processing
QPlainTextEdit::keyPressEvent(event);
}
//sets the completer to the widget
void CodeEditor::focusInEvent(QFocusEvent *event) {
if (completer) {
completer->setWidget(this);
}
QPlainTextEdit::focusInEvent(event);
}
//when the completer is activated insert the word
void CodeEditor::insertCompletion(const QString& completion) {
QString temp = completer->completionPrefix();
QTextCursor tc = textCursor();
int oldPosition = tc.position();
tc.select(QTextCursor::WordUnderCursor);
tc.insertText(completion);
tc.setPosition(oldPosition);
tc.movePosition(QTextCursor::WordRight);
setTextCursor(tc);
}
bool CodeEditor::checkForWordInList(QString word) {
if (masterList.contains(word)) {
return true;
} else {
return false;
}
}
//updates the completer
void CodeEditor::updateCompleter() {
completer = new QCompleter(masterList, this);
//i have to reset this everytime. I thought the point of passing in the old one as param was to update the new one?
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setWidget(this);
//gotta reconnect it too
QObject::connect(completer, SIGNAL(activated(QString)),
this, SLOT(insertCompletion(QString)));
}
bool CodeEditor::updateCompleter(LanguageType language) {
QString filename;
QString finalName;
switch (language) {
case CPP:
filename = "cpp";
break;
case C:
filename = "c";
break;
case JAVASCRIPT:
filename = "javascript";
break;
case PYTHON:
filename = "python";
break;
case JAVA:
filename = "java";
break;
}
//update the masterlist to be empty for the new file
masterList.clear();
finalName = ":/" + filename + ".txt";
QFile file(finalName);
file.open(QFile::ReadOnly);
if (file.isOpen()) {
while(!file.atEnd()) {
QString line = file.readLine();
masterList.append(line);
}
for (QStringList::const_iterator it = additionalItems.constBegin(); it != additionalItems.constEnd(); it++) {
masterList.push_back(*it);
}
completer = new QCompleter(masterList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setWidget(this);
QObject::connect(completer, SIGNAL(activated(QString)),
this, SLOT(insertCompletion(QString)));
file.close();
return true;
} else {
return false;
}
}
QStringList* CodeEditor::getMasterList() {
return &masterList;
}
QStringList* CodeEditor::getAdditionalItems() {
return &additionalItems;
}
void CodeEditor::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void CodeEditor::dropEvent(QDropEvent *event) {
if (event->mimeData()->hasUrls()) {
QStringList pathList;
if (event->mimeData()->urls().size() > 1) {
QMessageBox errorBox;
errorBox.setText("Sorry. You are only able to drag and drop one file at a time");
errorBox.exec();
} else {
QUrl url = event->mimeData()->urls()[0];
QString filename = url.toLocalFile();
GatorText* parent = (GatorText*) this->parent();
if (parent->checkForSave()) {
parent->loadFile(filename);
}
}
}
}
void CodeEditor::updateWindowTitleDocumentChanged() {
GatorText* gatorText = (GatorText*) this->parent();
QString windowTitle = gatorText->windowTitle();
if (!windowTitle.contains("*")) {
windowTitle.append("*");
gatorText->setWindowTitle(windowTitle);
}
}