/
filemanager.cpp
496 lines (396 loc) · 10.9 KB
/
filemanager.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
#include "filemanager.h"
#include <QFileDialog>
#include <QRegularExpression>
#include <QStandardPaths>
FileManager::FileManager(QWidget *parent) :
QTabWidget(parent)
{
setTabsClosable(true);
setMovable(true);
createBackgroundImage();
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeFile(int)));
connect(this, SIGNAL(currentChanged(int)), this, SLOT(changeTab(int)));
createHome();
}
bool FileManager::createHome()
{
QSettings settings;
settings.beginGroup("Paths");
QDir projects(QDir::homePath() + "/" + tr("PropellerIDE Projects"));
if (!projects.exists())
{
if (!QDir().mkdir(projects.path()))
{
qCWarning(logfilemanager) << "failed to create projects folder:" << projects.path();
return false;
}
}
settings.setValue("homeDirectory", projects.path());
if (settings.value("lastDirectory").toString().isEmpty())
settings.setValue("lastDirectory", projects.path());
settings.endGroup();
return true;
}
QString FileManager::getDirectory()
{
QSettings settings;
settings.beginGroup("Paths");
return settings.value("lastDirectory",
settings.value("homeDirectory", QDir::homePath()).toString()).toString();
}
void FileManager::setDirectory(QString dir)
{
QSettings settings;
settings.beginGroup("Paths");
settings.setValue("lastDirectory", dir);
}
// THIS IS A HACK OMG SUCH A HACK
void FileManager::setLanguage(Language * language)
{
this->language = language;
}
int FileManager::newFile()
{
// removes the background image (need to move this elsewhere)
setStyleSheet("");
Editor * editor = new Editor(language, QWidget::window());
editor->setAttribute(Qt::WA_DeleteOnClose);
editor->installEventFilter(QWidget::window());
editor->saveContent();
int index = addTab(editor,tr("Untitled"));
setTabToolTip(index,"");
connect(editor,SIGNAL(textChanged()),this,SLOT(fileChanged()));
connect(editor,SIGNAL(undoAvailable(bool)),this,SLOT(setUndo(bool)));
connect(editor,SIGNAL(redoAvailable(bool)),this,SLOT(setRedo(bool)));
connect(editor,SIGNAL(copyAvailable(bool)),this,SLOT(setCopy(bool)));
emit closeAvailable(true);
setCurrentIndex(index);
return index;
}
void FileManager::setUndo(bool available)
{
if (sender() == currentWidget())
emit undoAvailable(available);
}
void FileManager::setRedo(bool available)
{
if (sender() == currentWidget())
emit redoAvailable(available);
}
void FileManager::setCopy(bool available)
{
if (sender() == currentWidget())
emit copyAvailable(available);
}
void FileManager::open()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this,
tr("Open File"), getDirectory(), "Spin Files (*.spin);;All Files (*)");
setDirectory(QDir(fileNames[0]).path());
for (int i = 0; i < fileNames.size(); i++)
if (!fileNames.at(i).isEmpty())
openFile(fileNames.at(i));
}
int FileManager::isFileOpen(const QString & fileName)
{
// check if file already open before opening another instance
for (int i = 0; i < count(); i++)
{
if (fileName == tabToolTip(i))
{
setCurrentIndex(i);
emit sendMessage(tr("File already open: %1").arg(fileName));
return 1;
}
}
return 0;
}
int FileManager::isFileEmpty(int index)
{
if (count() && (tabToolTip(index).isEmpty()
&& getEditor(index)->toPlainText().isEmpty()
&& !getEditor(index)->contentChanged()) )
return 1;
else
return 0;
}
QString FileManager::reformatText(QString text)
{
QRegularExpression newlines("\r\n|\r|\n",QRegularExpression::DotMatchesEverythingOption);
text.replace(newlines,"\n");
return text;
}
int FileManager::openFile(const QString & fileName)
{
qCDebug(logfilemanager) << "opening" << fileName;
if (fileName.isEmpty())
return 1;
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
{
QMessageBox::warning(this, tr("Warning"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return 1;
}
if (isFileOpen(fileName))
return 1;
int index;
if (isFileEmpty(currentIndex()))
index = currentIndex();
else
index = newFile();
QTextStream in(&file);
in.setAutoDetectUnicode(true);
in.setCodec("UTF-8");
getEditor(index)->setPlainText(reformatText(in.readAll()));
setTabToolTip(index,QFileInfo(fileName).canonicalFilePath());
setTabText(index,QFileInfo(fileName).fileName());
getEditor(index)->saveContent();
fileChanged();
emit fileUpdated(index);
emit sendMessage(tr("File opened successfully: %1").arg(fileName));
return 0;
}
void FileManager::newFromFile()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("New From File..."), getDirectory(), "Spin Files (*.spin);;All Files (*)");
if (!fileName.isEmpty())
newFromFile(fileName);
}
int FileManager::newFromFile(const QString & fileName)
{
qCDebug(logfilemanager) << "creating file from" << fileName;
if (fileName.isEmpty())
return 1;
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
{
QMessageBox::warning(this, tr("Warning"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return 1;
}
int index = newFile();
QTextStream in(&file);
in.setAutoDetectUnicode(true);
in.setCodec("UTF-8");
getEditor(index)->setPlainText(reformatText(in.readAll()));
fileChanged();
emit fileUpdated(index);
return 0;
}
void FileManager::save()
{
save(currentIndex());
}
void FileManager::save(int index)
{
QString fileName = tabToolTip(index);
if (fileName.isEmpty())
saveAs(index);
else
saveFile(fileName, index);
}
void FileManager::saveAs()
{
saveAs(currentIndex());
}
void FileManager::saveAs(int index)
{
QString fileName = tabToolTip(index);
QString lastDir = QDir(fileName).path();
if (fileName.isEmpty())
{
fileName = tr("Untitled.spin");
lastDir = getDirectory();
}
fileName = QFileDialog::getSaveFileName(this,
tr("Save File As..."),
lastDir,
tr("Spin Files (*.spin)"));
if (fileName.isEmpty())
return;
saveFile(fileName, index);
}
void FileManager::saveAll()
{
for (int i = 0; i < count(); i++)
save(i);
}
void FileManager::saveFile(const QString & fileName, int index)
{
qCDebug(logfilemanager) << "saving" << fileName;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Recent Files"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QTextStream os(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
os.setCodec("UTF-8");
os << getEditor(index)->toPlainText();
os.flush();
QApplication::restoreOverrideCursor();
setTabToolTip(index,QFileInfo(fileName).canonicalFilePath());
setTabText(index,QFileInfo(fileName).fileName());
getEditor(index)->saveContent();
fileChanged();
emit fileUpdated(index);
emit sendMessage(tr("File saved successfully: %1").arg(fileName));
}
void FileManager::closeFile()
{
closeFile(currentIndex());
}
void FileManager::closeAll()
{
setCurrentIndex(0);
while (count() > 0)
{
if (!closeFile(0))
return;
}
}
bool FileManager::saveAndClose(int index)
{
QMessageBox dialog;
dialog.setText(tr("Your code has been modified."));
dialog.setInformativeText(tr("Do you want to save your changes?"));
dialog.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
dialog.setDefaultButton(QMessageBox::Cancel);
switch (dialog.exec())
{
case QMessageBox::Save:
save(index);
return true;
break;
case QMessageBox::Discard:
return true;
break;
case QMessageBox::Cancel:
default:
break;
}
return false;
}
bool FileManager::closeFile(int index)
{
if (getEditor(index)->contentChanged())
{
if (!saveAndClose(index))
{
return false;
}
}
if (count() > 0 && index >= 0 && index < count())
{
getEditor(index)->disconnect();
getEditor(index)->close();
removeTab(index);
}
if (count() == 0)
{
createBackgroundImage();
emit closeAvailable(false);
}
return true;
}
void FileManager::createBackgroundImage()
{
setStyleSheet("background-image: url(:/icons/propellerhat.png);"
"background-repeat: no-repeat;"
"background-position: center;");
}
void FileManager::nextTab()
{
int n = currentIndex();
n++;
if (n > count()-1)
n = 0;
setCurrentIndex(n);
}
void FileManager::previousTab()
{
int n = currentIndex();
n--;
if (n < 0)
n = count()-1;
setCurrentIndex(n);
}
// this function is needed to set focus after changing the index
void FileManager::changeTab(int index)
{
if(index < 0) return;
Editor * editor = getEditor(currentIndex());
editor->setFocus();
emit undoAvailable(editor->getUndo());
emit redoAvailable(editor->getRedo());
emit copyAvailable(editor->getCopy());
emit fileUpdated(index);
}
Editor * FileManager::getEditor(int num)
{
return (Editor *)widget(num);
}
// this needs some rethinking a lot
void FileManager::fileChanged()
{
int index = currentIndex();
QString file = tabToolTip(index);
QString name = QFileInfo(file).fileName();
if (file.isEmpty())
name = tr("Untitled");
if (getEditor(index)->contentChanged())
{
name += '*';
emit saveAvailable(true);
}
else
{
emit saveAvailable(false);
}
setTabText(index, name);
}
// does anybody have a good idea of how to
// replace this hacky structure with something
// more flexible?
// -------------------------------------------
void FileManager::cut()
{
if (count() > 0)
getEditor(currentIndex())->cut();
}
void FileManager::copy()
{
if (count() > 0)
getEditor(currentIndex())->copy();
}
void FileManager::paste()
{
if (count() > 0)
getEditor(currentIndex())->paste();
}
void FileManager::undo()
{
if (count() > 0)
getEditor(currentIndex())->undo();
}
void FileManager::redo()
{
if (count() > 0)
getEditor(currentIndex())->redo();
}
void FileManager::selectAll()
{
if (count() > 0)
getEditor(currentIndex())->selectAll();
}
// -------------------------------------------