From 405399a091b434ecabaa7ec6da4a57cac2f45705 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 16 Apr 2023 11:24:19 +0200 Subject: [PATCH] stats: remove theme-access from QtQuick root node To make the QtQuick code more general, remove the access to the StatsTheme, which only makes sense for the statistics module. Store the background color in a separate variable, since that will be needed by any potential users of the code. Signed-off-by: Berthold Stoeger --- stats/statsview.cpp | 16 +++++++++++----- stats/statsview.h | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/stats/statsview.cpp b/stats/statsview.cpp index 550bd8b7d..9eaa93768 100644 --- a/stats/statsview.cpp +++ b/stats/statsview.cpp @@ -37,6 +37,7 @@ static const double selectionLassoWidth = 2.0; // Border between title and char StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent), backgroundDirty(true), currentTheme(&getStatsTheme(false)), + backgroundColor(currentTheme->backgroundColor), highlightedSeries(nullptr), xAxis(nullptr), yAxis(nullptr), @@ -128,7 +129,7 @@ using ZNode = HideableQSGNode; class RootNode : public QSGNode { public: - RootNode(StatsView &view); + RootNode(StatsView &view, QColor backgroundColor); ~RootNode(); StatsView &view; std::unique_ptr backgroundNode; // solid background @@ -136,13 +137,12 @@ public: std::array, (size_t)ChartZValue::Count> zNodes; }; -RootNode::RootNode(StatsView &view) : view(view) +RootNode::RootNode(StatsView &view, QColor backgroundColor) : view(view) { // Add a background rectangle with a solid color. This could // also be done on the widget level, but would have to be done // separately for desktop and mobile, so do it here. backgroundNode.reset(view.w()->createRectangleNode()); - backgroundNode->setColor(view.getCurrentTheme().backgroundColor); appendChildNode(backgroundNode.get()); for (auto &zNode: zNodes) { @@ -172,7 +172,7 @@ QSGNode *StatsView::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNod // This is just a copy of what is found in Qt's documentation. RootNode *n = static_cast(oldNode); if (!n) - n = rootNode = new RootNode(*this); + n = rootNode = new RootNode(*this, backgroundColor); // Delete all chart items that are marked for deletion. freeDeletedChartItems(); @@ -298,10 +298,16 @@ QQuickWindow *StatsView::w() const return window(); } +void StatsView::setBackgroundColor(QColor color) +{ + backgroundColor = color; + rootNode->backgroundNode->setColor(color); +} + void StatsView::setTheme(bool dark) { currentTheme = &getStatsTheme(dark); - rootNode->backgroundNode->setColor(currentTheme->backgroundColor); + setBackgroundColor(currentTheme->backgroundColor); } const StatsTheme &StatsView::getCurrentTheme() const diff --git a/stats/statsview.h b/stats/statsview.h index b85c55244..a0a44bc89 100644 --- a/stats/statsview.h +++ b/stats/statsview.h @@ -53,6 +53,7 @@ public: QQuickWindow *w() const; // Make window available to items QSizeF size() const; QRectF plotArea() const; + void setBackgroundColor(QColor color); // Chart must be replot for color 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! @@ -143,6 +144,7 @@ private: StatsState state; const StatsTheme *currentTheme; + QColor backgroundColor; std::vector> series; std::unique_ptr grid; std::vector> quartileMarkers;