-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
314 lines (268 loc) · 9.6 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "assetmanager.h"
#include <QMessageBox>
#include <QtGui>
#include <QSplitter>
#include <QFontDatabase>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QDateTime>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
MainWindow::MainWindow(QWidget *parent, QApplication *main) :
QMainWindow(parent),
ui(new Ui::MainWindow),
mainApp(main)
{
ui->setupUi(this);
textEditTab = new TextEditTabWidget();
fileView = new FileViewWidget();
loadMenuBar();
}
void MainWindow::loadMenuBar()
{
menu_bar = new QMenuBar(this);
menu_bar->setNativeMenuBar(true);
QMenu *oneMenu = new QMenu("File");
QAction *newFileLaunch = oneMenu->addAction("New File");
connect(newFileLaunch, SIGNAL(triggered()), this, SLOT(createFile()));
QAction *fileOpenAction = oneMenu->addAction("Open File");
connect(fileOpenAction, SIGNAL(triggered()), this, SLOT(loadFile()));
QAction *folderOpenAction = oneMenu->addAction("Open Folder");
connect(folderOpenAction, SIGNAL(triggered()), this, SLOT(loadFolder()));
QAction *fileSaveAction = oneMenu->addAction("Save");
connect(fileSaveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
QMenu *windowMenu = new QMenu("Window");
QAction *launchSettingsAction = windowMenu->addAction("More");
connect(launchSettingsAction, SIGNAL(triggered()), this, SLOT(launchSettingsWindow()));
menu_bar->addMenu(oneMenu);
menu_bar->addMenu(windowMenu);
}
void MainWindow::launchSettingsWindow()
{
settingsWindow = new SettingsWindow();
settingsWindow->load(assetManager, this);
connect(settingsWindow, SIGNAL(settingsChanged(AppConfigObject)),
this, SLOT(updateSettings(AppConfigObject)));
settingsWindow->show();
}
void MainWindow::updateSettings(AppConfigObject cfg)
{
mainApp->setStyleSheet(assetManager->loadStyleSheetByFilename(cfg.getStyleSheetLocation()));
}
void MainWindow::saveFile()
{
// saves the current open editor to its filepath
textEditTab->saveCurrentEditor();
}
void MainWindow::loadFolder()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
loadFileToTree(dir);
}
void MainWindow::loadFileToTree(QString dir)
{
if(dir != "")
{
fileView->loadFolder(dir);
// resize to fit with folder view
resizeWithFileView();
ui->welcomeScreen->hide();
}
}
void MainWindow::createFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/home/documents");
QFile file( fileName );
file.open( QIODevice::WriteOnly );
file.close();
loadFileByName(fileName);
}
void MainWindow::loadFileByName(QString &name)
{
textEditTab->addCodeTab(name);
ui->welcomeScreen->hide();
}
void MainWindow::loadFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home/documents");
if(fileName != "")
{
loadFileByName(fileName);
}
}
void MainWindow::load(AssetManager *inAssetManager)
{
// load static files
assetManager = inAssetManager;
textEditTab->load(assetManager);
fileView->load(assetManager, textEditTab);
// Create code edit tab area
textEditTab->setTabShape(QTabWidget::Triangular);
textEditTab->setDocumentMode(false);
textEditTab->setMovable(true);
textEditTab->setTabsClosable(true);
ui->editArea->setWidget(textEditTab);
ui->projectViewArea->setWidget(fileView);
fileViewWidth = ui->projectViewArea->width();
//fileView->loadFolder("/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets");
//ui->welcomeScreen->hide();
//runUnitTests();
if(textEditTab->numOfTabsOpen != 0)
{
ui->welcomeScreen->hide();
}
loadUserCfg();
}
void MainWindow::loadUserCfg()
{
QStringList cfg = assetManager->loadUserCfg();
for(int i = 0; i < cfg.length(); i++)
{
if(i-1 >= 0)
{
// if load tab command found
if(cfg[i-1] == "CT:")
{
loadFileByName(cfg[i]);
}
// if load file directory found
if(cfg[i-1] == "PD:")
{
loadFileToTree(cfg[i]);
}
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QStringList userCfg;
// save user details here
for(int i= 0; i < textEditTab->numOfTabsOpen; i++)
{
userCfg << "CT:" << textEditTab->getEditorAtIndex(i)->getCurrentFileInfo()->absoluteFilePath();
}
QStringList openDirs = fileView->getMainParentFolders();
for(int i2 = 0; i2 < openDirs.length(); i2++)
{
userCfg << "PD:" << openDirs[i2];
}
assetManager->saveUserCfg(userCfg);
}
MainWindow::~MainWindow()
{
delete ui;
delete textEditTab;
textEditTab = NULL;
delete fileView;
fileView = NULL;
}
// Resize the code text area and fileListBox to fit the screen responsively
void MainWindow::resizeEvent(QResizeEvent *event)
{
// project loaded into file view
if(fileView->isProjectLoaded())
{
resizeWithFileView();
} else
{
ui->projectViewArea->resize(0, ui->centralWidget->height());
fileView->resize(0, ui->projectViewArea->height());
ui->editArea->move(0, 0);
ui->editArea->resize(ui->centralWidget->width(), ui->centralWidget->height());
textEditTab->resize(ui->editArea->width(), ui->centralWidget->height());
ui->welcomeScreen->resize(ui->centralWidget->width(), ui->centralWidget->height()+30);
ui->welcomeLabel->resize(ui->welcomeScreen->width(), ui->welcomeLabel->height());
ui->welcomeButtonWidget->resize(ui->welcomeScreen->width(), ui->welcomeButtonWidget->height());
ui->welcomeLabelLogo->resize(ui->welcomeScreen->width(), ui->welcomeLabelLogo->height());
}
}
void MainWindow::runUnitTests()
{
// create timer
QElapsedTimer timer;
// current dat/time for saving
QDateTime now = QDateTime::currentDateTime();
timer.start();
// open file
ofstream testFile;
// get current time to print ot file name
string date_time_now = now.toString("dd.MM.yyyy h:m:s ap").toStdString();
date_time_now = "/Users/jacobplaster/Documents/Able/libs/tests/speedTests/speedTest-(" + date_time_now + ").txt";
testFile.open (date_time_now.c_str());
QStringList dirsToTest;
dirsToTest << "/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets/Zompage-Game";
dirsToTest << "/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets/Nether-Game";
dirsToTest << "/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets/ACW-08338-Student";
dirsToTest << "/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets/folder3";
dirsToTest << "/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets/folder4";
// load initial tab so all resources get initiated
textEditTab->addCodeTab("/Users/jacobplaster/Documents/Able/mainwindow.cpp");
testFile << date_time_now << "\n";
testFile << "------ SPEED TESTING FOR LOADING FUNCTIONS ------\n\n\n";
// Run tests on all files in this dir
QDir lsDir("/Users/jacobplaster/Documents/Able/libs/tests/TestDatasets");
QFileInfoList allLs = lsDir.entryInfoList();
foreach (QFileInfo fileI, allLs){
// if is a file and is a cfg file
if (!fileI.isDir())
{
qDebug() << "Loading: " << fileI.absoluteFilePath();
testFile << fileI.completeSuffix().toStdString() << " test: \n";
testFile << "File: " << fileI.absoluteFilePath().toStdString() << "\n";
timer.restart();
textEditTab->addCodeTab(fileI.absoluteFilePath());
testFile << "Time to load and process: " << timer.elapsed() << " milliseconds \n";
testFile << "File size: " << fileI.size() << "bytes";
testFile << "\n\n\n";
}
}
testFile << "------ SPEED TESTING FOR LOADING FOLDERS ------\n\n\n";
foreach(QFileInfo fileI, dirsToTest)
{
qDebug() << "Loading: " << fileI.absoluteFilePath();
testFile << fileI.fileName().toStdString() << " test: \n";
testFile << "File: " << fileI.absoluteFilePath().toStdString() << "\n";
timer.restart();
fileView->loadFolder(fileI.absoluteFilePath());
testFile << "Time to load directory: " << timer.elapsed() << " milliseconds \n";
testFile << "Folder size: " << fileI.size() << "bytes";
testFile << "\n\n\n";
}
testFile.flush();
testFile.close();
textEditTab->clear();
// open newly created file
textEditTab->addCodeTab(date_time_now.c_str());
}
void MainWindow::resizeWithFileView()
{
ui->projectViewArea->resize(fileViewWidth, ui->centralWidget->height());
fileView->resize(ui->projectViewArea->width(), ui->projectViewArea->height());
ui->editArea->move(ui->projectViewArea->width(), 0);
ui->editArea->resize((ui->centralWidget->width() - ui->projectViewArea->width()) +1, ui->centralWidget->height());
textEditTab->resize(ui->editArea->width(), ui->centralWidget->height());
fileViewWidth = ui->projectViewArea->width();
}
/*
QMessageBox Msgbox;
Msgbox.setText("Resized.");
Msgbox.exec();
*/
void MainWindow::on_welcomeCreateButton_clicked()
{
createFile();
}
void MainWindow::on_welcomeOpenProjectButton_clicked()
{
loadFolder();
}