statistics: convert scatter series to use QSG

The original plan to reuse the ChartPixmapItem for the
scatteritems was dumped, because it is unclear if the
textures are shared if generated for each item.

Instead, a new ChartScatterItem was created, where all
items share the same textures (one for highlighted,
one for non-highlighted). This means that the rendering
of the scatter items is now done in the chartitem.cpp
file, which feels like a layering violation. Not good,
but the easiest for now.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-01-17 22:03:27 +01:00 committed by bstoeger
parent 409f159e1d
commit b07a7fe5f1
4 changed files with 123 additions and 69 deletions

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "chartitem.h"
#include "statscolors.h"
#include "statsview.h"
#include <cmath>
@ -95,6 +96,94 @@ QRectF ChartPixmapItem::getRect() const
return rect;
}
static const int scatterItemDiameter = 10;
static const int scatterItemBorder = 1;
ChartScatterItem::ChartScatterItem(StatsView &v, ChartZValue z) : HideableChartItem(v, z),
positionDirty(false), textureDirty(false), highlighted(false)
{
rect.setSize(QSizeF(static_cast<double>(scatterItemDiameter), static_cast<double>(scatterItemDiameter)));
}
ChartScatterItem::~ChartScatterItem()
{
}
static std::unique_ptr<QSGTexture> createScatterTexture(StatsView &view, const QColor &color, const QColor &borderColor)
{
QImage img(scatterItemDiameter, scatterItemDiameter, QImage::Format_ARGB32);
img.fill(Qt::transparent);
QPainter painter(&img);
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(borderColor);
painter.drawEllipse(0, 0, scatterItemDiameter, scatterItemDiameter);
painter.setBrush(color);
painter.drawEllipse(scatterItemBorder, scatterItemBorder,
scatterItemDiameter - 2 * scatterItemBorder,
scatterItemDiameter - 2 * scatterItemBorder);
return std::unique_ptr<QSGTexture>(
view.w()->createTextureFromImage(img, QQuickWindow::TextureHasAlphaChannel)
);
}
std::unique_ptr<QSGTexture> scatterItemTexture;
std::unique_ptr<QSGTexture> scatterItemHighlightedTexture;
void ChartScatterItem::render()
{
if (!scatterItemTexture) {
scatterItemTexture = createScatterTexture(view, fillColor, borderColor);
scatterItemHighlightedTexture = createScatterTexture(view, highlightedColor, highlightedBorderColor);
}
if (!node) {
createNode(view.w()->createImageNode());
view.addQSGNode(node.get(), zValue);
textureDirty = positionDirty = true;
}
if (textureDirty) {
node->node->setTexture(highlighted ? scatterItemHighlightedTexture.get() : scatterItemTexture.get());
textureDirty = false;
}
if (positionDirty) {
node->node->setRect(rect);
positionDirty = false;
}
}
void ChartScatterItem::setPos(QPointF pos)
{
pos -= QPointF(scatterItemDiameter / 2.0, scatterItemDiameter / 2.0);
rect.moveTopLeft(pos);
positionDirty = true;
view.registerDirtyChartItem(*this);
}
static double squareDist(const QPointF &p1, const QPointF &p2)
{
QPointF diff = p1 - p2;
return QPointF::dotProduct(diff, diff);
}
bool ChartScatterItem::contains(QPointF point) const
{
return squareDist(point, rect.center()) <= (scatterItemDiameter / 2.0) * (scatterItemDiameter / 2.0);
}
void ChartScatterItem::setHighlight(bool highlightedIn)
{
if (highlighted == highlightedIn)
return;
highlighted = highlightedIn;
textureDirty = true;
view.registerDirtyChartItem(*this);
}
QRectF ChartScatterItem::getRect() const
{
return rect;
}
ChartRectItem::ChartRectItem(StatsView &v, ChartZValue z,
const QPen &pen, const QBrush &brush, double radius) : ChartPixmapItem(v, z),
pen(pen), brush(brush), radius(radius)