mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
stats: break out common QtQuick part of the code
Move most of the QtQuick code to its own directory, so that it can be reused in the future for the chart. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
533cff96f3
commit
2eebae13dd
34 changed files with 1031 additions and 915 deletions
|
|
@ -1,113 +1,17 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
// Wrappers around QSGImageNode that allow painting onto an image
|
||||
// and then turning that into a texture to be displayed in a QQuickItem.
|
||||
#ifndef CHART_ITEM_H
|
||||
#define CHART_ITEM_H
|
||||
// Chart item specific to the statistics module
|
||||
#ifndef STATS_CHART_ITEM_H
|
||||
#define STATS_CHART_ITEM_H
|
||||
|
||||
#include "statshelper.h"
|
||||
#include "qt-quick/chartitem.h"
|
||||
|
||||
#include <memory>
|
||||
#include <QPainter>
|
||||
|
||||
class QSGGeometry;
|
||||
class QSGGeometryNode;
|
||||
class QSGFlatColorMaterial;
|
||||
class QSGImageNode;
|
||||
class QSGRectangleNode;
|
||||
class QSGTexture;
|
||||
class QSGTextureMaterial;
|
||||
class StatsTheme;
|
||||
class StatsView;
|
||||
|
||||
class ChartItem {
|
||||
public:
|
||||
// Only call on render thread!
|
||||
virtual void render() = 0;
|
||||
bool dirty; // If true, call render() when rebuilding the scene
|
||||
ChartItem *prev, *next; // Double linked list of items
|
||||
const size_t zValue;
|
||||
virtual ~ChartItem(); // Attention: must only be called by render thread.
|
||||
protected:
|
||||
ChartItem(StatsView &v, size_t z);
|
||||
QSizeF sceneSize() const;
|
||||
StatsView &view;
|
||||
void markDirty();
|
||||
};
|
||||
|
||||
template <typename Node>
|
||||
class HideableChartItem : public ChartItem {
|
||||
protected:
|
||||
HideableChartItem(StatsView &v, size_t z);
|
||||
std::unique_ptr<Node> node;
|
||||
bool visible;
|
||||
bool visibleChanged;
|
||||
template<class... Args>
|
||||
void createNode(Args&&... args); // Call to create node with visibility flag.
|
||||
void updateVisible(); // Must be called by child class to update visibility flag!
|
||||
public:
|
||||
void setVisible(bool visible);
|
||||
};
|
||||
|
||||
// A shortcut for ChartItems based on a hideable proxy item
|
||||
template <typename Node>
|
||||
using HideableChartProxyItem = HideableChartItem<HideableQSGNode<QSGProxyNode<Node>>>;
|
||||
|
||||
// A chart item that blits a precalculated pixmap onto the scene.
|
||||
class ChartPixmapItem : public HideableChartProxyItem<QSGImageNode> {
|
||||
public:
|
||||
ChartPixmapItem(StatsView &v, size_t z);
|
||||
~ChartPixmapItem();
|
||||
|
||||
void setPos(QPointF pos);
|
||||
void render() override;
|
||||
QRectF getRect() const;
|
||||
protected:
|
||||
void resize(QSizeF size); // Resets the canvas. Attention: image is *unitialized*.
|
||||
std::unique_ptr<QPainter> painter;
|
||||
std::unique_ptr<QImage> img;
|
||||
void setTextureDirty();
|
||||
void setPositionDirty();
|
||||
QRectF rect;
|
||||
private:
|
||||
bool positionDirty; // true if the position changed since last render
|
||||
bool textureDirty; // true if the pixmap changed since last render
|
||||
std::unique_ptr<QSGTexture> texture;
|
||||
};
|
||||
|
||||
// Draw a rectangular background after resize. Children are responsible for calling update().
|
||||
class ChartRectItem : public ChartPixmapItem {
|
||||
public:
|
||||
ChartRectItem(StatsView &v, size_t z, const QPen &pen, const QBrush &brush, double radius);
|
||||
~ChartRectItem();
|
||||
void resize(QSizeF size);
|
||||
private:
|
||||
QPen pen;
|
||||
QBrush brush;
|
||||
double radius;
|
||||
};
|
||||
|
||||
// Attention: text is only drawn after calling setColor()!
|
||||
class ChartTextItem : public ChartPixmapItem {
|
||||
public:
|
||||
ChartTextItem(StatsView &v, size_t z, const QFont &f, const std::vector<QString> &text, bool center);
|
||||
ChartTextItem(StatsView &v, size_t z, const QFont &f, const QString &text);
|
||||
void setColor(const QColor &color); // Draw on transparent background
|
||||
void setColor(const QColor &color, const QColor &background); // Fill rectangle with given background color
|
||||
private:
|
||||
const QFont &f;
|
||||
double fontHeight;
|
||||
bool center;
|
||||
struct Item {
|
||||
QString s;
|
||||
double width;
|
||||
};
|
||||
std::vector<Item> items;
|
||||
};
|
||||
class ChartView;
|
||||
|
||||
// A pie chart item: draws disk segments onto a pixmap.
|
||||
class ChartPieItem : public ChartPixmapItem {
|
||||
public:
|
||||
ChartPieItem(StatsView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
ChartPieItem(ChartView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
void drawSegment(double from, double to, QColor fill, QColor border, bool selected); // from and to are relative (0-1 is full disk).
|
||||
void resize(QSizeF size); // As in base class, but clears the canvas
|
||||
private:
|
||||
|
|
@ -115,39 +19,10 @@ private:
|
|||
double borderWidth;
|
||||
};
|
||||
|
||||
// Common data for line and rect items. Both are represented by two points.
|
||||
class ChartLineItemBase : public HideableChartItem<HideableQSGNode<QSGGeometryNode>> {
|
||||
public:
|
||||
ChartLineItemBase(StatsView &v, size_t z, QColor color, double width);
|
||||
~ChartLineItemBase();
|
||||
void setLine(QPointF from, QPointF to);
|
||||
protected:
|
||||
QPointF from, to;
|
||||
QColor color;
|
||||
double width;
|
||||
bool positionDirty;
|
||||
bool materialDirty;
|
||||
std::unique_ptr<QSGFlatColorMaterial> material;
|
||||
std::unique_ptr<QSGGeometry> geometry;
|
||||
};
|
||||
|
||||
class ChartLineItem : public ChartLineItemBase {
|
||||
public:
|
||||
using ChartLineItemBase::ChartLineItemBase;
|
||||
void render() override;
|
||||
};
|
||||
|
||||
// A simple rectangle without fill. Specified by any two opposing vertices.
|
||||
class ChartRectLineItem : public ChartLineItemBase {
|
||||
public:
|
||||
using ChartLineItemBase::ChartLineItemBase;
|
||||
void render() override;
|
||||
};
|
||||
|
||||
// A bar in a bar chart: a rectangle bordered by lines.
|
||||
class ChartBarItem : public HideableChartProxyItem<QSGRectangleNode> {
|
||||
public:
|
||||
ChartBarItem(StatsView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
ChartBarItem(ChartView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
~ChartBarItem();
|
||||
void setColor(QColor color, QColor borderColor);
|
||||
void setRect(const QRectF &rect);
|
||||
|
|
@ -177,7 +52,7 @@ private:
|
|||
// A box-and-whiskers item. This is a bit lazy: derive from the bar item and add whiskers.
|
||||
class ChartBoxItem : public ChartBarItem {
|
||||
public:
|
||||
ChartBoxItem(StatsView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
ChartBoxItem(ChartView &v, size_t z, const StatsTheme &theme, double borderWidth);
|
||||
~ChartBoxItem();
|
||||
void setBox(const QRectF &rect, double min, double max, double median); // The rect describes Q1, Q3.
|
||||
QRectF getRect() const; // Note: this extends the center rectangle to include the whiskers.
|
||||
|
|
@ -195,7 +70,7 @@ private:
|
|||
// scatter item here, but so it is for now.
|
||||
class ChartScatterItem : public HideableChartProxyItem<QSGImageNode> {
|
||||
public:
|
||||
ChartScatterItem(StatsView &v, size_t z, const StatsTheme &theme, bool selected);
|
||||
ChartScatterItem(ChartView &v, size_t z, const StatsTheme &theme, bool selected);
|
||||
~ChartScatterItem();
|
||||
|
||||
// Currently, there is no highlighted and selected status.
|
||||
|
|
@ -219,37 +94,4 @@ private:
|
|||
Highlight highlight;
|
||||
};
|
||||
|
||||
// Implementation detail of templates - move to serparate header file
|
||||
template <typename Node>
|
||||
void HideableChartItem<Node>::setVisible(bool visibleIn)
|
||||
{
|
||||
if (visible == visibleIn)
|
||||
return;
|
||||
visible = visibleIn;
|
||||
visibleChanged = true;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
template <typename Node>
|
||||
template<class... Args>
|
||||
void HideableChartItem<Node>::createNode(Args&&... args)
|
||||
{
|
||||
node.reset(new Node(visible, std::forward<Args>(args)...));
|
||||
visibleChanged = false;
|
||||
}
|
||||
|
||||
template <typename Node>
|
||||
HideableChartItem<Node>::HideableChartItem(StatsView &v, size_t z) : ChartItem(v, z),
|
||||
visible(true), visibleChanged(false)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Node>
|
||||
void HideableChartItem<Node>::updateVisible()
|
||||
{
|
||||
if (visibleChanged)
|
||||
node->setVisible(visible);
|
||||
visibleChanged = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue