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