forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeManager.cpp
More file actions
525 lines (448 loc) · 14.7 KB
/
ThemeManager.cpp
File metadata and controls
525 lines (448 loc) · 14.7 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
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
/*
* 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 <QDir>
#include <QFile>
#include <QPalette>
#include <QProcess>
#include <QJsonArray>
#include <QJsonObject>
#include <QApplication>
#include <QJsonDocument>
#include <AppInfo.h>
#include <Misc/Utilities.h>
#include <Misc/ThemeManager.h>
/**
* Constructor function, searches for available themes & loads
* the theme variant selected by the user.
*/
Misc::ThemeManager::ThemeManager()
{
populateThemes();
loadTheme(m_settings.value("themeId", 0).toInt());
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
setCustomWindowDecorations(m_settings.value("customWindows", false).toBool());
#else
setCustomWindowDecorations(m_settings.value("customWindows", true).toBool());
#endif
}
/**
* Returns a pointer to the only instance of this class
*/
Misc::ThemeManager &Misc::ThemeManager::instance()
{
static ThemeManager singleton;
return singleton;
}
/**
* Returns the ID of the theme that the user has selected.
*/
int Misc::ThemeManager::themeId() const
{
return m_themeId;
}
/**
* Returns @c true if the application should draw the window decorations & controls by
* itself. This feature makes it look cooler, but it can lead to some trouble on
* not-so-common desktop environments, such as CDE.
*/
bool Misc::ThemeManager::customWindowDecorations() const
{
return m_customWindowDecorations;
}
/**
* Updates the theme ID to be used & saves the changes to the
* application settings.
*
* Finally, this function prompts the user to restart the application
* in order to apply changes.
*
* Unfortunately, an app restart is required because the application
* palette must be set before the GUI is initialized.
*/
void Misc::ThemeManager::setTheme(const int id)
{
// Validate theme ID
if (id >= m_availableThemesPaths.count())
return;
// Save settings for next run
m_themeId = id;
m_settings.setValue("themeId", m_themeId);
// Ask user to quit application
// clang-format off
auto ans = Utilities::showMessageBox(
tr("The theme change will take effect after restart"),
tr("Do you want to restart %1 now?").arg(APP_NAME), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
// clang-format on
// Restart application
if (ans == QMessageBox::Yes)
Utilities::rebootApplication();
}
/**
* Enables/disables the custom window feature. See the @c customWindowDecorations()
* function for more information.
*/
void Misc::ThemeManager::setCustomWindowDecorations(const bool enabled)
{
m_customWindowDecorations = enabled;
m_settings.setValue("customWindows", enabled);
Q_EMIT customWindowDecorationsChanged();
}
/**
* Parses the JSON theme definition file for the given theme ID.
* The colors are then "extracted" from the JSON file & loaded into the
* class, which is later used to set the colors of the QML user interface.
*/
void Misc::ThemeManager::loadTheme(const int id)
{
// Validate theme ID
if (id >= m_availableThemesPaths.count())
return;
// Open theme
QFile file(m_availableThemesPaths.at(id));
if (!file.open(QFile::ReadOnly))
return;
// Read theme data into JSON
auto document = QJsonDocument::fromJson(file.readAll());
if (document.isEmpty())
return;
// Get colors object
auto colors = document.object().value("colors").toObject();
if (colors.isEmpty())
return;
// Read colors from JSON file
// clang-format off
m_titlebarSeparator = document.object().value("titlebarSeparator").toBool();
m_base = QColor(colors.value("base").toString());
m_link = QColor(colors.value("link").toString());
m_button = QColor(colors.value("button").toString());
m_window = QColor(colors.value("window").toString());
m_text = QColor(colors.value("text").toString());
m_border = QColor(colors.value("border").toString());
m_midlight = QColor(colors.value("midlight").toString());
m_highlight = QColor(colors.value("highlight").toString());
m_brightText = QColor(colors.value("brightText").toString());
m_buttonText = QColor(colors.value("buttonText").toString());
m_windowText = QColor(colors.value("windowText").toString());
m_tooltipText = QColor(colors.value("tooltipText").toString());
m_tooltipBase = QColor(colors.value("tooltipBase").toString());
m_highlightedText = QColor(colors.value("highlightedText").toString());
m_highlightedTextAlternative = QColor(colors.value("highlightedTextAlternative").toString());
m_placeholderText = QColor(colors.value("placeholderText").toString());
m_toolbarGradient1 = QColor(colors.value("toolbarGradient1").toString());
m_toolbarGradient2 = QColor(colors.value("toolbarGradient2").toString());
m_consoleText = QColor(colors.value("consoleText").toString());
m_consoleBase = QColor(colors.value("consoleBase").toString());
m_consoleButton = QColor(colors.value("consoleButton").toString());
m_consoleWindow = QColor(colors.value("consoleWindow").toString());
m_consoleHighlight = QColor(colors.value("consoleHighlight").toString());
m_consoleHighlightedText = QColor(colors.value("consoleHighlightedText").toString());
m_consolePlaceholderText = QColor(colors.value("consolePlaceholderText").toString());
m_windowBackground = QColor(colors.value("windowBackground").toString());
m_windowGradient1 = QColor(colors.value("windowGradient1").toString());
m_windowGradient2 = QColor(colors.value("windowGradient2").toString());
m_menubarText = QColor(colors.value("menubarText").toString());
m_dialogBackground = QColor(colors.value("dialogBackground").toString());
m_alternativeHighlight = QColor(colors.value("alternativeHighlight").toString());
m_setupPanelBackground = QColor(colors.value("setupPanelBackground").toString());
m_widgetTextPrimary = QColor(colors.value("widgetTextPrimary").toString());
m_widgetTextSecondary = QColor(colors.value("widgetTextSecondary").toString());
m_widgetWindowBackground = QColor(colors.value("widgetWindowBackground").toString());
m_widgetWindowBorder = QColor(colors.value("widgetWindowBorder").toString());
m_paneWindowBackground = QColor(colors.value("paneWindowBackground").toString());
m_ledEnabled = QColor(colors.value("ledEnabled").toString());
m_ledDisabled = QColor(colors.value("ledDisabled").toString());
m_csvCheckbox = QColor(colors.value("csvCheckbox").toString());
m_widgetForegroundPrimary = QColor(colors.value("widgetForegroundPrimary").toString());
m_widgetForegroundSecondary = QColor(colors.value("widgetForegroundSecondary").toString());
m_widgetIndicator = QColor(colors.value("widgetIndicator").toString());
m_widgetControlBackground = QColor(colors.value("widgetControlBackground").toString());
m_connectButtonChecked = QColor(colors.value("connectButtonChecked").toString());
m_connectButtonUnchecked = QColor(colors.value("connectButtonUnchecked").toString());
m_mqttButton = QColor(colors.value("mqttButton").toString());
// clang-format on
// Read bar widget colors
m_widgetColors.clear();
auto list = colors.value("widgetColors").toArray();
for (int i = 0; i < list.count(); ++i)
m_widgetColors.append(list.at(i).toString());
// Update application palette
QPalette palette;
palette.setColor(QPalette::Base, base());
palette.setColor(QPalette::Link, link());
palette.setColor(QPalette::Text, text());
palette.setColor(QPalette::Button, button());
palette.setColor(QPalette::Window, window());
palette.setColor(QPalette::Midlight, midlight());
palette.setColor(QPalette::Highlight, highlight());
palette.setColor(QPalette::BrightText, brightText());
palette.setColor(QPalette::ButtonText, buttonText());
palette.setColor(QPalette::WindowText, windowText());
palette.setColor(QPalette::ToolTipBase, tooltipBase());
palette.setColor(QPalette::ToolTipText, tooltipText());
palette.setColor(QPalette::HighlightedText, highlightedText());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
palette.setColor(QPalette::PlaceholderText, placeholderText());
#endif
qApp->setPalette(palette);
// Update user interface
m_themeId = id;
Q_EMIT themeChanged();
}
/**
* Reads all the available theme files from the application resources
* folder.
* @note theme definitions are bundled during the compilatopn process.
*/
void Misc::ThemeManager::populateThemes()
{
// Clear available thems
m_availableThemes.clear();
m_availableThemesPaths.clear();
// Scan themes directory & get list of files
auto themeList = QDir(":/themes").entryList();
// Open each JSON file & get theme names
for (int i = 0; i < themeList.count(); ++i)
{
QFile file(QString(":/themes/%1").arg(themeList.at(i)));
if (file.open(QFile::ReadOnly))
{
auto data = file.readAll();
file.close();
auto document = QJsonDocument::fromJson(data);
auto name = document.object().value("name").toString();
if (!name.isEmpty())
{
m_availableThemes.append(name);
m_availableThemesPaths.append(file.fileName());
}
}
}
// Update UI
Q_EMIT availableThemesChanged();
}
//----------------------------------------------------------------------------------------
// Dumb access functions
//----------------------------------------------------------------------------------------
bool Misc::ThemeManager::titlebarSeparator() const
{
return m_titlebarSeparator;
}
QColor Misc::ThemeManager::base() const
{
return m_base;
}
QColor Misc::ThemeManager::link() const
{
return m_link;
}
QColor Misc::ThemeManager::button() const
{
return m_button;
}
QColor Misc::ThemeManager::window() const
{
return m_window;
}
QColor Misc::ThemeManager::text() const
{
return m_text;
}
QColor Misc::ThemeManager::border() const
{
return m_border;
}
QColor Misc::ThemeManager::midlight() const
{
return m_midlight;
}
QColor Misc::ThemeManager::highlight() const
{
return m_highlight;
}
QColor Misc::ThemeManager::brightText() const
{
return m_brightText;
}
QColor Misc::ThemeManager::buttonText() const
{
return m_buttonText;
}
QColor Misc::ThemeManager::windowText() const
{
return m_windowText;
}
QColor Misc::ThemeManager::tooltipText() const
{
return m_tooltipText;
}
QColor Misc::ThemeManager::tooltipBase() const
{
return m_tooltipBase;
}
QColor Misc::ThemeManager::highlightedText() const
{
return m_highlightedText;
}
QColor Misc::ThemeManager::highlightedTextAlternative() const
{
return m_highlightedTextAlternative;
}
QColor Misc::ThemeManager::placeholderText() const
{
return m_placeholderText;
}
QColor Misc::ThemeManager::toolbarGradient1() const
{
return m_toolbarGradient1;
}
QColor Misc::ThemeManager::toolbarGradient2() const
{
return m_toolbarGradient2;
}
QColor Misc::ThemeManager::menubarText() const
{
return m_menubarText;
}
QColor Misc::ThemeManager::dialogBackground() const
{
return m_dialogBackground;
}
QColor Misc::ThemeManager::consoleText() const
{
return m_consoleText;
}
QColor Misc::ThemeManager::consoleBase() const
{
return m_consoleBase;
}
QColor Misc::ThemeManager::consoleButton() const
{
return m_consoleButton;
}
QColor Misc::ThemeManager::consoleWindow() const
{
return m_consoleWindow;
}
QColor Misc::ThemeManager::consoleHighlight() const
{
return m_consoleHighlight;
}
QColor Misc::ThemeManager::consoleHighlightedText() const
{
return m_consoleHighlightedText;
}
QColor Misc::ThemeManager::consolePlaceholderText() const
{
return m_consolePlaceholderText;
}
QColor Misc::ThemeManager::windowBackground() const
{
return m_windowBackground;
}
QColor Misc::ThemeManager::windowGradient1() const
{
return m_windowGradient1;
}
QColor Misc::ThemeManager::windowGradient2() const
{
return m_windowGradient2;
}
QColor Misc::ThemeManager::alternativeHighlight() const
{
return m_alternativeHighlight;
}
QColor Misc::ThemeManager::setupPanelBackground() const
{
return m_setupPanelBackground;
}
QColor Misc::ThemeManager::widgetTextPrimary() const
{
return m_widgetTextPrimary;
}
QColor Misc::ThemeManager::widgetTextSecondary() const
{
return m_widgetTextSecondary;
}
QColor Misc::ThemeManager::widgetWindowBackground() const
{
return m_widgetWindowBackground;
}
QColor Misc::ThemeManager::widgetWindowBorder() const
{
return m_widgetWindowBorder;
}
QColor Misc::ThemeManager::paneWindowBackground() const
{
return m_paneWindowBackground;
}
QColor Misc::ThemeManager::ledEnabled() const
{
return m_ledEnabled;
}
QColor Misc::ThemeManager::ledDisabled() const
{
return m_ledDisabled;
}
QColor Misc::ThemeManager::csvCheckbox() const
{
return m_csvCheckbox;
}
QColor Misc::ThemeManager::widgetForegroundPrimary() const
{
return m_widgetForegroundPrimary;
}
QColor Misc::ThemeManager::widgetForegroundSecondary() const
{
return m_widgetForegroundSecondary;
}
QColor Misc::ThemeManager::widgetIndicator() const
{
return m_widgetIndicator;
}
QColor Misc::ThemeManager::widgetControlBackground() const
{
return m_widgetControlBackground;
}
QColor Misc::ThemeManager::connectButtonChecked() const
{
return m_connectButtonChecked;
}
QColor Misc::ThemeManager::connectButtonUnchecked() const
{
return m_connectButtonUnchecked;
}
QColor Misc::ThemeManager::mqttButton() const
{
return m_mqttButton;
}
StringList Misc::ThemeManager::widgetColors() const
{
return m_widgetColors;
}
StringList Misc::ThemeManager::availableThemes() const
{
return m_availableThemes;
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC
# include "moc_ThemeManager.cpp"
#endif