2021-01-12 14:20:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "chartitem.h"
|
|
|
|
#include "statsview.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <QQuickWindow>
|
2021-01-14 07:48:56 +00:00
|
|
|
#include <QSGFlatColorMaterial>
|
2021-01-12 14:20:05 +00:00
|
|
|
#include <QSGImageNode>
|
2021-01-15 21:48:32 +00:00
|
|
|
#include <QSGRectangleNode>
|
2021-01-12 14:20:05 +00:00
|
|
|
#include <QSGTexture>
|
|
|
|
|
|
|
|
static int round_up(double f)
|
|
|
|
{
|
|
|
|
return static_cast<int>(ceil(f));
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:19:27 +00:00
|
|
|
ChartItem::ChartItem(StatsView &v, ChartZValue z) :
|
2021-01-15 11:22:32 +00:00
|
|
|
dirty(false), dirtyPrev(nullptr), dirtyNext(nullptr),
|
|
|
|
zValue(z), view(v)
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartItem::~ChartItem()
|
|
|
|
{
|
2021-01-15 11:22:32 +00:00
|
|
|
if (dirty)
|
|
|
|
view.unregisterDirtyChartItem(*this);
|
2021-01-12 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF ChartItem::sceneSize() const
|
|
|
|
{
|
|
|
|
return view.size();
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
ChartPixmapItem::ChartPixmapItem(StatsView &v, ChartZValue z) : ChartItem(v, z),
|
|
|
|
positionDirty(false), textureDirty(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartPixmapItem::~ChartPixmapItem()
|
|
|
|
{
|
|
|
|
painter.reset(); // Make sure to destroy painter before image that is painted on
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartPixmapItem::setTextureDirty()
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
textureDirty = true;
|
2021-01-15 11:22:32 +00:00
|
|
|
view.registerDirtyChartItem(*this);
|
2021-01-12 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
void ChartPixmapItem::setPositionDirty()
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
positionDirty = true;
|
2021-01-15 11:22:32 +00:00
|
|
|
view.registerDirtyChartItem(*this);
|
2021-01-12 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
void ChartPixmapItem::render()
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
node.reset(view.w()->createImageNode());
|
2021-01-13 15:19:27 +00:00
|
|
|
view.addQSGNode(node.get(), zValue);
|
2021-01-12 14:20:05 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
void ChartPixmapItem::resize(QSizeF size)
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
void ChartPixmapItem::setPos(QPointF pos)
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
rect.moveTopLeft(pos);
|
|
|
|
setPositionDirty();
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
QRectF ChartPixmapItem::getRect() const
|
2021-01-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:19:27 +00:00
|
|
|
ChartRectItem::ChartRectItem(StatsView &v, ChartZValue z,
|
2021-01-14 07:48:56 +00:00
|
|
|
const QPen &pen, const QBrush &brush, double radius) : ChartPixmapItem(v, z),
|
2021-01-12 14:20:05 +00:00
|
|
|
pen(pen), brush(brush), radius(radius)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartRectItem::~ChartRectItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartRectItem::resize(QSizeF size)
|
|
|
|
{
|
2021-01-14 07:48:56 +00:00
|
|
|
ChartPixmapItem::resize(size);
|
2021-01-12 14:20:05 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-01-14 07:48:56 +00:00
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
ChartTextItem::ChartTextItem(StatsView &v, ChartZValue z, const QFont &f, const std::vector<QString> &text, bool center) :
|
|
|
|
ChartPixmapItem(v, z), f(f), center(center)
|
|
|
|
{
|
|
|
|
QFontMetrics fm(f);
|
|
|
|
double totalWidth = 1.0;
|
|
|
|
fontHeight = static_cast<double>(fm.height());
|
|
|
|
double totalHeight = std::max(1.0, static_cast<double>(text.size()) * fontHeight);
|
|
|
|
|
|
|
|
items.reserve(text.size());
|
|
|
|
for (const QString &s: text) {
|
|
|
|
double w = fm.size(Qt::TextSingleLine, s).width();
|
|
|
|
items.push_back({ s, w });
|
|
|
|
if (w > totalWidth)
|
|
|
|
totalWidth = w;
|
|
|
|
}
|
|
|
|
resize(QSizeF(totalWidth, totalHeight));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartTextItem::setColor(const QColor &c)
|
|
|
|
{
|
|
|
|
img->fill(Qt::transparent);
|
|
|
|
double y = 0.0;
|
|
|
|
painter->setPen(QPen(c));
|
|
|
|
painter->setFont(f);
|
|
|
|
double totalWidth = getRect().width();
|
|
|
|
for (const auto &[s, w]: items) {
|
|
|
|
double x = center ? round((totalWidth - w) / 2.0) : 0.0;
|
|
|
|
QRectF rect(x, y, w, fontHeight);
|
|
|
|
painter->drawText(rect, s);
|
|
|
|
y += fontHeight;
|
|
|
|
}
|
|
|
|
setTextureDirty();
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
ChartLineItem::ChartLineItem(StatsView &v, ChartZValue z, QColor color, double width) : ChartItem(v, z),
|
|
|
|
color(color), width(width), positionDirty(false), materialDirty(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartLineItem::~ChartLineItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-15 21:48:32 +00:00
|
|
|
// Helper function to set points
|
|
|
|
void setPoint(QSGGeometry::Point2D &v, const QPointF &p)
|
|
|
|
{
|
|
|
|
v.set(static_cast<float>(p.x()), static_cast<float>(p.y()));
|
|
|
|
}
|
|
|
|
|
2021-01-14 07:48:56 +00:00
|
|
|
void ChartLineItem::render()
|
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
geometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2));
|
|
|
|
geometry->setDrawingMode(QSGGeometry::DrawLines);
|
|
|
|
material.reset(new QSGFlatColorMaterial);
|
|
|
|
node.reset(new QSGGeometryNode);
|
|
|
|
node->setGeometry(geometry.get());
|
|
|
|
node->setMaterial(material.get());
|
|
|
|
view.addQSGNode(node.get(), zValue);
|
|
|
|
positionDirty = materialDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (positionDirty) {
|
|
|
|
// Attention: width is a geometry property and therefore handled by position dirty!
|
|
|
|
geometry->setLineWidth(static_cast<float>(width));
|
|
|
|
auto vertices = geometry->vertexDataAsPoint2D();
|
2021-01-15 21:48:32 +00:00
|
|
|
setPoint(vertices[0], from);
|
|
|
|
setPoint(vertices[1], to);
|
2021-01-14 07:48:56 +00:00
|
|
|
node->markDirty(QSGNode::DirtyGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (materialDirty) {
|
|
|
|
material->setColor(color);
|
|
|
|
node->markDirty(QSGNode::DirtyMaterial);
|
|
|
|
}
|
|
|
|
|
|
|
|
positionDirty = materialDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartLineItem::setLine(QPointF fromIn, QPointF toIn)
|
|
|
|
{
|
|
|
|
from = fromIn;
|
|
|
|
to = toIn;
|
|
|
|
positionDirty = true;
|
2021-01-15 11:22:32 +00:00
|
|
|
view.registerDirtyChartItem(*this);
|
2021-01-14 07:48:56 +00:00
|
|
|
}
|
2021-01-15 21:48:32 +00:00
|
|
|
|
|
|
|
ChartBarItem::ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal) : ChartItem(v, z),
|
|
|
|
borderWidth(borderWidth), horizontal(horizontal),
|
|
|
|
positionDirty(false), colorDirty(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartBarItem::~ChartBarItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartBarItem::render()
|
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
node.reset(view.w()->createRectangleNode());
|
|
|
|
|
|
|
|
borderGeometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4));
|
|
|
|
borderGeometry->setDrawingMode(QSGGeometry::DrawLineLoop);
|
|
|
|
borderGeometry->setLineWidth(static_cast<float>(borderWidth));
|
|
|
|
borderMaterial.reset(new QSGFlatColorMaterial);
|
|
|
|
borderNode.reset(new QSGGeometryNode);
|
|
|
|
borderNode->setGeometry(borderGeometry.get());
|
|
|
|
borderNode->setMaterial(borderMaterial.get());
|
|
|
|
|
|
|
|
node->appendChildNode(borderNode.get());
|
|
|
|
view.addQSGNode(node.get(), zValue);
|
|
|
|
positionDirty = colorDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colorDirty) {
|
|
|
|
node->setColor(color);
|
|
|
|
borderMaterial->setColor(borderColor);
|
|
|
|
borderNode->markDirty(QSGNode::DirtyMaterial);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (positionDirty) {
|
|
|
|
node->setRect(rect);
|
|
|
|
auto vertices = borderGeometry->vertexDataAsPoint2D();
|
|
|
|
if (horizontal) {
|
|
|
|
setPoint(vertices[0], rect.topLeft());
|
|
|
|
setPoint(vertices[1], rect.topRight());
|
|
|
|
setPoint(vertices[2], rect.bottomRight());
|
|
|
|
setPoint(vertices[3], rect.bottomLeft());
|
|
|
|
} else {
|
|
|
|
setPoint(vertices[0], rect.bottomLeft());
|
|
|
|
setPoint(vertices[1], rect.topLeft());
|
|
|
|
setPoint(vertices[2], rect.topRight());
|
|
|
|
setPoint(vertices[3], rect.bottomRight());
|
|
|
|
}
|
|
|
|
borderNode->markDirty(QSGNode::DirtyGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
positionDirty = colorDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartBarItem::setColor(QColor colorIn, QColor borderColorIn)
|
|
|
|
{
|
|
|
|
color = colorIn;
|
|
|
|
borderColor = borderColorIn;
|
|
|
|
colorDirty = true;
|
|
|
|
view.registerDirtyChartItem(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartBarItem::setRect(const QRectF &rectIn)
|
|
|
|
{
|
|
|
|
rect = rectIn;
|
|
|
|
positionDirty = true;
|
|
|
|
view.registerDirtyChartItem(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ChartBarItem::getRect() const
|
|
|
|
{
|
|
|
|
return rect;
|
|
|
|
}
|