summaryrefslogtreecommitdiffstats
path: root/stats/chartitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'stats/chartitem.cpp')
-rw-r--r--stats/chartitem.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/stats/chartitem.cpp b/stats/chartitem.cpp
new file mode 100644
index 000000000..c0fc50841
--- /dev/null
+++ b/stats/chartitem.cpp
@@ -0,0 +1,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);
+}