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>
This commit is contained in:
Berthold Stoeger 2021-01-17 18:29:54 +01:00 committed by bstoeger
parent b42e19e36b
commit 5d5ebfcf3c
4 changed files with 138 additions and 20 deletions

View file

@ -97,7 +97,7 @@ BarSeries::BarLabel::BarLabel(StatsView &view, const std::vector<QString> &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)

View file

@ -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);
}

View file

@ -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

View file

@ -4,9 +4,11 @@
// is for historical reasons to ease transition from QtCharts
// and might be removed.
#ifndef STATSHELPER_H
#define STATSHELPER_H
#include <memory>
#include <QGraphicsScene>
#include <QSGNode>
template <typename T, class... Args>
T *createItem(QGraphicsScene *scene, Args&&... args)
@ -22,4 +24,81 @@ std::unique_ptr<T> createItemPtr(QGraphicsScene *scene, Args&&... args)
return std::unique_ptr<T>(createItem<T>(scene, std::forward<Args>(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<Node>" template augments the QSGNode "Node"
// by a "setVisible()" function and overrides "isSubtreeBlocked()"
//
// The "QSGProxyNode<Node>" template is a QSGNode with a single
// child of type "Node".
//
// Thus, if the node can be created, use:
// HideableQSGNode<NodeTypeThatCanBeCreated> node
// and if the node can only be obtained from QQuickWindow, use:
// HideableQSGNode<QSGProxyNode<NodeThatCantBeCreated>> node
// The latter should obviously be typedef-ed.
//
// Yes, that's all horrible, but if nothing else it teaches us about
// composition.
template <typename Node>
class HideableQSGNode : public Node {
bool hidden;
bool isSubtreeBlocked() const override final;
public:
template<class... Args>
HideableQSGNode(bool visible, Args&&... args);
void setVisible(bool visible);
};
template <typename Node>
class QSGProxyNode : public QSGNode {
public:
std::unique_ptr<Node> node;
QSGProxyNode(Node *node);
};
// Implementation detail of templates - move to serparate header file
template <typename Node>
QSGProxyNode<Node>::QSGProxyNode(Node *node) : node(node)
{
appendChildNode(node);
}
template <typename Node>
bool HideableQSGNode<Node>::isSubtreeBlocked() const
{
return hidden;
}
template <typename Node>
template<class... Args>
HideableQSGNode<Node>::HideableQSGNode(bool visible, Args&&... args) :
Node(std::forward<Args>(args)...),
hidden(!visible)
{
}
template <typename Node>
void HideableQSGNode<Node>::setVisible(bool visible)
{
hidden = !visible;
}
#endif