summaryrefslogtreecommitdiffstats
path: root/stats/chartitem.h
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2021-01-17 18:29:54 +0100
committerGravatar bstoeger <32835590+bstoeger@users.noreply.github.com>2021-01-20 08:47:18 +0100
commit5d5ebfcf3c737aa8c36192eb7bf7d7dee6b33099 (patch)
treef28e670816bc6a2676b914709419665d98b28dbd /stats/chartitem.h
parentb42e19e36b4d80716059011581851c8600338cbb (diff)
downloadsubsurface-5d5ebfcf3c737aa8c36192eb7bf7d7dee6b33099.tar.gz
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats/chartitem.h')
-rw-r--r--stats/chartitem.h53
1 files changed, 45 insertions, 8 deletions
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 <memory>
#include <QPainter>
@@ -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 <typename Node>
+class HideableChartItem : public ChartItem {
+protected:
+ HideableChartItem(StatsView &v, ChartZValue z);
+ std::unique_ptr<Node> node;
+ bool visible; // Argh. If visibility is set before node is created, we have to cache it.
+ template<class... Args>
+ 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 <typename Node>
+using HideableChartProxyItem = HideableChartItem<HideableQSGNode<QSGProxyNode<Node>>>;
+
// A chart item that blits a precalculated pixmap onto the scene.
-class ChartPixmapItem : public ChartItem {
+class ChartPixmapItem : public HideableChartProxyItem<QSGImageNode> {
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<QSGImageNode> node;
std::unique_ptr<QSGTexture> texture;
};
@@ -80,7 +97,7 @@ private:
std::vector<Item> items;
};
-class ChartLineItem : public ChartItem {
+class ChartLineItem : public HideableChartItem<HideableQSGNode<QSGGeometryNode>> {
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<QSGGeometryNode> node;
std::unique_ptr<QSGFlatColorMaterial> material;
std::unique_ptr<QSGGeometry> geometry;
};
// A bar in a bar chart: a rectangle bordered by lines.
-class ChartBarItem : public ChartItem {
+class ChartBarItem : public HideableChartProxyItem<QSGRectangleNode> {
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<QSGRectangleNode> node;
std::unique_ptr<QSGGeometryNode> borderNode;
std::unique_ptr<QSGFlatColorMaterial> borderMaterial;
std::unique_ptr<QSGGeometry> borderGeometry;
};
+// Implementation detail of templates - move to serparate header file
+template <typename Node>
+void HideableChartItem<Node>::setVisible(bool visibleIn)
+{
+ visible = visibleIn;
+ if (node)
+ node->setVisible(visible);
+}
+
+template <typename Node>
+template<class... Args>
+void HideableChartItem<Node>::createNode(Args&&... args)
+{
+ node.reset(new Node(visible, std::forward<Args>(args)...));
+}
+
+template <typename Node>
+HideableChartItem<Node>::HideableChartItem(StatsView &v, ChartZValue z) : ChartItem(v, z),
+ visible(true)
+{
+}
+
#endif