-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
303 lines (257 loc) · 7.35 KB
/
Main.cpp
File metadata and controls
303 lines (257 loc) · 7.35 KB
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
#include <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDir>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QFontDatabase>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QTimer>
#include "QuickView.h"
#include "Settings.h"
#include "File.h"
//! The QQuickView instance used to display our QML app
QuickView * view = nullptr;
//! True when the view is loading
bool loading = false;
//! Get the system's fixed font
QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
//! Current dir
QString currentDir = QDir::currentPath();
//! Current exe dir
QString exeDir;
//! Timer used to avoid re-creating the view too many times when multiple file events are sent
QTimer timer;
//! Main QML file
QString mainFile("Main.qml");
//! Instance of the settings
Settings * settings = nullptr;
//! The errors
QString errors;
//! Transparent option
bool transparent = false;
//! Instance of the settings
File * fileHelper = nullptr;
//!
//! Capture errors
//!
void messageHandler(QtMsgType type, const QMessageLogContext &, const QString & message)
{
// only get the errors / warnings (QML compile errors are sent as warnings...)
bool error = false;
switch (type)
{
case QtCriticalMsg:
case QtFatalMsg:
error = true;
errors += message + "\n";
break;
default:
break;
}
// log to either Visual Studio debug output or the standard output, we're still interested
// in console.log and such
#ifdef OutputDebugString
OutputDebugStringA(qPrintable(message + "\n"));
#else
printf("%s\n", qPrintable(message));
#endif
// if we get a warning during runtime, reload to show it
if (loading == false && error == true)
{
timer.start(100);
}
}
//!
//! Get a file from (first) the working directory, and if not found, from the
//! executable folder.
//!
QUrl findFile(const QString & file)
{
if (QDir(currentDir).exists(file) == true)
{
return QUrl::fromLocalFile(QDir(currentDir).filePath(file));
}
else if (QDir(exeDir).exists(file) == true)
{
return QUrl::fromLocalFile(QDir(exeDir).filePath(file));
}
return QUrl();
}
//!
//! Set the application engine with our main QML file
//!
void setup(void)
{
// delete the previous view
if (view != nullptr)
{
// cleanup the previous view
qInstallMessageHandler(nullptr);
view->close();
delete view;
view = nullptr;
}
// re-create the view
view = new QuickView();
auto * engine = view->engine();
loading = true;
qInstallMessageHandler(messageHandler);
// add import paths
engine->addImportPath(currentDir);
engine->addImportPath(exeDir);
// set the source, only if we don't have errors already
if (errors.isEmpty() == true)
{
// set options
if (transparent == true)
{
view->setDefaultAlphaBuffer(true);
view->setColor(Qt::transparent);
}
// expose a few usefull things
engine->rootContext()->setContextProperty("rootView", view);
engine->rootContext()->setContextProperty("fixedFont", fixedFont);
engine->rootContext()->setContextProperty("file", fileHelper);
engine->rootContext()->setContextProperty("settings", settings);
// set the source
view->setSource(findFile(mainFile));
}
// check for errors
if (errors.isEmpty() == false)
{
// recreate the view, it's the easiest way to restore its default state: at this
// point, the users might have changed the window flags, default color, transparency,
// etc. And reading errors on a transparent window is kinda hard
view->close();
delete view;
view = new QuickView();
engine = view->engine();
// display the errors
engine->rootContext()->setContextProperty("fixedFont", fixedFont);
engine->rootContext()->setContextProperty("errors", errors);
view->setSource(findFile("Error.qml"));
errors.clear();
}
// done loading
loading = false;
// restore and raise
view->Restore(800, 600, QWindow::Visibility::Windowed);
view->raise();
view->requestActivate();
}
//!
//! Recursively watch everything
//!
void watch(QFileSystemWatcher & watcher, QDir directory)
{
watcher.addPath(directory.currentPath());
for (const auto & name : directory.entryList())
{
if (name.startsWith(".") == false)
{
QString file = directory.absoluteFilePath(name);
QFileInfo(file).isDir() == true ?
watch(watcher, QDir(file)) :
(void)watcher.addPath(file);
}
}
}
//!
//! Process the command line arguments
//!
void options(int argc, char ** argv)
{
// command line
QCommandLineParser parser;
parser.setApplicationDescription("QmlDev");
parser.addHelpOption();
parser.addVersionOption();
parser.addOption(QCommandLineOption("transparent", "Create a transparent window. This allows to test frameless QML application."));
parser.addOption(QCommandLineOption("style", "Override the default `Material` style. See Qt's QQuickStyle::setStyle documentation.", "style", "Material"));
parser.addOption(QCommandLineOption("backend", "Override the default QQuick rendering backend. See Qt's QQuickWindow::setSceneGraphBackend documentation.", "backend"));
parser.addOption(QCommandLineOption("app", "Override the default QML file which is loaded as the main application file.", mainFile));
parser.addOption(QCommandLineOption("clearSettings", "Clears the settings to simulate a first-time run."));
// get the arguments manually (we disabled QApplication's access to them to avoid
// the default arguments to mix with ours)
QStringList arguments;
for (int i = 0; i < argc; ++i)
{
arguments << argv[i];
}
// process
parser.process(arguments);
// style (Material by default, no need to check if it's set)
QQuickStyle::setStyle(parser.value("style"));
// backend
if (parser.isSet("backend") == true)
{
QQuickWindow::setSceneGraphBackend(parser.value("backend"));
}
// transparency (used in setup())
if (parser.isSet("transparent") == true)
{
transparent = true;
}
// main app file
if (parser.isSet("app") == true)
{
mainFile = parser.value("app");
}
// clear the settings
if (parser.isSet("clearSettings") == true)
{
settings->Clear();
}
}
//!
//! Entry point of the application
//!
int main(int argc, char ** argv)
{
int code = -1;
{
// create and setup the application (note that we disable arguments to avoid
// QApplication handling our command line arguments)
int dummyArgc = 1;
QApplication app(dummyArgc, argv);
app.setOrganizationName("Citron");
app.setOrganizationDomain("Citron.org");
app.setApplicationName("QmlDev");
app.setApplicationVersion("0.3");
// init some helpers
settings = new Settings();
fileHelper = new File();
// process options
options(argc, argv);
// get the application directory
exeDir = QApplication::applicationDirPath();
// initialize the application engine
setup();
Q_ASSERT(view != nullptr);
// install a file system watcher to be able to hot-reload the QML when it changes
// note: we're using a timer to avoid too many reloads
QFileSystemWatcher watcher;
watch(watcher, exeDir);
watch(watcher, currentDir);
timer.callOnTimeout(setup);
timer.setSingleShot(true);
QObject::connect(&watcher, &QFileSystemWatcher::directoryChanged, [&] (const QString & name) {
watch(watcher, name);
timer.start(250);
});
QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [] (const QString & name) {
Q_UNUSED(name);
timer.start(250);
});
// run the application
code = app.exec();
// cleanup
delete view;
delete settings;
delete fileHelper;
}
return code;
}