// SPDX-License-Identifier: GPL-2.0 #ifndef STATS_VIEW_H #define STATS_VIEW_H #include "statsstate.h" #include #include #include #include #include #include struct dive; struct StatsBinner; struct StatsBin; struct StatsState; struct StatsVariable; class StatsSeries; class CategoryAxis; class ChartItem; class ChartTextItem; class CountAxis; class HistogramAxis; class HistogramMarker; class QuartileMarker; class RegressionItem; class StatsAxis; class StatsGrid; class Legend; class QSGTexture; class RootNode; // Internal implementation detail enum class ChartSubType : int; enum class ChartZValue : int; enum class StatsOperation : int; class StatsView : public QQuickItem { Q_OBJECT public: StatsView(); StatsView(QQuickItem *parent); ~StatsView(); void plot(const StatsState &state); QQuickWindow *w() const; // Make window available to items QSizeF size() const; QRectF plotArea() const; void addQSGNode(QSGNode *node, ChartZValue z); // Must only be called in render thread! void registerDirtyChartItem(ChartItem &item); void unregisterDirtyChartItem(ChartItem &item); template std::unique_ptr createChartItem(Args&&... args); private slots: void replotIfVisible(); private: // QtQuick related things QRectF plotRect; QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override; std::unique_ptr img; std::unique_ptr painter; QGraphicsScene scene; std::unique_ptr texture; void plotAreaChanged(const QSizeF &size); void reset(); // clears all series and axes void setAxes(StatsAxis *x, StatsAxis *y); void plotBarChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable, const StatsBinner *valueBinner, bool labels, bool legend); void plotValueChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable, StatsOperation valueAxisOperation, bool labels); void plotDiscreteCountChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, bool labels); void plotPieChart(const std::vector &dives, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, bool labels, bool legend); void plotDiscreteBoxChart(const std::vector &dives, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable); void plotDiscreteScatter(const std::vector &dives, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable, bool quartiles); void plotHistogramCountChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, bool labels, bool showMedian, bool showMean); void plotHistogramValueChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable, StatsOperation valueAxisOperation, bool labels); void plotHistogramStackedChart(const std::vector &dives, ChartSubType subType, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable, const StatsBinner *valueBinner, bool labels, bool legend); void plotHistogramBoxChart(const std::vector &dives, const StatsVariable *categoryVariable, const StatsBinner *categoryBinner, const StatsVariable *valueVariable); void plotScatter(const std::vector &dives, const StatsVariable *categoryVariable, const StatsVariable *valueVariable); void setTitle(const QString &); void updateTitlePos(); // After resizing, set title to correct position void plotChart(); template T *createSeries(Args&&... args); template T *createAxis(const QString &title, Args&&... args); template CategoryAxis *createCategoryAxis(const QString &title, const StatsBinner &binner, const std::vector &bins, bool isHorizontal); template HistogramAxis *createHistogramAxis(const QString &title, const StatsBinner &binner, const std::vector &bins, bool isHorizontal); CountAxis *createCountAxis(int maxVal, bool isHorizontal); // Helper functions to add feature to the chart void addLineMarker(double pos, double low, double high, const QPen &pen, bool isHorizontal); void addHistogramMarker(double pos, QColor color, bool isHorizontal, StatsAxis *xAxis, StatsAxis *yAxis); StatsState state; QFont titleFont; std::vector> axes; std::unique_ptr grid; std::vector> series; std::unique_ptr legend; std::vector> quartileMarkers; std::vector> histogramMarkers; std::unique_ptr title; std::unique_ptr regressionItem; StatsSeries *highlightedSeries; StatsAxis *xAxis, *yAxis; Legend *draggedItem; QPointF dragStartMouse, dragStartItem; void hoverEnterEvent(QHoverEvent *event) override; void hoverMoveEvent(QHoverEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; RootNode *rootNode; ChartItem *firstDirtyChartItem, *lastDirtyChartItem; }; // This implementation detail must be known to users of the class. // Perhaps move it into a statsview_impl.h file. template std::unique_ptr StatsView::createChartItem(Args&&... args) { std::unique_ptr res(new T(*this, std::forward(args)...)); return res; } #endif