##// END OF EJS Templates
Further fixes to threaded rendering...
Miikka Heikkinen -
r2506:59c477efbea1
parent child
Show More
@@ -1,964 +1,991
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include "declarativelineseries.h"
23 #include "declarativelineseries.h"
24 #include "declarativeareaseries.h"
24 #include "declarativeareaseries.h"
25 #include "declarativebarseries.h"
25 #include "declarativebarseries.h"
26 #include "declarativepieseries.h"
26 #include "declarativepieseries.h"
27 #include "declarativesplineseries.h"
27 #include "declarativesplineseries.h"
28 #include "declarativescatterseries.h"
28 #include "declarativescatterseries.h"
29 #include "qbarcategoryaxis.h"
29 #include "qbarcategoryaxis.h"
30 #include "qvalueaxis.h"
30 #include "qvalueaxis.h"
31 #include "qlogvalueaxis.h"
31 #include "qlogvalueaxis.h"
32 #include "qcategoryaxis.h"
32 #include "qcategoryaxis.h"
33 #include "qabstractseries_p.h"
33 #include "qabstractseries_p.h"
34 #include "declarativemargins.h"
34 #include "declarativemargins.h"
35 #include "chartdataset_p.h"
35 #include "chartdataset_p.h"
36 #include "declarativeaxes.h"
36 #include "declarativeaxes.h"
37 #include "qchart_p.h"
37 #include "qchart_p.h"
38 #include "qpolarchart.h"
38 #include "qpolarchart.h"
39
39
40 #ifndef QT_ON_ARM
40 #ifndef QT_ON_ARM
41 #include "qdatetimeaxis.h"
41 #include "qdatetimeaxis.h"
42 #endif
42 #endif
43
43
44 #ifdef CHARTS_FOR_QUICK2
44 #ifdef CHARTS_FOR_QUICK2
45 #include <QGraphicsSceneMouseEvent>
45 #include <QGraphicsSceneMouseEvent>
46 #include <QGraphicsSceneHoverEvent>
46 #include <QGraphicsSceneHoverEvent>
47 #include <QApplication>
47 #include <QApplication>
48 #include <QTimer>
48 #include <QTimer>
49 #include <QThread>
49 #endif
50 #endif
50
51
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52
53
53 /*!
54 /*!
54 \qmlclass ChartView DeclarativeChart
55 \qmlclass ChartView DeclarativeChart
55
56
56 ChartView element is the parent that is responsible for showing different chart series types.
57 ChartView element is the parent that is responsible for showing different chart series types.
57
58
58 The following QML shows how to create a simple chart with one pie series:
59 The following QML shows how to create a simple chart with one pie series:
59 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
60 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
60 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
61 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
61 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
62 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
62
63
63 \beginfloatleft
64 \beginfloatleft
64 \image examples_qmlpiechart.png
65 \image examples_qmlpiechart.png
65 \endfloat
66 \endfloat
66 \clearfloat
67 \clearfloat
67 */
68 */
68
69
69 /*!
70 /*!
70 \qmlproperty Theme ChartView::theme
71 \qmlproperty Theme ChartView::theme
71 Theme defines the visual appearance of the chart, including for example colors, fonts, line
72 Theme defines the visual appearance of the chart, including for example colors, fonts, line
72 widths and chart background.
73 widths and chart background.
73 */
74 */
74
75
75 /*!
76 /*!
76 \qmlproperty Animation ChartView::animationOptions
77 \qmlproperty Animation ChartView::animationOptions
77 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
78 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
78 ChartView.SeriesAnimations or ChartView.AllAnimations.
79 ChartView.SeriesAnimations or ChartView.AllAnimations.
79 */
80 */
80
81
81 /*!
82 /*!
82 \qmlproperty Font ChartView::titleFont
83 \qmlproperty Font ChartView::titleFont
83 The title font of the chart
84 The title font of the chart
84
85
85 See the \l {Font} {QML Font Element} for detailed documentation.
86 See the \l {Font} {QML Font Element} for detailed documentation.
86 */
87 */
87
88
88 /*!
89 /*!
89 \qmlproperty string ChartView::title
90 \qmlproperty string ChartView::title
90 The title of the chart, shown on top of the chart.
91 The title of the chart, shown on top of the chart.
91 \sa ChartView::titleColor
92 \sa ChartView::titleColor
92 */
93 */
93
94
94 /*!
95 /*!
95 \qmlproperty color ChartView::titleColor
96 \qmlproperty color ChartView::titleColor
96 The color of the title text.
97 The color of the title text.
97 */
98 */
98
99
99 /*!
100 /*!
100 \qmlproperty Legend ChartView::legend
101 \qmlproperty Legend ChartView::legend
101 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
102 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
102 */
103 */
103
104
104 /*!
105 /*!
105 \qmlproperty int ChartView::count
106 \qmlproperty int ChartView::count
106 The count of series added to the chart.
107 The count of series added to the chart.
107 */
108 */
108
109
109 /*!
110 /*!
110 \qmlproperty color ChartView::backgroundColor
111 \qmlproperty color ChartView::backgroundColor
111 The color of the chart's background. By default background color is defined by chart theme.
112 The color of the chart's background. By default background color is defined by chart theme.
112 \sa ChartView::theme
113 \sa ChartView::theme
113 */
114 */
114
115
115 /*!
116 /*!
116 \qmlproperty color ChartView::plotAreaColor
117 \qmlproperty color ChartView::plotAreaColor
117 The color of the background of the chart's plot area. By default plot area background uses chart's
118 The color of the background of the chart's plot area. By default plot area background uses chart's
118 background color.
119 background color.
119 \sa ChartView::backgroundColor
120 \sa ChartView::backgroundColor
120 */
121 */
121
122
122 /*!
123 /*!
123 \qmlproperty bool ChartView::dropShadowEnabled
124 \qmlproperty bool ChartView::dropShadowEnabled
124 The chart's border drop shadow. Set to true to enable drop shadow.
125 The chart's border drop shadow. Set to true to enable drop shadow.
125 */
126 */
126
127
127 /*!
128 /*!
128 \qmlproperty real ChartView::topMargin
129 \qmlproperty real ChartView::topMargin
129 */
130 */
130
131
131 /*!
132 /*!
132 \qmlproperty real ChartView::bottomMargin
133 \qmlproperty real ChartView::bottomMargin
133 */
134 */
134
135
135 /*!
136 /*!
136 \qmlproperty real ChartView::leftMargin
137 \qmlproperty real ChartView::leftMargin
137 */
138 */
138
139
139 /*!
140 /*!
140 \qmlproperty real ChartView::rightMargin
141 \qmlproperty real ChartView::rightMargin
141 */
142 */
142
143
143 /*!
144 /*!
144 \qmlproperty Margins ChartView::minimumMargins
145 \qmlproperty Margins ChartView::minimumMargins
145 Deprecated; use margins instead.
146 Deprecated; use margins instead.
146 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
147 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
147 area of ChartView is used for drawing title, axes and legend. Please note that setting the
148 area of ChartView is used for drawing title, axes and legend. Please note that setting the
148 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
149 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
149 properties that affect it's layout. If you need to know the actual plotting area used at any
150 properties that affect it's layout. If you need to know the actual plotting area used at any
150 given time, you can check ChartView::plotArea instead.
151 given time, you can check ChartView::plotArea instead.
151 */
152 */
152
153
153 /*!
154 /*!
154 \qmlproperty rect ChartView::plotArea
155 \qmlproperty rect ChartView::plotArea
155 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
156 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
156 margins.
157 margins.
157 \sa ChartView::minimumMargins
158 \sa ChartView::minimumMargins
158 */
159 */
159
160
160 /*!
161 /*!
161 \qmlproperty Margins ChartView::margins
162 \qmlproperty Margins ChartView::margins
162 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
163 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
163 area of ChartView is used for drawing title, axes and legend.
164 area of ChartView is used for drawing title, axes and legend.
164 */
165 */
165
166
166 /*!
167 /*!
167 \qmlmethod AbstractSeries ChartView::series(int index)
168 \qmlmethod AbstractSeries ChartView::series(int index)
168 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
169 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
169 the count property of the chart.
170 the count property of the chart.
170 */
171 */
171
172
172 /*!
173 /*!
173 \qmlmethod AbstractSeries ChartView::series(string name)
174 \qmlmethod AbstractSeries ChartView::series(string name)
174 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
175 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
175 */
176 */
176
177
177 /*!
178 /*!
178 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
179 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
179 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
180 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
180 optional axis \a axisY. For example:
181 optional axis \a axisY. For example:
181 \code
182 \code
182 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
183 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
183 var myAxisX = chartView.axisX(lineSeries);
184 var myAxisX = chartView.axisX(lineSeries);
184 var myAxisY = chartView.axisY(lineSeries);
185 var myAxisY = chartView.axisY(lineSeries);
185 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
186 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
186 \endcode
187 \endcode
187 */
188 */
188
189
189 /*!
190 /*!
190 \qmlmethod ChartView::removeSeries(AbstractSeries series)
191 \qmlmethod ChartView::removeSeries(AbstractSeries series)
191 Removes the \a series from the chart. The series object is also destroyed.
192 Removes the \a series from the chart. The series object is also destroyed.
192 */
193 */
193
194
194 /*!
195 /*!
195 \qmlmethod ChartView::removeAllSeries()
196 \qmlmethod ChartView::removeAllSeries()
196 Removes all series from the chart. All the series objects are also destroyed.
197 Removes all series from the chart. All the series objects are also destroyed.
197 */
198 */
198
199
199 /*!
200 /*!
200 \qmlmethod Axis ChartView::axisX(AbstractSeries series)
201 \qmlmethod Axis ChartView::axisX(AbstractSeries series)
201 The x-axis of the series.
202 The x-axis of the series.
202 */
203 */
203
204
204 /*!
205 /*!
205 \qmlmethod Axis ChartView::axisY(AbstractSeries series)
206 \qmlmethod Axis ChartView::axisY(AbstractSeries series)
206 The y-axis of the series.
207 The y-axis of the series.
207 */
208 */
208
209
209 /*!
210 /*!
210 \qmlmethod ChartView::zoomY(real factor)
211 \qmlmethod ChartView::zoomY(real factor)
211 Zooms in by \a factor on the center of the chart.
212 Zooms in by \a factor on the center of the chart.
212 */
213 */
213
214
214 /*!
215 /*!
215 \qmlmethod ChartView::scrollLeft(real pixels)
216 \qmlmethod ChartView::scrollLeft(real pixels)
216 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
217 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
217 */
218 */
218
219
219 /*!
220 /*!
220 \qmlmethod ChartView::scrollRight(real pixels)
221 \qmlmethod ChartView::scrollRight(real pixels)
221 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
222 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
222 */
223 */
223
224
224 /*!
225 /*!
225 \qmlmethod ChartView::scrollUp(real pixels)
226 \qmlmethod ChartView::scrollUp(real pixels)
226 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
227 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
227 */
228 */
228
229
229 /*!
230 /*!
230 \qmlmethod ChartView::scrollDown(real pixels)
231 \qmlmethod ChartView::scrollDown(real pixels)
231 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
232 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
232 */
233 */
233
234
234 /*!
235 /*!
235 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
236 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
236 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
237 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
237 or if you resize the chart, or if you modify font size related properties of the legend or chart
238 or if you resize the chart, or if you modify font size related properties of the legend or chart
238 title.
239 title.
239 */
240 */
240
241
241 /*!
242 /*!
242 \qmlsignal ChartView::seriesAdded(AbstractSeries series)
243 \qmlsignal ChartView::seriesAdded(AbstractSeries series)
243 The \a series has been added to the chart.
244 The \a series has been added to the chart.
244 */
245 */
245
246
246 /*!
247 /*!
247 \qmlsignal ChartView::seriesRemoved(AbstractSeries series)
248 \qmlsignal ChartView::seriesRemoved(AbstractSeries series)
248 The \a series has been removed from the chart. Please note that \a series is no longer a valid
249 The \a series has been removed from the chart. Please note that \a series is no longer a valid
249 object after the signal handler has completed.
250 object after the signal handler has completed.
250 */
251 */
251
252
252 DeclarativeChart::DeclarativeChart(QDECLARATIVE_ITEM *parent)
253 DeclarativeChart::DeclarativeChart(QDECLARATIVE_ITEM *parent)
253 : QDECLARATIVE_PAINTED_ITEM(parent)
254 : QDECLARATIVE_PAINTED_ITEM(parent)
254 {
255 {
255 initChart(QChart::ChartTypeCartesian);
256 initChart(QChart::ChartTypeCartesian);
256 }
257 }
257
258
258 DeclarativeChart::DeclarativeChart(QChart::ChartType type, QDECLARATIVE_ITEM *parent)
259 DeclarativeChart::DeclarativeChart(QChart::ChartType type, QDECLARATIVE_ITEM *parent)
259 : QDECLARATIVE_PAINTED_ITEM(parent)
260 : QDECLARATIVE_PAINTED_ITEM(parent)
260 {
261 {
261 initChart(type);
262 initChart(type);
262 }
263 }
263
264
264 void DeclarativeChart::initChart(QChart::ChartType type)
265 void DeclarativeChart::initChart(QChart::ChartType type)
265 {
266 {
266 #ifdef CHARTS_FOR_QUICK2
267 #ifdef CHARTS_FOR_QUICK2
267 m_currentSceneImage = 0;
268 m_currentSceneImage = 0;
269 m_guiThreadId = QThread::currentThreadId();
270 m_paintThreadId = 0;
268
271
269 if (type == QChart::ChartTypePolar)
272 if (type == QChart::ChartTypePolar)
270 m_chart = new QPolarChart();
273 m_chart = new QPolarChart();
271 else
274 else
272 m_chart = new QChart();
275 m_chart = new QChart();
273
276
274 m_scene = new QGraphicsScene(this);
277 m_scene = new QGraphicsScene(this);
275 m_scene->addItem(m_chart);
278 m_scene->addItem(m_chart);
276
279
277 setAntialiasing(QQuickItem::antialiasing());
280 setAntialiasing(QQuickItem::antialiasing());
278 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(sceneChanged(QList<QRectF>)));
281 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(sceneChanged(QList<QRectF>)));
279 connect(this, SIGNAL(antialiasingChanged(bool)), this, SLOT(handleAntialiasingChanged(bool)));
282 connect(this, SIGNAL(antialiasingChanged(bool)), this, SLOT(handleAntialiasingChanged(bool)));
280
283
281 setAcceptedMouseButtons(Qt::AllButtons);
284 setAcceptedMouseButtons(Qt::AllButtons);
282 setAcceptHoverEvents(true);
285 setAcceptHoverEvents(true);
283 #else
286 #else
284 if (type == QChart::ChartTypePolar)
287 if (type == QChart::ChartTypePolar)
285 m_chart = new QPolarChart(this);
288 m_chart = new QPolarChart(this);
286 else
289 else
287 m_chart = new QChart(this);
290 m_chart = new QChart(this);
288
291
289 setFlag(QGraphicsItem::ItemHasNoContents, false);
292 setFlag(QGraphicsItem::ItemHasNoContents, false);
290 #endif
293 #endif
291
294
292 m_margins = new DeclarativeMargins(this);
295 m_margins = new DeclarativeMargins(this);
293 m_margins->setTop(m_chart->margins().top());
296 m_margins->setTop(m_chart->margins().top());
294 m_margins->setLeft(m_chart->margins().left());
297 m_margins->setLeft(m_chart->margins().left());
295 m_margins->setRight(m_chart->margins().right());
298 m_margins->setRight(m_chart->margins().right());
296 m_margins->setBottom(m_chart->margins().bottom());
299 m_margins->setBottom(m_chart->margins().bottom());
297 connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
300 connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
298 connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
301 connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
299 connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
302 connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
300 connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
303 connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
301 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), this, SLOT(handleSeriesAdded(QAbstractSeries*)));
304 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), this, SLOT(handleSeriesAdded(QAbstractSeries*)));
302 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*)));
305 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*)));
303 }
306 }
304
307
305 void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series)
308 void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series)
306 {
309 {
307 emit seriesAdded(series);
310 emit seriesAdded(series);
308 }
311 }
309
312
310 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
313 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
311 {
314 {
312 m_chart->setMargins(QMargins(left, top, right, bottom));
315 m_chart->setMargins(QMargins(left, top, right, bottom));
313 emit minimumMarginsChanged();
316 emit minimumMarginsChanged();
314 emit plotAreaChanged(m_chart->plotArea());
317 emit plotAreaChanged(m_chart->plotArea());
315 }
318 }
316
319
317 DeclarativeChart::~DeclarativeChart()
320 DeclarativeChart::~DeclarativeChart()
318 {
321 {
319 delete m_chart;
322 delete m_chart;
320 #ifdef CHARTS_FOR_QUICK2
323 #ifdef CHARTS_FOR_QUICK2
321 m_sceneImageLock.lock();
324 m_sceneImageLock.lock();
322 delete m_currentSceneImage;
325 delete m_currentSceneImage;
323 m_currentSceneImage = 0;
326 m_currentSceneImage = 0;
324 m_sceneImageLock.unlock();
327 m_sceneImageLock.unlock();
325 #endif
328 #endif
326 }
329 }
327
330
328 void DeclarativeChart::childEvent(QChildEvent *event)
331 void DeclarativeChart::childEvent(QChildEvent *event)
329 {
332 {
330 if (event->type() == QEvent::ChildAdded) {
333 if (event->type() == QEvent::ChildAdded) {
331 if (qobject_cast<QAbstractSeries *>(event->child())) {
334 if (qobject_cast<QAbstractSeries *>(event->child())) {
332 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
335 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
333 }
336 }
334 }
337 }
335 }
338 }
336
339
337 void DeclarativeChart::componentComplete()
340 void DeclarativeChart::componentComplete()
338 {
341 {
339 foreach (QObject *child, children()) {
342 foreach (QObject *child, children()) {
340 if (qobject_cast<QAbstractSeries *>(child)) {
343 if (qobject_cast<QAbstractSeries *>(child)) {
341 // Add series to the chart
344 // Add series to the chart
342 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
345 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
343 m_chart->addSeries(series);
346 m_chart->addSeries(series);
344
347
345 // Connect to axis changed signals (unless this is a pie series)
348 // Connect to axis changed signals (unless this is a pie series)
346 if (!qobject_cast<DeclarativePieSeries *>(series)) {
349 if (!qobject_cast<DeclarativePieSeries *>(series)) {
347 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
350 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
348 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
351 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
349 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
352 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
350 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
353 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
351 }
354 }
352
355
353 initializeAxes(series);
356 initializeAxes(series);
354 }
357 }
355 }
358 }
356
359
357 QDECLARATIVE_ITEM::componentComplete();
360 QDECLARATIVE_ITEM::componentComplete();
358 }
361 }
359
362
360 void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis)
363 void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis)
361 {
364 {
362 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
365 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
363 if (axis && s) {
366 if (axis && s) {
364 if (!m_chart->axes(Qt::Horizontal).contains(axis))
367 if (!m_chart->axes(Qt::Horizontal).contains(axis))
365 m_chart->addAxis(axis, Qt::AlignBottom);
368 m_chart->addAxis(axis, Qt::AlignBottom);
366 if (!s->attachedAxes().contains(axis))
369 if (!s->attachedAxes().contains(axis))
367 s->attachAxis(axis);
370 s->attachAxis(axis);
368 } else {
371 } else {
369 qWarning() << "Trying to set axisX to null.";
372 qWarning() << "Trying to set axisX to null.";
370 }
373 }
371 }
374 }
372
375
373 void DeclarativeChart::handleAxisXTopSet(QAbstractAxis *axis)
376 void DeclarativeChart::handleAxisXTopSet(QAbstractAxis *axis)
374 {
377 {
375 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
378 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
376 if (axis && s) {
379 if (axis && s) {
377 if (!m_chart->axes(Qt::Horizontal).contains(axis))
380 if (!m_chart->axes(Qt::Horizontal).contains(axis))
378 m_chart->addAxis(axis, Qt::AlignTop);
381 m_chart->addAxis(axis, Qt::AlignTop);
379 if (!s->attachedAxes().contains(axis))
382 if (!s->attachedAxes().contains(axis))
380 s->attachAxis(axis);
383 s->attachAxis(axis);
381 } else {
384 } else {
382 qWarning() << "Trying to set axisXTop to null.";
385 qWarning() << "Trying to set axisXTop to null.";
383 }
386 }
384 }
387 }
385
388
386 void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis)
389 void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis)
387 {
390 {
388 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
391 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
389 if (axis && s) {
392 if (axis && s) {
390 if (!m_chart->axes(Qt::Vertical).contains(axis))
393 if (!m_chart->axes(Qt::Vertical).contains(axis))
391 m_chart->addAxis(axis, Qt::AlignLeft);
394 m_chart->addAxis(axis, Qt::AlignLeft);
392 if (!s->attachedAxes().contains(axis))
395 if (!s->attachedAxes().contains(axis))
393 s->attachAxis(axis);
396 s->attachAxis(axis);
394 } else {
397 } else {
395 qWarning() << "Trying to set axisY to null.";
398 qWarning() << "Trying to set axisY to null.";
396 }
399 }
397 }
400 }
398
401
399 void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis)
402 void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis)
400 {
403 {
401 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
404 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
402 if (axis && s) {
405 if (axis && s) {
403 if (!m_chart->axes(Qt::Vertical).contains(axis))
406 if (!m_chart->axes(Qt::Vertical).contains(axis))
404 m_chart->addAxis(axis, Qt::AlignRight);
407 m_chart->addAxis(axis, Qt::AlignRight);
405 if (!s->attachedAxes().contains(axis))
408 if (!s->attachedAxes().contains(axis))
406 s->attachAxis(axis);
409 s->attachAxis(axis);
407 } else {
410 } else {
408 qWarning() << "Trying to set axisYRight to null.";
411 qWarning() << "Trying to set axisYRight to null.";
409 }
412 }
410 }
413 }
411
414
412 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
415 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
413 {
416 {
414 if (newGeometry.isValid()) {
417 if (newGeometry.isValid()) {
415 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
418 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
416 m_chart->resize(newGeometry.width(), newGeometry.height());
419 m_chart->resize(newGeometry.width(), newGeometry.height());
417 }
420 }
418 }
421 }
419 QDECLARATIVE_ITEM::geometryChanged(newGeometry, oldGeometry);
422 QDECLARATIVE_ITEM::geometryChanged(newGeometry, oldGeometry);
420
423
421 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
424 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
422 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
425 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
423 // to implement it here for the QML API purposes.
426 // to implement it here for the QML API purposes.
424 emit plotAreaChanged(m_chart->plotArea());
427 emit plotAreaChanged(m_chart->plotArea());
425 }
428 }
426
429
427 #ifdef CHARTS_FOR_QUICK2
430 #ifdef CHARTS_FOR_QUICK2
428 void DeclarativeChart::sceneChanged(QList<QRectF> region)
431 void DeclarativeChart::sceneChanged(QList<QRectF> region)
429 {
432 {
430 Q_UNUSED(region);
433 Q_UNUSED(region);
431
434
435 if (m_guiThreadId == m_paintThreadId) {
436 // Rendering in gui thread, no need for shenannigans, just update
437 update();
438 } else {
439 // Multi-threaded rendering, need to ensure scene is actually rendered in gui thread
432 if (!m_updatePending) {
440 if (!m_updatePending) {
433 m_updatePending = true;
441 m_updatePending = true;
434 // Do async render to avoid some unnecessary renders.
442 // Do async render to avoid some unnecessary renders.
435 QTimer::singleShot(0, this, SLOT(renderScene()));
443 QTimer::singleShot(0, this, SLOT(renderScene()));
436 }
444 }
437 }
445 }
446 }
438
447
439 void DeclarativeChart::renderScene()
448 void DeclarativeChart::renderScene()
440 {
449 {
441 m_updatePending = false;
450 m_updatePending = false;
442 m_sceneImageLock.lock();
451 m_sceneImageLock.lock();
443 delete m_currentSceneImage;
452 delete m_currentSceneImage;
444 m_currentSceneImage = new QImage(m_chart->size().toSize(), QImage::Format_ARGB32);
453 m_currentSceneImage = new QImage(m_chart->size().toSize(), QImage::Format_ARGB32);
445 m_currentSceneImage->fill(Qt::transparent);
454 m_currentSceneImage->fill(Qt::transparent);
446 QPainter painter(m_currentSceneImage);
455 QPainter painter(m_currentSceneImage);
456 if (antialiasing())
457 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
447 QRect renderRect(QPoint(0, 0), m_chart->size().toSize());
458 QRect renderRect(QPoint(0, 0), m_chart->size().toSize());
448 m_scene->render(&painter, renderRect, renderRect);
459 m_scene->render(&painter, renderRect, renderRect);
449 m_sceneImageLock.unlock();
460 m_sceneImageLock.unlock();
450
461
451 update();
462 update();
452 }
463 }
453
464
454 void DeclarativeChart::paint(QPainter *painter)
465 void DeclarativeChart::paint(QPainter *painter)
455 {
466 {
467 if (!m_paintThreadId) {
468 m_paintThreadId = QThread::currentThreadId();
469 if (m_guiThreadId == m_paintThreadId) {
470 // No need for scene image in single threaded rendering, so delete
471 // the one that got made by default before the rendering type was
472 // detected.
473 delete m_currentSceneImage;
474 m_currentSceneImage = 0;
475 }
476 }
477
478 if (m_guiThreadId == m_paintThreadId) {
479 QRectF renderRect(QPointF(0, 0), m_chart->size());
480 m_scene->render(painter, renderRect, renderRect);
481 } else {
456 m_sceneImageLock.lock();
482 m_sceneImageLock.lock();
457 if (m_currentSceneImage) {
483 if (m_currentSceneImage) {
458 QRect imageRect(QPoint(0, 0), m_currentSceneImage->size());
484 QRect imageRect(QPoint(0, 0), m_currentSceneImage->size());
459 QRect itemRect(QPoint(0, 0), QSize(width(), height()));
485 QRect itemRect(QPoint(0, 0), QSize(width(), height()));
460 painter->drawImage(itemRect, *m_currentSceneImage, imageRect);
486 painter->drawImage(itemRect, *m_currentSceneImage, imageRect);
461 }
487 }
462 m_sceneImageLock.unlock();
488 m_sceneImageLock.unlock();
463 }
489 }
490 }
464
491
465 void DeclarativeChart::mousePressEvent(QMouseEvent *event)
492 void DeclarativeChart::mousePressEvent(QMouseEvent *event)
466 {
493 {
467 m_mousePressScenePoint = event->pos();
494 m_mousePressScenePoint = event->pos();
468 m_mousePressScreenPoint = event->globalPos();
495 m_mousePressScreenPoint = event->globalPos();
469 m_lastMouseMoveScenePoint = m_mousePressScenePoint;
496 m_lastMouseMoveScenePoint = m_mousePressScenePoint;
470 m_lastMouseMoveScreenPoint = m_mousePressScreenPoint;
497 m_lastMouseMoveScreenPoint = m_mousePressScreenPoint;
471 m_mousePressButton = event->button();
498 m_mousePressButton = event->button();
472 m_mousePressButtons = event->buttons();
499 m_mousePressButtons = event->buttons();
473
500
474 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
501 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
475 mouseEvent.setWidget(0);
502 mouseEvent.setWidget(0);
476 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
503 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
477 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
504 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
478 mouseEvent.setScenePos(m_mousePressScenePoint);
505 mouseEvent.setScenePos(m_mousePressScenePoint);
479 mouseEvent.setScreenPos(m_mousePressScreenPoint);
506 mouseEvent.setScreenPos(m_mousePressScreenPoint);
480 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
507 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
481 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
508 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
482 mouseEvent.setButtons(m_mousePressButtons);
509 mouseEvent.setButtons(m_mousePressButtons);
483 mouseEvent.setButton(m_mousePressButton);
510 mouseEvent.setButton(m_mousePressButton);
484 mouseEvent.setModifiers(event->modifiers());
511 mouseEvent.setModifiers(event->modifiers());
485 mouseEvent.setAccepted(false);
512 mouseEvent.setAccepted(false);
486
513
487 QApplication::sendEvent(m_scene, &mouseEvent);
514 QApplication::sendEvent(m_scene, &mouseEvent);
488 }
515 }
489
516
490 void DeclarativeChart::mouseReleaseEvent(QMouseEvent *event)
517 void DeclarativeChart::mouseReleaseEvent(QMouseEvent *event)
491 {
518 {
492 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease);
519 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease);
493 mouseEvent.setWidget(0);
520 mouseEvent.setWidget(0);
494 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
521 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
495 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
522 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
496 mouseEvent.setScenePos(event->pos());
523 mouseEvent.setScenePos(event->pos());
497 mouseEvent.setScreenPos(event->globalPos());
524 mouseEvent.setScreenPos(event->globalPos());
498 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
525 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
499 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
526 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
500 mouseEvent.setButtons(event->buttons());
527 mouseEvent.setButtons(event->buttons());
501 mouseEvent.setButton(event->button());
528 mouseEvent.setButton(event->button());
502 mouseEvent.setModifiers(event->modifiers());
529 mouseEvent.setModifiers(event->modifiers());
503 mouseEvent.setAccepted(false);
530 mouseEvent.setAccepted(false);
504
531
505 QApplication::sendEvent(m_scene, &mouseEvent);
532 QApplication::sendEvent(m_scene, &mouseEvent);
506
533
507 m_mousePressButtons = event->buttons();
534 m_mousePressButtons = event->buttons();
508 m_mousePressButton = Qt::NoButton;
535 m_mousePressButton = Qt::NoButton;
509 }
536 }
510
537
511 void DeclarativeChart::hoverMoveEvent(QHoverEvent *event)
538 void DeclarativeChart::hoverMoveEvent(QHoverEvent *event)
512 {
539 {
513 // Convert hover move to mouse move, since we don't seem to get actual mouse move events.
540 // Convert hover move to mouse move, since we don't seem to get actual mouse move events.
514 // QGraphicsScene generates hover events from mouse move events, so we don't need
541 // QGraphicsScene generates hover events from mouse move events, so we don't need
515 // to pass hover events there.
542 // to pass hover events there.
516 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
543 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
517 mouseEvent.setWidget(0);
544 mouseEvent.setWidget(0);
518 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
545 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
519 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
546 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
520 mouseEvent.setScenePos(event->pos());
547 mouseEvent.setScenePos(event->pos());
521 // Hover events do not have global pos in them, and the screen position doesn't seem to
548 // Hover events do not have global pos in them, and the screen position doesn't seem to
522 // matter anyway in this use case, so just pass event pos instead of trying to
549 // matter anyway in this use case, so just pass event pos instead of trying to
523 // calculate the real screen position.
550 // calculate the real screen position.
524 mouseEvent.setScreenPos(event->pos());
551 mouseEvent.setScreenPos(event->pos());
525 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
552 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
526 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
553 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
527 mouseEvent.setButtons(m_mousePressButtons);
554 mouseEvent.setButtons(m_mousePressButtons);
528 mouseEvent.setButton(m_mousePressButton);
555 mouseEvent.setButton(m_mousePressButton);
529 mouseEvent.setModifiers(event->modifiers());
556 mouseEvent.setModifiers(event->modifiers());
530 m_lastMouseMoveScenePoint = mouseEvent.scenePos();
557 m_lastMouseMoveScenePoint = mouseEvent.scenePos();
531 m_lastMouseMoveScreenPoint = mouseEvent.screenPos();
558 m_lastMouseMoveScreenPoint = mouseEvent.screenPos();
532 mouseEvent.setAccepted(false);
559 mouseEvent.setAccepted(false);
533
560
534 QApplication::sendEvent(m_scene, &mouseEvent);
561 QApplication::sendEvent(m_scene, &mouseEvent);
535 }
562 }
536
563
537 void DeclarativeChart::handleAntialiasingChanged(bool enable)
564 void DeclarativeChart::handleAntialiasingChanged(bool enable)
538 {
565 {
539 setAntialiasing(enable);
566 setAntialiasing(enable);
540 }
567 }
541 #endif
568 #endif
542
569
543 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
570 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
544 {
571 {
545 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
572 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
546 if (chartTheme != m_chart->theme())
573 if (chartTheme != m_chart->theme())
547 m_chart->setTheme(chartTheme);
574 m_chart->setTheme(chartTheme);
548 }
575 }
549
576
550 DeclarativeChart::Theme DeclarativeChart::theme()
577 DeclarativeChart::Theme DeclarativeChart::theme()
551 {
578 {
552 return (DeclarativeChart::Theme) m_chart->theme();
579 return (DeclarativeChart::Theme) m_chart->theme();
553 }
580 }
554
581
555 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
582 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
556 {
583 {
557 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
584 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
558 if (animationOptions != m_chart->animationOptions())
585 if (animationOptions != m_chart->animationOptions())
559 m_chart->setAnimationOptions(animationOptions);
586 m_chart->setAnimationOptions(animationOptions);
560 }
587 }
561
588
562 DeclarativeChart::Animation DeclarativeChart::animationOptions()
589 DeclarativeChart::Animation DeclarativeChart::animationOptions()
563 {
590 {
564 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
591 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
565 return DeclarativeChart::AllAnimations;
592 return DeclarativeChart::AllAnimations;
566 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
593 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
567 return DeclarativeChart::GridAxisAnimations;
594 return DeclarativeChart::GridAxisAnimations;
568 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
595 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
569 return DeclarativeChart::SeriesAnimations;
596 return DeclarativeChart::SeriesAnimations;
570 else
597 else
571 return DeclarativeChart::NoAnimation;
598 return DeclarativeChart::NoAnimation;
572 }
599 }
573
600
574 void DeclarativeChart::setTitle(QString title)
601 void DeclarativeChart::setTitle(QString title)
575 {
602 {
576 if (title != m_chart->title())
603 if (title != m_chart->title())
577 m_chart->setTitle(title);
604 m_chart->setTitle(title);
578 }
605 }
579 QString DeclarativeChart::title()
606 QString DeclarativeChart::title()
580 {
607 {
581 return m_chart->title();
608 return m_chart->title();
582 }
609 }
583
610
584 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
611 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
585 {
612 {
586 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Horizontal, series);
613 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Horizontal, series);
587 if (axes.count())
614 if (axes.count())
588 return axes[0];
615 return axes[0];
589 return 0;
616 return 0;
590 }
617 }
591
618
592 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
619 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
593 {
620 {
594 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Vertical, series);
621 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Vertical, series);
595 if (axes.count())
622 if (axes.count())
596 return axes[0];
623 return axes[0];
597 return 0;
624 return 0;
598 }
625 }
599
626
600 QLegend *DeclarativeChart::legend()
627 QLegend *DeclarativeChart::legend()
601 {
628 {
602 return m_chart->legend();
629 return m_chart->legend();
603 }
630 }
604
631
605 void DeclarativeChart::setTitleColor(QColor color)
632 void DeclarativeChart::setTitleColor(QColor color)
606 {
633 {
607 QBrush b = m_chart->titleBrush();
634 QBrush b = m_chart->titleBrush();
608 if (color != b.color()) {
635 if (color != b.color()) {
609 b.setColor(color);
636 b.setColor(color);
610 m_chart->setTitleBrush(b);
637 m_chart->setTitleBrush(b);
611 emit titleColorChanged(color);
638 emit titleColorChanged(color);
612 }
639 }
613 }
640 }
614
641
615 QFont DeclarativeChart::titleFont() const
642 QFont DeclarativeChart::titleFont() const
616 {
643 {
617 return m_chart->titleFont();
644 return m_chart->titleFont();
618 }
645 }
619
646
620 void DeclarativeChart::setTitleFont(const QFont &font)
647 void DeclarativeChart::setTitleFont(const QFont &font)
621 {
648 {
622 m_chart->setTitleFont(font);
649 m_chart->setTitleFont(font);
623 }
650 }
624
651
625 QColor DeclarativeChart::titleColor()
652 QColor DeclarativeChart::titleColor()
626 {
653 {
627 return m_chart->titleBrush().color();
654 return m_chart->titleBrush().color();
628 }
655 }
629
656
630 void DeclarativeChart::setBackgroundColor(QColor color)
657 void DeclarativeChart::setBackgroundColor(QColor color)
631 {
658 {
632 QBrush b = m_chart->backgroundBrush();
659 QBrush b = m_chart->backgroundBrush();
633 if (b.style() != Qt::SolidPattern || color != b.color()) {
660 if (b.style() != Qt::SolidPattern || color != b.color()) {
634 b.setStyle(Qt::SolidPattern);
661 b.setStyle(Qt::SolidPattern);
635 b.setColor(color);
662 b.setColor(color);
636 m_chart->setBackgroundBrush(b);
663 m_chart->setBackgroundBrush(b);
637 emit backgroundColorChanged();
664 emit backgroundColorChanged();
638 }
665 }
639 }
666 }
640
667
641 QColor DeclarativeChart::backgroundColor()
668 QColor DeclarativeChart::backgroundColor()
642 {
669 {
643 return m_chart->backgroundBrush().color();
670 return m_chart->backgroundBrush().color();
644 }
671 }
645
672
646 void QtCommercialChart::DeclarativeChart::setPlotAreaColor(QColor color)
673 void QtCommercialChart::DeclarativeChart::setPlotAreaColor(QColor color)
647 {
674 {
648 QBrush b = m_chart->plotAreaBackgroundBrush();
675 QBrush b = m_chart->plotAreaBackgroundBrush();
649 if (b.style() != Qt::SolidPattern || color != b.color()) {
676 if (b.style() != Qt::SolidPattern || color != b.color()) {
650 b.setStyle(Qt::SolidPattern);
677 b.setStyle(Qt::SolidPattern);
651 b.setColor(color);
678 b.setColor(color);
652 m_chart->setPlotAreaBackgroundBrush(b);
679 m_chart->setPlotAreaBackgroundBrush(b);
653 m_chart->setPlotAreaBackgroundVisible(true);
680 m_chart->setPlotAreaBackgroundVisible(true);
654 emit plotAreaColorChanged();
681 emit plotAreaColorChanged();
655 }
682 }
656 }
683 }
657
684
658 QColor QtCommercialChart::DeclarativeChart::plotAreaColor()
685 QColor QtCommercialChart::DeclarativeChart::plotAreaColor()
659 {
686 {
660 return m_chart->plotAreaBackgroundBrush().color();
687 return m_chart->plotAreaBackgroundBrush().color();
661 }
688 }
662
689
663 int DeclarativeChart::count()
690 int DeclarativeChart::count()
664 {
691 {
665 return m_chart->series().count();
692 return m_chart->series().count();
666 }
693 }
667
694
668 void DeclarativeChart::setDropShadowEnabled(bool enabled)
695 void DeclarativeChart::setDropShadowEnabled(bool enabled)
669 {
696 {
670 if (enabled != m_chart->isDropShadowEnabled()) {
697 if (enabled != m_chart->isDropShadowEnabled()) {
671 m_chart->setDropShadowEnabled(enabled);
698 m_chart->setDropShadowEnabled(enabled);
672 dropShadowEnabledChanged(enabled);
699 dropShadowEnabledChanged(enabled);
673 }
700 }
674 }
701 }
675
702
676 bool DeclarativeChart::dropShadowEnabled()
703 bool DeclarativeChart::dropShadowEnabled()
677 {
704 {
678 return m_chart->isDropShadowEnabled();
705 return m_chart->isDropShadowEnabled();
679 }
706 }
680
707
681 qreal DeclarativeChart::topMargin()
708 qreal DeclarativeChart::topMargin()
682 {
709 {
683 qWarning() << "ChartView.topMargin is deprecated. Use margins instead.";
710 qWarning() << "ChartView.topMargin is deprecated. Use margins instead.";
684 return m_chart->margins().top();
711 return m_chart->margins().top();
685 }
712 }
686
713
687 qreal DeclarativeChart::bottomMargin()
714 qreal DeclarativeChart::bottomMargin()
688 {
715 {
689 qWarning() << "ChartView.bottomMargin is deprecated. Use margins instead.";
716 qWarning() << "ChartView.bottomMargin is deprecated. Use margins instead.";
690 return m_chart->margins().bottom();
717 return m_chart->margins().bottom();
691 }
718 }
692
719
693 qreal DeclarativeChart::leftMargin()
720 qreal DeclarativeChart::leftMargin()
694 {
721 {
695 qWarning() << "ChartView.leftMargin is deprecated. Use margins instead.";
722 qWarning() << "ChartView.leftMargin is deprecated. Use margins instead.";
696 return m_chart->margins().left();
723 return m_chart->margins().left();
697 }
724 }
698
725
699 qreal DeclarativeChart::rightMargin()
726 qreal DeclarativeChart::rightMargin()
700 {
727 {
701 qWarning() << "ChartView.rightMargin is deprecated. Use margins instead.";
728 qWarning() << "ChartView.rightMargin is deprecated. Use margins instead.";
702 return m_chart->margins().right();
729 return m_chart->margins().right();
703 }
730 }
704
731
705 void DeclarativeChart::zoom(qreal factor)
732 void DeclarativeChart::zoom(qreal factor)
706 {
733 {
707 m_chart->zoom(factor);
734 m_chart->zoom(factor);
708 }
735 }
709
736
710 void DeclarativeChart::scrollLeft(qreal pixels)
737 void DeclarativeChart::scrollLeft(qreal pixels)
711 {
738 {
712 m_chart->scroll(-pixels, 0);
739 m_chart->scroll(-pixels, 0);
713 }
740 }
714
741
715 void DeclarativeChart::scrollRight(qreal pixels)
742 void DeclarativeChart::scrollRight(qreal pixels)
716 {
743 {
717 m_chart->scroll(pixels, 0);
744 m_chart->scroll(pixels, 0);
718 }
745 }
719
746
720 void DeclarativeChart::scrollUp(qreal pixels)
747 void DeclarativeChart::scrollUp(qreal pixels)
721 {
748 {
722 m_chart->scroll(0, pixels);
749 m_chart->scroll(0, pixels);
723 }
750 }
724
751
725 void DeclarativeChart::scrollDown(qreal pixels)
752 void DeclarativeChart::scrollDown(qreal pixels)
726 {
753 {
727 m_chart->scroll(0, -pixels);
754 m_chart->scroll(0, -pixels);
728 }
755 }
729
756
730 QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> DeclarativeChart::axes()
757 QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> DeclarativeChart::axes()
731 {
758 {
732 return QDECLARATIVE_LIST_PROPERTY<QAbstractAxis>(this, 0,
759 return QDECLARATIVE_LIST_PROPERTY<QAbstractAxis>(this, 0,
733 &DeclarativeChart::axesAppendFunc,
760 &DeclarativeChart::axesAppendFunc,
734 &DeclarativeChart::axesCountFunc,
761 &DeclarativeChart::axesCountFunc,
735 #ifdef CHARTS_FOR_QUICK2
762 #ifdef CHARTS_FOR_QUICK2
736 &DeclarativeChart::axesAtFunc,
763 &DeclarativeChart::axesAtFunc,
737 &DeclarativeChart::axesClearFunc);
764 &DeclarativeChart::axesClearFunc);
738 #else
765 #else
739 &DeclarativeChart::axesAtFunc);
766 &DeclarativeChart::axesAtFunc);
740 #endif
767 #endif
741 }
768 }
742
769
743 void DeclarativeChart::axesAppendFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, QAbstractAxis *element)
770 void DeclarativeChart::axesAppendFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, QAbstractAxis *element)
744 {
771 {
745 // Empty implementation
772 // Empty implementation
746 Q_UNUSED(list);
773 Q_UNUSED(list);
747 Q_UNUSED(element);
774 Q_UNUSED(element);
748 }
775 }
749
776
750 int DeclarativeChart::axesCountFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list)
777 int DeclarativeChart::axesCountFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list)
751 {
778 {
752 if (qobject_cast<DeclarativeChart *>(list->object)) {
779 if (qobject_cast<DeclarativeChart *>(list->object)) {
753 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
780 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
754 return chart->m_chart->axes(Qt::Horizontal | Qt::Vertical).count();
781 return chart->m_chart->axes(Qt::Horizontal | Qt::Vertical).count();
755 }
782 }
756 return 0;
783 return 0;
757 }
784 }
758
785
759 QAbstractAxis *DeclarativeChart::axesAtFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, int index)
786 QAbstractAxis *DeclarativeChart::axesAtFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, int index)
760 {
787 {
761 if (qobject_cast<DeclarativeChart *>(list->object)) {
788 if (qobject_cast<DeclarativeChart *>(list->object)) {
762 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
789 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
763 QList<QAbstractAxis *> axes = chart->m_chart->axes(Qt::Horizontal | Qt::Vertical, chart->m_chart->series()[0]);
790 QList<QAbstractAxis *> axes = chart->m_chart->axes(Qt::Horizontal | Qt::Vertical, chart->m_chart->series()[0]);
764 return axes.at(index);
791 return axes.at(index);
765 }
792 }
766 return 0;
793 return 0;
767 }
794 }
768
795
769 void DeclarativeChart::axesClearFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list)
796 void DeclarativeChart::axesClearFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list)
770 {
797 {
771 // Empty implementation
798 // Empty implementation
772 Q_UNUSED(list);
799 Q_UNUSED(list);
773 }
800 }
774
801
775
802
776 QAbstractSeries *DeclarativeChart::series(int index)
803 QAbstractSeries *DeclarativeChart::series(int index)
777 {
804 {
778 if (index < m_chart->series().count()) {
805 if (index < m_chart->series().count()) {
779 return m_chart->series().at(index);
806 return m_chart->series().at(index);
780 }
807 }
781 return 0;
808 return 0;
782 }
809 }
783
810
784 QAbstractSeries *DeclarativeChart::series(QString seriesName)
811 QAbstractSeries *DeclarativeChart::series(QString seriesName)
785 {
812 {
786 foreach (QAbstractSeries *series, m_chart->series()) {
813 foreach (QAbstractSeries *series, m_chart->series()) {
787 if (series->name() == seriesName)
814 if (series->name() == seriesName)
788 return series;
815 return series;
789 }
816 }
790 return 0;
817 return 0;
791 }
818 }
792
819
793 QAbstractSeries *DeclarativeChart::createSeries(int type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
820 QAbstractSeries *DeclarativeChart::createSeries(int type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
794 {
821 {
795 QAbstractSeries *series = 0;
822 QAbstractSeries *series = 0;
796
823
797 switch (type) {
824 switch (type) {
798 case DeclarativeChart::SeriesTypeLine:
825 case DeclarativeChart::SeriesTypeLine:
799 series = new DeclarativeLineSeries();
826 series = new DeclarativeLineSeries();
800 break;
827 break;
801 case DeclarativeChart::SeriesTypeArea: {
828 case DeclarativeChart::SeriesTypeArea: {
802 DeclarativeAreaSeries *area = new DeclarativeAreaSeries();
829 DeclarativeAreaSeries *area = new DeclarativeAreaSeries();
803 area->setUpperSeries(new DeclarativeLineSeries());
830 area->setUpperSeries(new DeclarativeLineSeries());
804 series = area;
831 series = area;
805 break;
832 break;
806 }
833 }
807 case DeclarativeChart::SeriesTypeStackedBar:
834 case DeclarativeChart::SeriesTypeStackedBar:
808 series = new DeclarativeStackedBarSeries();
835 series = new DeclarativeStackedBarSeries();
809 break;
836 break;
810 case DeclarativeChart::SeriesTypePercentBar:
837 case DeclarativeChart::SeriesTypePercentBar:
811 series = new DeclarativePercentBarSeries();
838 series = new DeclarativePercentBarSeries();
812 break;
839 break;
813 case DeclarativeChart::SeriesTypeBar:
840 case DeclarativeChart::SeriesTypeBar:
814 series = new DeclarativeBarSeries();
841 series = new DeclarativeBarSeries();
815 break;
842 break;
816 case DeclarativeChart::SeriesTypeHorizontalBar:
843 case DeclarativeChart::SeriesTypeHorizontalBar:
817 series = new DeclarativeHorizontalBarSeries();
844 series = new DeclarativeHorizontalBarSeries();
818 break;
845 break;
819 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
846 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
820 series = new DeclarativeHorizontalPercentBarSeries();
847 series = new DeclarativeHorizontalPercentBarSeries();
821 break;
848 break;
822 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
849 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
823 series = new DeclarativeHorizontalStackedBarSeries();
850 series = new DeclarativeHorizontalStackedBarSeries();
824 break;
851 break;
825 case DeclarativeChart::SeriesTypePie:
852 case DeclarativeChart::SeriesTypePie:
826 series = new DeclarativePieSeries();
853 series = new DeclarativePieSeries();
827 break;
854 break;
828 case DeclarativeChart::SeriesTypeScatter:
855 case DeclarativeChart::SeriesTypeScatter:
829 series = new DeclarativeScatterSeries();
856 series = new DeclarativeScatterSeries();
830 break;
857 break;
831 case DeclarativeChart::SeriesTypeSpline:
858 case DeclarativeChart::SeriesTypeSpline:
832 series = new DeclarativeSplineSeries();
859 series = new DeclarativeSplineSeries();
833 break;
860 break;
834 default:
861 default:
835 qWarning() << "Illegal series type";
862 qWarning() << "Illegal series type";
836 }
863 }
837
864
838 if (series) {
865 if (series) {
839 // Connect to axis changed signals (unless this is a pie series)
866 // Connect to axis changed signals (unless this is a pie series)
840 if (!qobject_cast<DeclarativePieSeries *>(series)) {
867 if (!qobject_cast<DeclarativePieSeries *>(series)) {
841 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
868 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
842 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
869 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
843 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
870 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
844 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
871 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
845 }
872 }
846
873
847 series->setName(name);
874 series->setName(name);
848 m_chart->addSeries(series);
875 m_chart->addSeries(series);
849
876
850 if (axisX)
877 if (axisX)
851 setAxisX(axisX, series);
878 setAxisX(axisX, series);
852 if (axisY)
879 if (axisY)
853 setAxisY(axisY, series);
880 setAxisY(axisY, series);
854
881
855 if (series->attachedAxes().count() < 2)
882 if (series->attachedAxes().count() < 2)
856 initializeAxes(series);
883 initializeAxes(series);
857 }
884 }
858
885
859 return series;
886 return series;
860 }
887 }
861
888
862 void DeclarativeChart::removeSeries(QAbstractSeries *series)
889 void DeclarativeChart::removeSeries(QAbstractSeries *series)
863 {
890 {
864 if (series)
891 if (series)
865 m_chart->removeSeries(series);
892 m_chart->removeSeries(series);
866 else
893 else
867 qWarning("removeSeries: cannot remove null");
894 qWarning("removeSeries: cannot remove null");
868 }
895 }
869
896
870 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
897 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
871 {
898 {
872 if (axis)
899 if (axis)
873 m_chart->setAxisX(axis, series);
900 m_chart->setAxisX(axis, series);
874 }
901 }
875
902
876 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
903 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
877 {
904 {
878 if (axis)
905 if (axis)
879 m_chart->setAxisY(axis, series);
906 m_chart->setAxisY(axis, series);
880 }
907 }
881
908
882 void DeclarativeChart::createDefaultAxes()
909 void DeclarativeChart::createDefaultAxes()
883 {
910 {
884 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
911 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
885 }
912 }
886
913
887 QAbstractAxis *DeclarativeChart::defaultAxis(Qt::Orientation orientation, QAbstractSeries *series)
914 QAbstractAxis *DeclarativeChart::defaultAxis(Qt::Orientation orientation, QAbstractSeries *series)
888 {
915 {
889 if (!series) {
916 if (!series) {
890 qWarning() << "No axis type defined for null series";
917 qWarning() << "No axis type defined for null series";
891 return 0;
918 return 0;
892 }
919 }
893
920
894 foreach (QAbstractAxis *existingAxis, m_chart->axes(orientation)) {
921 foreach (QAbstractAxis *existingAxis, m_chart->axes(orientation)) {
895 if (existingAxis->type() == series->d_ptr->defaultAxisType(orientation))
922 if (existingAxis->type() == series->d_ptr->defaultAxisType(orientation))
896 return existingAxis;
923 return existingAxis;
897 }
924 }
898
925
899 switch (series->d_ptr->defaultAxisType(orientation)) {
926 switch (series->d_ptr->defaultAxisType(orientation)) {
900 case QAbstractAxis::AxisTypeValue:
927 case QAbstractAxis::AxisTypeValue:
901 return new QValueAxis(this);
928 return new QValueAxis(this);
902 case QAbstractAxis::AxisTypeBarCategory:
929 case QAbstractAxis::AxisTypeBarCategory:
903 return new QBarCategoryAxis(this);
930 return new QBarCategoryAxis(this);
904 case QAbstractAxis::AxisTypeCategory:
931 case QAbstractAxis::AxisTypeCategory:
905 return new QCategoryAxis(this);
932 return new QCategoryAxis(this);
906 #ifndef QT_ON_ARM
933 #ifndef QT_ON_ARM
907 case QAbstractAxis::AxisTypeDateTime:
934 case QAbstractAxis::AxisTypeDateTime:
908 return new QDateTimeAxis(this);
935 return new QDateTimeAxis(this);
909 #endif
936 #endif
910 case QAbstractAxis::AxisTypeLogValue:
937 case QAbstractAxis::AxisTypeLogValue:
911 return new QLogValueAxis(this);
938 return new QLogValueAxis(this);
912 default:
939 default:
913 // assume AxisTypeNoAxis
940 // assume AxisTypeNoAxis
914 return 0;
941 return 0;
915 }
942 }
916 }
943 }
917
944
918 void DeclarativeChart::initializeAxes(QAbstractSeries *series)
945 void DeclarativeChart::initializeAxes(QAbstractSeries *series)
919 {
946 {
920 if (qobject_cast<DeclarativeLineSeries *>(series))
947 if (qobject_cast<DeclarativeLineSeries *>(series))
921 doInitializeAxes(series, qobject_cast<DeclarativeLineSeries *>(series)->m_axes);
948 doInitializeAxes(series, qobject_cast<DeclarativeLineSeries *>(series)->m_axes);
922 else if (qobject_cast<DeclarativeScatterSeries *>(series))
949 else if (qobject_cast<DeclarativeScatterSeries *>(series))
923 doInitializeAxes(series, qobject_cast<DeclarativeScatterSeries *>(series)->m_axes);
950 doInitializeAxes(series, qobject_cast<DeclarativeScatterSeries *>(series)->m_axes);
924 else if (qobject_cast<DeclarativeSplineSeries *>(series))
951 else if (qobject_cast<DeclarativeSplineSeries *>(series))
925 doInitializeAxes(series, qobject_cast<DeclarativeSplineSeries *>(series)->m_axes);
952 doInitializeAxes(series, qobject_cast<DeclarativeSplineSeries *>(series)->m_axes);
926 else if (qobject_cast<DeclarativeAreaSeries *>(series))
953 else if (qobject_cast<DeclarativeAreaSeries *>(series))
927 doInitializeAxes(series, qobject_cast<DeclarativeAreaSeries *>(series)->m_axes);
954 doInitializeAxes(series, qobject_cast<DeclarativeAreaSeries *>(series)->m_axes);
928 else if (qobject_cast<DeclarativeBarSeries *>(series))
955 else if (qobject_cast<DeclarativeBarSeries *>(series))
929 doInitializeAxes(series, qobject_cast<DeclarativeBarSeries *>(series)->m_axes);
956 doInitializeAxes(series, qobject_cast<DeclarativeBarSeries *>(series)->m_axes);
930 else if (qobject_cast<DeclarativeStackedBarSeries *>(series))
957 else if (qobject_cast<DeclarativeStackedBarSeries *>(series))
931 doInitializeAxes(series, qobject_cast<DeclarativeStackedBarSeries *>(series)->m_axes);
958 doInitializeAxes(series, qobject_cast<DeclarativeStackedBarSeries *>(series)->m_axes);
932 else if (qobject_cast<DeclarativePercentBarSeries *>(series))
959 else if (qobject_cast<DeclarativePercentBarSeries *>(series))
933 doInitializeAxes(series, qobject_cast<DeclarativePercentBarSeries *>(series)->m_axes);
960 doInitializeAxes(series, qobject_cast<DeclarativePercentBarSeries *>(series)->m_axes);
934 else if (qobject_cast<DeclarativeHorizontalBarSeries *>(series))
961 else if (qobject_cast<DeclarativeHorizontalBarSeries *>(series))
935 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalBarSeries *>(series)->m_axes);
962 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalBarSeries *>(series)->m_axes);
936 else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series))
963 else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series))
937 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series)->m_axes);
964 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series)->m_axes);
938 else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series))
965 else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series))
939 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series)->m_axes);
966 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series)->m_axes);
940 // else: do nothing
967 // else: do nothing
941 }
968 }
942
969
943 void DeclarativeChart::doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes)
970 void DeclarativeChart::doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes)
944 {
971 {
945 // Initialize axis X
972 // Initialize axis X
946 if (axes->axisX())
973 if (axes->axisX())
947 axes->emitAxisXChanged();
974 axes->emitAxisXChanged();
948 else if (axes->axisXTop())
975 else if (axes->axisXTop())
949 axes->emitAxisXTopChanged();
976 axes->emitAxisXTopChanged();
950 else
977 else
951 axes->setAxisX(defaultAxis(Qt::Horizontal, series));
978 axes->setAxisX(defaultAxis(Qt::Horizontal, series));
952
979
953 // Initialize axis Y
980 // Initialize axis Y
954 if (axes->axisY())
981 if (axes->axisY())
955 axes->emitAxisYChanged();
982 axes->emitAxisYChanged();
956 else if (axes->axisYRight())
983 else if (axes->axisYRight())
957 axes->emitAxisYRightChanged();
984 axes->emitAxisYRightChanged();
958 else
985 else
959 axes->setAxisY(defaultAxis(Qt::Vertical, series));
986 axes->setAxisY(defaultAxis(Qt::Vertical, series));
960 }
987 }
961
988
962 #include "moc_declarativechart.cpp"
989 #include "moc_declarativechart.cpp"
963
990
964 QTCOMMERCIALCHART_END_NAMESPACE
991 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,227 +1,229
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef DECLARATIVECHART_H
21 #ifndef DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
23
23
24 #include <QtCore/QtGlobal>
24 #include <QtCore/QtGlobal>
25 #include "shared_defines.h"
25 #include "shared_defines.h"
26
26
27 #ifdef CHARTS_FOR_QUICK2
27 #ifdef CHARTS_FOR_QUICK2
28 #include <QtQuick/QQuickItem>
28 #include <QtQuick/QQuickItem>
29 #include <QtQuick/QQuickPaintedItem>
29 #include <QtQuick/QQuickPaintedItem>
30 #include <QtWidgets/QGraphicsScene>
30 #include <QtWidgets/QGraphicsScene>
31 #include <QtCore/QMutex>
31 #include <QtCore/QMutex>
32 #else
32 #else
33 #include <QtDeclarative/QDeclarativeItem>
33 #include <QtDeclarative/QDeclarativeItem>
34 #endif
34 #endif
35
35
36 #include "qchart.h"
36 #include "qchart.h"
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class DeclarativeMargins;
40 class DeclarativeMargins;
41 class Domain;
41 class Domain;
42 class DeclarativeAxes;
42 class DeclarativeAxes;
43
43
44 class DeclarativeChart : public QDECLARATIVE_PAINTED_ITEM
44 class DeclarativeChart : public QDECLARATIVE_PAINTED_ITEM
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 Q_PROPERTY(Theme theme READ theme WRITE setTheme)
47 Q_PROPERTY(Theme theme READ theme WRITE setTheme)
48 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions)
48 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions)
49 Q_PROPERTY(QString title READ title WRITE setTitle)
49 Q_PROPERTY(QString title READ title WRITE setTitle)
50 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
50 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
51 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
51 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
52 Q_PROPERTY(QLegend *legend READ legend)
52 Q_PROPERTY(QLegend *legend READ legend)
53 Q_PROPERTY(int count READ count)
53 Q_PROPERTY(int count READ count)
54 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
54 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
55 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
55 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
56 Q_PROPERTY(qreal topMargin READ topMargin)
56 Q_PROPERTY(qreal topMargin READ topMargin)
57 Q_PROPERTY(qreal bottomMargin READ bottomMargin)
57 Q_PROPERTY(qreal bottomMargin READ bottomMargin)
58 Q_PROPERTY(qreal leftMargin READ leftMargin)
58 Q_PROPERTY(qreal leftMargin READ leftMargin)
59 Q_PROPERTY(qreal rightMargin READ rightMargin)
59 Q_PROPERTY(qreal rightMargin READ rightMargin)
60 Q_PROPERTY(DeclarativeMargins *minimumMargins READ minimumMargins NOTIFY minimumMarginsChanged REVISION 1)
60 Q_PROPERTY(DeclarativeMargins *minimumMargins READ minimumMargins NOTIFY minimumMarginsChanged REVISION 1)
61 Q_PROPERTY(DeclarativeMargins *margins READ margins NOTIFY marginsChanged REVISION 2)
61 Q_PROPERTY(DeclarativeMargins *margins READ margins NOTIFY marginsChanged REVISION 2)
62 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged REVISION 1)
62 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged REVISION 1)
63 Q_PROPERTY(QColor plotAreaColor READ plotAreaColor WRITE setPlotAreaColor NOTIFY plotAreaColorChanged REVISION 3)
63 Q_PROPERTY(QColor plotAreaColor READ plotAreaColor WRITE setPlotAreaColor NOTIFY plotAreaColorChanged REVISION 3)
64 #ifdef CHARTS_FOR_QUICK2
64 #ifdef CHARTS_FOR_QUICK2
65 Q_PROPERTY(QQmlListProperty<QAbstractAxis> axes READ axes REVISION 2)
65 Q_PROPERTY(QQmlListProperty<QAbstractAxis> axes READ axes REVISION 2)
66 #else
66 #else
67 Q_PROPERTY(QDeclarativeListProperty<QAbstractAxis> axes READ axes REVISION 2)
67 Q_PROPERTY(QDeclarativeListProperty<QAbstractAxis> axes READ axes REVISION 2)
68 #endif
68 #endif
69 Q_ENUMS(Animation)
69 Q_ENUMS(Animation)
70 Q_ENUMS(Theme)
70 Q_ENUMS(Theme)
71 Q_ENUMS(SeriesType)
71 Q_ENUMS(SeriesType)
72
72
73 public:
73 public:
74 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
74 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
75 enum Theme {
75 enum Theme {
76 ChartThemeLight = 0,
76 ChartThemeLight = 0,
77 ChartThemeBlueCerulean,
77 ChartThemeBlueCerulean,
78 ChartThemeDark,
78 ChartThemeDark,
79 ChartThemeBrownSand,
79 ChartThemeBrownSand,
80 ChartThemeBlueNcs,
80 ChartThemeBlueNcs,
81 ChartThemeHighContrast,
81 ChartThemeHighContrast,
82 ChartThemeBlueIcy
82 ChartThemeBlueIcy
83 };
83 };
84
84
85 enum Animation {
85 enum Animation {
86 NoAnimation = 0x0,
86 NoAnimation = 0x0,
87 GridAxisAnimations = 0x1,
87 GridAxisAnimations = 0x1,
88 SeriesAnimations = 0x2,
88 SeriesAnimations = 0x2,
89 AllAnimations = 0x3
89 AllAnimations = 0x3
90 };
90 };
91
91
92 enum SeriesType {
92 enum SeriesType {
93 SeriesTypeLine,
93 SeriesTypeLine,
94 SeriesTypeArea,
94 SeriesTypeArea,
95 SeriesTypeBar,
95 SeriesTypeBar,
96 SeriesTypeStackedBar,
96 SeriesTypeStackedBar,
97 SeriesTypePercentBar,
97 SeriesTypePercentBar,
98 SeriesTypePie,
98 SeriesTypePie,
99 SeriesTypeScatter,
99 SeriesTypeScatter,
100 SeriesTypeSpline,
100 SeriesTypeSpline,
101 SeriesTypeHorizontalBar,
101 SeriesTypeHorizontalBar,
102 SeriesTypeHorizontalStackedBar,
102 SeriesTypeHorizontalStackedBar,
103 SeriesTypeHorizontalPercentBar
103 SeriesTypeHorizontalPercentBar
104 };
104 };
105
105
106 public:
106 public:
107 DeclarativeChart(QDECLARATIVE_ITEM *parent = 0);
107 DeclarativeChart(QDECLARATIVE_ITEM *parent = 0);
108 ~DeclarativeChart();
108 ~DeclarativeChart();
109
109
110 public: // From parent classes
110 public: // From parent classes
111 void childEvent(QChildEvent *event);
111 void childEvent(QChildEvent *event);
112 void componentComplete();
112 void componentComplete();
113 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
113 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
114 #ifdef CHARTS_FOR_QUICK2
114 #ifdef CHARTS_FOR_QUICK2
115 void paint(QPainter *painter);
115 void paint(QPainter *painter);
116 protected:
116 protected:
117 void mousePressEvent(QMouseEvent *event);
117 void mousePressEvent(QMouseEvent *event);
118 void mouseReleaseEvent(QMouseEvent *event);
118 void mouseReleaseEvent(QMouseEvent *event);
119 void hoverMoveEvent(QHoverEvent *event);
119 void hoverMoveEvent(QHoverEvent *event);
120 private Q_SLOTS:
120 private Q_SLOTS:
121 void handleAntialiasingChanged(bool enable);
121 void handleAntialiasingChanged(bool enable);
122 void sceneChanged(QList<QRectF> region);
122 void sceneChanged(QList<QRectF> region);
123 void renderScene();
123 void renderScene();
124 #endif
124 #endif
125
125
126 public:
126 public:
127 void setTheme(DeclarativeChart::Theme theme);
127 void setTheme(DeclarativeChart::Theme theme);
128 DeclarativeChart::Theme theme();
128 DeclarativeChart::Theme theme();
129 void setAnimationOptions(DeclarativeChart::Animation animations);
129 void setAnimationOptions(DeclarativeChart::Animation animations);
130 DeclarativeChart::Animation animationOptions();
130 DeclarativeChart::Animation animationOptions();
131 void setTitle(QString title);
131 void setTitle(QString title);
132 QString title();
132 QString title();
133 QLegend *legend();
133 QLegend *legend();
134 QFont titleFont() const;
134 QFont titleFont() const;
135 void setTitleFont(const QFont &font);
135 void setTitleFont(const QFont &font);
136 void setTitleColor(QColor color);
136 void setTitleColor(QColor color);
137 QColor titleColor();
137 QColor titleColor();
138 void setBackgroundColor(QColor color);
138 void setBackgroundColor(QColor color);
139 QColor backgroundColor();
139 QColor backgroundColor();
140 Q_REVISION(3) void setPlotAreaColor(QColor color);
140 Q_REVISION(3) void setPlotAreaColor(QColor color);
141 Q_REVISION(3) QColor plotAreaColor();
141 Q_REVISION(3) QColor plotAreaColor();
142 int count();
142 int count();
143 void setDropShadowEnabled(bool enabled);
143 void setDropShadowEnabled(bool enabled);
144 bool dropShadowEnabled();
144 bool dropShadowEnabled();
145
145
146 // Margins & plotArea
146 // Margins & plotArea
147 qreal topMargin();
147 qreal topMargin();
148 qreal bottomMargin();
148 qreal bottomMargin();
149 qreal leftMargin();
149 qreal leftMargin();
150 qreal rightMargin();
150 qreal rightMargin();
151 DeclarativeMargins *minimumMargins() { return m_margins; }
151 DeclarativeMargins *minimumMargins() { return m_margins; }
152 Q_REVISION(2) DeclarativeMargins *margins() { return m_margins; }
152 Q_REVISION(2) DeclarativeMargins *margins() { return m_margins; }
153 QRectF plotArea() { return m_chart->plotArea(); }
153 QRectF plotArea() { return m_chart->plotArea(); }
154
154
155 // Axis handling
155 // Axis handling
156 QAbstractAxis *defaultAxis(Qt::Orientation orientation, QAbstractSeries *series);
156 QAbstractAxis *defaultAxis(Qt::Orientation orientation, QAbstractSeries *series);
157 void initializeAxes(QAbstractSeries *series);
157 void initializeAxes(QAbstractSeries *series);
158 void doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes);
158 void doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes);
159 QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> axes();
159 QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> axes();
160 static void axesAppendFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, QAbstractAxis *element);
160 static void axesAppendFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, QAbstractAxis *element);
161 static int axesCountFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list);
161 static int axesCountFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list);
162 static QAbstractAxis *axesAtFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, int index);
162 static QAbstractAxis *axesAtFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list, int index);
163 static void axesClearFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list);
163 static void axesClearFunc(QDECLARATIVE_LIST_PROPERTY<QAbstractAxis> *list);
164
164
165 public:
165 public:
166 Q_INVOKABLE QAbstractSeries *series(int index);
166 Q_INVOKABLE QAbstractSeries *series(int index);
167 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
167 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
168 Q_INVOKABLE QAbstractSeries *createSeries(int type, QString name = "", QAbstractAxis *axisX = 0, QAbstractAxis *axisY = 0);
168 Q_INVOKABLE QAbstractSeries *createSeries(int type, QString name = "", QAbstractAxis *axisX = 0, QAbstractAxis *axisY = 0);
169 Q_INVOKABLE void removeSeries(QAbstractSeries *series);
169 Q_INVOKABLE void removeSeries(QAbstractSeries *series);
170 Q_INVOKABLE void removeAllSeries() { m_chart->removeAllSeries(); }
170 Q_INVOKABLE void removeAllSeries() { m_chart->removeAllSeries(); }
171 Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0);
171 Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0);
172 Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0);
172 Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0);
173 Q_INVOKABLE void createDefaultAxes();
173 Q_INVOKABLE void createDefaultAxes();
174 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
174 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
175 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
175 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
176 Q_INVOKABLE void zoom(qreal factor);
176 Q_INVOKABLE void zoom(qreal factor);
177 Q_INVOKABLE void scrollLeft(qreal pixels);
177 Q_INVOKABLE void scrollLeft(qreal pixels);
178 Q_INVOKABLE void scrollRight(qreal pixels);
178 Q_INVOKABLE void scrollRight(qreal pixels);
179 Q_INVOKABLE void scrollUp(qreal pixels);
179 Q_INVOKABLE void scrollUp(qreal pixels);
180 Q_INVOKABLE void scrollDown(qreal pixels);
180 Q_INVOKABLE void scrollDown(qreal pixels);
181
181
182 Q_SIGNALS:
182 Q_SIGNALS:
183 void axisLabelsChanged();
183 void axisLabelsChanged();
184 void titleColorChanged(QColor color);
184 void titleColorChanged(QColor color);
185 void backgroundColorChanged();
185 void backgroundColorChanged();
186 void dropShadowEnabledChanged(bool enabled);
186 void dropShadowEnabledChanged(bool enabled);
187 void minimumMarginsChanged();
187 void minimumMarginsChanged();
188 Q_REVISION(2) void marginsChanged();
188 Q_REVISION(2) void marginsChanged();
189 void plotAreaChanged(QRectF plotArea);
189 void plotAreaChanged(QRectF plotArea);
190 void seriesAdded(QAbstractSeries *series);
190 void seriesAdded(QAbstractSeries *series);
191 void seriesRemoved(QAbstractSeries *series);
191 void seriesRemoved(QAbstractSeries *series);
192 Q_REVISION(3) void plotAreaColorChanged();
192 Q_REVISION(3) void plotAreaColorChanged();
193
193
194 private Q_SLOTS:
194 private Q_SLOTS:
195 void changeMinimumMargins(int top, int bottom, int left, int right);
195 void changeMinimumMargins(int top, int bottom, int left, int right);
196 void handleAxisXSet(QAbstractAxis *axis);
196 void handleAxisXSet(QAbstractAxis *axis);
197 void handleAxisYSet(QAbstractAxis *axis);
197 void handleAxisYSet(QAbstractAxis *axis);
198 void handleAxisXTopSet(QAbstractAxis *axis);
198 void handleAxisXTopSet(QAbstractAxis *axis);
199 void handleAxisYRightSet(QAbstractAxis *axis);
199 void handleAxisYRightSet(QAbstractAxis *axis);
200 void handleSeriesAdded(QAbstractSeries *series);
200 void handleSeriesAdded(QAbstractSeries *series);
201
201
202 protected:
202 protected:
203 explicit DeclarativeChart(QChart::ChartType type, QDECLARATIVE_ITEM *parent);
203 explicit DeclarativeChart(QChart::ChartType type, QDECLARATIVE_ITEM *parent);
204
204
205 private:
205 private:
206 void initChart(QChart::ChartType type);
206 void initChart(QChart::ChartType type);
207 // Extending QChart with DeclarativeChart is not possible because QObject does not support
207 // Extending QChart with DeclarativeChart is not possible because QObject does not support
208 // multi inheritance, so we now have a QChart as a member instead
208 // multi inheritance, so we now have a QChart as a member instead
209 QChart *m_chart;
209 QChart *m_chart;
210 #ifdef CHARTS_FOR_QUICK2
210 #ifdef CHARTS_FOR_QUICK2
211 QGraphicsScene *m_scene;
211 QGraphicsScene *m_scene;
212 QPointF m_mousePressScenePoint;
212 QPointF m_mousePressScenePoint;
213 QPoint m_mousePressScreenPoint;
213 QPoint m_mousePressScreenPoint;
214 QPointF m_lastMouseMoveScenePoint;
214 QPointF m_lastMouseMoveScenePoint;
215 QPoint m_lastMouseMoveScreenPoint;
215 QPoint m_lastMouseMoveScreenPoint;
216 Qt::MouseButton m_mousePressButton;
216 Qt::MouseButton m_mousePressButton;
217 Qt::MouseButtons m_mousePressButtons;
217 Qt::MouseButtons m_mousePressButtons;
218 QMutex m_sceneImageLock;
218 QMutex m_sceneImageLock;
219 QImage *m_currentSceneImage;
219 QImage *m_currentSceneImage;
220 bool m_updatePending;
220 bool m_updatePending;
221 Qt::HANDLE m_paintThreadId;
222 Qt::HANDLE m_guiThreadId;
221 #endif
223 #endif
222 DeclarativeMargins *m_margins;
224 DeclarativeMargins *m_margins;
223 };
225 };
224
226
225 QTCOMMERCIALCHART_END_NAMESPACE
227 QTCOMMERCIALCHART_END_NAMESPACE
226
228
227 #endif // DECLARATIVECHART_H
229 #endif // DECLARATIVECHART_H
General Comments 0
You need to be logged in to leave comments. Login now