summaryrefslogtreecommitdiffstats
path: root/stats/chartitem.h
blob: 8cb9ef3a76bd8463d652822047ab49cb527ff7d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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