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

source code of qtcharts/src/chartsqml2/declarativechart_p.h