-
Notifications
You must be signed in to change notification settings - Fork 27
/
mainwindow.cpp
443 lines (365 loc) · 16.1 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Utils/dialogabout.h"
#include "utilWindows/helpwindow.h"
#include <QGuiApplication>
#include <QScreen>
#include <QRect>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDateTime>
#include <QTextBrowser>
#include <QStandardItemModel>
#include <QDebug>
#include "customizeditemmodel.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->headerWidget->setHeadingText("Turbulence Inflow Tool");
standardModel = new CustomizedItemModel(); //QStandardItemModel ;
QStandardItem *rootNode = standardModel->invisibleRootItem();
//defining bunch of items for inclusion in model
QStandardItem *sourceItem = new QStandardItem("Source");
QStandardItem *parameterItem = new QStandardItem("Parameters");
QStandardItem *exportItem = new QStandardItem("Export");
//building up the hierarchy of the model
rootNode->appendRow(sourceItem);
rootNode->appendRow(parameterItem);
rootNode->appendRow(exportItem);
infoItemIdx = sourceItem->index();
#ifdef Q_OS_WIN
#define COLWIDTH 140
#else
#define COLWIDTH 110
#endif
//register the model
ui->treeView->setModel(standardModel);
ui->treeView->expandAll();
//ui->treeView->setHeaderHidden(true);
//ui->treeView->header()->setStretchLastSection(true);
ui->treeView->setMinimumWidth(COLWIDTH);
ui->treeView->setMaximumWidth(COLWIDTH);
ui->treeView->setColumnWidth(0,COLWIDTH);
ui->treeView->setIconSize(QSize(0,0));
//Disable Edit for the TreeView
ui->treeView->setEditTriggers(QTreeView::EditTrigger::NoEditTriggers);
//
// customize the apperance of the menu on the left
//
ui->treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff ); // hide the horizontal scroll bar
ui->treeView->setObjectName("treeViewOnTheLeft");
ui->treeView->setIndentation(0);
QFile file(":/styles/menuBar.qss");
if (file.open(QFile::ReadOnly)) {
ui->treeView->setStyleSheet(file.readAll());
file.close();
}
else
{
qDebug() << "Open Style File Failed!";
}
//
// set up so that a slection change triggers the selectionChanged slot
//
QItemSelectionModel *selectionModel= ui->treeView->selectionModel();
connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
connect(ui->fileWidget, SIGNAL(hasValidSource(bool, QDir &)),
ui->exportWidget, SLOT(setLocationAvailable(bool, QDir &)));
connect(ui->exportWidget, SIGNAL(sendParameterMap()),
ui->inflowWidget, SLOT(sendParameterMap()));
connect(ui->inflowWidget, SIGNAL(parametersReady(QMap<QString, double> &)),
ui->exportWidget, SLOT(setParameterMap(QMap<QString, double> &)));
connect(ui->fileWidget, SIGNAL(hasValidSource(bool, QDir &)),
this, SLOT(fetchUFileData(bool, QDir &)));
connect(ui->fileWidget, SIGNAL(boundarySelection(int)),
ui->exportWidget, SLOT(setBoundarySelection(int)));
connect(ui->exportWidget, SIGNAL(boundarySelection(int)),
ui->fileWidget, SLOT(setBoundarySelection(int)));
connect(ui->fileWidget, SIGNAL(sendModel(QStandardItemModel *)),
ui->exportWidget, SLOT(setModel(QStandardItemModel *)));
//
// set active index
//
ui->treeView->setCurrentIndex(infoItemIdx);
//
// adjust size of application window to the available display
//
QRect rec = QGuiApplication::primaryScreen()->geometry();
int height = this->height()<0.65*rec.height()?int(0.65*rec.height()):this->height();
int width = this->width()<0.65*rec.width()?int(0.65*rec.width()):this->width();
this->resize(width, height);
// setting text
versionText = "Turbulence Inflow Tool - Version " + QString(APP_VERSION);
citeText = "Jiawei Wan, Peter Mackenzie-Helnwein, and Frank McKenna. (2019, October 22). NHERI-SimCenter/TurbulenceInflowTool: Versions 1.0.2 (Version v1.0.2). Zenodo. http://doi.org/10.5281/zenodo.3462805";
manualURL = "https://nheri-simcenter.github.io/TinF-Documentation/";
feedbackURL = "https://docs.google.com/forms/d/e/1FAIpQLSfh20kBxDmvmHgz9uFwhkospGLCeazZzL770A2GuYZ2KgBZBA/viewform";
featureRequestURL = "https://docs.google.com/forms/d/e/1FAIpQLScTLkSwDjPNzH8wx8KxkyhoIT7AI9KZ16Wg9TuW1GOhSYFOag/viewform";
copyrightText = QString("\
<p>\
The source code is licensed under a BSD 2-Clause License:<p>\
\"Copyright (c) 2017-2018, The Regents of the University of California (Regents).\"\
All rights reserved.<p>\
<p>\
Redistribution and use in source and binary forms, with or without \
modification, are permitted provided that the following conditions are met:\
<p>\
1. Redistributions of source code must retain the above copyright notice, this\
list of conditions and the following disclaimer.\
\
\
2. Redistributions in binary form must reproduce the above copyright notice,\
this list of conditions and the following disclaimer in the documentation\
and/or other materials provided with the distribution.\
<p>\
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\
<p>\
The views and conclusions contained in the software and documentation are those\
of the authors and should not be interpreted as representing official policies,\
either expressed or implied, of the FreeBSD Project.\
<p>\
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, \
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\
THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS \
PROVIDED \"AS IS\". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,\
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\
<p>\
------------------------------------------------------------------------------------\
<p>\
The compiled binary form of this application is licensed under a GPL Version 3 license.\
The licenses are as published by the Free Software Foundation and appearing in the LICENSE file\
included in the packaging of this application. \
<p>\
------------------------------------------------------------------------------------\
<p>\
This software makes use of the QT packages (unmodified): core, gui, widgets and network\
<p>\
QT is copyright \"The Qt Company Ltd" and licensed under the GNU Lesser General \
Public License (version 3) which references the GNU General Public License (version 3)\
<p>\
The licenses are as published by the Free Software Foundation and appearing in the LICENSE file\
included in the packaging of this application. \
<p>\
");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_Quit_triggered()
{
this->close();
}
void MainWindow::on_action_About_triggered()
{
DialogAbout *dlg = new DialogAbout(this);
dlg->setTitle(versionText);
dlg->setTextSource(tr(":/docs/help/About/CWEabout.html"));
//
// adjust size of application window to the available display
//
QRect rec = QGuiApplication::primaryScreen()->availableGeometry();
int height = static_cast<int>(0.50*rec.height());
int width = static_cast<int>(0.50*rec.width());
dlg->resize(width, height);
dlg->exec();
delete dlg;
}
void MainWindow::on_action_New_triggered()
{
ui->inflowWidget->reset();
}
void MainWindow::on_action_Open_triggered()
{
/* identify filename and location for loading */
QString theFolder = QDir::homePath();
QString theFilter = "Json file (*.json)";
QFileDialog dlg;
QString filename = dlg.getOpenFileName(this, "Load file", theFolder, theFilter);
qWarning() << filename;
/* load JSON object from file */
QFile loadFile;
loadFile.setFileName(filename);
if (!loadFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox msg(QMessageBox::Information, "Info", "Could not open file.");
msg.exec();
return;
}
QString theFile = loadFile.readAll();
loadFile.close();
bool sourceValid = false;
QJsonDocument infoDoc = QJsonDocument::fromJson(theFile.toUtf8());
/* start a JSON object to represent the system */
QJsonObject json = infoDoc.object();
QString creator;
creator = json["creator"].toString();
if (creator == "TurbulentInflowTool") sourceValid = true; // v1.0.0 and 1.0.1
if (creator == "TurbulenceInflowTool") sourceValid = true; // >= v1.0.2
QString version;
version = json["version"].toString();
bool versionValid = false;
if (version.startsWith("1.") ) versionValid = true;
//if (version.startsWith("2.0") ) versionValid = true;
//if (version.startsWith("2.1") ) versionValid = true;
if (sourceValid && versionValid) {
QString username;
username = json["username"].toString();
QString author;
author = json["author"].toString();
QString filedate;
filedate = json["date"].toString();
/* read parameter information and update UI */
if (version.startsWith("1.0"))
{
if (json.contains("parameters")) {
QJsonObject params = json["parameters"].toObject();
ui->inflowWidget->inputFromJSON(params);
}
}
else
{
QMessageBox msg(QMessageBox::Information, "Info", "Could not read file: invalid Version");
msg.exec();
}
}
else
{
QMessageBox msg(QMessageBox::Information, "Info", "Not a valid model file.");
msg.exec();
}
}
void MainWindow::on_action_Save_triggered()
{
/* identify filename and location for saving */
QString path = QDir::homePath();
QDir d;
d.setPath(path);
QString filename = d.filePath("TurbulenceInflowTool.json");
QString theFilter = "Json file (*.json)";
QFileDialog dlg;
filename = dlg.getSaveFileName(this, "Save file", filename, theFilter );
// check if cancelled
if (!filename.isEmpty())
{
/* start a JSON object to represent the system */
QJsonObject *json = new QJsonObject();
json->insert("creator", QString("TurbulenceInflowTool"));
json->insert("version", QString(APP_VERSION));
#ifdef Q_OS_WIN
QString username = qgetenv("USERNAME");
#else
QString username = qgetenv("USER");
#endif
json->insert("author", username);
json->insert("date", QDateTime::currentDateTime().toString());
QJsonObject params;
ui->inflowWidget->outputToJSON(params);
json->insert("parameters", params);
QJsonDocument infoDoc = QJsonDocument(*json);
/* write JSON object to file */
QFile saveFile( filename );
if (saveFile.open(QIODevice::WriteOnly)) {
saveFile.write( infoDoc.toJson() );
saveFile.close();
}
else
{
QMessageBox msg(QMessageBox::Information, "Info", "Could not save to file.");
msg.exec();
}
// clean up
delete json;
}
}
void MainWindow::on_btn_selectSource_clicked()
{
ui->inflowWidget->selectSourceLocation();
}
void
MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection &/*oldSelection*/) {
//get the text of the selected item
const QModelIndex index = ui->treeView->selectionModel()->currentIndex();
QString selectedText = index.data(Qt::DisplayRole).toString();
if (selectedText == "Source") { ui->theStackedWidget->setCurrentIndex(0); }
else if (selectedText == "Parameters") { ui->theStackedWidget->setCurrentIndex(1); }
else if (selectedText == "Export") { ui->theStackedWidget->setCurrentIndex(2); }
else {
qWarning() << "Unknown page selected: " << selectedText;
ui->theStackedWidget->setCurrentIndex(0);
}
}
void MainWindow::on_action_Documentation_triggered()
{
QDesktopServices::openUrl(QUrl(manualURL, QUrl::TolerantMode));
//QDesktopServices::openUrl(QUrl("https://www.designsafe-ci.org/help/new-ticket/", QUrl::TolerantMode));
//HelpWindow *help = new HelpWindow();
//help->show();
}
void MainWindow::fetchUFileData(bool hasValidData, QDir &)
{
if (hasValidData) {
if (ui->fileWidget->fetchUFileData(head, tail, data))
{
ui->exportWidget->setUFileData(head, tail, data);
}
}
/* do we need an else to reset info??? */
}
void MainWindow::on_actionLicense_triggered()
{
QDialog msgBox;
QTextBrowser *theBrowser = new QTextBrowser();
theBrowser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
theBrowser->setText(copyrightText);
QGridLayout *layout = new QGridLayout();
layout->addWidget(theBrowser);
msgBox.setLayout(layout);
QRect rec = QGuiApplication::primaryScreen()->geometry();
int height = this->height()<0.80*rec.height()?int(0.80*rec.height()):this->height();
int width = this->width()<0.65*rec.width()?int(0.65*rec.width()):this->width();
msgBox.resize(width, height);
msgBox.exec();
}
void MainWindow::on_actionHow_to_cite_triggered()
{
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 MainWindow::on_actionProvide_feeback_triggered()
{
QDesktopServices::openUrl(QUrl(feedbackURL, QUrl::TolerantMode));
//QDesktopServices::openUrl(QUrl("https://www.designsafe-ci.org/help/new-ticket/", QUrl::TolerantMode));
}
void MainWindow::on_actionSubmit_Feature_Request_triggered()
{
QDesktopServices::openUrl(QUrl(featureRequestURL, QUrl::TolerantMode));
//QDesktopServices::openUrl(QUrl("https://www.designsafe-ci.org/help/new-ticket/", QUrl::TolerantMode));
}
void MainWindow::on_action_Version_triggered()
{
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();
}