-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindowWorkflowApp.cpp
576 lines (435 loc) · 16.6 KB
/
MainWindowWorkflowApp.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
#include "MainWindowWorkflowApp.h"
#include "sectiontitle.h"
#include "WorkflowAppWidget.h"
#include "HeaderWidget.h"
#include "OpenSRAPreferences.h"
#include "Utils/RelativePathResolver.h"
#include "Utils/dialogabout.h"
#include "Utils/ProgramOutputDialog.h"
#include "Utils/EventFilter.h"
#include "WorkflowAppOpenSRA.h"
#include <QTreeView>
#include <QDockWidget>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QDebug>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QMenuBar>
#include <QAction>
#include <QMenu>
#include <QApplication>
#include <QGuiApplication>
#include <QScreen>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QSettings>
MainWindowWorkflowApp::MainWindowWorkflowApp(QString appName, WorkflowAppWidget *theApp, QWidget *parent)
: QMainWindow(parent), inputWidget(theApp)
{
//
// create a layout & widget for central area of this QMainWidget
// to this widget we will add a header, selection, button and footer widgets
//
QWidget *centralWidget = new QWidget();
QVBoxLayout *layout = new QVBoxLayout();
centralWidget->setLayout(layout);
centralWidget->setContentsMargins(0,0,0,0);
statusWidget = ProgramOutputDialog::getInstance();
statusDockWidget = new QDockWidget(tr("Program Output"), this);
statusDockWidget->setContentsMargins(0,0,0,0);
statusDockWidget->setWidget(statusWidget);
this->addDockWidget(Qt::BottomDockWidgetArea, statusDockWidget);
resizeDocks({statusDockWidget}, {30}, Qt::Vertical);
connect(statusWidget,&ProgramOutputDialog::showDialog,statusDockWidget,&QDockWidget::setVisible);
//
// resize to primary screen
//
/*************************** keep around
QSize availableSize = qApp->desktop()->availableGeometry().size();
int availWidth = availableSize.width();
int availHeight = availableSize.height();
QSize newSize( availWidth*.85, availHeight*.65 );
setGeometry(QStyle::alignedRect(Qt::LeftToRight,
Qt::AlignCenter,
newSize,
qApp->desktop()->availableGeometry()
)
);
********************************************************/
// QRect rec = QGuiApplication::primaryScreen()->geometry();
// int height = this->height()<int(rec.height())?int(rec.height()):this->height();
// int width = this->width()<int(rec.width())?int(rec.width()):this->width();
// this->resize(width, height);
//
// add SimCenter Header
//
HeaderWidget *header = new HeaderWidget();
header->setHeadingText(appName);
layout->addWidget(header);
// place login info
layout->addWidget(inputWidget);
//
// add run, run-DesignSafe and exit buttons into a new widget for buttons
//
// create the buttons widget and a layout for it
QHBoxLayout *pushButtonLayout = new QHBoxLayout();
// Create the preprocess button
preProcessButton = new QPushButton();
preProcessButton->setText(tr("PREPROCESS"));
pushButtonLayout->addWidget(preProcessButton);
// Create run and exit buttons
runButton = new QPushButton();
runButton->setText(tr("PERFORM ANALYSIS"));
pushButtonLayout->addWidget(runButton);
QPushButton *exitButton = new QPushButton();
exitButton->setText(tr("Exit"));
pushButtonLayout->addWidget(exitButton);
// connect some signals and slots
connect(runButton, SIGNAL(clicked(bool)),this,SLOT(onRunButtonClicked()));
connect(preProcessButton, SIGNAL(clicked(bool)),this,SLOT(onPreprocessButtonClicked()));
connect(exitButton, SIGNAL(clicked(bool)),this,SLOT(onExitButtonClicked()));
// add button widget to layout
//layout->addWidget(buttonWidget);
pushButtonLayout->setSpacing(10);
layout->addLayout(pushButtonLayout);
//
// add SimCenter footer
//
//FooterWidget *footer = new FooterWidget();
//layout->addWidget(footer);
layout->setSpacing(0);
this->setCentralWidget(centralWidget);
this->createActions();
inputWidget->setMainWindow(this);
//
// if have save login and passowrd fill in lineedits
//
//
// strings needed in about menu, use set methods to override
//
manualURL = QString("");
feedbackURL = QString("https://docs.google.com/forms/d/e/1FAIpQLSfh20kBxDmvmHgz9uFwhkospGLCeazZzL770A2GuYZ2KgBZBA/viewform");
// featureRequestURL = QString("https://docs.google.com/forms/d/e/1FAIpQLScTLkSwDjPNzH8wx8KxkyhoIT7AI9KZ16Wg9TuW1GOhSYFOag/viewform");
versionText = QString("");
citeText = QString("");
aboutText = QString(tr("This is a SimCenter Workflow Application"));
// aboutTitle = "About this Application"; // this is the title displayed in the on About dialog
// aboutSource = ":/Resources/docs/textAbout.html"; // this is an HTML file stored under resources
copyrightText = QString("\
<p>\
Copyright text. \
<p>\
");
}
MainWindowWorkflowApp::~MainWindowWorkflowApp()
{
}
bool MainWindowWorkflowApp::save()
{
if (currentFile.isEmpty()) {
return saveAs();
} else {
return saveFile(currentFile);
}
}
bool MainWindowWorkflowApp::saveAs()
{
//
// get filename
//
QFileDialog dialog(this, "Save Simulation Model");
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList filters;
filters << "Json files (*.json)"
<< "All files (*)";
dialog.setNameFilters(filters);
dialog.exec();
auto fileToSave = dialog.selectedFiles().constFirst();
// and save the file
return saveFile(fileToSave);
}
void MainWindowWorkflowApp::openConfigJson()
{
WorkflowAppOpenSRA::getInstance()->clear();
auto openDir = this->thePreferences->getLocalWorkDir() + QDir::separator() + "Input";
QFileInfo openDirStatus(openDir);
if (!openDirStatus.exists())
openDir = this->thePreferences->getLocalWorkDir();
QString fileName = QFileDialog::getOpenFileName(this, "Load SetupConfig.Jjson file (under analysisDir/Input)", openDir, "Json files (*.json);;All files (*)");
if (!fileName.isEmpty())
loadFile(fileName);
}
void MainWindowWorkflowApp::openRunFolder()
{
WorkflowAppOpenSRA::getInstance()->clear();
auto openDir = this->thePreferences->getLocalWorkDir();
QDir runDir = QFileDialog::getExistingDirectory(this, "Load from an existing analysis (working) folder", openDir, QFileDialog::ShowDirsOnly);
// find SetupConfig.json from runDir
auto configJsonFile = runDir.path() + QDir::separator() + "Input" + QDir::separator() + "SetupConfig.json";
if (!configJsonFile.isEmpty())
loadFile(configJsonFile);
else
this->sendErrorMessage("Error, could not locate SetupConfig.json under analysisDir/Input; try another analysis folder");
}
void MainWindowWorkflowApp::openFile(QString fileName)
{
if (!fileName.isEmpty())
loadFile(fileName);
}
void MainWindowWorkflowApp::newFile()
{
// clear old
inputWidget->clear();
// set currentFile blank
setCurrentFile(QString());
}
void MainWindowWorkflowApp::setCurrentFile(const QString &fileName)
{
currentFile = fileName;
// setWindowModified(false);
QString shownName = currentFile;
if (currentFile.isEmpty())
shownName = "untitled.json";
setWindowFilePath(shownName);
}
bool MainWindowWorkflowApp::saveFile(const QString &fileName)
{
//
// open file
//
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;
}
//
// create a json object, fill it in & then use a QJsonDocument
// to write the contents of the object to the file in JSON format
//
QJsonObject json;
inputWidget->outputToJSON(json);
//Resolve relative paths before saving
QFileInfo fileInfo(fileName);
SCUtils::ResolveRelativePaths(json, fileInfo.dir());
QJsonDocument doc(json);
file.write(doc.toJson());
// close file
file.close();
// set current file
// setCurrentFile(fileName);
return true;
}
void MainWindowWorkflowApp::loadFile(const QString &fileName)
{
//
// Set current path to dir of fileName for relative pathing used in setup_config.json
QFileInfo fileInfo(fileName);
QString absPath = fileInfo.absoluteDir().absolutePath();
QDir::setCurrent(absPath);
//
QString testPath = QDir::currentPath();
//
// open file
//
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName), file.errorString()));
return;
}
// validate the document
// JsonValidator class already takes a model type param, add additional model types as required
/*
JsonValidator *jval = new JsonValidator();
jval->validate(this, BIM, fileName);
*/
// place contents of file into json object
QString val;
val=file.readAll();
QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
QJsonObject jsonObj = doc.object();
//
// QFileInfo fileInfo(fileName);
SCUtils::ResolveAbsolutePaths(jsonObj, fileInfo.dir());
// close file
file.close();
// given the json object, create the C++ objects
if ( ! (currentFile.isNull() || currentFile.isEmpty()) ) {
inputWidget->clear();
}
inputWidget->inputFromJSON(jsonObj);
// setCurrentFile(fileName);
}
void MainWindowWorkflowApp::createActions() {
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
//const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
//const QIcon saveIcon = QIcon::fromTheme("document-save", QIcon(":/images/save.png"));
//QToolBar *fileToolBar = addToolBar(tr("File"));
// QAction *newAction = new QAction(tr("&New"), this);
// newAction->setShortcuts(QKeySequence::New);
// newAction->setStatusTip(tr("Create a new file"));
// connect(newAction, &QAction::triggered, this, &MainWindowWorkflowApp::newFile);
// fileMenu->addAction(newAction);
QAction *openRunFolderAction = new QAction(tr("&Open working directory"), this);
openRunFolderAction->setShortcuts(QKeySequence::Open);
openRunFolderAction->setStatusTip(tr("Load from an existing analysis folder"));
connect(openRunFolderAction, &QAction::triggered, this, &MainWindowWorkflowApp::openRunFolder);
fileMenu->addAction(openRunFolderAction);
//fileToolBar->addAction(openAction);
QAction *openConfigJsonAction = new QAction(tr("&Open SetupConfig.json"), this);
openConfigJsonAction->setShortcuts(QKeySequence::Open);
openConfigJsonAction->setStatusTip(tr("Open an existing SetupConfig.json file (under workDir/Input)"));
connect(openConfigJsonAction, &QAction::triggered, this, &MainWindowWorkflowApp::openConfigJson);
fileMenu->addAction(openConfigJsonAction);
//fileToolBar->addAction(openAction);
QAction *saveAction = new QAction(tr("&Save"), this);
saveAction->setShortcuts(QKeySequence::Save);
saveAction->setStatusTip(tr("Save the document to disk"));
connect(saveAction, &QAction::triggered, this, &MainWindowWorkflowApp::save);
fileMenu->addAction(saveAction);
QAction *saveAsAction = new QAction(tr("&Save As"), this);
saveAsAction->setStatusTip(tr("Save the document with new filename to disk"));
connect(saveAsAction, &QAction::triggered, this, &MainWindowWorkflowApp::saveAs);
fileMenu->addAction(saveAsAction);
thePreferences = OpenSRAPreferences::getInstance(this);
QAction *preferenceAction = new QAction(tr("&Preferences"), this);
preferenceAction->setMenuRole(QAction::ApplicationSpecificRole);
preferenceAction->setStatusTip(tr("Set application preferences"));
connect(preferenceAction, &QAction::triggered, this, &MainWindowWorkflowApp::preferences);
fileMenu->addAction(preferenceAction);
// strangely, this does not appear in menu (at least on a mac)!! ..
// does Qt not allow as in tool menu by default?
// check for yourself by changing Quit to drivel and it works
QAction *exitAction = new QAction(tr("&Quit"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
exitAction->setStatusTip(tr("Exit the application"));
fileMenu->addAction(exitAction);
}
QPushButton *MainWindowWorkflowApp::getRunButton() const
{
return runButton;
}
QPushButton *MainWindowWorkflowApp::getPreProcessButton() const
{
return preProcessButton;
}
void MainWindowWorkflowApp::createHelpMenu()
{
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&Version"), this, &MainWindowWorkflowApp::version);
helpMenu->addAction(tr("&About"), this, &MainWindowWorkflowApp::about);
helpMenu->addAction(tr("&Manual"), this, &MainWindowWorkflowApp::manual);
helpMenu->addAction(tr("&Submit Bug/Feature Request"), this, &MainWindowWorkflowApp::submitFeedback);
// QAction *submitFeature = helpMenu->addAction(tr("&Submit Bug/Feature Request"), this, &MainWindowWorkflowApp::submitFeatureRequest);
helpMenu->addAction(tr("&How to Cite"), this, &MainWindowWorkflowApp::cite);
helpMenu->addAction(tr("&License"), this, &MainWindowWorkflowApp::copyright);
}
void MainWindowWorkflowApp::version()
{
QMessageBox msgBox;
QSpacerItem *theSpacer = new QSpacerItem(700, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText(versionText);
QGridLayout *layout = (QGridLayout*)msgBox.layout();
layout->addItem(theSpacer, layout->rowCount(),0,1,layout->columnCount());
msgBox.exec();
}
void MainWindowWorkflowApp::cite()
{
QMessageBox msgBox;
QSpacerItem *theSpacer = new QSpacerItem(700, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText(citeText);
QGridLayout *layout = (QGridLayout*)msgBox.layout();
layout->addItem(theSpacer, layout->rowCount(),0,1,layout->columnCount());
msgBox.exec();
}
void MainWindowWorkflowApp::about()
{
DialogAbout *dlg = new DialogAbout();
dlg->setTitle(aboutTitle);
dlg->setTextSource(aboutSource);
//
// adjust size of application window to the available display
//
QRect rec = QApplication::desktop()->screenGeometry();
int height = 0.50*rec.height();
int width = 0.50*rec.width();
dlg->resize(width, height);
dlg->exec();
delete dlg;
}
void MainWindowWorkflowApp::preferences()
{
thePreferences->show();
}
void MainWindowWorkflowApp::submitFeedback()
{
QDesktopServices::openUrl(QUrl(feedbackURL, QUrl::TolerantMode));
//QDesktopServices::openUrl(QUrl("https://www.designsafe-ci.org/help/new-ticket/", QUrl::TolerantMode));
}
void MainWindowWorkflowApp::manual()
{
QDesktopServices::openUrl(QUrl(manualURL, QUrl::TolerantMode));
//QDesktopServices::openUrl(QUrl("https://www.designsafe-ci.org/help/new-ticket/", QUrl::TolerantMode));
}
void MainWindowWorkflowApp::copyright()
{
QMessageBox msgBox;
QSpacerItem *theSpacer = new QSpacerItem(700, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText(copyrightText);
QGridLayout *layout = (QGridLayout*)msgBox.layout();
layout->addItem(theSpacer, layout->rowCount(),0,1,layout->columnCount());
msgBox.exec();
}
void MainWindowWorkflowApp::setCopyright(QString &newText)
{
copyrightText = newText;
}
void MainWindowWorkflowApp::setVersion(QString &newText)
{
versionText = newText;
}
void MainWindowWorkflowApp::setAbout(QString &/*newText*/)
{
}
void MainWindowWorkflowApp::setAbout(QString &newTitle, QString &newTextSource)
{
aboutTitle = newTitle;
aboutSource = newTextSource;
}
void MainWindowWorkflowApp::setDocumentationURL(QString &newText)
{
manualURL = newText;
}
void MainWindowWorkflowApp::setFeedbackURL(QString &newText)
{
feedbackURL = newText;
}
void MainWindowWorkflowApp::setCite(QString &newText)
{
citeText = newText;
}
void MainWindowWorkflowApp::onRunButtonClicked()
{
inputWidget->onRunButtonClicked(runButton);
}
void MainWindowWorkflowApp::onPreprocessButtonClicked()
{
inputWidget->onPreprocessButtonClicked(preProcessButton);
}
void MainWindowWorkflowApp::onExitButtonClicked()
{
inputWidget->onExitButtonClicked();
QCoreApplication::exit(0);
}