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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// SPDX-License-Identifier: GPL-2.0
#include "chartitem.h"
#include "statsview.h"
#include <cmath>
#include <QQuickWindow>
#include <QSGImageNode>
#include <QSGTexture>
static int round_up(double f)
{
return static_cast<int>(ceil(f));
}
ChartItem::ChartItem(StatsView &v) :
dirty(false), view(v), positionDirty(false), textureDirty(false)
{
}
ChartItem::~ChartItem()
{
painter.reset(); // Make sure to destroy painter before image that is painted on
view.unregisterChartItem(this);
}
QSizeF ChartItem::sceneSize() const
{
return view.size();
}
void ChartItem::setTextureDirty()
{
textureDirty = true;
dirty = true;
}
void ChartItem::setPositionDirty()
{
positionDirty = true;
dirty = true;
}
void ChartItem::render()
{
if (!dirty)
return;
if (!node) {
node.reset(view.w()->createImageNode());
view.addQSGNode(node.get(), 0);
}
if (!img) {
resize(QSizeF(1,1));
img->fill(Qt::transparent);
}
if (textureDirty) {
texture.reset(view.w()->createTextureFromImage(*img, QQuickWindow::TextureHasAlphaChannel));
node->setTexture(texture.get());
textureDirty = false;
}
if (positionDirty) {
node->setRect(rect);
positionDirty = false;
}
dirty = false;
}
void ChartItem::resize(QSizeF size)
{
painter.reset();
img.reset(new QImage(round_up(size.width()), round_up(size.height()), QImage::Format_ARGB32));
painter.reset(new QPainter(img.get()));
painter->setRenderHint(QPainter::Antialiasing);
rect.setSize(size);
setTextureDirty();
}
void ChartItem::setPos(QPointF pos)
{
rect.moveTopLeft(pos);
setPositionDirty();
}
QRectF ChartItem::getRect() const
{
return rect;
}
ChartRectItem::ChartRectItem(StatsView &v, const QPen &pen, const QBrush &brush, double radius) : ChartItem(v),
pen(pen), brush(brush), radius(radius)
{
}
ChartRectItem::~ChartRectItem()
{
}
void ChartRectItem::resize(QSizeF size)
{
ChartItem::resize(size);
img->fill(Qt::transparent);
painter->setPen(pen);
painter->setBrush(brush);
QSize imgSize = img->size();
int width = pen.width();
QRect rect(width / 2, width / 2, imgSize.width() - width, imgSize.height() - width);
painter->drawRoundedRect(rect, radius, radius, Qt::AbsoluteSize);
}
|