diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-17 22:03:27 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2021-01-20 08:47:18 +0100 |
commit | b07a7fe5f10529f7fde0b7d5f614aa311b18dcc7 (patch) | |
tree | f7d8b5d62bcadeb20ea09728915cf851863ad5db /stats/chartitem.cpp | |
parent | 409f159e1d8875f72f50d371860d4aef1975f065 (diff) | |
download | subsurface-b07a7fe5f10529f7fde0b7d5f614aa311b18dcc7.tar.gz |
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>
Diffstat (limited to 'stats/chartitem.cpp')
-rw-r--r-- | stats/chartitem.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/stats/chartitem.cpp b/stats/chartitem.cpp index 67cfb4937..b8bf0f189 100644 --- a/stats/chartitem.cpp +++ b/stats/chartitem.cpp @@ -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) |