1 | // Copyright (C) 2023 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QGRAPHSVIEW_H |
5 | #define QGRAPHSVIEW_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the QtGraphs API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | |
17 | #include <QtQuick/QQuickItem> |
18 | #include <QtCore/QList> |
19 | #include <QtQml/QQmlListProperty> |
20 | #include <QtGraphs/qabstractseries.h> |
21 | #include <QtGraphs/qgraphstheme.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | class QQuickRectangle; |
26 | class QAbstractAxis; |
27 | class AxisRenderer; |
28 | class BarsRenderer; |
29 | class PointRenderer; |
30 | class PieRenderer; |
31 | class AreaRenderer; |
32 | |
33 | class Q_GRAPHS_EXPORT QGraphsView : public QQuickItem |
34 | { |
35 | Q_OBJECT |
36 | Q_PROPERTY(QGraphsTheme *theme READ theme WRITE setTheme NOTIFY themeChanged FINAL) |
37 | Q_PROPERTY(QQmlListProperty<QObject> seriesList READ seriesList CONSTANT) |
38 | Q_PROPERTY(qreal marginTop READ marginTop WRITE setMarginTop NOTIFY marginTopChanged FINAL) |
39 | Q_PROPERTY(qreal marginBottom READ marginBottom WRITE setMarginBottom NOTIFY marginBottomChanged FINAL) |
40 | Q_PROPERTY(qreal marginLeft READ marginLeft WRITE setMarginLeft NOTIFY marginLeftChanged FINAL) |
41 | Q_PROPERTY(qreal marginRight READ marginRight WRITE setMarginRight NOTIFY marginRightChanged FINAL) |
42 | |
43 | Q_PROPERTY(qreal axisXSmoothing READ axisXSmoothing WRITE setAxisXSmoothing NOTIFY axisXSmoothingChanged FINAL) |
44 | Q_PROPERTY(qreal axisYSmoothing READ axisYSmoothing WRITE setAxisYSmoothing NOTIFY axisYSmoothingChanged FINAL) |
45 | Q_PROPERTY(qreal gridSmoothing READ gridSmoothing WRITE setGridSmoothing NOTIFY gridSmoothingChanged FINAL) |
46 | |
47 | Q_PROPERTY(bool shadowVisible READ isShadowVisible WRITE setShadowVisible NOTIFY |
48 | shadowVisibleChanged FINAL) |
49 | Q_PROPERTY(QColor shadowColor READ shadowColor WRITE setShadowColor NOTIFY shadowColorChanged FINAL) |
50 | Q_PROPERTY(qreal shadowBarWidth READ shadowBarWidth WRITE setShadowBarWidth NOTIFY shadowBarWidthChanged FINAL) |
51 | Q_PROPERTY(qreal shadowXOffset READ shadowXOffset WRITE setShadowXOffset NOTIFY shadowXOffsetChanged FINAL) |
52 | Q_PROPERTY(qreal shadowYOffset READ shadowYOffset WRITE setShadowYOffset NOTIFY shadowYOffsetChanged FINAL) |
53 | Q_PROPERTY(qreal shadowSmoothing READ shadowSmoothing WRITE setShadowSmoothing NOTIFY shadowSmoothingChanged FINAL) |
54 | |
55 | Q_PROPERTY(QAbstractAxis *axisX READ axisX WRITE setAxisX NOTIFY axisXChanged FINAL) |
56 | Q_PROPERTY(QAbstractAxis *axisY READ axisY WRITE setAxisY NOTIFY axisYChanged FINAL) |
57 | Q_PROPERTY( |
58 | Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged FINAL) |
59 | |
60 | Q_CLASSINFO("DefaultProperty" , "seriesList" ) |
61 | QML_NAMED_ELEMENT(GraphsView) |
62 | |
63 | public: |
64 | explicit QGraphsView(QQuickItem *parent = nullptr); |
65 | ~QGraphsView() override; |
66 | |
67 | Q_INVOKABLE void addSeries(QObject *series); |
68 | Q_INVOKABLE void insertSeries(qsizetype index, QObject *series); |
69 | Q_INVOKABLE void removeSeries(QObject *series); |
70 | Q_INVOKABLE void removeSeries(qsizetype index); |
71 | Q_INVOKABLE bool hasSeries(QObject *series); |
72 | |
73 | QList<QObject *> getSeriesList() const { |
74 | return m_seriesList; |
75 | } |
76 | |
77 | QQmlListProperty<QObject> seriesList(); |
78 | static void appendSeriesFunc(QQmlListProperty<QObject> *list, QObject *series); |
79 | static qsizetype countSeriesFunc(QQmlListProperty<QObject> *list); |
80 | static QObject *atSeriesFunc(QQmlListProperty<QObject> *list, qsizetype index); |
81 | static void clearSeriesFunc(QQmlListProperty<QObject> *list); |
82 | |
83 | QGraphsTheme *theme() const; |
84 | void setTheme(QGraphsTheme *newTheme); |
85 | |
86 | qreal marginTop() const; |
87 | void setMarginTop(qreal newMarginTop); |
88 | |
89 | qreal marginBottom() const; |
90 | void setMarginBottom(qreal newMarginBottom); |
91 | |
92 | qreal marginLeft() const; |
93 | void setMarginLeft(qreal newMarginLeft); |
94 | |
95 | qreal marginRight() const; |
96 | void setMarginRight(qreal newMarginRight); |
97 | |
98 | QRectF plotArea() const; |
99 | void updatePlotArea(); |
100 | void updateAxisAreas(); |
101 | |
102 | void addAxis(QAbstractAxis *axis); |
103 | void removeAxis(QAbstractAxis *axis); |
104 | |
105 | qsizetype graphSeriesCount() const; |
106 | void setGraphSeriesCount(qsizetype count); |
107 | |
108 | void createBarsRenderer(); |
109 | void createAxisRenderer(); |
110 | void createPointRenderer(); |
111 | void createPieRenderer(); |
112 | void createAreaRenderer(); |
113 | |
114 | qreal axisXSmoothing() const; |
115 | void setAxisXSmoothing(qreal smoothing); |
116 | qreal axisYSmoothing() const; |
117 | void setAxisYSmoothing(qreal smoothing); |
118 | qreal gridSmoothing() const; |
119 | void setGridSmoothing(qreal smoothing); |
120 | |
121 | bool isShadowVisible() const; |
122 | void setShadowVisible(bool newShadowVisibility); |
123 | QColor shadowColor() const; |
124 | void setShadowColor(QColor newShadowColor); |
125 | qreal shadowBarWidth() const; |
126 | void setShadowBarWidth(qreal newShadowBarWidth); |
127 | qreal shadowXOffset() const; |
128 | void setShadowXOffset(qreal newShadowXOffset); |
129 | qreal shadowYOffset() const; |
130 | void setShadowYOffset(qreal newShadowYOffset); |
131 | qreal shadowSmoothing() const; |
132 | void setShadowSmoothing(qreal smoothing); |
133 | |
134 | QAbstractAxis *axisX() const; |
135 | void setAxisX(QAbstractAxis *axis); |
136 | |
137 | QAbstractAxis *axisY() const; |
138 | void setAxisY(QAbstractAxis *axis); |
139 | |
140 | Qt::Orientation orientation() const; |
141 | void setOrientation(Qt::Orientation newOrientation); |
142 | |
143 | protected: |
144 | void handleHoverEnter(const QString &seriesName, QPointF position, QPointF value); |
145 | void handleHoverExit(const QString &seriesName, QPointF position); |
146 | void handleHover(const QString &seriesName, QPointF position, QPointF value); |
147 | void updateComponentSizes(); |
148 | void componentComplete() override; |
149 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
150 | void mouseMoveEvent(QMouseEvent *event) override; |
151 | void mousePressEvent(QMouseEvent *event) override; |
152 | void mouseReleaseEvent(QMouseEvent *event) override; |
153 | void hoverMoveEvent(QHoverEvent *event) override; |
154 | QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override; |
155 | void updatePolish() override; |
156 | |
157 | Q_SIGNALS: |
158 | void themeChanged(); |
159 | void marginTopChanged(); |
160 | void marginBottomChanged(); |
161 | void marginLeftChanged(); |
162 | void marginRightChanged(); |
163 | void hoverEnter(const QString &seriesName, QPointF position, QPointF value); |
164 | void hoverExit(const QString &seriesName, QPointF position); |
165 | void hover(const QString &seriesName, QPointF position, QPointF value); |
166 | |
167 | void axisXSmoothingChanged(); |
168 | void axisYSmoothingChanged(); |
169 | void gridSmoothingChanged(); |
170 | |
171 | void shadowVisibleChanged(); |
172 | void shadowColorChanged(); |
173 | void shadowBarWidthChanged(); |
174 | void shadowXOffsetChanged(); |
175 | void shadowYOffsetChanged(); |
176 | void shadowSmoothingChanged(); |
177 | |
178 | void axisXChanged(); |
179 | void axisYChanged(); |
180 | |
181 | void orientationChanged(); |
182 | |
183 | private: |
184 | friend class AxisRenderer; |
185 | friend class BarsRenderer; |
186 | friend class PointRenderer; |
187 | friend class AreaRenderer; |
188 | friend class QAbstractAxis; |
189 | |
190 | void polishAndUpdate(); |
191 | int getSeriesRendererIndex(QAbstractSeries *series); |
192 | |
193 | AxisRenderer *m_axisRenderer = nullptr; |
194 | BarsRenderer *m_barsRenderer = nullptr; |
195 | PointRenderer *m_pointRenderer = nullptr; |
196 | PieRenderer *m_pieRenderer = nullptr; |
197 | AreaRenderer *m_areaRenderer = nullptr; |
198 | QList<QObject *> m_seriesList; |
199 | QHash<int, QList<QAbstractSeries *>> m_cleanupSeriesList; |
200 | QQuickRectangle *m_backgroundRectangle = nullptr; |
201 | |
202 | QAbstractAxis *m_axisX = nullptr; |
203 | QAbstractAxis *m_axisY = nullptr; |
204 | Qt::Orientation m_orientation = Qt::Orientation::Vertical; |
205 | |
206 | QGraphsTheme *m_theme = nullptr; |
207 | QGraphsTheme *m_defaultTheme = nullptr; |
208 | |
209 | qsizetype m_graphSeriesCount = 0; |
210 | |
211 | qreal m_marginTop = 20; |
212 | qreal m_marginBottom = 20; |
213 | qreal m_marginLeft = 20; |
214 | qreal m_marginRight = 20; |
215 | QRectF m_plotArea; |
216 | // Areas of axis |
217 | QRectF m_xAxisArea; |
218 | QRectF m_yAxisArea; |
219 | // Areas of axis tickers |
220 | QRectF m_xAxisTickersArea; |
221 | QRectF m_yAxisTickersArea; |
222 | // Areas of axis labels |
223 | QRectF m_xAxisLabelsArea; |
224 | QRectF m_yAxisLabelsArea; |
225 | // Note: Add properties for these |
226 | qreal m_axisTickersWidth = 15; |
227 | qreal m_axisTickersHeight = 15; |
228 | qreal m_axisLabelsWidth = 40; |
229 | qreal m_axisLabelsHeight = 25; |
230 | qreal m_axisXLabelsMargin = 0; |
231 | qreal m_axisYLabelsMargin = 5; |
232 | // Calculated based the the above |
233 | qreal m_axisWidth = 0; |
234 | qreal m_axisHeight = 0; |
235 | |
236 | int m_hoverCount = 0; |
237 | |
238 | qreal m_axisXSmoothing = 1.0; |
239 | qreal m_axisYSmoothing = 1.0; |
240 | qreal m_gridSmoothing = 1.0; |
241 | |
242 | bool m_isShadowVisible = false; |
243 | QColor m_shadowColor = QColorConstants::Black; |
244 | qreal m_shadowBarWidth = 2.0; |
245 | qreal m_shadowXOffset = 0.0; |
246 | qreal m_shadowYOffset = 0.0; |
247 | qreal m_shadowSmoothing = 4.0; |
248 | }; |
249 | |
250 | QT_END_NAMESPACE |
251 | |
252 | #endif |
253 | |