2021-01-12 14:20:05 +00:00
|
|
|
// 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>
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
class QSGGeometry;
|
|
|
|
class QSGGeometryNode;
|
|
|
|
class QSGFlatColorMaterial;
|
2021-01-12 14:20:05 +00:00
|
|
|
class QSGImageNode;
|
2021-01-15 21:48:32 +00:00
|
|
|
class QSGRectangleNode;
|
2021-01-12 14:20:05 +00:00
|
|
|
class QSGTexture;
|
|
|
|
class StatsView;
|
2021-01-13 15:19:27 +00:00
|
|
|
enum class ChartZValue : int;
|
2021-01-12 14:20:05 +00:00
|
|
|
|
|
|
|
class ChartItem {
|
|
|
|
public:
|
2021-01-13 15:19:27 +00:00
|
|
|
ChartItem(StatsView &v, ChartZValue z);
|
2021-01-14 07:48:56 +00:00
|
|
|
virtual ~ChartItem();
|
2021-01-15 11:22:32 +00:00
|
|
|
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
|
2021-01-13 15:19:27 +00:00
|
|
|
const ChartZValue zValue;
|
2021-01-12 14:20:05 +00:00
|
|
|
protected:
|
2021-01-14 07:48:56 +00:00
|
|
|
QSizeF sceneSize() const;
|
|
|
|
StatsView &view;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chart item that blits a precalculated pixmap onto the scene.
|
|
|
|
class ChartPixmapItem : public ChartItem {
|
|
|
|
public:
|
|
|
|
ChartPixmapItem(StatsView &v, ChartZValue z);
|
|
|
|
~ChartPixmapItem();
|
|
|
|
|
|
|
|
void setPos(QPointF pos);
|
|
|
|
void render() override; // Only call on render thread!
|
|
|
|
QRectF getRect() const;
|
|
|
|
protected:
|
|
|
|
void resize(QSizeF size); // Resets the canvas. Attention: image is *unitialized*.
|
2021-01-12 14:20:05 +00:00
|
|
|
std::unique_ptr<QPainter> painter;
|
|
|
|
std::unique_ptr<QImage> img;
|
|
|
|
void setTextureDirty();
|
|
|
|
void setPositionDirty();
|
|
|
|
private:
|
|
|
|
QRectF rect;
|
2021-01-14 07:48:56 +00:00
|
|
|
bool positionDirty; // true if the position changed since last render
|
|
|
|
bool textureDirty; // true if the pixmap changed since last render
|
2021-01-12 14:20:05 +00:00
|
|
|
std::unique_ptr<QSGImageNode> node;
|
|
|
|
std::unique_ptr<QSGTexture> texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Draw a rectangular background after resize. Children are responsible for calling update().
|
2021-01-14 07:48:56 +00:00
|
|
|
class ChartRectItem : public ChartPixmapItem {
|
2021-01-12 14:20:05 +00:00
|
|
|
public:
|
2021-01-13 15:19:27 +00:00
|
|
|
ChartRectItem(StatsView &v, ChartZValue z, const QPen &pen, const QBrush &brush, double radius);
|
2021-01-12 14:20:05 +00:00
|
|
|
~ChartRectItem();
|
|
|
|
void resize(QSizeF size);
|
|
|
|
private:
|
|
|
|
QPen pen;
|
|
|
|
QBrush brush;
|
|
|
|
double radius;
|
|
|
|
};
|
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
// Attention: text is only drawn after calling setColor()!
|
|
|
|
class ChartTextItem : public ChartPixmapItem {
|
|
|
|
public:
|
|
|
|
ChartTextItem(StatsView &v, ChartZValue z, const QFont &f, const std::vector<QString> &text, bool center);
|
|
|
|
void setColor(const QColor &color);
|
|
|
|
private:
|
|
|
|
QFont f;
|
|
|
|
double fontHeight;
|
|
|
|
bool center;
|
|
|
|
struct Item {
|
|
|
|
QString s;
|
|
|
|
double width;
|
|
|
|
};
|
|
|
|
std::vector<Item> items;
|
|
|
|
};
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
class ChartLineItem : public ChartItem {
|
|
|
|
public:
|
|
|
|
ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width);
|
|
|
|
~ChartLineItem();
|
|
|
|
void setLine(QPointF from, QPointF to);
|
|
|
|
void render() override; // Only call on render thread!
|
|
|
|
private:
|
|
|
|
QPointF from, to;
|
|
|
|
QColor color;
|
|
|
|
double width;
|
2021-01-15 21:48:32 +00:00
|
|
|
bool horizontal;
|
2021-01-14 07:48:56 +00:00
|
|
|
bool positionDirty;
|
|
|
|
bool materialDirty;
|
|
|
|
std::unique_ptr<QSGGeometryNode> node;
|
|
|
|
std::unique_ptr<QSGFlatColorMaterial> material;
|
|
|
|
std::unique_ptr<QSGGeometry> geometry;
|
|
|
|
};
|
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
// A bar in a bar chart: a rectangle bordered by lines.
|
|
|
|
class ChartBarItem : public ChartItem {
|
|
|
|
public:
|
|
|
|
ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal);
|
|
|
|
~ChartBarItem();
|
|
|
|
void setColor(QColor color, QColor borderColor);
|
|
|
|
void setRect(const QRectF &rect);
|
|
|
|
QRectF getRect() const;
|
|
|
|
void render() override; // Only call on render thread!
|
|
|
|
private:
|
|
|
|
QColor color, borderColor;
|
|
|
|
double borderWidth;
|
|
|
|
QRectF rect;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-01-12 14:20:05 +00:00
|
|
|
#endif
|