-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeeditor.cpp
More file actions
485 lines (367 loc) · 14.3 KB
/
codeeditor.cpp
File metadata and controls
485 lines (367 loc) · 14.3 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
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
#include "codeeditor.h"
#include "./ui_codeeditor.h"
#include "openedfiletab.h"
#include <QAction>
#include <QFileDialog>
#include <QFileSystemModel>
#include <QDir>
#include <QTreeWidgetItemIterator>
#include <QTreeView>
#include <QAbstractScrollArea>
#include <QToolBar>
#include <QMessageBox>
#include <QInputDialog>
#include <QHash>
CodeEditor::CodeEditor(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::CodeEditor) {
ui->setupUi(this);
// Set default values of variables
currentTab = nullptr;
highlighter = new Highlighter();
this->blockChange = this->contentChanged = false;
// Set up hash map for icons
icons.insert("cpp", ":/Resources/Resources/Logos/cpp_logo_icon.svg");
icons.insert("c",":/Resources/Resources/Logos/c_logo_icon.svg");
icons.insert("h", ":/Resources/Resources/Logos/header_logo_icon.svg");
// Checks if the text in editor has changed
connect(ui->editor,&QPlainTextEdit::textChanged, [this]()->void{
if (!this->blockChange)
this->contentChanged = true;
});
// Set up the toolbar
QToolBar* toolBar = new QToolBar();
ui->toolbarHolder->addWidget(toolBar);
toolBar->addAction(ui->actionRemove);
toolBar->addAction(ui->actionNew_Folder);
toolBar->addAction(ui->actionNew_File);
toolBar->addAction(ui->actionSave);
// Set up the font size of the editor
ui->editor->setFontSize(14);
// Set up all the connections for actions
setUpMenu();
// Set up default values for the tree directory view
setUpTreeView();
// Checks the editor for status
checkEditor();
}
CodeEditor::~CodeEditor() {
for (QList<activeFileInformation*>::ConstIterator i = activeFiles.constBegin() ; i < activeFiles.constEnd(); i++)
delete *i;
activeFiles.clear();
delete highlighter;
delete ui;
}
/* SET UPS */
void CodeEditor::setUpMenu() {
connect(ui->actionAbout_QT, &QAction::triggered, this, QApplication::aboutQt);
connect(ui->actionQuit, &QAction::triggered, this, QApplication::quit);
connect(ui->actionOpen_Folder, &QAction::triggered, this, &CodeEditor::openFolder);
connect(this, &CodeEditor::workingDirectoryChanged, this, &CodeEditor::updateTreeView);
connect(ui->actionAbout_CodeLab_Notes, &QAction::triggered, this, &CodeEditor::aboutCodeLabNotes);
connect(ui->actionNew_File, &QAction::triggered, this, &CodeEditor::createNewFile);
connect(ui->actionNew_Folder, &QAction::triggered, this, &CodeEditor::createNewFolder);
//connect(ui->actionSave, &QAction::triggered, this, &CodeEditor::saveFile);
connect(ui->actionSave_All, &QAction::triggered, this, &CodeEditor::saveAllFiles);
connect(ui->actionSave_as, &QAction::triggered, this, &CodeEditor::saveFileAs);
connect(ui->actionAuto_Save, &QAction::toggled, this, &CodeEditor::autoSaveToggle);
connect(ui->actionRemove, &QAction::triggered, this, &CodeEditor::deleteFile);
connect(ui->actionOpen_File, &QAction::triggered, [this]()->void{
QString filePath = QFileDialog::getOpenFileName(this,"Select the file to open",this->workingDirectory), fileName;
if (filePath.isEmpty())
return;
unsigned int index = filePath.size();
while(filePath[--index] != '/');
fileName = filePath.sliced(index+1);
this->openFile(filePath, fileName);
});
connect(this, &CodeEditor::workingDirectoryChanged, [this]()->void{
qDebug() << "Working directory changed!";
});
connect(ui->actionSave, &QAction::triggered, [this]()->void{
if (this->currentTab != nullptr)
this->saveFile(this->currentTab->fileInfo,ui->editor->toPlainText());
});
}
void CodeEditor::setUpTreeView()
{
this->dirModel = new QFileSystemModel(this);
this->dirModel->setFilter(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
this->dirModel->setRootPath(QString());
this->dirModel->setReadOnly(true);
this->workingDirectory = QString();
ui->treeView->setAnimated(true);
ui->treeView->setModel(this->dirModel);
ui->treeView->hideColumn(1);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
}
void CodeEditor::checkEditor() {
if (activeFiles.size() == 0) {
ui->editor->setPlaceholderText("");
ui->editor->hide();
}else if (activeFiles.size() > 0) {
ui->editor->show();
}
}
void CodeEditor::setupEditor() {
this->blockChange = true;
ui->editor->setPlainText(currentTab->code);
this->blockChange = false;
}
/* ACTIVE FILE INFORMATION STRUCT SET UP */
/* Other slots */
void CodeEditor::aboutCodeLabNotes() {
QMessageBox msgBox(this);
msgBox.setWindowTitle("About CodeLab Notes");
msgBox.setTextFormat(Qt::RichText);
msgBox.setText("This program is made for the CS103 project at IUS.<br>"
"Made by : <br>"
"<a href= \"https://github.com/VedadSiljic\">Vedad Siljic</a><br>"
"<a href= \"https://github.com/EmreArapcicUevak\">Emre Arapcic Uevak</a><br>"
"Source code : <br><a href= \"https://github.com/EmreArapcicUevak/CodeLab-Notes\">https://github.com/EmreArapcicUevak/CodeLab-Notes</a>");
msgBox.exec();
}
void CodeEditor::on_actionChange_Font_Size_triggered() {
int fontSize = ui->editor->getFontSize();
fontSize = QInputDialog::getInt(this, "Change Font Size", "Font Size : ", fontSize);
ui->editor->setFontSize(fontSize);
}
/*
*
*
* CODE EDITOR BASED SLOTS
*
*
*/
void CodeEditor::createTab(activeFileInformation& fileInfo, QString& code, bool pressed) {
OpenedFileTab* tab = new OpenedFileTab(fileInfo);
tab->changeColor();
tab->code = code;
qDebug() << "Current Extension is : " << tab->fileExtension;
QString iconLocation = icons.contains(fileInfo.fileExtension) ? icons.value(fileInfo.fileExtension) : ":/Resources/Resources/Logos/text_logo_icon.svg";
QPixmap icon(iconLocation);
tab->iconHolder->setPixmap(icon.scaled(15, 15, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
ui->tabContainer->addWidget(tab, 0, Qt::AlignLeft);
connect(tab, &OpenedFileTab::tabClosed, this, &CodeEditor::fileCloseSlot);
connect(tab, &OpenedFileTab::thisTabPressed, this, &CodeEditor::tabChangedProcess);
connect(tab, &OpenedFileTab::thisTabClosed, this, &CodeEditor::tabClosedProcess);
if (currentTab != nullptr)
currentTab->changeColor();
currentTab = tab;
activeTabs.append(tab);
setupEditor();
setHighlighting();
}
void CodeEditor::tabChangedProcess(OpenedFileTab* tab) {
if (tab == currentTab)
return;
QString code = ui->editor->toPlainText();
if (ui->actionAuto_Save->isChecked() && this->contentChanged){
this->saveFile(currentTab->fileInfo, code);
qDebug() << "File saved" << currentTab->fileInfo.fileName;
}
currentTab->code = code;
currentTab->changeColor();
currentTab = tab;
currentTab->changeColor();
this->contentChanged = false;
setupEditor();
setHighlighting();
}
void CodeEditor::tabClosedProcess(OpenedFileTab* tab) {
for (int i = 0; i < activeTabs.count(); i++) {
if (activeTabs[i] == tab) {
activeTabs.removeAt(i);
break;
}
}
if (tab != currentTab)
return;
if (activeTabs.count() == 0) {
currentTab = nullptr;
return;
}
currentTab = activeTabs[0];
currentTab->changeColor();
setupEditor();
setHighlighting();
}
void CodeEditor::displayFile() {
if (!activeFiles[activeFiles.size() - 1]->fileInstance->open(QFile::ReadWrite)) {
qDebug() << "File did not open";
return;
}
QTextStream textStream(activeFiles[activeFiles.size() - 1]->fileInstance);
QString text = textStream.readAll();
this->blockChange = true;
ui->editor->setPlainText(text);
this->blockChange = false;
createTab(*activeFiles[activeFiles.size() - 1], text);
checkEditor();
}
void CodeEditor::on_actionUndo_triggered() {
ui->editor->undo();
}
void CodeEditor::on_actionRedo_triggered() {
ui->editor->redo();
}
void CodeEditor::on_actionCut_triggered() {
ui->editor->cut();
}
void CodeEditor::on_actionCopy_triggered() {
ui->editor->copy();
}
void CodeEditor::on_actionPaste_triggered() {
ui->editor->paste();
}
/*
*
* FILE BASED SLOTS
*
*/
void CodeEditor::setWorkingDirectory(const QString &newWorkingDirectory){
workingDirectory = newWorkingDirectory;
emit this->workingDirectoryChanged();
}
void CodeEditor::fileCloseSlot(QString filePath) {
qDebug() << filePath;
for (QList<activeFileInformation*>::ConstIterator i = activeFiles.constBegin() ; i < activeFiles.constEnd(); i++)
if ((*i)->fileInstance->fileName() == filePath) {
activeFiles.erase(i);
checkEditor();
break;
}
}
void CodeEditor::setHighlighting() {
if (currentTab->fileExtension == "cpp" || currentTab->fileExtension == "c" || currentTab->fileExtension == "h") {
highlighter->setDocument(ui->editor->document());
}
else {
highlighter->setDocument(nullptr);
}
}
void CodeEditor::openFile(const QString &filePath, const QString &fileName) {
QFileInfo fileInfo(filePath);
if (fileInfo.exists() && fileInfo.isFile()){
for (unsigned int i = 0; i < activeFiles.count(); i++)
if (activeFiles[i]->fileInstance->fileName() == filePath){
QMessageBox::warning(this,"File couldn't open", "This file is already open", QMessageBox::Ok,QMessageBox::Ok);
return;
}
activeFiles.append(new activeFileInformation(fileName,new QFile(filePath)));
displayFile();
}else
QMessageBox::warning(this,"File couldn't open", "There was a problem trying to open a file", QMessageBox::Ok,QMessageBox::Ok);
}
void CodeEditor::openFolder(){
QString selectedDirectory = QFileDialog::getExistingDirectory(this,"Select Working Directory", QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!selectedDirectory.isEmpty()){
this->workingDirectory = selectedDirectory;
unsigned int i = selectedDirectory.size();
while (selectedDirectory[--i] != '/');
this->rootFileName = selectedDirectory.sliced(i+1);
emit this->workingDirectoryChanged();
}
}
void CodeEditor::createNewFile(){
QString newFilePath = QFileDialog::getSaveFileName(this,"Save File",this->workingDirectory);
qDebug() << "New file name is " << newFilePath;
if (newFilePath.isEmpty())
return;
else{
QFile file(newFilePath);
qDebug() << file.exists();
if (!file.exists())
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
file.close();
else
QMessageBox::warning(this,"Can not make wanted file","Something happened while trying to make the file.");
else if(file.open(QIODevice::WriteOnly | QIODevice::Text))
file.close();
else
QMessageBox::warning(this,"Can not replace wanted file","This file is currently being used by another application.");
}
}
void CodeEditor::createNewFolder()
{
QString newFolderPath = QFileDialog::getSaveFileName(this,"Save Folder",this->workingDirectory, "Folder (*)");
qDebug() << newFolderPath;
if (newFolderPath.isEmpty())
return;
else{
QDir dir(newFolderPath);
if (dir.exists())
QMessageBox::warning(this,"Can not make this directory","A folder with this name already exists");
else if (!dir.mkpath(newFolderPath))
QMessageBox::warning(this,"Can not make this directory","Something went wrong while creating the directory");
}
}
void CodeEditor::saveFile(activeFileInformation& filePath, const QString& fileContent){
filePath.fileInstance->close();
if (filePath.fileInstance->open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out(filePath.fileInstance);
out << fileContent;
filePath.fileInstance->close();
filePath.fileInstance->open(QFile::ReadWrite);
}else
QMessageBox::warning(this,"File failed to save", filePath.fileInstance->fileName() + " failed to save");
}
void CodeEditor::saveAllFiles(){
for (QList<OpenedFileTab*>::iterator i = activeTabs.begin() ; i < activeTabs.end(); i++)
if (*i != this->currentTab)
this->saveFile((*i)->fileInfo,(*i)->code);
else
this->saveFile((*i)->fileInfo,ui->editor->toPlainText());
}
void CodeEditor::saveFileAs(){
if (this->currentTab == nullptr)
return;
else{
QString fileName,newFilePath = QFileDialog::getSaveFileName(this,"Select how you want to save your file",this->workingDirectory);
if (newFilePath.isEmpty())
return;
QFile tempFile(newFilePath);
unsigned int index = newFilePath.size();
for (QString::iterator i = newFilePath.end(); i > newFilePath.begin(); i--,index--)
if (*i == '/'){
fileName = newFilePath.sliced(index+1);
break;
}
activeFileInformation* fileInfo = new activeFileInformation(fileName,&tempFile);
this->saveFile(*fileInfo, ui->editor->toPlainText());
}
}
void CodeEditor::autoSaveToggle(const bool state){
if (state)
this->statusBar()->showMessage("Auto saved turned on!");
else
this->statusBar()->showMessage("Auto saved turned off!");
}
void CodeEditor::deleteFile()
{
QModelIndex currentIndex = ui->treeView->currentIndex();
QString filePath = dirModel->filePath(ui->treeView->currentIndex());
if (filePath.isEmpty())
return;
QFileInfo fileInfo(filePath);
if (fileInfo.isFile()){
QFile fileToDelete(filePath);
if (!fileToDelete.remove())
QMessageBox::information(this, "File has failed to delete", "The file you tried to delete is currently being used by an application.");
}else if (fileInfo.isDir())
if (!dirModel->rmdir(currentIndex))
QMessageBox::information(this, "Folder has failed to delete", "The folder you tried to delete is currently being used by an application.");
}
/*
*
* TREE VIEW BASED SLOTS
*
*/
void CodeEditor::on_treeView_doubleClicked(const QModelIndex &index){
openFile(dirModel->filePath(index), index.data().toString());
}
void CodeEditor::updateTreeView(){
ui->treeView->setRootIndex(this->dirModel->index(this->workingDirectory));
}
/* */