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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-21 16:17:24 +01:00 committed by Dirk Hohndel
parent 7f1107408d
commit 5f91c69f9c
4 changed files with 21 additions and 10 deletions

View file

@ -1,5 +1,7 @@
#include "statscolors.h" #include "statscolors.h"
#include "statstranslations.h" #include "statstranslations.h"
#include "core/globals.h"
#include <array>
// Colors created using the Chroma.js Color Palette Helper // Colors created using the Chroma.js Color Palette Helper
// https://vis4.net/palettes/#/50|d|00108c,3ed8ff,ffffe0|ffffe0,ff005e,743535|1|1 // https://vis4.net/palettes/#/50|d|00108c,3ed8ff,ffffe0|ffffe0,ff005e,743535|1|1
@ -143,6 +145,16 @@ private:
} }
}; };
static StatsThemeLight statsThemeLight; // Currently, we only support two themes: bright and dark.
static StatsThemeDark statsThemeDark; // The themes are generated on first use. Thus, the constructors are run
std::vector<const StatsTheme *> statsThemes = { &statsThemeLight, &statsThemeDark }; // once the overall application is initialized. This ensures that the themes'
// constructors can access the settings, etc.
static std::array<const StatsTheme *, 2> statsThemes;
const StatsTheme &getStatsTheme(bool dark)
{
if (!statsThemes[0]) {
statsThemes[0] = make_global<StatsThemeLight>();
statsThemes[1] = make_global<StatsThemeDark>();
}
return *statsThemes[dark ? 1 : 0];
}

View file

@ -3,7 +3,7 @@
#ifndef STATSCOLORS_H #ifndef STATSCOLORS_H
#define STATSCOLORS_H #define STATSCOLORS_H
#include <vector> #include <memory>
#include <QColor> #include <QColor>
#include <QString> #include <QString>
@ -50,6 +50,6 @@ public:
mutable QSGTexture *selectedTexture = nullptr; // A checkerboard pattern. mutable QSGTexture *selectedTexture = nullptr; // A checkerboard pattern.
}; };
extern std::vector<const StatsTheme *> statsThemes; extern const StatsTheme &getStatsTheme(bool dark);
#endif #endif

View file

@ -35,7 +35,7 @@ static const double selectionLassoWidth = 2.0; // Border between title and char
StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent), StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
backgroundDirty(true), backgroundDirty(true),
currentTheme(statsThemes[0]), currentTheme(&getStatsTheme(false)),
highlightedSeries(nullptr), highlightedSeries(nullptr),
xAxis(nullptr), xAxis(nullptr),
yAxis(nullptr), yAxis(nullptr),
@ -300,10 +300,9 @@ QQuickWindow *StatsView::w() const
return window(); return window();
} }
void StatsView::setTheme(int idx) void StatsView::setTheme(bool dark)
{ {
idx = std::clamp(idx, 0, (int)statsThemes.size() - 1); currentTheme = &getStatsTheme(dark);
currentTheme = statsThemes[idx];
rootNode->backgroundNode->setColor(currentTheme->backgroundColor); rootNode->backgroundNode->setColor(currentTheme->backgroundColor);
} }

View file

@ -54,7 +54,7 @@ public:
QQuickWindow *w() const; // Make window available to items QQuickWindow *w() const; // Make window available to items
QSizeF size() const; QSizeF size() const;
QRectF plotArea() 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; const StatsTheme &getCurrentTheme() const;
void addQSGNode(QSGNode *node, ChartZValue z); // Must only be called in render thread! void addQSGNode(QSGNode *node, ChartZValue z); // Must only be called in render thread!
void registerChartItem(ChartItem &item); void registerChartItem(ChartItem &item);