From 5d5ebfcf3c737aa8c36192eb7bf7d7dee6b33099 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 17 Jan 2021 18:29:54 +0100 Subject: statistics: implement showing / hiding of items in QSG To replace the QGraphicsScene, we need the possibility of showing and hiding items. Turns out, the QSG API is completely insane. Whether an item should be shown is queried by the virtual function isSubtreeBlocked(), which is supposed to be overriden by the derived classes. However, the common nodes for rectangles and pixmaps are supposed to be created by QQuickWindow, for hardware optimization. This gives nodes that cannot be derived from and therefore whether the item is shown or not cannot be controlled. There are therefore two distinct cases to consider: The node is allocated by the code directly or indirectly by QQuickWindow. In the latter case, we use a proxy node with the only purpose of having a "visible" flag and add the obtained node as a child. This madness is performed with template trickery to get unified code. Signed-off-by: Berthold Stoeger --- stats/barseries.cpp | 2 +- stats/chartitem.cpp | 24 ++++++++-------- stats/chartitem.h | 53 +++++++++++++++++++++++++++++------ stats/statshelper.h | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 20 deletions(-) (limited to 'stats') diff --git a/stats/barseries.cpp b/stats/barseries.cpp index d7211212e..7218c3255 100644 --- a/stats/barseries.cpp +++ b/stats/barseries.cpp @@ -97,7 +97,7 @@ BarSeries::BarLabel::BarLabel(StatsView &view, const std::vector &label void BarSeries::BarLabel::setVisible(bool visible) { - // item->setVisible(visible); TODO! + item->setVisible(visible); } void BarSeries::BarLabel::highlight(bool highlight, int bin_nr, int binCount) diff --git a/stats/chartitem.cpp b/stats/chartitem.cpp index bb01c6e2c..ce5e6860c 100644 --- a/stats/chartitem.cpp +++ b/stats/chartitem.cpp @@ -31,7 +31,7 @@ QSizeF ChartItem::sceneSize() const return view.size(); } -ChartPixmapItem::ChartPixmapItem(StatsView &v, ChartZValue z) : ChartItem(v, z), +ChartPixmapItem::ChartPixmapItem(StatsView &v, ChartZValue z) : HideableChartItem(v, z), positionDirty(false), textureDirty(false) { } @@ -56,7 +56,7 @@ void ChartPixmapItem::setPositionDirty() void ChartPixmapItem::render() { if (!node) { - node.reset(view.w()->createImageNode()); + createNode(view.w()->createImageNode()); view.addQSGNode(node.get(), zValue); } if (!img) { @@ -65,11 +65,11 @@ void ChartPixmapItem::render() } if (textureDirty) { texture.reset(view.w()->createTextureFromImage(*img, QQuickWindow::TextureHasAlphaChannel)); - node->setTexture(texture.get()); + node->node->setTexture(texture.get()); textureDirty = false; } if (positionDirty) { - node->setRect(rect); + node->node->setRect(rect); positionDirty = false; } } @@ -151,7 +151,7 @@ void ChartTextItem::setColor(const QColor &c) setTextureDirty(); } -ChartLineItem::ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width) : ChartItem(v, z), +ChartLineItem::ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width) : HideableChartItem(v, z), color(color), width(width), positionDirty(false), materialDirty(false) { } @@ -172,7 +172,7 @@ void ChartLineItem::render() geometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2)); geometry->setDrawingMode(QSGGeometry::DrawLines); material.reset(new QSGFlatColorMaterial); - node.reset(new QSGGeometryNode); + createNode(); node->setGeometry(geometry.get()); node->setMaterial(material.get()); view.addQSGNode(node.get(), zValue); @@ -204,7 +204,7 @@ void ChartLineItem::setLine(QPointF fromIn, QPointF toIn) view.registerDirtyChartItem(*this); } -ChartBarItem::ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal) : ChartItem(v, z), +ChartBarItem::ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal) : HideableChartItem(v, z), borderWidth(borderWidth), horizontal(horizontal), positionDirty(false), colorDirty(false) { @@ -217,7 +217,7 @@ ChartBarItem::~ChartBarItem() void ChartBarItem::render() { if (!node) { - node.reset(view.w()->createRectangleNode()); + createNode(view.w()->createRectangleNode()); borderGeometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4)); borderGeometry->setDrawingMode(QSGGeometry::DrawLineLoop); @@ -227,19 +227,20 @@ void ChartBarItem::render() borderNode->setGeometry(borderGeometry.get()); borderNode->setMaterial(borderMaterial.get()); - node->appendChildNode(borderNode.get()); + node->node->appendChildNode(borderNode.get()); view.addQSGNode(node.get(), zValue); positionDirty = colorDirty = true; } if (colorDirty) { - node->setColor(color); + node->node->setColor(color); borderMaterial->setColor(borderColor); + node->node->markDirty(QSGNode::DirtyMaterial); borderNode->markDirty(QSGNode::DirtyMaterial); } if (positionDirty) { - node->setRect(rect); + node->node->setRect(rect); auto vertices = borderGeometry->vertexDataAsPoint2D(); if (horizontal) { setPoint(vertices[0], rect.topLeft()); @@ -252,6 +253,7 @@ void ChartBarItem::render() setPoint(vertices[2], rect.topRight()); setPoint(vertices[3], rect.bottomRight()); } + node->node->markDirty(QSGNode::DirtyGeometry); borderNode->markDirty(QSGNode::DirtyGeometry); } diff --git a/stats/chartitem.h b/stats/chartitem.h index 93c80547f..a87d5fe10 100644 --- a/stats/chartitem.h +++ b/stats/chartitem.h @@ -4,6 +4,8 @@ #ifndef CHART_ITEM_H #define CHART_ITEM_H +#include "statshelper.h" + #include #include @@ -18,19 +20,35 @@ enum class ChartZValue : int; class ChartItem { public: - ChartItem(StatsView &v, ChartZValue z); - virtual ~ChartItem(); virtual void render() = 0; // Only call on render thread! bool dirty; // If true, call render() when rebuilding the scene ChartItem *dirtyPrev, *dirtyNext; // Double linked list of dirty items const ChartZValue zValue; protected: + ChartItem(StatsView &v, ChartZValue z); + virtual ~ChartItem(); QSizeF sceneSize() const; StatsView &view; }; +template +class HideableChartItem : public ChartItem { +protected: + HideableChartItem(StatsView &v, ChartZValue z); + std::unique_ptr node; + bool visible; // Argh. If visibility is set before node is created, we have to cache it. + template + void createNode(Args&&... args); // Call to create node with visibility flag. +public: + void setVisible(bool visible); +}; + +// A shortcut for ChartItems based on a hideable proxy item +template +using HideableChartProxyItem = HideableChartItem>>; + // A chart item that blits a precalculated pixmap onto the scene. -class ChartPixmapItem : public ChartItem { +class ChartPixmapItem : public HideableChartProxyItem { public: ChartPixmapItem(StatsView &v, ChartZValue z); ~ChartPixmapItem(); @@ -48,7 +66,6 @@ private: QRectF rect; bool positionDirty; // true if the position changed since last render bool textureDirty; // true if the pixmap changed since last render - std::unique_ptr node; std::unique_ptr texture; }; @@ -80,7 +97,7 @@ private: std::vector items; }; -class ChartLineItem : public ChartItem { +class ChartLineItem : public HideableChartItem> { public: ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width); ~ChartLineItem(); @@ -93,13 +110,12 @@ private: bool horizontal; bool positionDirty; bool materialDirty; - std::unique_ptr node; std::unique_ptr material; std::unique_ptr geometry; }; // A bar in a bar chart: a rectangle bordered by lines. -class ChartBarItem : public ChartItem { +class ChartBarItem : public HideableChartProxyItem { public: ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal); ~ChartBarItem(); @@ -114,10 +130,31 @@ private: bool horizontal; bool positionDirty; bool colorDirty; - std::unique_ptr node; std::unique_ptr borderNode; std::unique_ptr borderMaterial; std::unique_ptr borderGeometry; }; +// Implementation detail of templates - move to serparate header file +template +void HideableChartItem::setVisible(bool visibleIn) +{ + visible = visibleIn; + if (node) + node->setVisible(visible); +} + +template +template +void HideableChartItem::createNode(Args&&... args) +{ + node.reset(new Node(visible, std::forward(args)...)); +} + +template +HideableChartItem::HideableChartItem(StatsView &v, ChartZValue z) : ChartItem(v, z), + visible(true) +{ +} + #endif diff --git a/stats/statshelper.h b/stats/statshelper.h index 0ea39763d..29dc083a4 100644 --- a/stats/statshelper.h +++ b/stats/statshelper.h @@ -4,9 +4,11 @@ // is for historical reasons to ease transition from QtCharts // and might be removed. #ifndef STATSHELPER_H +#define STATSHELPER_H #include #include +#include template T *createItem(QGraphicsScene *scene, Args&&... args) @@ -22,4 +24,81 @@ std::unique_ptr createItemPtr(QGraphicsScene *scene, Args&&... args) return std::unique_ptr(createItem(scene, std::forward(args)...)); } +// In general, we want chart items to be hideable. For example to show/hide +// labels on demand. Very sadly, the QSG API is absolutely terrible with +// respect to temporarily disabling. Instead of simply having a flag, +// a QSGNode is queried using the "isSubtreeBlocked()" virtual function(!). +// +// Not only is this a slow operation performed on every single node, it +// also is often not possible to override this function: For improved +// performance, the documentation recommends to create QSG nodes via +// QQuickWindow. This provides nodes optimized for the actual hardware. +// However, this obviously means that these nodes cannot be derived from! +// +// In that case, there are two possibilities: Add a proxy node with an +// overridden "isSubtreeBlocked()" function or remove the node from the +// scene. The former was chosen here, because it is less complex. +// +// The following slightly cryptic templates are used to unify the two +// cases: The QSGNode is generated by our own code or the QSGNode is +// obtained from QQuickWindow. +// +// The "HideableQSGNode" template augments the QSGNode "Node" +// by a "setVisible()" function and overrides "isSubtreeBlocked()" +// +// The "QSGProxyNode" template is a QSGNode with a single +// child of type "Node". +// +// Thus, if the node can be created, use: +// HideableQSGNode node +// and if the node can only be obtained from QQuickWindow, use: +// HideableQSGNode> node +// The latter should obviously be typedef-ed. +// +// Yes, that's all horrible, but if nothing else it teaches us about +// composition. +template +class HideableQSGNode : public Node { + bool hidden; + bool isSubtreeBlocked() const override final; +public: + template + HideableQSGNode(bool visible, Args&&... args); + void setVisible(bool visible); +}; + +template +class QSGProxyNode : public QSGNode { +public: + std::unique_ptr node; + QSGProxyNode(Node *node); +}; + +// Implementation detail of templates - move to serparate header file +template +QSGProxyNode::QSGProxyNode(Node *node) : node(node) +{ + appendChildNode(node); +} + +template +bool HideableQSGNode::isSubtreeBlocked() const +{ + return hidden; +} + +template +template +HideableQSGNode::HideableQSGNode(bool visible, Args&&... args) : + Node(std::forward(args)...), + hidden(!visible) +{ +} + +template +void HideableQSGNode::setVisible(bool visible) +{ + hidden = !visible; +} + #endif -- cgit v1.2.3-70-g09d2