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

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