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:
Berthold Stoeger 2023-04-16 20:05:36 +02:00
parent 533cff96f3
commit 2eebae13dd
34 changed files with 1031 additions and 915 deletions

View file

@ -3,12 +3,12 @@
#define STATS_VIEW_H
#include "statsstate.h"
#include "statshelper.h"
#include "statsselection.h"
#include "qt-quick/chartview.h"
#include <memory>
#include <QImage>
#include <QPainter>
#include <QQuickItem>
struct dive;
struct StatsBinner;
@ -18,7 +18,6 @@ struct StatsVariable;
class StatsSeries;
class CategoryAxis;
class ChartItem;
class ChartRectLineItem;
class ChartTextItem;
class CountAxis;
@ -30,14 +29,12 @@ class StatsAxis;
class StatsGrid;
class StatsTheme;
class Legend;
class QSGTexture;
class RootNode; // Internal implementation detail
enum class ChartSubType : int;
enum class StatsOperation : int;
enum class ChartSortMode : int;
class StatsView : public QQuickItem {
class StatsView : public ChartView {
Q_OBJECT
public:
StatsView();
@ -49,44 +46,15 @@ public:
void restrictToSelection();
void unrestrict();
int restrictionCount() const; // <0: no restriction
QQuickWindow *w() const; // Make window available to items
QSizeF size() const;
QRectF plotArea() const;
void setBackgroundColor(QColor color); // Chart must be replot for color to become effective.
void setTheme(bool dark); // Chart must be replot for theme to become effective.
const StatsTheme &getCurrentTheme() const;
void addQSGNode(QSGNode *node, size_t z); // Must only be called in render thread!
void registerChartItem(ChartItem &item);
void registerDirtyChartItem(ChartItem &item);
void emergencyShutdown(); // Called when QQuick decides to delete our root node.
// Create a chart item and add it to the scene.
// The item must not be deleted by the caller, but can be
// scheduled for deletion using deleteChartItem() below.
// Most items can be made invisible, which is preferred over deletion.
// All items on the scene will be deleted once the chart is reset.
template <typename T, class... Args>
ChartItemPtr<T> createChartItem(Args&&... args);
template <typename T>
void deleteChartItem(ChartItemPtr<T> &item);
private slots:
void replotIfVisible();
void divesSelected(const QVector<dive *> &dives);
private:
// QtQuick related things
size_t maxZ;
bool backgroundDirty;
QRectF plotRect;
QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
#else
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
#endif
void plotAreaChanged(const QSizeF &size);
void plotAreaChanged(const QSizeF &size) override;
void reset(); // clears all series and axes
void resetPointers() override;
void setAxes(StatsAxis *x, StatsAxis *y);
void plotBarChart(const std::vector<dive *> &dives,
ChartSubType subType, ChartSortMode sortMode,
@ -144,7 +112,6 @@ private:
StatsState state;
const StatsTheme *currentTheme;
QColor backgroundColor;
std::vector<std::unique_ptr<StatsSeries>> series;
std::unique_ptr<StatsGrid> grid;
std::vector<ChartItemPtr<QuartileMarker>> quartileMarkers;
@ -167,40 +134,6 @@ private:
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
RootNode *rootNode;
// There are three double linked lists of chart items:
// clean items, dirty items and items to be deleted.
// Note that only the render thread must delete chart items,
// and therefore these lists are the only owning pointers
// to chart items. All other pointers are non-owning and
// can therefore become stale.
struct ChartItemList {
ChartItemList();
ChartItem *first, *last;
void append(ChartItem &item);
void remove(ChartItem &item);
void clear();
void splice(ChartItemList &list);
};
ChartItemList cleanItems, dirtyItems, deletedItems;
void deleteChartItemInternal(ChartItem &item);
void freeDeletedChartItems();
};
// This implementation detail must be known to users of the class.
// Perhaps move it into a statsview_impl.h file.
template <typename T, class... Args>
ChartItemPtr<T> StatsView::createChartItem(Args&&... args)
{
return ChartItemPtr(new T(*this, std::forward<Args>(args)...));
}
template <typename T>
void StatsView::deleteChartItem(ChartItemPtr<T> &item)
{
deleteChartItemInternal(*item);
item.reset();
}
#endif