-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.cpp
364 lines (281 loc) · 12.8 KB
/
main.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
// Written: fmckenna
// Modified: Ajay B Harish (Feb 2021)
// Modified: Justin Bonus (2024)
// Purpose: the typical Qt main for running a QMainWindow
// Include headers
#include <MainWindowWorkflowApp.h>
#include <QApplication>
#include <QFile>
#include <QThread>
#include <QObject>
#include <TapisV3.h>
#include <WorkflowAppHydroUQ.h>
#include <QCoreApplication>
#include <QMessageBox>
#include <QString>
#include <QTime>
#include <QTextStream>
#include <QOpenGLWidget>
#include <QStandardPaths>
#include <QDir>
#include <QDebug>
#include <QPushButton>
#include <GoogleAnalytics.h>
#include <QStatusBar>
#include <QWebEngineView>
// #include <QtWebEngine>
// #include <QWebEngineView>
// #include <QWebEngineSettings>
// #include <QWebEngineProfile>
// #include <QQmlApplicationEngine>
// #include <SimCenterPreferences.h>
// #include <QSurfaceFormat>
#include <QtGlobal>
#include <stdio.h>
#include <stdlib.h>
#ifdef ENDLN
#undef ENDLN
#endif
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
#define ENDLN endl
#else
#define ENDLN Qt::endl
#endif
// Set up logging of output messages for user debugging
static QString logFilePath;
static bool logToFile = false;
// static bool logToFile; // To be changed to true if the app is run in Qt Creator, using QTDIR env variable as proxy
// customMessgaeOutput code taken from web:
// https://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output
void customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QHash<QtMsgType, QString> msgLevelHash({{QtDebugMsg, "Debug"}, {QtInfoMsg, "Info"}, {QtWarningMsg, "Warning"}, {QtCriticalMsg, "Critical"}, {QtFatalMsg, "Fatal"}});
QByteArray localMsg = msg.toLocal8Bit();
QTime time = QTime::currentTime();
QString formattedTime = time.toString("hh:mm:ss.zzz");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();
QString logLevelName = msgLevelHash[type];
QByteArray logLevelMsg = logLevelName.toLocal8Bit();
// TODO: Better way to check if the app is run in Qt Creator than QTDIR, as its not guaranteed to only belong to Qt Creator
static bool logToFile; // Compiler should default static bool init to 0. Changes to true if the app is run in Qt Creator, using QTDIR env variable as proxy
static bool logToStdErr; // If true, output to stderr
QByteArray envVar = qgetenv("QTDIR"); // check if the app is run in Qt Creator
if (envVar.isEmpty()) {
logToFile = true;
} else {
logToFile = false;
}
if (logToFile) {
QString txt = QString("%1 %2: %3 (%4)").arg(formattedTime, logLevelName, msg, context.file);
QFile outFile(logFilePath);
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt << Qt::endl;
outFile.close();
}
if (logToStdErr || (!logToFile && !logToStdErr)) {
fprintf(stderr, "%s %s: %s (%s:%u, %s)\n", formattedTimeMsg.constData(), logLevelMsg.constData(), localMsg.constData(), context.file, context.line, context.function);
fflush(stderr);
}
if (type == QtFatalMsg)
abort();
}
int main(int argc, char *argv[])
{
// Extensive documentation on how to set up OpenGL on Windows, macOS, and Linux can be found at:
// https://doc.qt.io/qt-5/windows-requirements.html
#ifdef Q_OS_WIN
QApplication::setAttribute(Qt::AA_UseOpenGLES); // Use ANGLE on Windows
#else
#ifdef Q_OS_MACOS
QApplication::setAttribute(Qt::AA_UseDesktopOpenGL); // Use Desktop OpenGL on macOS
#else // Linux
QApplication::setAttribute(Qt::AA_UseOpenGLES); // Use OpenGLES on Linux
// QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#endif
// QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//Setting Core Application Name, Organization, and Version
QCoreApplication::setApplicationName("HydroUQ");
QCoreApplication::setOrganizationName("SimCenter");
QCoreApplication::setApplicationVersion("4.0.5");
//Init resources from static libraries (e.g. SimCenterCommonQt or s3hark)
Q_INIT_RESOURCE(images);
Q_INIT_RESOURCE(images1);
// Q_INIT_RESOURCE(resources);
// Q_INIT_RESOURCE(Resources);
//
// Set up logging of output messages for user debugging
//
logFilePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ QDir::separator() + QCoreApplication::applicationName();
// make sure tool dir exists in Documents folder
QDir dirWork(logFilePath);
if (!dirWork.exists())
if (!dirWork.mkpath(logFilePath)) {
qDebug() << QString("Could not create Working Dir: ") << logFilePath;
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText("FATAL: You do not have permissions to write to your Documents folder. You need this access to run this application.");
// msg = msg + text + QString("\n Please report this as a bug to SimCenter.");
//msgBox.setInformativeText(text);
msgBox.setWindowTitle("Fatal Error");
QPushButton *exitButton = msgBox.addButton(QMessageBox::Ok);
QObject::connect(exitButton, &QPushButton::clicked, qApp, &QApplication::quit);
msgBox.exec();
}
// full path to debug.log file
logFilePath = logFilePath + QDir::separator() + QString("debug.log");
// remove old log file
QFile debugFile(logFilePath);
debugFile.remove();
// Issues with web engine (notably with hardware acceleration) are
// sometimes resolved by setting various flags.
// https://github.com/probonopd/linuxdeployqt/issues/554
// qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu --no-sandbox");
// qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blacklist --ignore-gpu-blocklist --enable-gpu-rasterization --use-gl=egl");
// qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blacklist --ignore-gpu-blocklist --enable-gpu-rasterization");
// logToFile = logToFileDefault;
// if constexpr (false) {
// QByteArray envVar = qgetenv("QTDIR"); // check if the app is run in Qt Creator
// if (envVar.isEmpty()) {
// logToFile = true;
// } else {
// logToFile = false;
// }
// }
QByteArray envVar = qgetenv("QTDIR"); // check if the app is run in Qt Creator
if (envVar.isEmpty())
logToFile = true;
qInstallMessageHandler(customMessageOutput);
// qDebug() << "logFile: " << logFilePath;
/****************** code to reset openGL version .. keep around in case need again
QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(glFormat);
***********************************************************************************/
//
// window scaling
//
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
// QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication a(argc, argv);
// To have a plugin for web engine, uncomment the following lines
// https://doc.qt.io/qt-5/qtwebengine-overview.html
// QtWebEngine::initialize(); // Initialize the QtWebEngine
// QQmlApplicationEngine engine; // This one only for qt quick applications
// engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// regular Qt startup
// QApplication a(argc, argv);
// create a remote interface
QString tenant("designsafe");
QString storage("designsafe.storage.default/");
QString dirName("HydroUQ"); // this is the default directory for the application
TapisV3 *theRemoteService = new TapisV3(tenant, storage, &dirName);
// create the main window
WorkflowAppWidget *theInputApp = new WorkflowAppHydroUQ(theRemoteService);
MainWindowWorkflowApp w(QString("HydroUQ: Water-borne Hazards Engineering with Uncertainty Quantification"), theInputApp, theRemoteService);
// About the application
QString aboutTitle = "About the SimCenter HydroUQ Application"; // this is the title displayed in the on About dialog
QString aboutSource = ":/resources/docs/textAboutHydroUQ.html"; // this is an HTML file stored under resources
w.setAbout(aboutTitle, aboutSource);
// Version
QString version = QString("Version ") + QCoreApplication::applicationVersion();
w.setVersion(version);
// Citation
QString citeText("1) Frank McKenna, Justin Bonus, Ajay B Harish, & Nicolette Lewis. (2024). NHERI-SimCenter/HydroUQ: Version 4.0.0 (v4.0.0). Zenodo. https://doi.org/10.5281/zenodo.13865413 \n\n2) Gregory G. Deierlein, Frank McKenna, Adam Zsarnóczay, Tracy Kijewski-Correa, Ahsan Kareem, Wael Elhaddad, Laura Lowes, Matthew J. Schoettler, and Sanjay Govindjee (2020) A Cloud-Enabled Application Framework for Simulating Regional-Scale Impacts of Natural Hazards on the Built Environment. Frontiers in the Built Environment. 6:558706. doi: 10.3389/fbuil.2020.558706");
w.setCite(citeText);
// Link to repository
QString manualURL("https://nheri-simcenter.github.io/Hydro-Documentation/");
w.setDocumentationURL(manualURL);
// Link to message board
QString messageBoardURL("https://github.com/orgs/NHERI-SimCenter/discussions/categories/hydro-uq");
w.setFeedbackURL(messageBoardURL);
//
// Move remote interface to a thread
//
QThread *thread = new QThread();
theRemoteService->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), theRemoteService, SLOT(process()));
QObject::connect(theRemoteService, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(theRemoteService, SIGNAL(finished()), theRemoteService, SLOT(deleteLater())); // ? is finished() a signal of theRemoteService?
// QObject::connect(thread, SIGNAL(finished()), theRemoteService, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
//
// Show the main window, set styles & start the event loop
//
// https://www.qtcentre.org/threads/62377-Best-place-for-setting-stylesheet
// Moving this to the place after MainWindow creation fixes unwanted padding,
// but may produce really weird results on Mac OS X when styling combobox drop-down area.
// w.show();
// w.statusBar()->showMessage("Ready", startupDelay);
#ifdef Q_OS_WIN
QFile file(":/styleCommon/stylesheetWIN.qss");
#endif
#ifdef Q_OS_MACOS
QFile file(":/styleCommon/stylesheetMAC.qss");
#endif
#ifdef Q_OS_LINUX
QFile file(":/styleCommon/stylesheetLinux.qss");
#endif
if (file.open(QFile::ReadOnly)) {
a.setStyleSheet(file.readAll());
qDebug() << "Stylesheet loaded: " << file.fileName();
file.close();
} else {
qDebug() << "Could not open stylesheet: " << file.fileName();
}
w.show();
constexpr int startupDelay = 6000; // milliseconds
w.statusBar()->showMessage("Ready", startupDelay);
#ifdef _SC_RELEASE
//Setting Google Analytics Tracking Information
qDebug() << "HydroUQ -- running Release Build";
GoogleAnalytics::SetMeasurementId("G-MC7SGPGWVQ");
GoogleAnalytics::SetAPISecret("LrEiuSuaSqeh_v1928odog");
GoogleAnalytics::CreateSessionId();
GoogleAnalytics::StartSession();
// Opening a QWebEngineView and using github to get app geographic usage
QWebEngineView view;
view.setUrl(QUrl("https://nheri-simcenter.github.io/HydroUQ/GA4.html"));
view.resize(1024, 750);
view.show();
view.hide();
#endif
#ifdef _ANALYTICS
//Setting Google Analytics Tracking Information
qDebug() << "compiled with: ANALYTICS";
GoogleAnalytics::SetMeasurementId("G-MC7SGPGWVQ");
GoogleAnalytics::SetAPISecret("LrEiuSuaSqeh_v1928odog");
GoogleAnalytics::CreateSessionId();
GoogleAnalytics::StartSession();
#endif
// Result of execution
int res = a.exec();
#ifdef _GA_AFTER
// Opening a QWebEngineView and using github to get app geographic usage
qDebug() << "compiled with: GA_AFTER";
QWebEngineView view;
view.setUrl(QUrl("https://nheri-simcenter.github.io/HydroUQ/GA4.html"));
view.resize(1024, 750);
view.show();
view.hide();
#endif
// On done with event loop, logout & stop the thread
theRemoteService->logout();
// Clean up
theRemoteService = nullptr;
theInputApp = nullptr;
thread = nullptr;
// thread->deleteLater();
// Close Google Analytics session
// GoogleAnalytics::SetClientId("");
// GoogleAnalytics::SetSessionId("");
GoogleAnalytics::SetMeasurementId("");
GoogleAnalytics::SetAPISecret("");
GoogleAnalytics::EndSession();
// Complete
return res;
}