From 5f91c69f9c81c1f12999c1a6094e1a0b6cd0caf1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 21 Feb 2021 16:17:24 +0100 Subject: [PATCH] statistics: create themes on demand Create the themes only when needed (singleton pattern). If the themes should do more than colors, such as for example fonts, it is not clear whether that can be done before main() runs. By creating the themes on demand, the Qt UI should be initialized in the constructors of the themes. Signed-off-by: Berthold Stoeger --- stats/statscolors.cpp | 18 +++++++++++++++--- stats/statscolors.h | 4 ++-- stats/statsview.cpp | 7 +++---- stats/statsview.h | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/stats/statscolors.cpp b/stats/statscolors.cpp index d171bb6e5..f438437b7 100644 --- a/stats/statscolors.cpp +++ b/stats/statscolors.cpp @@ -1,5 +1,7 @@ #include "statscolors.h" #include "statstranslations.h" +#include "core/globals.h" +#include // Colors created using the Chroma.js Color Palette Helper // https://vis4.net/palettes/#/50|d|00108c,3ed8ff,ffffe0|ffffe0,ff005e,743535|1|1 @@ -143,6 +145,16 @@ private: } }; -static StatsThemeLight statsThemeLight; -static StatsThemeDark statsThemeDark; -std::vector statsThemes = { &statsThemeLight, &statsThemeDark }; +// Currently, we only support two themes: bright and dark. +// The themes are generated on first use. Thus, the constructors are run +// once the overall application is initialized. This ensures that the themes' +// constructors can access the settings, etc. +static std::array statsThemes; +const StatsTheme &getStatsTheme(bool dark) +{ + if (!statsThemes[0]) { + statsThemes[0] = make_global(); + statsThemes[1] = make_global(); + } + return *statsThemes[dark ? 1 : 0]; +} diff --git a/stats/statscolors.h b/stats/statscolors.h index e7f6c0886..2b8c09d73 100644 --- a/stats/statscolors.h +++ b/stats/statscolors.h @@ -3,7 +3,7 @@ #ifndef STATSCOLORS_H #define STATSCOLORS_H -#include +#include #include #include @@ -50,6 +50,6 @@ public: mutable QSGTexture *selectedTexture = nullptr; // A checkerboard pattern. }; -extern std::vector statsThemes; +extern const StatsTheme &getStatsTheme(bool dark); #endif diff --git a/stats/statsview.cpp b/stats/statsview.cpp index a0e059f88..101746251 100644 --- a/stats/statsview.cpp +++ b/stats/statsview.cpp @@ -35,7 +35,7 @@ static const double selectionLassoWidth = 2.0; // Border between title and char StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent), backgroundDirty(true), - currentTheme(statsThemes[0]), + currentTheme(&getStatsTheme(false)), highlightedSeries(nullptr), xAxis(nullptr), yAxis(nullptr), @@ -300,10 +300,9 @@ QQuickWindow *StatsView::w() const return window(); } -void StatsView::setTheme(int idx) +void StatsView::setTheme(bool dark) { - idx = std::clamp(idx, 0, (int)statsThemes.size() - 1); - currentTheme = statsThemes[idx]; + currentTheme = &getStatsTheme(dark); rootNode->backgroundNode->setColor(currentTheme->backgroundColor); } diff --git a/stats/statsview.h b/stats/statsview.h index 249520481..71d85c211 100644 --- a/stats/statsview.h +++ b/stats/statsview.h @@ -54,7 +54,7 @@ public: QQuickWindow *w() const; // Make window available to items QSizeF size() const; QRectF plotArea() const; - void setTheme(int idx); // Invalid indexes will result in the default theme. Chart must be replot for theme to become effective. + void setTheme(bool dark); // Chart must be replot for theme to become effective. const StatsTheme &getCurrentTheme() const; void addQSGNode(QSGNode *node, ChartZValue z); // Must only be called in render thread! void registerChartItem(ChartItem &item);