1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | // W A R N I N G |
31 | // ------------- |
32 | // |
33 | // This file is not part of the Qt Chart API. It exists purely as an |
34 | // implementation detail. This header file may change from version to |
35 | // version without notice, or even be removed. |
36 | // |
37 | // We mean it. |
38 | |
39 | #ifndef DECLARATIVECHART_H |
40 | #define DECLARATIVECHART_H |
41 | |
42 | #include <private/glxyseriesdata_p.h> |
43 | #include <private/declarativechartglobal_p.h> |
44 | #include <private/declarativeabstractrendernode_p.h> |
45 | |
46 | #include <QtCore/QtGlobal> |
47 | #include <QtQuick/QQuickItem> |
48 | #include <QtWidgets/QGraphicsScene> |
49 | |
50 | #include <QtCharts/QChart> |
51 | #include <QtCore/QLocale> |
52 | #include <QQmlComponent> |
53 | |
54 | QT_CHARTS_BEGIN_NAMESPACE |
55 | |
56 | class DeclarativeMargins; |
57 | class Domain; |
58 | class DeclarativeAxes; |
59 | |
60 | class Q_QMLCHARTS_PRIVATE_EXPORT DeclarativeChart : public QQuickItem |
61 | { |
62 | Q_OBJECT |
63 | Q_PROPERTY(Theme theme READ theme WRITE setTheme) |
64 | Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions) |
65 | Q_PROPERTY(int animationDuration READ animationDuration WRITE setAnimationDuration NOTIFY animationDurationChanged REVISION 5) |
66 | Q_PROPERTY(QEasingCurve animationEasingCurve READ animationEasingCurve WRITE setAnimationEasingCurve NOTIFY animationEasingCurveChanged REVISION 5) |
67 | Q_PROPERTY(QString title READ title WRITE setTitle) |
68 | Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont) |
69 | Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged) |
70 | Q_PROPERTY(QtCharts::QLegend *legend READ legend CONSTANT) |
71 | Q_PROPERTY(int count READ count) |
72 | Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) |
73 | Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged) |
74 | Q_PROPERTY(qreal backgroundRoundness READ backgroundRoundness WRITE setBackgroundRoundness NOTIFY backgroundRoundnessChanged REVISION 3) |
75 | Q_PROPERTY(QtCharts::DeclarativeMargins *margins READ margins NOTIFY marginsChanged REVISION 2) |
76 | Q_PROPERTY(QRectF plotArea READ plotArea WRITE setPlotArea NOTIFY plotAreaChanged REVISION 1) |
77 | Q_PROPERTY(QColor plotAreaColor READ plotAreaColor WRITE setPlotAreaColor NOTIFY plotAreaColorChanged REVISION 3) |
78 | Q_PROPERTY(QQmlListProperty<QAbstractAxis> axes READ axes REVISION 2) |
79 | Q_PROPERTY(bool localizeNumbers READ localizeNumbers WRITE setLocalizeNumbers NOTIFY localizeNumbersChanged REVISION 4) |
80 | Q_PROPERTY(QLocale locale READ locale WRITE setLocale NOTIFY localeChanged REVISION 4) |
81 | Q_ENUMS(Animation) |
82 | Q_ENUMS(Theme) |
83 | Q_ENUMS(SeriesType) |
84 | |
85 | public: |
86 | // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api |
87 | enum Theme { |
88 | ChartThemeLight = 0, |
89 | ChartThemeBlueCerulean, |
90 | ChartThemeDark, |
91 | ChartThemeBrownSand, |
92 | ChartThemeBlueNcs, |
93 | ChartThemeHighContrast, |
94 | ChartThemeBlueIcy, |
95 | ChartThemeQt |
96 | }; |
97 | |
98 | enum Animation { |
99 | NoAnimation = 0x0, |
100 | GridAxisAnimations = 0x1, |
101 | SeriesAnimations = 0x2, |
102 | AllAnimations = 0x3 |
103 | }; |
104 | |
105 | enum SeriesType { |
106 | SeriesTypeLine, |
107 | SeriesTypeArea, |
108 | SeriesTypeBar, |
109 | SeriesTypeStackedBar, |
110 | SeriesTypePercentBar, |
111 | SeriesTypePie, |
112 | SeriesTypeScatter, |
113 | SeriesTypeSpline, |
114 | SeriesTypeHorizontalBar, |
115 | SeriesTypeHorizontalStackedBar, |
116 | SeriesTypeHorizontalPercentBar, |
117 | SeriesTypeBoxPlot, |
118 | SeriesTypeCandlestick |
119 | }; |
120 | |
121 | public: |
122 | DeclarativeChart(QQuickItem *parent = 0); |
123 | ~DeclarativeChart(); |
124 | |
125 | public: // From parent classes |
126 | void childEvent(QChildEvent *event); |
127 | void componentComplete(); |
128 | void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); |
129 | QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *); |
130 | protected: |
131 | void mousePressEvent(QMouseEvent *event); |
132 | void mouseReleaseEvent(QMouseEvent *event); |
133 | void hoverMoveEvent(QHoverEvent *event); |
134 | void mouseDoubleClickEvent(QMouseEvent *event); |
135 | private Q_SLOTS: |
136 | void handleAntialiasingChanged(bool enable); |
137 | void sceneChanged(QList<QRectF> region); |
138 | void renderScene(); |
139 | |
140 | public: |
141 | void setTheme(DeclarativeChart::Theme theme); |
142 | DeclarativeChart::Theme theme(); |
143 | void setAnimationOptions(DeclarativeChart::Animation animations); |
144 | DeclarativeChart::Animation animationOptions(); |
145 | void setAnimationDuration(int msecs); |
146 | int animationDuration() const; |
147 | void setAnimationEasingCurve(const QEasingCurve &curve); |
148 | QEasingCurve animationEasingCurve() const; |
149 | void setTitle(QString title); |
150 | QString title(); |
151 | QLegend *legend(); |
152 | QFont titleFont() const; |
153 | void setTitleFont(const QFont &font); |
154 | void setTitleColor(QColor color); |
155 | QColor titleColor(); |
156 | void setBackgroundColor(QColor color); |
157 | QColor backgroundColor(); |
158 | void setPlotAreaColor(QColor color); |
159 | QColor plotAreaColor(); |
160 | void setLocalizeNumbers(bool localize); |
161 | bool localizeNumbers() const; |
162 | void setLocale(const QLocale &locale); |
163 | QLocale locale() const; |
164 | |
165 | int count(); |
166 | void setDropShadowEnabled(bool enabled); |
167 | bool dropShadowEnabled(); |
168 | qreal backgroundRoundness() const; |
169 | void setBackgroundRoundness(qreal diameter); |
170 | |
171 | // Margins & plotArea |
172 | DeclarativeMargins *margins() { return m_margins; } |
173 | QRectF plotArea() { return m_chart->plotArea(); } |
174 | void setPlotArea(const QRectF &rect); |
175 | |
176 | // Axis handling |
177 | QAbstractAxis *defaultAxis(Qt::Orientation orientation, QAbstractSeries *series); |
178 | void initializeAxes(QAbstractSeries *series); |
179 | void doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes); |
180 | QQmlListProperty<QAbstractAxis> axes(); |
181 | static void axesAppendFunc(QQmlListProperty<QAbstractAxis> *list, QAbstractAxis *element); |
182 | static int axesCountFunc(QQmlListProperty<QAbstractAxis> *list); |
183 | static QAbstractAxis *axesAtFunc(QQmlListProperty<QAbstractAxis> *list, int index); |
184 | static void axesClearFunc(QQmlListProperty<QAbstractAxis> *list); |
185 | |
186 | public: |
187 | Q_INVOKABLE QAbstractSeries *series(int index); |
188 | Q_INVOKABLE QAbstractSeries *series(QString seriesName); |
189 | Q_INVOKABLE QAbstractSeries *createSeries(int type, QString name = "" , QAbstractAxis *axisX = 0, QAbstractAxis *axisY = 0); |
190 | Q_INVOKABLE void removeSeries(QAbstractSeries *series); |
191 | Q_INVOKABLE void removeAllSeries() { m_chart->removeAllSeries(); } |
192 | Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0); |
193 | Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0); |
194 | Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0); |
195 | Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0); |
196 | Q_INVOKABLE void zoom(qreal factor); |
197 | Q_REVISION(5) Q_INVOKABLE void zoomIn(); |
198 | Q_REVISION(5) Q_INVOKABLE void zoomIn(const QRectF &rectangle); |
199 | Q_REVISION(5) Q_INVOKABLE void zoomOut(); |
200 | Q_REVISION(5) Q_INVOKABLE void zoomReset(); |
201 | Q_REVISION(5) Q_INVOKABLE bool isZoomed(); |
202 | Q_INVOKABLE void scrollLeft(qreal pixels); |
203 | Q_INVOKABLE void scrollRight(qreal pixels); |
204 | Q_INVOKABLE void scrollUp(qreal pixels); |
205 | Q_INVOKABLE void scrollDown(qreal pixels); |
206 | Q_REVISION(5) Q_INVOKABLE QPointF mapToValue(const QPointF &position, |
207 | QAbstractSeries *series = 0); |
208 | Q_REVISION(5) Q_INVOKABLE QPointF mapToPosition(const QPointF &value, |
209 | QAbstractSeries *series = 0); |
210 | |
211 | |
212 | Q_SIGNALS: |
213 | void axisLabelsChanged(); |
214 | void titleColorChanged(QColor color); |
215 | void backgroundColorChanged(); |
216 | void dropShadowEnabledChanged(bool enabled); |
217 | Q_REVISION(2) void marginsChanged(); |
218 | void plotAreaChanged(QRectF plotArea); |
219 | void seriesAdded(QAbstractSeries *series); |
220 | void seriesRemoved(QAbstractSeries *series); |
221 | Q_REVISION(3) void plotAreaColorChanged(); |
222 | Q_REVISION(3) void backgroundRoundnessChanged(qreal diameter); |
223 | Q_REVISION(4) void localizeNumbersChanged(); |
224 | Q_REVISION(4) void localeChanged(); |
225 | Q_REVISION(5) void animationDurationChanged(int msecs); |
226 | Q_REVISION(5) void animationEasingCurveChanged(QEasingCurve curve); |
227 | void needRender(); |
228 | void pendingRenderNodeMouseEventResponses(); |
229 | |
230 | private Q_SLOTS: |
231 | void changeMargins(int top, int bottom, int left, int right); |
232 | void handleAxisXSet(QAbstractAxis *axis); |
233 | void handleAxisYSet(QAbstractAxis *axis); |
234 | void handleAxisXTopSet(QAbstractAxis *axis); |
235 | void handleAxisYRightSet(QAbstractAxis *axis); |
236 | void handleSeriesAdded(QAbstractSeries *series); |
237 | void handlePendingRenderNodeMouseEventResponses(); |
238 | |
239 | protected: |
240 | explicit DeclarativeChart(QChart::ChartType type, QQuickItem *parent); |
241 | |
242 | private: |
243 | void initChart(QChart::ChartType type); |
244 | void seriesAxisAttachHelper(QAbstractSeries *series, QAbstractAxis *axis, |
245 | Qt::Orientations orientation, Qt::Alignment alignment); |
246 | void findMinMaxForSeries(QAbstractSeries *series,Qt::Orientations orientation, |
247 | qreal &min, qreal &max); |
248 | void queueRendererMouseEvent(QMouseEvent *event); |
249 | |
250 | // Extending QChart with DeclarativeChart is not possible because QObject does not support |
251 | // multi inheritance, so we now have a QChart as a member instead |
252 | QChart *m_chart; |
253 | QGraphicsScene *m_scene; |
254 | QPointF m_mousePressScenePoint; |
255 | QPoint m_mousePressScreenPoint; |
256 | QPointF m_lastMouseMoveScenePoint; |
257 | QPoint m_lastMouseMoveScreenPoint; |
258 | Qt::MouseButton m_mousePressButton; |
259 | Qt::MouseButtons m_mousePressButtons; |
260 | QImage *m_sceneImage; |
261 | bool m_sceneImageDirty; |
262 | bool m_updatePending; |
263 | Qt::HANDLE m_paintThreadId; |
264 | Qt::HANDLE m_guiThreadId; |
265 | DeclarativeMargins *m_margins; |
266 | GLXYSeriesDataManager *m_glXYDataManager; |
267 | bool m_sceneImageNeedsClear; |
268 | QVector<QMouseEvent *> m_pendingRenderNodeMouseEvents; |
269 | QVector<MouseEventResponse> m_pendingRenderNodeMouseEventResponses; |
270 | QRectF m_adjustedPlotArea; |
271 | }; |
272 | |
273 | QT_CHARTS_END_NAMESPACE |
274 | |
275 | #endif // DECLARATIVECHART_H |
276 | |