diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-14 08:48:56 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2021-01-20 08:47:18 +0100 |
commit | 790d2b2ddb36fd39fe8f002290288603ba1f0065 (patch) | |
tree | 95382527bd02bb60be9ebf8c22e577d74b41d8c7 /stats/chartitem.cpp | |
parent | b1c0d424081530161c1637a1a24ff07a4b021097 (diff) | |
download | subsurface-790d2b2ddb36fd39fe8f002290288603ba1f0065.tar.gz |
statistics: convert QuartileMarkers to QSGNodes
Slowly converting the QGraphicsScene items to QSGNodes to
avoid full replot of the scene.
This adds a new abstraction for line-nodes. Since the render()
function here is fundamentally different from the pixmap-nodes
we had so far, this has to be made virtual.
Also, move the quartile markers to their own source file,
since the StatsView source file is quite huge already.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats/chartitem.cpp')
-rw-r--r-- | stats/chartitem.cpp | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/stats/chartitem.cpp b/stats/chartitem.cpp index 68e7fa371..6fef5e44f 100644 --- a/stats/chartitem.cpp +++ b/stats/chartitem.cpp @@ -4,6 +4,7 @@ #include <cmath> #include <QQuickWindow> +#include <QSGFlatColorMaterial> #include <QSGImageNode> #include <QSGTexture> @@ -13,13 +14,12 @@ static int round_up(double f) } ChartItem::ChartItem(StatsView &v, ChartZValue z) : - dirty(false), zValue(z), view(v), positionDirty(false), textureDirty(false) + dirty(false), zValue(z), view(v) { } ChartItem::~ChartItem() { - painter.reset(); // Make sure to destroy painter before image that is painted on view.unregisterChartItem(this); } @@ -28,19 +28,29 @@ QSizeF ChartItem::sceneSize() const return view.size(); } -void ChartItem::setTextureDirty() +ChartPixmapItem::ChartPixmapItem(StatsView &v, ChartZValue z) : ChartItem(v, z), + positionDirty(false), textureDirty(false) +{ +} + +ChartPixmapItem::~ChartPixmapItem() +{ + painter.reset(); // Make sure to destroy painter before image that is painted on +} + +void ChartPixmapItem::setTextureDirty() { textureDirty = true; dirty = true; } -void ChartItem::setPositionDirty() +void ChartPixmapItem::setPositionDirty() { positionDirty = true; dirty = true; } -void ChartItem::render() +void ChartPixmapItem::render() { if (!dirty) return; @@ -64,7 +74,7 @@ void ChartItem::render() dirty = false; } -void ChartItem::resize(QSizeF size) +void ChartPixmapItem::resize(QSizeF size) { painter.reset(); img.reset(new QImage(round_up(size.width()), round_up(size.height()), QImage::Format_ARGB32)); @@ -74,19 +84,19 @@ void ChartItem::resize(QSizeF size) setTextureDirty(); } -void ChartItem::setPos(QPointF pos) +void ChartPixmapItem::setPos(QPointF pos) { rect.moveTopLeft(pos); setPositionDirty(); } -QRectF ChartItem::getRect() const +QRectF ChartPixmapItem::getRect() const { return rect; } ChartRectItem::ChartRectItem(StatsView &v, ChartZValue z, - const QPen &pen, const QBrush &brush, double radius) : ChartItem(v, z), + const QPen &pen, const QBrush &brush, double radius) : ChartPixmapItem(v, z), pen(pen), brush(brush), radius(radius) { } @@ -97,7 +107,7 @@ ChartRectItem::~ChartRectItem() void ChartRectItem::resize(QSizeF size) { - ChartItem::resize(size); + ChartPixmapItem::resize(size); img->fill(Qt::transparent); painter->setPen(pen); painter->setBrush(brush); @@ -106,3 +116,50 @@ void ChartRectItem::resize(QSizeF size) QRect rect(width / 2, width / 2, imgSize.width() - width, imgSize.height() - width); painter->drawRoundedRect(rect, radius, radius, Qt::AbsoluteSize); } + +ChartLineItem::ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width) : ChartItem(v, z), + color(color), width(width), positionDirty(false), materialDirty(false) +{ +} + +ChartLineItem::~ChartLineItem() +{ +} + +void ChartLineItem::render() +{ + if (!node) { + geometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2)); + geometry->setDrawingMode(QSGGeometry::DrawLines); + material.reset(new QSGFlatColorMaterial); + node.reset(new QSGGeometryNode); + node->setGeometry(geometry.get()); + node->setMaterial(material.get()); + view.addQSGNode(node.get(), zValue); + positionDirty = materialDirty = true; + } + + if (positionDirty) { + // Attention: width is a geometry property and therefore handled by position dirty! + geometry->setLineWidth(static_cast<float>(width)); + auto vertices = geometry->vertexDataAsPoint2D(); + vertices[0].set(static_cast<float>(from.x()), static_cast<float>(from.y())); + vertices[1].set(static_cast<float>(to.x()), static_cast<float>(to.y())); + node->markDirty(QSGNode::DirtyGeometry); + } + + if (materialDirty) { + material->setColor(color); + node->markDirty(QSGNode::DirtyMaterial); + } + + positionDirty = materialDirty = false; +} + +void ChartLineItem::setLine(QPointF fromIn, QPointF toIn) +{ + from = fromIn; + to = toIn; + positionDirty = true; + dirty = true; +} |