-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatortext.cpp
executable file
·1054 lines (879 loc) · 33.7 KB
/
gatortext.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gatortext.h"
#include "codeeditor.h"
#include <QMenuBar>
#include <QIcon>
#include "highlighter.h"
#include <iostream>
#include <QWebEngineView>
#include <QUrl>
#include "findreplacedialog.h"
#include "changefontdialog.h"
#include "changefontsizedialog.h"
GatorText::GatorText() {
editor = new CodeEditor();
highlighter = new Highlighter(editor->document());
setCentralWidget(editor);
//creates the menu and toolbar
createMenus();
//creates all the actions neccessary and usually conenct them to their functions
createActions();
//create web docks
createWebDock();
//creates the code viewer dock
createWordDock();
//update the list automatically
updateMasterList();
}
void GatorText::createWebDock() {
//i have to create the web engine in this function
//if i attempt to declare it in my header file the compiler crashes
//idk what the hell is wrong with it. I can later access it by finding it through
//its children so its not a big deal
QString finalUrl = url + urlModifier;
QWebEngineView* webEngine = new QWebEngineView(this);
webEngine->setUrl(finalUrl);
dock = new QDockWidget("Documentation Viewer", this);
//i only want the docks on the left or right
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
dock->setWidget(webEngine);
dock->setVisible(false);
addDockWidget(Qt::RightDockWidgetArea, dock);
toolsMenu->addAction(dock->toggleViewAction());
//default window title when an unsaved window is open
setWindowTitle("GatorText - Untitled");
}
void GatorText::createWordDock() {
dock = new QDockWidget("Function and Variable explorer", this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
//set it invisible by default
dock->setVisible(false);
QListWidget* list = new QListWidget(this);
list->setContextMenuPolicy(Qt::CustomContextMenu);
connect(list, &QListWidget::customContextMenuRequested, this, &GatorText::listItemContextSelected);
list->addItems(*masterListPtr);
dock->setWidget(list);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
void GatorText::createMenus() {
fileMenu = menuBar()->addMenu("&File");
editMenu = menuBar()->addMenu("&Edit");
viewMenu = menuBar()->addMenu("&View");
optionsMenu = menuBar()->addMenu("&Options");
helpMenu = menuBar()->addMenu("&Help");
toolsMenu = viewMenu->addMenu("&Tools");
toolBar = addToolBar("&File");
//I dont want anyone to move the toolbar
toolBar->setMovable(false);
}
void GatorText::createActions() {
//load all the icons for the actions with icons
const QIcon newIcon = QIcon(":/new.png");
const QIcon openIcon = QIcon(":/open.png");
const QIcon saveIcon = QIcon(":/save.png");
const QIcon cutIcon = QIcon(":/cut.png");
const QIcon pasteIcon = QIcon(":/paste.png");
const QIcon copyIcon = QIcon(":/copy.png");
const QIcon saveAsIcon = QIcon(":/saveas.png");
//declare all the actions
QAction* newAction = new QAction(newIcon, "&New", this);
QAction* openAction = new QAction(openIcon, "&Open", this);
QAction* saveAction = new QAction(saveIcon, "&Save", this);
QAction* cutAction = new QAction(cutIcon, "&Cut", this);
QAction* copyAction = new QAction(copyIcon, "&Copy", this);
QAction* pasteAction = new QAction(pasteIcon, "&Paste", this);
QAction* saveAsAction = new QAction(saveAsIcon, "&Save as", this);
QAction* commentOutAction = new QAction("&Comment out selected text", this);
QAction* unCommentOutAction = new QAction("&Uncomment out selected Text", this);
QAction* quitAction = new QAction("&Exit Program", this);
QAction* displayCodeViewerAction = new QAction("&Toggle Code Viewer", this);
QAction* findWordAction = new QAction ("&Find/Replace", this);
QAction* undoAction = new QAction("&Undo", this);
QAction* redoAction = new QAction("&Redo", this);
QAction* tabAction = new QAction("&Add indentation");
QAction* changeFontAction = new QAction("&Change Font");
QAction* changeFontSizeAction = new QAction("&Change Font Size");
QAction* darkAction = new QAction("&Dark");
QAction* darkOrangeAction = new QAction("&Dark Orange");
QAction* darkFusionAction = new QAction("&Dark Fusion");
QAction* defaultThemeAction = new QAction("Default");
QAction* aboutAction = new QAction("&About");
//view language actions
QAction* cppAction = new QAction("&C++", this);
QAction* cAction = new QAction("&C", this);
QAction* pythonAction = new QAction("&Python", this);
QAction* javascriptAction = new QAction("&Javascript", this);
QAction* javaAction = new QAction("&Java", this);
//set language options to be checkable and set the default to C++
cppAction->setCheckable(true);
cppAction->setChecked(true);
cAction->setCheckable(true);
pythonAction->setCheckable(true);
javascriptAction->setCheckable(true);
javaAction->setCheckable(true);
//setup shortcut sequences for several actions. all shortcuts are the commong default
//so it should be intuitive for the user
newAction->setShortcut(QKeySequence::New);
openAction->setShortcut(QKeySequence::Open);
saveAction->setShortcut(QKeySequence::Save);
cutAction->setShortcut(QKeySequence::Cut);
copyAction->setShortcut(QKeySequence::Copy);
pasteAction->setShortcut(QKeySequence::Paste);
saveAsAction->setShortcut(QKeySequence::SaveAs);
quitAction->setShortcut(QKeySequence::Quit);
undoAction->setShortcut(QKeySequence::Undo);
redoAction->setShortcut(QKeySequence::Redo);
//handle all connections
//normal connections
connect(saveAction, &QAction::triggered, this, &GatorText::save);
connect(newAction, &QAction::triggered, this, &GatorText::newFile);
connect(openAction, &QAction::triggered, this, &GatorText::openFile);
connect(saveAsAction, &QAction::triggered, this, &GatorText::saveAs);
connect(cutAction, &QAction::triggered, this, &GatorText::cut);
connect(copyAction, &QAction::triggered, this, &GatorText::copy);
connect(pasteAction, &QAction::triggered, this, &GatorText::paste);
connect(commentOutAction, &QAction::triggered, this, &GatorText::commentOutCode);
connect(unCommentOutAction, &QAction::triggered, this, &GatorText::unCommentOutCode);
connect(quitAction, &QAction::triggered, this, &GatorText::quitProgram);
connect(displayCodeViewerAction, &QAction::triggered, this, &GatorText::toggleCodeViewer);
connect(undoAction, &QAction::triggered, editor, &CodeEditor::undo);
connect(redoAction, &QAction::triggered, editor, &CodeEditor::redo);
connect(findWordAction, &QAction::triggered, this, &GatorText::find);
connect(tabAction, &QAction::triggered, this, &GatorText::addIndentation);
connect(changeFontAction, &QAction::triggered, this, &GatorText::changeFont);
connect(changeFontSizeAction, &QAction::triggered, this, &GatorText::changeFontSize);
connect(aboutAction, &QAction::triggered, this, &GatorText::about);
//lambda language connections
//lambda features were useful because it allowed me to not use a signal mapper and cut down on the code needed
connect(cppAction, &QAction::triggered, [=] {languageChanged(CPP);});
connect(cAction, &QAction::triggered, [=] {languageChanged(C);});
connect(javascriptAction, &QAction::triggered, [=] {languageChanged(JAVASCRIPT);});
connect(javaAction, &QAction::triggered, [=] {languageChanged(JAVA);});
connect(pythonAction, &QAction::triggered, [=] {languageChanged(PYTHON);});
//lambda theme connections
connect(defaultThemeAction, &QAction::triggered, [=] {changeTheme("default");});
connect(darkAction, &QAction::triggered, [=] {changeTheme("dark");});
connect(darkOrangeAction, &QAction::triggered, [=] {changeTheme("dark-orange");});
connect(darkFusionAction, &QAction::triggered, [=] {changeTheme("dark-fusion");});
//add the actions to the toolbar
toolBar->addAction(newAction);
toolBar->addAction(openAction);
toolBar->addAction(saveAction);
toolBar->addAction(saveAsAction);
toolBar->addAction(cutAction);
toolBar->addAction(copyAction);
toolBar->addAction(pasteAction);
//add the actions to the file menu
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addSeparator();
fileMenu->addAction(cutAction);
fileMenu->addAction(copyAction);
fileMenu->addAction(pasteAction);
fileMenu->addSeparator();
fileMenu->addAction(quitAction);
//add actions to the edit menu
editMenu->addAction(undoAction);
editMenu->addAction(redoAction);
editMenu->addAction(findWordAction);
editMenu->addAction(commentOutAction);
editMenu->addAction(unCommentOutAction);
editMenu->addAction(tabAction);
//add a separator for A E S T H E T I C S
editMenu->addSeparator();
languageMenu = editMenu->addMenu("Language");
//adds the language options to the language menu
languageMenu->addAction(cppAction);
languageMenu->addAction(cAction);
languageMenu->addAction(pythonAction);
languageMenu->addAction(javascriptAction);
languageMenu->addAction(javaAction);
toolsMenu->addAction(displayCodeViewerAction);
optionsMenu->addAction(changeFontAction);
optionsMenu->addAction(changeFontSizeAction);
helpMenu->addAction(aboutAction);
//also couldnt declare the themeMenu in the header or I had the same issue as the webengine
//this is the only way I can make it work
QMenu* themeMenu = optionsMenu->addMenu("Change Theme");
themeMenu->addAction(defaultThemeAction);
themeMenu->addAction(darkAction);
themeMenu->addAction(darkOrangeAction);
themeMenu->addAction(darkFusionAction);
//updates the window title
setWindowTitle("GatorText - Untitled");
}
//checks if the document has been save if it has been modified.
//presents a dialog to confirm
//just like all programs
bool GatorText::checkForSave() {
if (!editor->document()->isModified()) {
return true;
} else {
const QMessageBox::StandardButton ret
= QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
switch (ret) {
case QMessageBox::Save:
return save();
case QMessageBox::Cancel:
return false;
default:
break;
}
return true;
}
}
//checks for save before closing
void GatorText::closeEvent(QCloseEvent* event) {
(checkForSave()) ? event->accept() : event->ignore();
}
//creates new file
void GatorText::newFile() {
if (checkForSave()) {
editor->clear();
setCurrentFile(QString());
}
setWindowTitle("GatorText - Untitled");
}
//open file action. not responsible for loading the file
void GatorText::openFile() {
if (checkForSave()) {
QString filename = QFileDialog::getOpenFileName(this);
if (!filename.isEmpty()) {
loadFile(filename);
}
}
}
//save file action. not responsible for saving the data
bool GatorText::save() {
if (currentFile.isEmpty()) {
return saveAs();
} else {
return saveFile(currentFile);
}
}
//save as action. Also not responsible for saving the data. just a check function
bool GatorText::saveAs() {
QFileDialog dialog(this);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (dialog.exec() != QDialog::Accepted) {
return false;
} else {
return saveFile(dialog.selectedFiles().first());
}
}
//function responsible for loading a file and setting the editor to display it
void GatorText::loadFile(const QString& filename) {
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
return;
} else {
QTextStream in(&file);
editor->setPlainText(in.readAll());
setCurrentFile(filename);
setWindowTitle("GatorText - " + filename);
QFileInfo fileInfo(filename);
QString fileType = fileInfo.completeSuffix();
LanguageType newLanguage;
//reads the language type by file extension
//and chnages the language type to match
if (fileType == "cpp") {
newLanguage = CPP;
} else if (fileType == "c") {
newLanguage = C;
} else if (fileType == "py") {
newLanguage = PYTHON;
} else if (fileType == "js") {
newLanguage = JAVASCRIPT;
} else if (fileType == "java") {
newLanguage = JAVA;
} else {
newLanguage = NOTSET;
}
//only change the language if its different than the other one
if (newLanguage != NOTSET && newLanguage != selectedLanguage) {
languageChanged(newLanguage);
QMessageBox languageUpdateBox;
//display an update so the use knows
languageUpdateBox.setText(fileType + " file detected. Switching language to " + mapLanguageToEnum(newLanguage));
languageUpdateBox.exec();
}
}
}
//responsible for saving the file data. Used by save and save as function
bool GatorText::saveFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot write file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName),
file.errorString()));
return false;
}
//changes the currsor to waiting if reading is slow
QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << editor->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
QFileInfo fileInfo(fileName);
QString fileType = fileInfo.completeSuffix();
LanguageType newLanguage;
//reads the language type and updates the language
if (fileType == "cpp") {
newLanguage = CPP;
} else if (fileType == "c") {
newLanguage = C;
} else if (fileType == "py") {
newLanguage = PYTHON;
} else if (fileType == "js") {
newLanguage = JAVASCRIPT;
} else if (fileType == "java") {
newLanguage = JAVA;
} else {
newLanguage = NOTSET;
}
if (newLanguage != NOTSET && newLanguage != selectedLanguage) {
languageChanged(newLanguage);
QMessageBox languageUpdateBox;
languageUpdateBox.setText(fileType + " file detected. Switching language to " + mapLanguageToEnum(newLanguage));
languageUpdateBox.exec();
}
setCurrentFile(fileName);
this->setWindowTitle("GatorText - " + fileName);
return true;
}
//sets the current file and updates the window title
void GatorText::setCurrentFile(const QString& filename) {
currentFile = filename;
editor->document()->setModified(false);
QString shownName = "GatorText - " + currentFile;
if (currentFile.isEmpty()) {
shownName = "GatorText - untitled.txt";
}
}
//changes the language and updates the completer
//loads the neccessary text file and updates the completer
void GatorText::languageChanged(LanguageType type) {
this->selectedLanguage = type;
QString actionName;
switch(selectedLanguage) {
case CPP:
urlModifier = "cpp";
actionName = "C++";
break;
case C:
urlModifier = "c";
actionName = "C";
break;
case JAVASCRIPT:
urlModifier = "javascript";
actionName = "Javascript";
break;
case PYTHON:
urlModifier = "python";
actionName = "Python";
break;
case JAVA:
urlModifier = "openjdk";
actionName = "Python";
break;
default:
break;
}
QList<QAction*> actions = languageMenu->actions();
foreach(QAction* action, actions) {
if (action->iconText() != actionName) {
action->setChecked(false);
}
}
QString finalUrl = url + urlModifier;
changeWebUrl(finalUrl);
if (!editor->updateCompleter(type)) {
QMessageBox errorBox;
errorBox.setText("Error. Unable to open wordlist. AutoComplete Disabled");
errorBox.exec();
}
updateMasterList();
highlighter->updateKeyWordList(masterListPtr);
}
void GatorText::updateMasterList() {
masterListPtr = editor->getMasterList();
}
void GatorText::updateAdditionalItems() {
additionalItemsPtr = editor->getAdditionalItems();
}
//comment out code action
void GatorText::commentOutCode() {
QTextCursor tc = editor->textCursor();
if (tc.selectedText() != "") {
QString tempString = tc.selection().toPlainText();
tempString.prepend("//");
tempString.replace("\n", "\n//");
tc.removeSelectedText();
tc.insertText(tempString);
editor->setTextCursor(tc);
}
}
//uncomment out code action
void GatorText::unCommentOutCode() {
QTextCursor tc = editor->textCursor();
if (tc.selectedText() != "") {
QString tempString = tc.selection().toPlainText();
if (tempString.left(2) == "//") {
tempString = tempString.right(tempString.length() -2);
}
tempString.replace("\n//", "\n");
tc.removeSelectedText();
tc.insertText(tempString);
editor->setTextCursor(tc);
}
}
//quits the program
void GatorText::quitProgram() {
if (checkForSave()) {
QApplication::quit();
}
}
//displays the code viewer
void GatorText::toggleCodeViewer() {
QList<QDockWidget*> dockWidgets = this->findChildren<QDockWidget*>();
foreach (QDockWidget* widget, dockWidgets) {
QString widgetName = widget->widget()->metaObject()->className();
if (widgetName == "QListWidget") {
if (widget->isVisible()) {
widget->setVisible(false);
return;
}
updateAdditionalItems();
QListWidget* listWidget = (QListWidget*) widget->widget();
listWidget->clear();
listWidget->addItems(*additionalItemsPtr);
widget->setVisible(true);
}
}
}
//used by the code viewer to view all occurences of a function or var in code
void GatorText::highlightSelectedWord(QListWidgetItem *item) {
QList<QTextEdit::ExtraSelection> selections = editor->extraSelections();
QTextCursor cursor(editor->document());
QTextCharFormat highlightFormat;
highlightFormat.setBackground(Qt::yellow);
while (!cursor.isNull() && !cursor.atEnd()) {
cursor = editor->document()->find(item->text(), cursor, QTextDocument::FindWholeWords);
//cursor.select(QTextCursor::WordUnderCursor);
//extra selections overrides text format so we have to check for that
if (cursor.block() == selections[0].cursor.block()) {
QTextEdit::ExtraSelection selection;
selection.cursor = cursor;
selection.format.setBackground(Qt::yellow);
selections.append(selection);
editor->setExtraSelections(selections);
} else {
cursor.mergeCharFormat(highlightFormat);
}
}
}
void GatorText::unHighlightSelectedWord(QListWidgetItem *item) {
QList<QTextEdit::ExtraSelection> selections = editor->extraSelections();
selections.clear();
editor->setExtraSelections(selections);
editor->highlightCurrentLine();
QTextCursor cursor(editor->document());
QTextCharFormat highlightFormat;
highlightFormat.setBackground(Qt::white);
while (!cursor.isNull() && !cursor.atEnd()) {
cursor = editor->document()->find(item->text(), cursor, QTextDocument::FindWholeWords);
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(highlightFormat);
}
}
//creates a context menu for when a code viewer item is right clicked
void GatorText::listItemContextSelected(const QPoint &point) {
QList<QDockWidget*> dockWidgets = this->findChildren<QDockWidget*>();
foreach (QDockWidget* widget, dockWidgets) {
QString widgetName = widget->widget()->metaObject()->className();
if (widgetName == "QListWidget") {
QListWidget* list = (QListWidget*) widget->widget();
QListWidgetItem* item = list->itemAt(point);
QString selectedWord = item->text();
QPoint globalPos = list->mapToGlobal(point);
QMenu contextMenu;
QAction* highlightWordAction = new QAction("&Highlight word", this);
QAction* unselectWordAction = new QAction("&Unhighlight word", this);
contextMenu.addAction(highlightWordAction);
contextMenu.addAction(unselectWordAction);
connect(highlightWordAction, &QAction::triggered, [=] {highlightSelectedWord(item);});
connect(unselectWordAction, &QAction::triggered, [=] {unHighlightSelectedWord(item);});
contextMenu.exec(globalPos);
}
}
}
//cut action
void GatorText::cut() {
QClipboard* clipboard = QApplication::clipboard();
QTextCursor cursor = editor->textCursor();
QString selectedText = cursor.selectedText();
if (selectedText != "") {
cursor.removeSelectedText();
clipboard->setText(selectedText);
}
}
//copy action
void GatorText::copy() {
QClipboard* clipboard = QApplication::clipboard();
QTextCursor cursor = editor->textCursor();
QString selectedText = cursor.selectedText();
if (selectedText != "") {
clipboard->setText(selectedText);
}
}
void GatorText::paste() {
QClipboard* clipboard = QApplication::clipboard();
QTextCursor cursor = editor->textCursor();
if (clipboard->mimeData()->hasText()) {
cursor.insertText(clipboard->mimeData()->text());
clipboard->clear();
}
}
//helper function that mapes a languagetype to a string for display purposes
//should rewrite the wordlist to use this same function but it basically does the same process again.
QString GatorText::mapLanguageToEnum(LanguageType type) {
QString language;
switch (type) {
case CPP:
language = "C++";
break;
case C:
language = "C";
break;
case JAVASCRIPT:
language = "Javascript";
break;
case PYTHON:
language = "Python";
break;
case JAVA:
language = "Python";
break;
case NOTSET:
language = "You Should Never See this";
break;
}
return language;
}
//check if current file exist
bool GatorText::currentFileExist() {
if (currentFile.isEmpty()) {
return false;
} else {
return true;
}
}
//opens the find and replace dialog
void GatorText::find() {
FindReplaceDialog* dialog = new FindReplaceDialog(this);
dialog->setEditor(editor);
dialog->show();
}
//indents code
void GatorText::addIndentation() {
QTextCursor tc = editor->textCursor();
QString selectedText = tc.selection().toPlainText();
if (!selectedText.isEmpty()) {
selectedText.prepend("\t");
selectedText.replace(QRegularExpression("\n"), "\n\t");
tc.removeSelectedText();
tc.insertText(selectedText);
}
}
//open the change font dialog
void GatorText::changeFont() {
ChangeFontDialog* dialog = new ChangeFontDialog(this);
dialog->setEditor(editor);
dialog->show();
}
//change font size dialog
void GatorText::changeFontSize() {
ChangeFontSizeDialog* dialog = new ChangeFontSizeDialog(this);
dialog->setEditor(editor);
dialog->show();
}
//i couldnt access the web engine normally so i need to do this shit and find
//it through the parent then update the page
void GatorText::changeWebUrl(QString url) {
QList<QDockWidget*> widgets = this->findChildren<QDockWidget*>();
foreach (QDockWidget* widget, widgets) {
QString name = widget->widget()->metaObject()->className();
if (name == "QWebEngineView") {
QWebEngineView* engine = (QWebEngineView*) widget->widget();
engine->setUrl(url);
}
}
}
//change the theme
void GatorText::changeTheme(QString theme) {
QString filename;
QString stylesheet;
if (theme == "default") {
stylesheet = "";
}
//i had to do this because this file wouldnt work either. Any time I would try it would crash the
//compiler like the webengine. Idk why but this works so dont touch
if (theme == "dark") {
stylesheet = R"(/*
Copyright 2013 Emanuel Claesson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
COLOR_DARK = #191919
COLOR_MEDIUM = #353535
COLOR_MEDLIGHT = #5A5A5A
COLOR_LIGHT = #DDDDDD
COLOR_ACCENT = #3D7848
*/
* {
background: #191919;
color: #DDDDDD;
border: 1px solid #5A5A5A;
}
QWidget::item:selected {
background: #3D7848;
}
QCheckBox, QRadioButton {
border: none;
}
QRadioButton::indicator, QCheckBox::indicator {
width: 13px;
height: 13px;
}
QRadioButton::indicator::unchecked, QCheckBox::indicator::unchecked {
border: 1px solid #5A5A5A;
background: none;
}
QRadioButton::indicator:unchecked:hover, QCheckBox::indicator:unchecked:hover {
border: 1px solid #DDDDDD;
}
QRadioButton::indicator::checked, QCheckBox::indicator::checked {
border: 1px solid #5A5A5A;
background: #5A5A5A;
}
QRadioButton::indicator:checked:hover, QCheckBox::indicator:checked:hover {
border: 1px solid #DDDDDD;
background: #DDDDDD;
}
QGroupBox {
margin-top: 6px;
}
QGroupBox::title {
top: -7px;
left: 7px;
}
QScrollBar {
border: 1px solid #5A5A5A;
background: #191919;
}
QScrollBar:horizontal {
height: 15px;
margin: 0px 0px 0px 32px;
}
QScrollBar:vertical {
width: 15px;
margin: 32px 0px 0px 0px;
}
QScrollBar::handle {
background: #353535;
border: 1px solid #5A5A5A;
}
QScrollBar::handle:horizontal {
border-width: 0px 1px 0px 1px;
}
QScrollBar::handle:vertical {
border-width: 1px 0px 1px 0px;
}
QScrollBar::handle:horizontal {
min-width: 20px;
}
QScrollBar::handle:vertical {
min-height: 20px;
}
QScrollBar::add-line, QScrollBar::sub-line {
background:#353535;
border: 1px solid #5A5A5A;
subcontrol-origin: margin;
}
QScrollBar::add-line {
position: absolute;
}
QScrollBar::add-line:horizontal {
width: 15px;
subcontrol-position: left;
left: 15px;
}
QScrollBar::add-line:vertical {
height: 15px;
subcontrol-position: top;
top: 15px;
}
QScrollBar::sub-line:horizontal {
width: 15px;
subcontrol-position: top left;
}
QScrollBar::sub-line:vertical {
height: 15px;
subcontrol-position: top;
}
QScrollBar:left-arrow, QScrollBar::right-arrow, QScrollBar::up-arrow, QScrollBar::down-arrow {
border: 1px solid #5A5A5A;
width: 3px;
height: 3px;
}
QScrollBar::add-page, QScrollBar::sub-page {
background: none;
}
QAbstractButton:hover {
background: #353535;
}
QAbstractButton:pressed {
background: #5A5A5A;
}
QAbstractItemView {
show-decoration-selected: 1;
selection-background-color: #3D7848;
selection-color: #DDDDDD;
alternate-background-color: #353535;
}
QHeaderView {
border: 1px solid #5A5A5A;
}
QHeaderView::section {
background: #191919;
border: 1px solid #5A5A5A;
padding: 4px;
}
QHeaderView::section:selected, QHeaderView::section::checked {
background: #353535;
}
QTableView {
gridline-color: #5A5A5A;
}
QTabBar {
margin-left: 2px;
}
QTabBar::tab {
border-radius: 0px;
padding: 4px;
margin: 4px;
}
QTabBar::tab:selected {
background: #353535;
}
QComboBox::down-arrow {
border: 1px solid #5A5A5A;
background: #353535;
}
QComboBox::drop-down {
border: 1px solid #5A5A5A;
background: #353535;
}
QComboBox::down-arrow {
width: 3px;
height: 3px;
border: 1px solid #5A5A5A;
}
QAbstractSpinBox {
padding-right: 15px;
}
QAbstractSpinBox::up-button, QAbstractSpinBox::down-button {
border: 1px solid #5A5A5A;
background: #353535;
subcontrol-origin: border;
}
QAbstractSpinBox::up-arrow, QAbstractSpinBox::down-arrow {
width: 3px;
height: 3px;
border: 1px solid #5A5A5A;
}
QSlider {
border: none;
}
QSlider::groove:horizontal {
height: 5px;
margin: 4px 0px 4px 0px;
}
QSlider::groove:vertical {
width: 5px;
margin: 0px 4px 0px 4px;
}
QSlider::handle {
border: 1px solid #5A5A5A;
background: #353535;
}
QSlider::handle:horizontal {
width: 15px;
margin: -4px 0px -4px 0px;
}
QSlider::handle:vertical {
height: 15px;
margin: 0px -4px 0px -4px;
}