This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.cpp
506 lines (392 loc) · 17.8 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
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
// Copyright (C) 2012-2015, Ali Baharev
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#include <QCloseEvent>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QMenuBar>
#include <QStatusBar>
#include <QToolBar>
#include <QVBoxLayout>
#include <QVector>
#include <string>
#include <vector>
#include "MainWindow.hpp"
#include "ErrorMsg.hpp"
#include "GlobalSettings.hpp"
#include "InfoWidget.hpp"
#include "Launcher.hpp"
#include "ReservedColumnNames.hpp"
#include "Runner.hpp"
#include "SettingsWidget.hpp"
namespace {
const char TITLE[] = " - Structural Geology to PostScript";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), fileDialog(new QFileDialog(this))
{
setWindowTitle(QString("SG2PS")+TITLE);
set_menu();
add_elements();
startDir = opts().getStartBrowsingFromDirectory();
connect_signal_slots();
}
void MainWindow::connect_signal_slots() {
connect(infoWidget, SIGNAL(runModeChanged(bool)),
settingsWidget, SLOT(setRunMode(bool)));
connect(settingsWidget, SIGNAL(newSettingsFileLoaded(bool,bool)),
infoWidget, SLOT(newSettingsFileLoaded(bool,bool)));
connect(runner, SIGNAL(generateSetFile()), settingsWidget, SLOT(writeSettings()));
// TODO I am not sure why this one is needed and it unconditionally sets the *.set
// file satuts to OK, which may not be true...
connect(runner, SIGNAL(generateSetFile()), infoWidget, SLOT(checkSetFile()));
}
void MainWindow::add_elements() {
QWidget* centralWidget = new QWidget(this);
mainLayout = new QVBoxLayout;
infoWidget = new InfoWidget(this);
mainLayout->addWidget(infoWidget);
settingsWidget = new SettingsWidget(this);
mainLayout->addWidget(settingsWidget);
statusBar = new QStatusBar;
statusBar->setStyleSheet("QStatusBar { border: 1px solid grey; border-radius: 3px; } ");
statusBar->showMessage("Ready", 2000);
setStatusBar(statusBar);
runner = new Runner(this, statusBar);
mainLayout->addWidget(runner);
mainLayout->addStretch(1);
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
}
void MainWindow::fixSize() {
setFixedSize(width(), height());
infoWidget->freezeLabelSize();
qDebug() << "Size: " << width() << "x" << height();
}
void MainWindow::set_menu() {
QAction *showAbout = new QAction("&About SG2PS", this);
connect(showAbout, SIGNAL(triggered()), SLOT(about()));
QAction *showAboutQt = new QAction("About &Qt", this);
connect(showAboutQt, SIGNAL(triggered()), SLOT(aboutQt()));
QAction *editSettings = new QAction("&GUI Settings", this);
connect(editSettings, SIGNAL(triggered()), SLOT(editGUISettings()));
QAction *manual = new QAction(QIcon(":/images/acroread48.png"), "&Manual", this);
connect(manual, SIGNAL(triggered()), SLOT(showManual()));
QAction* demo = new QAction(QIcon(":/images/blockdevice-cubes-icon.png"), "&Demo", this) ;
connect(demo, SIGNAL(triggered()), SLOT(runDemo()));
QAction *homepage = new QAction(QIcon(":/images/package_internet48.png"), "Jump to the &homepage", this);
connect(homepage, SIGNAL(triggered()), SLOT(showHomepage()));
QAction *paper = new QAction("&Jump to the paper", this);
connect(paper, SIGNAL(triggered()), SLOT(showPaper()));
QAction* createRGF = new QAction(QIcon(":/images/insert_table48.png"), "&New Data", this);
connect(createRGF, SIGNAL(triggered()), SLOT(newRGFRequested()));
QAction* createXY = new QAction(QIcon(":/images/document_new48.png"), "New &Coordinate", this);
connect(createXY, SIGNAL(triggered()), SLOT(newXYRequested()));
QAction* editRGF = new QAction(QIcon(":/images/spreadsheet48.png"), "&Edit Data", this);
connect(editRGF, SIGNAL(triggered()), SLOT(editRGFRequested()));
QAction* editXY = new QAction(QIcon(":/images/spreadsheetB48.png"), "Edit C&oordinate", this);
connect(editXY, SIGNAL(triggered()), SLOT(editXYRequested()));
QAction* loadRGF = new QAction(QIcon(":/images/document_import48.png"), "&Select Data", this);
connect(loadRGF, SIGNAL(triggered()), SLOT(loadRGFRequested()));
QAction* createTRJ = new QAction(QIcon(":/images/new_trj.png"), "New &Trajectory", this);
connect(createTRJ, SIGNAL(triggered()), SLOT(newTRJRequested()));
QAction* editTRJ = new QAction(QIcon(":/images/edit_trj.png"), "Edit T&rajectory", this);
connect(editTRJ, SIGNAL(triggered()), SLOT(editTRJRequested()));
QAction* closeApplication = new QAction(QIcon(":/images/exit.png"), "E&xit", this);
connect(closeApplication, SIGNAL(triggered()), SLOT(close()));
QMenu* file = menuBar()->addMenu("&File");
file->addAction(createRGF);
file->addAction(editRGF);
file->addAction(loadRGF);
file->addAction(createXY);
file->addAction(editXY);
file->addAction(createTRJ);
file->addAction(editTRJ);
file->addAction(manual);
file->addAction(demo);
file->addAction(closeApplication);
QToolBar* fileToolBar = new QToolBar(this);
fileToolBar->setAllowedAreas(Qt::LeftToolBarArea);
fileToolBar->setOrientation(Qt::Vertical);
fileToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
fileToolBar->setMovable(false);
fileToolBar->addAction(createRGF);
fileToolBar->addAction(editRGF);
fileToolBar->addAction(loadRGF);
fileToolBar->addAction(createXY);
fileToolBar->addAction(editXY);
fileToolBar->addAction(createTRJ);
fileToolBar->addAction(editTRJ);
fileToolBar->addAction(manual);
fileToolBar->addAction(demo);
addToolBar(Qt::LeftToolBarArea, fileToolBar);
QMenu* settingsMenu = menuBar()->addMenu("&Advanced");
settingsMenu->addAction(editSettings);
QMenu* help = menuBar()->addMenu("&Help");
help->addAction(manual);
help->addAction(demo);
help->addAction(homepage);
help->addAction(paper);
help->addAction(showAboutQt);
help->addAction(showAbout);
}
void MainWindow::closeEvent(QCloseEvent* event) {
bool quit = confirmQuit();
if (quit)
QMainWindow::closeEvent(event);
else
event->ignore();
}
bool MainWindow::confirmQuit() {
bool quit = true;
// Compare with ConvertAllEps.cpp loop() and Runner onConversionFinished()
const QString& msg = statusBar->currentMessage();
bool busy = msg.startsWith("Converting ") || msg.startsWith("Concatenating ");
//busy = true; // uncomment for testing
if (busy) {
int ret = QMessageBox::warning(this, "SG2PS",
"The EPS to PDF conversions are still running.\n"
"Are you sure you want to abort them and exit?",
QMessageBox::Yes | QMessageBox::Cancel);
quit = ret==QMessageBox::Yes;
}
return quit;
}
void MainWindow::about() {
QString msg("SG2PS "+QString::fromStdWString(L"—") +" Structural Geology to PostScript\n\n");
msg.append( "Please cite us!\n\n" +
QString::fromStdWString(L"Á. Sasvári, ") +
"A. Baharev. SG2PS (structural geology to postscript "
"converter) " + QString::fromStdWString(L" — ")+
"A graphical solution for brittle structural data "
"evaluation and paleostress calculation.\n"
"Computers and Geosciences. 66, 81-93, 2014\n\n"
"http://dx.doi.org/10.1016/j.cageo.2013.12.010\n\n");
msg.append("You can reach us at sg2ps@sg2ps.eu.\n\n");
msg.append("The SG2PS program is provided AS IS with NO WARRANTY OF ANY KIND, "
"INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
"FOR A PARTICULAR PURPOSE.\n\n");
msg.append("Command line application built on ");
msg.append(back_end_was_built_on()+"\n");
msg.append( "GUI built on " __DATE__ " at " __TIME__ "\n\n");
msg.append( "The heavy lifting is done by the back end, the external "
"command line application sg2ps, written and maintained by ");
msg.append(QString::fromStdWString(L"Ágoston Sasvári.\n\n"));
msg.append( "This graphical front end is written and maintained by Ali Baharev "
"and is based on Qt " QT_VERSION_STR ".\n\n"
"The final PDF files are generated by GPL Ghostscript which comes "
"with the GNU General Public License.\n\n"
"The PDF files are concatenated with PyPDF2, which comes with the "
"3-clause BSD license. It requires a Python interpreter which comes "
"with the Python Software Foundation License (PSFL). For details, please "
"consult the LICENSES.txt file in your installation directory.");
QMessageBox::about(this, "About SG2PS", msg);
qDebug() << "Built on " << __DATE__ << " at " << __TIME__;
}
void MainWindow::aboutQt() {
QMessageBox::about(this, "About Qt",
"This program uses Qt version " QT_VERSION_STR ".\n\n"
"Qt is a C++ toolkit for cross-platform application development.\n\n"
"Qt provides single-source portability across MS Windows, Mac OS X, "
"Linux, and all major commercial Unix variants. Qt is also available "
"for embedded devices as Qt for Embedded Linux and Qt for Windows CE.\n\n"
"Qt is available under three different licensing options designed to "
"accommodate the needs of our various users.\n\n"
"Qt licensed under our commercial license agreement is appropriate for "
"development of proprietary/commercial software where you do not want "
"to share any source code with third parties or otherwise cannot "
"comply with the terms of the GNU LGPL version 2.1 or GNU GPL version "
"3.0.\n\n"
"Qt licensed under the GNU LGPL version 2.1 is appropriate for the "
"development of Qt applications (proprietary or open source) provided "
"you can comply with the terms and conditions of the GNU LGPL version "
"2.1.\n\n"
"Qt licensed under the GNU General Public License version 3.0 is "
"appropriate for the development of Qt applications where you wish to "
"use such applications in combination with software subject to the "
"terms of the GNU GPL version 3.0 or where you are otherwise willing "
"to comply with the terms of the GNU GPL version 3.0.\n\n"
"Please see qt.nokia.com/products/licensing for an overview of Qt "
"licensing.\n\n"
"Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).\n\n"
"Qt is a Nokia product. See qt.nokia.com for more information.\n\n");
}
void MainWindow::editGUISettings() {
QString path = QDir::currentPath();
openInTextEditor(path+"/"+opts().getSettingsFileName());
QMessageBox mbox;
mbox.setWindowTitle("Information");
mbox.setText("Only if you are done editing the settings file, hit the button below!");
mbox.setIcon(QMessageBox::Information);
mbox.setButtonText(QMessageBox::Ok, "OK, I am done, reload the settings now!");
mbox.exec();
reloadGlobalSettings(); // I don't like it, global settings are now mutable
}
void MainWindow::showHomepage() {
openWithDefaultApp("http://www.sg2ps.eu/index.htm?version="+version_id_of_back_end());
}
void MainWindow::showPaper() {
openWithDefaultApp("http://dx.doi.org/10.1016/j.cageo.2013.12.010");
}
void MainWindow::showManual() {
QString path = QDir::currentPath();
openPDF(path+"/manual.pdf");
}
void MainWindow::runDemo() {
if (!isInstalledSoftwareOK())
return;
if (!setupDemoRgf())
return;
// The demo is run in well mode by default and we assume no existing set file
bool isWell = true;
bool cleanSetFile = false;
infoWidget->newSettingsFileLoaded(isWell, cleanSetFile);
QMessageBox mbox;
mbox.setText("Hit the <b>Run</b> button at the bottom of the main window. Let\'s hope everthing works.");
mbox.exec();
}
bool MainWindow::setupDemoRgf() {
QString src = QCoreApplication::applicationDirPath()+"/demo.rgf";
src = QDir::toNativeSeparators(src);
if (!QFileInfo(src).exists()) {
showErrorMsg("problems with installation, the demo.rgf file is missing");
return false;
}
return copyDemoRgf(src);
}
bool MainWindow::copyDemoRgf(const QString& src) {
QMessageBox mbox;
mbox.setText("You will be asked to save the demo data file somewhere.\n"
"Choose a directory at your convenience.");
mbox.exec();
QString dest = fileDialog->getSaveFileName(this, "Select where you wish to place the demo file",
startDir+"/demo.rgf", "*.rgf (*.rgf)");
if (dest.isEmpty()) {
return false;
}
dest = QDir::toNativeSeparators(dest);
if (src!=dest) { // stupid but legal to overwrite the file with itself
QFile::remove(dest); // User already confirmed forced overwrite
if (!QFile::copy(src,dest)) {
showErrorMsg("copying "+src+" to "+dest+" failed");
return false;
}
}
if (tryToSetFileAsProject(dest, "rgf").isEmpty())
return false;
return true;
}
QString MainWindow::selectFile(const QString& extension) {
QString filter = "*."+extension+" (*."+extension+")";
QString file = fileDialog->getOpenFileName(this,"Select an existing file",suggestedFileName(extension),filter);
return tryToSetFileAsProject(file, extension);
}
void MainWindow::editRGFRequested() {
QString file = selectFile("rgf");
if (!file.isEmpty())
openSpreadsheet(file); // TODO Should openSpreadsheet check for empty file name?
}
void MainWindow::loadRGFRequested() {
selectFile("rgf");
}
void MainWindow::editXYRequested() {
QString file = selectFile("xy");
if (!file.isEmpty())
openSpreadsheet(file);
}
void MainWindow::editTRJRequested() {
QString file = selectFile("trj");
if (!file.isEmpty())
openSpreadsheet(file);
}
QString MainWindow::saveFileAsDialog(const QString& extension) {
QString filter = "*."+extension+" (*."+extension+")";
bool read_only = false;
return fileDialog->getSaveFileName(this, "Create a new empty file", suggestedFileName(extension, read_only), filter);
}
QString MainWindow::suggestedFileName(const QString& extension, bool read_only) const {
if (projectName.isEmpty()) {
return startDir;
}
QString suggest_file = startDir+"/"+projectName+"."+extension;
bool exists = QFile::exists(suggest_file);
qDebug() << "File" << suggest_file << (exists ? "exists" : "doesn't exist");
// XOR
return ( (exists && read_only) || (!exists && !read_only) ) ? suggest_file : startDir;
}
QString MainWindow::newFileRequested(const QString& extension, const QVector<QString>& header) {
QString newFile = saveFileAsDialog(extension);
if (newFile.isEmpty()) {
return QString();
}
dumpHeader(newFile, header);
return tryToSetFileAsProject(newFile, extension);
}
void MainWindow::newRGFRequested() {
QVector<QString> rgf_header;
const std::vector<std::string>& header = reserved_column_names();
for (std::size_t i=0; i<header.size(); ++i) {
rgf_header.append(header.at(i).c_str());
}
QString file = newFileRequested("rgf", rgf_header);
if (!file.isEmpty()) {
openSpreadsheet(file);
}
}
void MainWindow::newXYRequested() {
// TODO Duplication: These are the same column names also apperaring in the reserved column names
const QVector<QString> XY_HEADER = QVector<QString>() << "LOCATION" << "LOC_X" << "LOC_Y" << "FORMATION";
QString file = newFileRequested("xy", XY_HEADER);
if (!file.isEmpty()) {
openSpreadsheet(file);
}
}
void MainWindow::newTRJRequested() {
// TODO Duplication regarding new xy file requested
const QVector<QString> TRJ_HEADER = QVector<QString>() << "DEPTH" << "LOC_X" << "LOC_Y";
QString file = newFileRequested("trj", TRJ_HEADER);
if (!file.isEmpty()) {
openSpreadsheet(file);
}
}
// TODO All file IO should be moved to a separate class, widgets should not deal with files
// TODO Remove duplication, file IO fits a pattern
void MainWindow::dumpHeader(const QString& newFile, const QVector<QString>& header) {
QFile file(newFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
QTextStream out(&file);
for (int i=0; i<header.size(); ++i) {
out << header.at(i) << ((i==header.size()-1)?'\n':'\t');
}
}
QString MainWindow::tryToSetFileAsProject(const QString& file, const QString& extension) {
QFileInfo fileInfo(file);
if (fileInfo.exists() && fileInfo.isFile() && fileInfo.suffix()==extension) {
projectPath = fileInfo.absolutePath();
projectName = fileInfo.fileName();
projectName.chop(extension.length()+1);
qDebug() << "New projected selected, file name:" << file;
newProjectSelected();
return file;
}
else {
qDebug() << "File: " << file;
return QString();
}
}
void MainWindow::newProjectSelected() {
startDir = projectPath;
infoWidget->newProjectSelected(projectPath, projectName);
// this also sets the *.set label on the infoWidget, and the run mode:
settingsWidget->newProjectSelected(projectPath, projectName);
runner->newProjectSelected(projectPath, projectName);
setWindowTitle(projectName + TITLE);
infoWidget->setFocus(Qt::OtherFocusReason);
}
MainWindow::~MainWindow() {
}