forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleManager.cpp
More file actions
261 lines (231 loc) · 8.79 KB
/
ModuleManager.cpp
File metadata and controls
261 lines (231 loc) · 8.79 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
/*
* Copyright (c) 2020-2023 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <AppInfo.h>
#include <CSV/Export.h>
#include <CSV/Player.h>
#include <JSON/Frame.h>
#include <JSON/Group.h>
#include <JSON/Dataset.h>
#include <JSON/Generator.h>
#include <Project/Model.h>
#include <Project/CodeEditor.h>
#include <IO/Manager.h>
#include <IO/Console.h>
#include <IO/Drivers/Serial.h>
#include <IO/Drivers/Network.h>
#include <IO/Drivers/BluetoothLE.h>
#include <Misc/MacExtras.h>
#include <Misc/Utilities.h>
#include <Misc/Translator.h>
#include <Misc/TimerEvents.h>
#include <Misc/ThemeManager.h>
#include <Misc/ModuleManager.h>
#include <MQTT/Client.h>
#include <Plugins/Server.h>
#include <UI/Dashboard.h>
#include <UI/DashboardWidget.h>
#include <UI/Widgets/Terminal.h>
#include <QQuickWindow>
#include <QSimpleUpdater.h>
/**
* Configures the application font and configures application signals/slots to destroy
* singleton classes before the application quits.
*/
Misc::ModuleManager::ModuleManager()
{
// Init translator
(void)Misc::Translator::instance();
// Load Roboto fonts from resources
QFontDatabase::addApplicationFont(":/fonts/Roboto-Bold.ttf");
QFontDatabase::addApplicationFont(":/fonts/Roboto-Regular.ttf");
QFontDatabase::addApplicationFont(":/fonts/RobotoMono-Bold.ttf");
QFontDatabase::addApplicationFont(":/fonts/RobotoMono-Regular.ttf");
// Set Roboto as default app font
QFont font("Roboto");
#if defined(Q_OS_WIN)
font.setPointSize(9);
#elif defined(Q_OS_MAC)
font.setPointSize(13);
#elif defined(Q_OS_LINUX)
font.setPointSize(10);
#endif
qApp->setFont(font);
// Enable software rendering
#ifdef SERIAL_STUDIO_SOFTWARE_RENDERING
QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
#endif
// Stop modules when application is about to quit
connect(engine(), SIGNAL(quit()), this, SLOT(onQuit()));
}
/**
* Returns a pointer to the QML application engine
*/
QQmlApplicationEngine *Misc::ModuleManager::engine()
{
return &m_engine;
}
/**
* Sets the default options for QSimpleUpdater, which are:
* - Notify user when a new update is found
* - Do not notify user when we finish checking for updates
* - Do not close application if update is found
*/
void Misc::ModuleManager::configureUpdater()
{
if (!autoUpdaterEnabled())
return;
QSimpleUpdater::getInstance()->setNotifyOnUpdate(APP_UPDATER_URL, true);
QSimpleUpdater::getInstance()->setNotifyOnFinish(APP_UPDATER_URL, false);
QSimpleUpdater::getInstance()->setMandatoryUpdate(APP_UPDATER_URL, false);
}
/**
* Register custom QML types, for the moment, we have:
* - JSON Frame object
* - JSON Group object
* - JSON Dataset object
*/
void Misc::ModuleManager::registerQmlTypes()
{
qmlRegisterType<Widgets::Terminal>("SerialStudio", 1, 0, "Terminal");
qmlRegisterType<UI::DashboardWidget>("SerialStudio", 1, 0, "DashboardWidget");
}
/**
* Enables or disables the auto-updater system (QSimpleUpdater).
*
* To disable QSimpleUpdater, you need to add DEFINES += DISABLE_QSU in the qmake project
* file. This option is provided for package managers, users are expected to update the
* application using the same package manager they used for installing it.
*/
bool Misc::ModuleManager::autoUpdaterEnabled()
{
#ifdef DISABLE_QSU
return false;
#else
return true;
#endif
}
/**
* Initializes all the application modules, registers them with the QML engine and loads
* the "main.qml" file as the root QML file.
*/
void Misc::ModuleManager::initializeQmlInterface()
{
// Initialize modules
auto csvExport = &CSV::Export::instance();
auto csvPlayer = &CSV::Player::instance();
auto ioManager = &IO::Manager::instance();
auto ioConsole = &IO::Console::instance();
auto mqttClient = &MQTT::Client::instance();
auto uiDashboard = &UI::Dashboard::instance();
auto projectModel = &Project::Model::instance();
auto ioSerial = &IO::Drivers::Serial::instance();
auto jsonGenerator = &JSON::Generator::instance();
auto pluginsBridge = &Plugins::Server::instance();
auto miscUtilities = &Misc::Utilities::instance();
auto miscMacExtras = &Misc::MacExtras::instance();
auto ioNetwork = &IO::Drivers::Network::instance();
auto miscTranslator = &Misc::Translator::instance();
auto miscTimerEvents = &Misc::TimerEvents::instance();
auto miscThemeManager = &Misc::ThemeManager::instance();
auto projectCodeEditor = &Project::CodeEditor::instance();
auto ioBluetoothLE = &IO::Drivers::BluetoothLE::instance();
// Initialize third-party modules
auto updater = QSimpleUpdater::getInstance();
// Operating system flags
bool isWin = false;
bool isMac = false;
bool isNix = false;
QString osName = tr("Unknown OS");
#if defined(Q_OS_MAC)
isMac = true;
osName = "macOS";
#elif defined(Q_OS_WIN)
isWin = true;
osName = "Windows";
#elif defined(Q_OS_LINUX)
isNix = true;
osName = "GNU/Linux";
#else
isNix = true;
osName = "UNIX";
#endif
// Qt version QML flag
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const bool qt6 = false;
#else
const bool qt6 = true;
#endif
// Start common event timers
miscTimerEvents->startTimers();
// Retranslate the QML interface automagically
connect(miscTranslator, SIGNAL(languageChanged()), engine(), SLOT(retranslate()));
// Register C++ modules with QML
auto c = engine()->rootContext();
c->setContextProperty("Cpp_Qt6", qt6);
c->setContextProperty("Cpp_IsWin", isWin);
c->setContextProperty("Cpp_IsMac", isMac);
c->setContextProperty("Cpp_IsNix", isNix);
c->setContextProperty("Cpp_OSName", osName);
c->setContextProperty("Cpp_Updater", updater);
c->setContextProperty("Cpp_IO_Serial", ioSerial);
c->setContextProperty("Cpp_CSV_Export", csvExport);
c->setContextProperty("Cpp_CSV_Player", csvPlayer);
c->setContextProperty("Cpp_IO_Console", ioConsole);
c->setContextProperty("Cpp_IO_Manager", ioManager);
c->setContextProperty("Cpp_IO_Network", ioNetwork);
c->setContextProperty("Cpp_MQTT_Client", mqttClient);
c->setContextProperty("Cpp_UI_Dashboard", uiDashboard);
c->setContextProperty("Cpp_Project_Model", projectModel);
c->setContextProperty("Cpp_JSON_Generator", jsonGenerator);
c->setContextProperty("Cpp_Plugins_Bridge", pluginsBridge);
c->setContextProperty("Cpp_Misc_MacExtras", miscMacExtras);
c->setContextProperty("Cpp_Misc_Utilities", miscUtilities);
c->setContextProperty("Cpp_IO_Bluetooth_LE", ioBluetoothLE);
c->setContextProperty("Cpp_ThemeManager", miscThemeManager);
c->setContextProperty("Cpp_Misc_Translator", miscTranslator);
c->setContextProperty("Cpp_Misc_TimerEvents", miscTimerEvents);
c->setContextProperty("Cpp_Project_CodeEditor", projectCodeEditor);
c->setContextProperty("Cpp_UpdaterEnabled", autoUpdaterEnabled());
c->setContextProperty("Cpp_ModuleManager", this);
// Register app info with QML
c->setContextProperty("Cpp_AppName", qApp->applicationName());
c->setContextProperty("Cpp_AppUpdaterUrl", APP_UPDATER_URL);
c->setContextProperty("Cpp_AppVersion", qApp->applicationVersion());
c->setContextProperty("Cpp_AppOrganization", qApp->organizationName());
c->setContextProperty("Cpp_AppOrganizationDomain", qApp->organizationDomain());
// Load main.qml
engine()->load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
}
/**
* Calls the functions needed to safely quit the application
*/
void Misc::ModuleManager::onQuit()
{
CSV::Export::instance().closeFile();
CSV::Player::instance().closeFile();
IO::Manager::instance().disconnectDriver();
Misc::TimerEvents::instance().stopTimers();
Plugins::Server::instance().removeConnection();
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC
# include "moc_ModuleManager.cpp"
#endif