subsurface/stats/chartitem.h
Berthold Stoeger e1c0cace95 statistics: add notion of Z-value to chart items
The chart items were drawn in order of creation. To control this,
add a notion of Z-value. In contrast to QGraphicsScene, make
this a small integer value.

To controll order of drawing, a plain QSGNode is created for
every possible Z-Value and items are added to these nodes.
Thus, items are rendered by Z-value and if the Z-value is equal
by order of creation.

Likewise split the list of chart-items into Z-values, so that
items can be quickly unregistered: The items that will be
removed individually will usuall be part of Z-levels with only
few items (e.g. legend, infobox). Z-levels with many items
(notably the series) will always be fully rebuilt.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-20 08:47:18 +01:00

53 lines
1.4 KiB
C++

// SPDX-License-Identifier: GPL-2.0
// Wrappers around QSGImageNode that allow painting onto an image
// and then turning that into a texture to be displayed in a QQuickItem.
#ifndef CHART_ITEM_H
#define CHART_ITEM_H
#include <memory>
#include <QPainter>
class QSGImageNode;
class QSGTexture;
class StatsView;
enum class ChartZValue : int;
class ChartItem {
public:
ChartItem(StatsView &v, ChartZValue z);
~ChartItem();
// Attention: The children are responsible for updating the item. None of these calls will.
void resize(QSizeF size); // Resets the canvas. Attention: image is *unitialized*.
void setPos(QPointF pos);
void render(); // Only call on render thread!
QRectF getRect() const;
bool dirty; // If true, call render() when rebuilding the scene
const ChartZValue zValue;
protected:
std::unique_ptr<QPainter> painter;
std::unique_ptr<QImage> img;
QSizeF sceneSize() const;
void setTextureDirty();
void setPositionDirty();
private:
StatsView &view;
QRectF rect;
bool positionDirty;
bool textureDirty;
std::unique_ptr<QSGImageNode> node;
std::unique_ptr<QSGTexture> texture;
};
// Draw a rectangular background after resize. Children are responsible for calling update().
class ChartRectItem : public ChartItem {
public:
ChartRectItem(StatsView &v, ChartZValue z, const QPen &pen, const QBrush &brush, double radius);
~ChartRectItem();
void resize(QSizeF size);
private:
QPen pen;
QBrush brush;
double radius;
};
#endif