##// END OF EJS Templates
Adds logic to fix overlaping axes
Michal Klocek -
r1626:7d4c8a772239
parent child
Show More
@@ -1,582 +1,585
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 "qabstractaxis.h"
21 #include "qabstractaxis.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QAbstractAxis
27 \class QAbstractAxis
28 \brief The QAbstractAxis class is used for manipulating chart's axis.
28 \brief The QAbstractAxis class is used for manipulating chart's axis.
29 \mainclass
29 \mainclass
30
30
31 There is only one x Axis, however there can be multiple y axes.
31 There is only one x Axis, however there can be multiple y axes.
32 Each chart series can be bound to exactly one Y axis and the shared common X axis.
32 Each chart series can be bound to exactly one Y axis and the shared common X axis.
33 Axis can be setup to show axis line with tick marks, grid lines and shades.
33 Axis can be setup to show axis line with tick marks, grid lines and shades.
34 */
34 */
35
35
36 /*!
36 /*!
37 \qmlclass Axis QAbstractAxis
37 \qmlclass Axis QAbstractAxis
38 \brief The Axis element is used for manipulating chart's axes
38 \brief The Axis element is used for manipulating chart's axes
39
39
40 There is only one x Axis, however there can be multiple y axes on a ChartView.
40 There is only one x Axis, however there can be multiple y axes on a ChartView.
41 Each chart series can be bound to exactly one Y axis and the shared common X axis.
41 Each chart series can be bound to exactly one Y axis and the shared common X axis.
42 Axis can be setup to show axis line with tick marks, grid lines and shades.
42 Axis can be setup to show axis line with tick marks, grid lines and shades.
43
43
44 To access Axes you can use ChartView API. For example:
44 To access Axes you can use ChartView API. For example:
45 \code
45 \code
46 ChartView {
46 ChartView {
47 axisX.min: 0
47 axisX.min: 0
48 axisX.max: 3
48 axisX.max: 3
49 axisX.ticksCount: 4
49 axisX.ticksCount: 4
50 axisY.min: 0
50 axisY.min: 0
51 axisY.max: 4
51 axisY.max: 4
52 // Add a few series...
52 // Add a few series...
53 }
53 }
54 \endcode
54 \endcode
55 */
55 */
56
56
57 /*!
57 /*!
58 \enum QAbstractAxis::AxisType
58 \enum QAbstractAxis::AxisType
59
59
60 The type of the series object.
60 The type of the series object.
61
61
62 \value AxisTypeNoAxis
62 \value AxisTypeNoAxis
63 \value AxisTypeValues
63 \value AxisTypeValues
64 \value AxisTypeCategories
64 \value AxisTypeCategories
65 */
65 */
66
66
67 /*!
67 /*!
68 \property QAbstractAxis::arrowVisible
68 \property QAbstractAxis::arrowVisible
69 The visibility of the axis arrow
69 The visibility of the axis arrow
70 */
70 */
71 /*!
71 /*!
72 \qmlproperty bool Axis::arrrowVisible
72 \qmlproperty bool Axis::arrrowVisible
73 The visibility of the axis arrow
73 The visibility of the axis arrow
74 */
74 */
75
75
76 /*!
76 /*!
77 \property QAbstractAxis::labelsVisible
77 \property QAbstractAxis::labelsVisible
78 Defines if axis labels are visible.
78 Defines if axis labels are visible.
79 */
79 */
80 /*!
80 /*!
81 \qmlproperty bool Axis::labelsVisible
81 \qmlproperty bool Axis::labelsVisible
82 Defines if axis labels are visible.
82 Defines if axis labels are visible.
83 */
83 */
84
84
85 /*!
85 /*!
86 \property QAbstractAxis::visible
86 \property QAbstractAxis::visible
87 The visibility of the axis.
87 The visibility of the axis.
88 */
88 */
89 /*!
89 /*!
90 \qmlproperty bool Axis::visible
90 \qmlproperty bool Axis::visible
91 The visibility of the axis.
91 The visibility of the axis.
92 */
92 */
93
93
94 /*!
94 /*!
95 \property QAbstractAxis::gridVisible
95 \property QAbstractAxis::gridVisible
96 The visibility of the grid lines.
96 The visibility of the grid lines.
97 */
97 */
98 /*!
98 /*!
99 \qmlproperty bool Axis::gridVisible
99 \qmlproperty bool Axis::gridVisible
100 The visibility of the grid lines.
100 The visibility of the grid lines.
101 */
101 */
102
102
103 /*!
103 /*!
104 \property QAbstractAxis::color
104 \property QAbstractAxis::color
105 The color of the axis and ticks.
105 The color of the axis and ticks.
106 */
106 */
107 /*!
107 /*!
108 \qmlproperty color Axis::color
108 \qmlproperty color Axis::color
109 The color of the axis and ticks.
109 The color of the axis and ticks.
110 */
110 */
111
111
112 /*!
112 /*!
113 \property QAbstractAxis::labelsFont
113 \property QAbstractAxis::labelsFont
114 The font of the axis labels.
114 The font of the axis labels.
115 */
115 */
116
116
117 /*!
117 /*!
118 \qmlproperty Font Axis::labelsFont
118 \qmlproperty Font Axis::labelsFont
119 The font of the axis labels.
119 The font of the axis labels.
120
120
121 See the \l {Font} {QML Font Element} for detailed documentation.
121 See the \l {Font} {QML Font Element} for detailed documentation.
122 */
122 */
123
123
124 /*!
124 /*!
125 \property QAbstractAxis::labelsColor
125 \property QAbstractAxis::labelsColor
126 The color of the axis labels.
126 The color of the axis labels.
127 */
127 */
128 /*!
128 /*!
129 \qmlproperty color Axis::labelsColor
129 \qmlproperty color Axis::labelsColor
130 The color of the axis labels.
130 The color of the axis labels.
131 */
131 */
132
132
133 /*!
133 /*!
134 \property QAbstractAxis::labelsAngle
134 \property QAbstractAxis::labelsAngle
135 The angle of the axis labels in degrees.
135 The angle of the axis labels in degrees.
136 */
136 */
137 /*!
137 /*!
138 \qmlproperty int Axis::labelsAngle
138 \qmlproperty int Axis::labelsAngle
139 The angle of the axis labels in degrees.
139 The angle of the axis labels in degrees.
140 */
140 */
141
141
142 /*!
142 /*!
143 \property QAbstractAxis::shadesVisible
143 \property QAbstractAxis::shadesVisible
144 The visibility of the axis shades.
144 The visibility of the axis shades.
145 */
145 */
146 /*!
146 /*!
147 \qmlproperty bool Axis::shadesVisible
147 \qmlproperty bool Axis::shadesVisible
148 The visibility of the axis shades.
148 The visibility of the axis shades.
149 */
149 */
150
150
151 /*!
151 /*!
152 \property QAbstractAxis::shadesColor
152 \property QAbstractAxis::shadesColor
153 The fill (brush) color of the axis shades.
153 The fill (brush) color of the axis shades.
154 */
154 */
155 /*!
155 /*!
156 \qmlproperty color Axis::shadesColor
156 \qmlproperty color Axis::shadesColor
157 The fill (brush) color of the axis shades.
157 The fill (brush) color of the axis shades.
158 */
158 */
159
159
160 /*!
160 /*!
161 \property QAbstractAxis::shadesBorderColor
161 \property QAbstractAxis::shadesBorderColor
162 The border (pen) color of the axis shades.
162 The border (pen) color of the axis shades.
163 */
163 */
164 /*!
164 /*!
165 \qmlproperty color Axis::shadesBorderColor
165 \qmlproperty color Axis::shadesBorderColor
166 The border (pen) color of the axis shades.
166 The border (pen) color of the axis shades.
167 */
167 */
168
168
169 /*!
169 /*!
170 \fn void QAbstractAxis::visibleChanged(bool)
170 \fn void QAbstractAxis::visibleChanged(bool)
171 Visiblity of the axis has changed to \a visible.
171 Visiblity of the axis has changed to \a visible.
172 */
172 */
173
173
174 /*!
174 /*!
175 \fn void QAbstractAxis::labelsVisibleChanged(bool)
175 \fn void QAbstractAxis::labelsVisibleChanged(bool)
176 Visiblity of the labels of the axis has changed to \a visible.
176 Visiblity of the labels of the axis has changed to \a visible.
177 */
177 */
178
178
179 /*!
179 /*!
180 \fn void QAbstractAxis::gridVisibleChanged(bool)
180 \fn void QAbstractAxis::gridVisibleChanged(bool)
181 Visiblity of the grid lines of the axis has changed to \a visible.
181 Visiblity of the grid lines of the axis has changed to \a visible.
182 */
182 */
183
183
184 /*
184 /*
185 \fn void QAbstractAxis::minChanged(qreal min)
185 \fn void QAbstractAxis::minChanged(qreal min)
186 Axis emits signal when \a min of axis has changed.
186 Axis emits signal when \a min of axis has changed.
187 */
187 */
188
188
189 /*
189 /*
190 \fn void QAbstractAxis::maxChanged(qreal max)
190 \fn void QAbstractAxis::maxChanged(qreal max)
191 Axis emits signal when \a max of axis has changed.
191 Axis emits signal when \a max of axis has changed.
192 */
192 */
193
193
194 /*
194 /*
195 \fn void QAbstractAxis::rangeChanged(qreal min, qreal max)
195 \fn void QAbstractAxis::rangeChanged(qreal min, qreal max)
196 Axis emits signal when \a min or \a max of axis has changed.
196 Axis emits signal when \a min or \a max of axis has changed.
197 */
197 */
198
198
199 /*
199 /*
200 \fn QChartAxisCategories* QAbstractAxis::categories()
200 \fn QChartAxisCategories* QAbstractAxis::categories()
201 Returns pointer to the list of categories which correspond to the values on the axis.
201 Returns pointer to the list of categories which correspond to the values on the axis.
202 */
202 */
203
203
204 /*!
204 /*!
205 \fn void QAbstractAxis::colorChanged(QColor)
205 \fn void QAbstractAxis::colorChanged(QColor)
206 Emitted if the \a color of the axis is changed.
206 Emitted if the \a color of the axis is changed.
207 */
207 */
208
208
209 /*!
209 /*!
210 \fn void QAbstractAxis::labelsColorChanged(QColor)
210 \fn void QAbstractAxis::labelsColorChanged(QColor)
211 Emitted if the \a color of the axis labels is changed.
211 Emitted if the \a color of the axis labels is changed.
212 */
212 */
213
213
214 /*!
214 /*!
215 \fn void QAbstractAxis::shadesVisibleChanged(bool)
215 \fn void QAbstractAxis::shadesVisibleChanged(bool)
216 Emitted if the visibility of the axis shades is changed to \a visible.
216 Emitted if the visibility of the axis shades is changed to \a visible.
217 */
217 */
218
218
219 /*!
219 /*!
220 \fn void QAbstractAxis::shadesColorChanged(QColor)
220 \fn void QAbstractAxis::shadesColorChanged(QColor)
221 Emitted if the \a color of the axis shades is changed.
221 Emitted if the \a color of the axis shades is changed.
222 */
222 */
223
223
224 /*!
224 /*!
225 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
225 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
226 Emitted if the border \a color of the axis shades is changed.
226 Emitted if the border \a color of the axis shades is changed.
227 */
227 */
228
228
229 /*!
229 /*!
230 Constructs new axis object which is a child of \a parent. Ownership is taken by
230 Constructs new axis object which is a child of \a parent. Ownership is taken by
231 QChart when axis added.
231 QChart when axis added.
232 */
232 */
233
233
234 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) :
234 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) :
235 QObject(parent),
235 QObject(parent),
236 d_ptr(&d)
236 d_ptr(&d)
237 {
237 {
238 }
238 }
239
239
240 /*!
240 /*!
241 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
241 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
242 */
242 */
243
243
244 QAbstractAxis::~QAbstractAxis()
244 QAbstractAxis::~QAbstractAxis()
245 {
245 {
246 }
246 }
247
247
248 /*!
248 /*!
249 Sets \a pen used to draw axis line and ticks.
249 Sets \a pen used to draw axis line and ticks.
250 */
250 */
251 void QAbstractAxis::setAxisPen(const QPen &pen)
251 void QAbstractAxis::setAxisPen(const QPen &pen)
252 {
252 {
253 if (d_ptr->m_axisPen!=pen) {
253 if (d_ptr->m_axisPen!=pen) {
254 d_ptr->m_axisPen = pen;
254 d_ptr->m_axisPen = pen;
255 emit d_ptr->updated();
255 emit d_ptr->updated();
256 }
256 }
257 }
257 }
258
258
259 /*!
259 /*!
260 Returns pen used to draw axis and ticks.
260 Returns pen used to draw axis and ticks.
261 */
261 */
262 QPen QAbstractAxis::axisPen() const
262 QPen QAbstractAxis::axisPen() const
263 {
263 {
264 return d_ptr->m_axisPen;
264 return d_ptr->m_axisPen;
265 }
265 }
266
266
267 void QAbstractAxis::setAxisPenColor(QColor color)
267 void QAbstractAxis::setAxisPenColor(QColor color)
268 {
268 {
269 QPen p = d_ptr->m_axisPen;
269 QPen p = d_ptr->m_axisPen;
270 if (p.color() != color) {
270 if (p.color() != color) {
271 p.setColor(color);
271 p.setColor(color);
272 setAxisPen(p);
272 setAxisPen(p);
273 emit colorChanged(color);
273 emit colorChanged(color);
274 }
274 }
275 }
275 }
276
276
277 QColor QAbstractAxis::axisPenColor() const
277 QColor QAbstractAxis::axisPenColor() const
278 {
278 {
279 return d_ptr->m_axisPen.color();
279 return d_ptr->m_axisPen.color();
280 }
280 }
281
281
282 /*!
282 /*!
283 Sets if axis and ticks are \a visible.
283 Sets if axis and ticks are \a visible.
284 */
284 */
285 void QAbstractAxis::setAxisVisible(bool visible)
285 void QAbstractAxis::setAxisVisible(bool visible)
286 {
286 {
287 if (d_ptr->m_axisVisible != visible) {
287 if (d_ptr->m_axisVisible != visible) {
288 d_ptr->m_axisVisible = visible;
288 d_ptr->m_axisVisible = visible;
289 emit d_ptr->updated();
289 emit d_ptr->updated();
290 emit visibleChanged(visible);
290 emit visibleChanged(visible);
291 }
291 }
292 }
292 }
293
293
294 bool QAbstractAxis::isAxisVisible() const
294 bool QAbstractAxis::isAxisVisible() const
295 {
295 {
296 return d_ptr->m_axisVisible;
296 return d_ptr->m_axisVisible;
297 }
297 }
298
298
299 void QAbstractAxis::setGridLineVisible(bool visible)
299 void QAbstractAxis::setGridLineVisible(bool visible)
300 {
300 {
301 if (d_ptr->m_gridLineVisible != visible) {
301 if (d_ptr->m_gridLineVisible != visible) {
302 d_ptr->m_gridLineVisible = visible;
302 d_ptr->m_gridLineVisible = visible;
303 emit d_ptr->updated();
303 emit d_ptr->updated();
304 emit gridVisibleChanged(visible);
304 emit gridVisibleChanged(visible);
305 }
305 }
306 }
306 }
307
307
308 bool QAbstractAxis::isGridLineVisible() const
308 bool QAbstractAxis::isGridLineVisible() const
309 {
309 {
310 return d_ptr->m_gridLineVisible;
310 return d_ptr->m_gridLineVisible;
311 }
311 }
312
312
313 /*!
313 /*!
314 Sets \a pen used to draw grid line.
314 Sets \a pen used to draw grid line.
315 */
315 */
316 void QAbstractAxis::setGridLinePen(const QPen &pen)
316 void QAbstractAxis::setGridLinePen(const QPen &pen)
317 {
317 {
318 if (d_ptr->m_gridLinePen != pen) {
318 if (d_ptr->m_gridLinePen != pen) {
319 d_ptr->m_gridLinePen = pen;
319 d_ptr->m_gridLinePen = pen;
320 emit d_ptr->updated();
320 emit d_ptr->updated();
321 }
321 }
322 }
322 }
323
323
324 /*!
324 /*!
325 Returns pen used to draw grid.
325 Returns pen used to draw grid.
326 */
326 */
327 QPen QAbstractAxis::gridLinePen() const
327 QPen QAbstractAxis::gridLinePen() const
328 {
328 {
329 return d_ptr->m_gridLinePen;
329 return d_ptr->m_gridLinePen;
330 }
330 }
331
331
332 void QAbstractAxis::setLabelsVisible(bool visible)
332 void QAbstractAxis::setLabelsVisible(bool visible)
333 {
333 {
334 if (d_ptr->m_labelsVisible != visible) {
334 if (d_ptr->m_labelsVisible != visible) {
335 d_ptr->m_labelsVisible = visible;
335 d_ptr->m_labelsVisible = visible;
336 emit d_ptr->updated();
336 emit d_ptr->updated();
337 emit labelsVisibleChanged(visible);
337 emit labelsVisibleChanged(visible);
338 }
338 }
339 }
339 }
340
340
341 bool QAbstractAxis::labelsVisible() const
341 bool QAbstractAxis::labelsVisible() const
342 {
342 {
343 return d_ptr->m_labelsVisible;
343 return d_ptr->m_labelsVisible;
344 }
344 }
345
345
346 /*!
346 /*!
347 Sets \a pen used to draw labels.
347 Sets \a pen used to draw labels.
348 */
348 */
349 void QAbstractAxis::setLabelsPen(const QPen &pen)
349 void QAbstractAxis::setLabelsPen(const QPen &pen)
350 {
350 {
351 if (d_ptr->m_labelsPen != pen) {
351 if (d_ptr->m_labelsPen != pen) {
352 d_ptr->m_labelsPen = pen;
352 d_ptr->m_labelsPen = pen;
353 emit d_ptr->updated();
353 emit d_ptr->updated();
354 }
354 }
355 }
355 }
356
356
357 /*!
357 /*!
358 Returns the pen used to labels.
358 Returns the pen used to labels.
359 */
359 */
360 QPen QAbstractAxis::labelsPen() const
360 QPen QAbstractAxis::labelsPen() const
361 {
361 {
362 return d_ptr->m_labelsPen;
362 return d_ptr->m_labelsPen;
363 }
363 }
364
364
365 /*!
365 /*!
366 Sets \a brush used to draw labels.
366 Sets \a brush used to draw labels.
367 */
367 */
368 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
368 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
369 {
369 {
370 if (d_ptr->m_labelsBrush != brush) {
370 if (d_ptr->m_labelsBrush != brush) {
371 d_ptr->m_labelsBrush = brush;
371 d_ptr->m_labelsBrush = brush;
372 emit d_ptr->updated();
372 emit d_ptr->updated();
373 }
373 }
374 }
374 }
375
375
376 /*!
376 /*!
377 Returns brush used to draw labels.
377 Returns brush used to draw labels.
378 */
378 */
379 QBrush QAbstractAxis::labelsBrush() const
379 QBrush QAbstractAxis::labelsBrush() const
380 {
380 {
381 return d_ptr->m_labelsBrush;
381 return d_ptr->m_labelsBrush;
382 }
382 }
383
383
384 /*!
384 /*!
385 Sets \a font used to draw labels.
385 Sets \a font used to draw labels.
386 */
386 */
387 void QAbstractAxis::setLabelsFont(const QFont &font)
387 void QAbstractAxis::setLabelsFont(const QFont &font)
388 {
388 {
389 if (d_ptr->m_labelsFont != font) {
389 if (d_ptr->m_labelsFont != font) {
390 d_ptr->m_labelsFont = font;
390 d_ptr->m_labelsFont = font;
391 emit d_ptr->updated();
391 emit d_ptr->updated();
392 }
392 }
393 }
393 }
394
394
395 /*!
395 /*!
396 Returns font used to draw labels.
396 Returns font used to draw labels.
397 */
397 */
398 QFont QAbstractAxis::labelsFont() const
398 QFont QAbstractAxis::labelsFont() const
399 {
399 {
400 return d_ptr->m_labelsFont;
400 return d_ptr->m_labelsFont;
401 }
401 }
402
402
403 void QAbstractAxis::setLabelsAngle(int angle)
403 void QAbstractAxis::setLabelsAngle(int angle)
404 {
404 {
405 if (d_ptr->m_labelsAngle != angle) {
405 if (d_ptr->m_labelsAngle != angle) {
406 d_ptr->m_labelsAngle = angle;
406 d_ptr->m_labelsAngle = angle;
407 emit d_ptr->updated();
407 emit d_ptr->updated();
408 }
408 }
409 }
409 }
410
410
411 int QAbstractAxis::labelsAngle() const
411 int QAbstractAxis::labelsAngle() const
412 {
412 {
413 return d_ptr->m_labelsAngle;
413 return d_ptr->m_labelsAngle;
414 }
414 }
415
415
416 void QAbstractAxis::setLabelsColor(QColor color)
416 void QAbstractAxis::setLabelsColor(QColor color)
417 {
417 {
418 QBrush b = d_ptr->m_labelsBrush;
418 QBrush b = d_ptr->m_labelsBrush;
419 if (b.color() != color) {
419 if (b.color() != color) {
420 b.setColor(color);
420 b.setColor(color);
421 setLabelsBrush(b);
421 setLabelsBrush(b);
422 emit labelsColorChanged(color);
422 emit labelsColorChanged(color);
423 }
423 }
424 }
424 }
425
425
426 QColor QAbstractAxis::labelsColor() const
426 QColor QAbstractAxis::labelsColor() const
427 {
427 {
428 return d_ptr->m_labelsBrush.color();
428 return d_ptr->m_labelsBrush.color();
429 }
429 }
430
430
431 void QAbstractAxis::setShadesVisible(bool visible)
431 void QAbstractAxis::setShadesVisible(bool visible)
432 {
432 {
433 if (d_ptr->m_shadesVisible != visible) {
433 if (d_ptr->m_shadesVisible != visible) {
434 d_ptr->m_shadesVisible = visible;
434 d_ptr->m_shadesVisible = visible;
435 emit d_ptr->updated();
435 emit d_ptr->updated();
436 emit shadesVisibleChanged(visible);
436 emit shadesVisibleChanged(visible);
437 }
437 }
438 }
438 }
439
439
440 bool QAbstractAxis::shadesVisible() const
440 bool QAbstractAxis::shadesVisible() const
441 {
441 {
442 return d_ptr->m_shadesVisible;
442 return d_ptr->m_shadesVisible;
443 }
443 }
444
444
445 /*!
445 /*!
446 Sets \a pen used to draw shades.
446 Sets \a pen used to draw shades.
447 */
447 */
448 void QAbstractAxis::setShadesPen(const QPen &pen)
448 void QAbstractAxis::setShadesPen(const QPen &pen)
449 {
449 {
450 if (d_ptr->m_shadesPen != pen) {
450 if (d_ptr->m_shadesPen != pen) {
451 d_ptr->m_shadesPen = pen;
451 d_ptr->m_shadesPen = pen;
452 emit d_ptr->updated();
452 emit d_ptr->updated();
453 }
453 }
454 }
454 }
455
455
456 /*!
456 /*!
457 Returns pen used to draw shades.
457 Returns pen used to draw shades.
458 */
458 */
459 QPen QAbstractAxis::shadesPen() const
459 QPen QAbstractAxis::shadesPen() const
460 {
460 {
461 return d_ptr->m_shadesPen;
461 return d_ptr->m_shadesPen;
462 }
462 }
463
463
464 /*!
464 /*!
465 Sets \a brush used to draw shades.
465 Sets \a brush used to draw shades.
466 */
466 */
467 void QAbstractAxis::setShadesBrush(const QBrush &brush)
467 void QAbstractAxis::setShadesBrush(const QBrush &brush)
468 {
468 {
469 if (d_ptr->m_shadesBrush != brush) {
469 if (d_ptr->m_shadesBrush != brush) {
470 d_ptr->m_shadesBrush = brush;
470 d_ptr->m_shadesBrush = brush;
471 emit d_ptr->updated();
471 emit d_ptr->updated();
472 emit shadesColorChanged(brush.color());
472 emit shadesColorChanged(brush.color());
473 }
473 }
474 }
474 }
475
475
476 /*!
476 /*!
477 Returns brush used to draw shades.
477 Returns brush used to draw shades.
478 */
478 */
479 QBrush QAbstractAxis::shadesBrush() const
479 QBrush QAbstractAxis::shadesBrush() const
480 {
480 {
481 return d_ptr->m_shadesBrush;
481 return d_ptr->m_shadesBrush;
482 }
482 }
483
483
484 void QAbstractAxis::setShadesColor(QColor color)
484 void QAbstractAxis::setShadesColor(QColor color)
485 {
485 {
486 QBrush b = d_ptr->m_shadesBrush;
486 QBrush b = d_ptr->m_shadesBrush;
487 b.setColor(color);
487 b.setColor(color);
488 setShadesBrush(b);
488 setShadesBrush(b);
489 }
489 }
490
490
491 QColor QAbstractAxis::shadesColor() const
491 QColor QAbstractAxis::shadesColor() const
492 {
492 {
493 return d_ptr->m_shadesBrush.color();
493 return d_ptr->m_shadesBrush.color();
494 }
494 }
495
495
496 void QAbstractAxis::setShadesBorderColor(QColor color)
496 void QAbstractAxis::setShadesBorderColor(QColor color)
497 {
497 {
498 QPen p = d_ptr->m_shadesPen;
498 QPen p = d_ptr->m_shadesPen;
499 p.setColor(color);
499 p.setColor(color);
500 setShadesPen(p);
500 setShadesPen(p);
501 }
501 }
502
502
503 QColor QAbstractAxis::shadesBorderColor() const
503 QColor QAbstractAxis::shadesBorderColor() const
504 {
504 {
505 return d_ptr->m_shadesPen.color();
505 return d_ptr->m_shadesPen.color();
506 }
506 }
507
507
508
508
509 bool QAbstractAxis::isVisible() const
509 bool QAbstractAxis::isVisible() const
510 {
510 {
511 return d_ptr->m_visible;
511 return d_ptr->m_visible;
512 }
512 }
513
513
514 /*!
514 /*!
515 Sets axis, shades, labels and grid lines to be visible.
515 Sets axis, shades, labels and grid lines to be visible.
516 */
516 */
517 void QAbstractAxis::setVisible(bool visible)
517 void QAbstractAxis::setVisible(bool visible)
518 {
518 {
519 d_ptr->m_visible=visible;
519 if(d_ptr->m_visible!=visible){
520 emit d_ptr->updated();
520 d_ptr->m_visible=visible;
521 emit visibleChanged(visible);
522 emit d_ptr->updated();
523 }
521 }
524 }
522
525
523
526
524 /*!
527 /*!
525 Sets axis, shades, labels and grid lines to be visible.
528 Sets axis, shades, labels and grid lines to be visible.
526 */
529 */
527 void QAbstractAxis::show()
530 void QAbstractAxis::show()
528 {
531 {
529 setVisible(true);
532 setVisible(true);
530 }
533 }
531
534
532 /*!
535 /*!
533 Sets axis, shades, labels and grid lines to not be visible.
536 Sets axis, shades, labels and grid lines to not be visible.
534 */
537 */
535 void QAbstractAxis::hide()
538 void QAbstractAxis::hide()
536 {
539 {
537 setVisible(false);
540 setVisible(false);
538 }
541 }
539
542
540
543
541 void QAbstractAxis::setMin(const QVariant &min)
544 void QAbstractAxis::setMin(const QVariant &min)
542 {
545 {
543 d_ptr->setMin(min);
546 d_ptr->setMin(min);
544 }
547 }
545 void QAbstractAxis::setMax(const QVariant &max)
548 void QAbstractAxis::setMax(const QVariant &max)
546 {
549 {
547 d_ptr->setMax(max);
550 d_ptr->setMax(max);
548 }
551 }
549 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
552 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
550 {
553 {
551 d_ptr->setRange(min,max);
554 d_ptr->setRange(min,max);
552 }
555 }
553
556
554 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
557 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
555
558
556 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
559 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
557 q_ptr(q),
560 q_ptr(q),
558 m_visible(true),
561 m_visible(false),
559 m_axisVisible(true),
562 m_axisVisible(true),
560 m_gridLineVisible(true),
563 m_gridLineVisible(true),
561 m_labelsVisible(true),
564 m_labelsVisible(true),
562 m_labelsAngle(0),
565 m_labelsAngle(0),
563 m_shadesVisible(false),
566 m_shadesVisible(false),
564 m_shadesBrush(Qt::SolidPattern),
567 m_shadesBrush(Qt::SolidPattern),
565 m_shadesOpacity(1.0),
568 m_shadesOpacity(1.0),
566 m_orientation(Qt::Orientation(0)),
569 m_orientation(Qt::Orientation(0)),
567 m_min(0),
570 m_min(0),
568 m_max(0),
571 m_max(0),
569 m_ticksCount(5)
572 m_ticksCount(5)
570 {
573 {
571
574
572 }
575 }
573
576
574 QAbstractAxisPrivate::~QAbstractAxisPrivate()
577 QAbstractAxisPrivate::~QAbstractAxisPrivate()
575 {
578 {
576
579
577 }
580 }
578
581
579 #include "moc_qabstractaxis.cpp"
582 #include "moc_qabstractaxis.cpp"
580 #include "moc_qabstractaxis_p.cpp"
583 #include "moc_qabstractaxis_p.cpp"
581
584
582 QTCOMMERCIALCHART_END_NAMESPACE
585 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,420 +1,446
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 #include "chartpresenter_p.h"
20 #include "chartpresenter_p.h"
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qabstractaxis.h"
23 #include "qabstractaxis.h"
24 #include "qabstractaxis_p.h"
24 #include "qabstractaxis_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include "chartanimation_p.h"
28 #include "chartanimation_p.h"
29 #include "qabstractseries_p.h"
29 #include "qabstractseries_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "chartaxis_p.h"
31 #include "chartaxis_p.h"
32 //#include "chartaxisx_p.h"
32 //#include "chartaxisx_p.h"
33 //#include "chartaxisy_p.h"
33 //#include "chartaxisy_p.h"
34 #include "areachartitem_p.h"
34 #include "areachartitem_p.h"
35 #include "chartbackground_p.h"
35 #include "chartbackground_p.h"
36 #include "chartlayout_p.h"
36 #include "chartlayout_p.h"
37 #include <QTimer>
37 #include <QTimer>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
41 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
42 m_chart(chart),
42 m_chart(chart),
43 m_animator(0),
43 m_animator(0),
44 m_dataset(dataset),
44 m_dataset(dataset),
45 m_chartTheme(0),
45 m_chartTheme(0),
46 m_options(QChart::NoAnimation),
46 m_options(QChart::NoAnimation),
47 m_state(ShowState),
47 m_state(ShowState),
48 m_layout(new ChartLayout(this)),
48 m_layout(new ChartLayout(this)),
49 m_backgroundItem(0),
49 m_backgroundItem(0),
50 m_titleItem(0)
50 m_titleItem(0)
51 {
51 {
52
52
53 }
53 }
54
54
55 ChartPresenter::~ChartPresenter()
55 ChartPresenter::~ChartPresenter()
56 {
56 {
57 delete m_chartTheme;
57 delete m_chartTheme;
58 }
58 }
59
59
60 void ChartPresenter::setGeometry(const QRectF& rect)
60 void ChartPresenter::setGeometry(const QRectF& rect)
61 {
61 {
62
62
63 Q_ASSERT(rect.isValid());
63 Q_ASSERT(rect.isValid());
64
64
65 if(m_rect!=rect) {
65 if(m_rect!=rect) {
66 m_rect=rect;
66 m_rect=rect;
67 emit geometryChanged(m_rect);
67 emit geometryChanged(m_rect);
68 }
68 }
69 }
69 }
70
70
71 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
71 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
72 {
72 {
73 ChartAxis* item = axis->d_ptr->createGraphics(this);
73 ChartAxis* item = axis->d_ptr->createGraphics(this);
74 item->setDomain(domain);
74 item->setDomain(domain);
75
75
76 if(m_options.testFlag(QChart::GridAxisAnimations)){
76 if(m_options.testFlag(QChart::GridAxisAnimations)){
77 item->setAnimator(m_animator);
77 item->setAnimator(m_animator);
78 item->setAnimation(new AxisAnimation(item));
78 item->setAnimation(new AxisAnimation(item));
79 }
79 }
80
80
81 if(item->axisType()==ChartAxis::X_AXIS){
81 if(item->axisType()==ChartAxis::X_AXIS){
82 m_chartTheme->decorate(axis,true);
82 m_chartTheme->decorate(axis,true);
83 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
83 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
84 //initialize
84 //initialize
85 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
85 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
86
86
87 }
87 }
88 else{
88 else{
89 m_chartTheme->decorate(axis,false);
89 m_chartTheme->decorate(axis,false);
90 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
90 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
91 //initialize
91 //initialize
92 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
92 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
93 }
93 }
94
94
95 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
95 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
96 QObject::connect(axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleAxisVisibleChanged(bool)));
96 //initialize
97 //initialize
97 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
98 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
99 //reload visiblity
98 m_axisItems.insert(axis, item);
100 m_axisItems.insert(axis, item);
101 if(axis->isVisible()) axis->hide();
102 axis->show();
103
99 }
104 }
100
105
101 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
106 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
102 {
107 {
103 ChartAxis* item = m_axisItems.take(axis);
108 ChartAxis* item = m_axisItems.take(axis);
104 Q_ASSERT(item);
109 Q_ASSERT(item);
105 if(m_animator) m_animator->removeAnimation(item);
110 if(m_animator) m_animator->removeAnimation(item);
106 item->deleteLater();
111 item->deleteLater();
107 }
112 }
108
113
109
114
110 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
115 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
111 {
116 {
112 Chart *item = series->d_ptr->createGraphics(this);
117 Chart *item = series->d_ptr->createGraphics(this);
113 Q_ASSERT(item);
118 Q_ASSERT(item);
114 item->setDomain(domain);
119 item->setDomain(domain);
115
120
116 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
121 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
117 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
122 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
118 //initialize
123 //initialize
119 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
124 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
120 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
125 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
121 m_chartItems.insert(series,item);
126 m_chartItems.insert(series,item);
122 }
127 }
123
128
124 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
129 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
125 {
130 {
126 Chart* item = m_chartItems.take(series);
131 Chart* item = m_chartItems.take(series);
127 Q_ASSERT(item);
132 Q_ASSERT(item);
128 if(m_animator) {
133 if(m_animator) {
129 //small hack to handle area animations
134 //small hack to handle area animations
130 if(series->type() == QAbstractSeries::SeriesTypeArea){
135 if(series->type() == QAbstractSeries::SeriesTypeArea){
131 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
136 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
132 AreaChartItem* area = static_cast<AreaChartItem*>(item);
137 AreaChartItem* area = static_cast<AreaChartItem*>(item);
133 m_animator->removeAnimation(area->upperLineItem());
138 m_animator->removeAnimation(area->upperLineItem());
134 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
139 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
135 }else
140 }else
136 m_animator->removeAnimation(item);
141 m_animator->removeAnimation(item);
137 }
142 }
138 item->deleteLater();
143 item->deleteLater();
139 }
144 }
140
145
146
147 void ChartPresenter::handleAxisVisibleChanged(bool visible)
148 {
149 QAbstractAxis* axis = static_cast<QAbstractAxis*> (sender());
150 Q_ASSERT(axis);
151 if(visible){
152
153 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
154
155 while (i.hasNext()) {
156 i.next();
157 if(i.key()==axis) {
158 continue;
159 }
160 if(i.key()->d_ptr->m_orientation==axis->d_ptr->m_orientation) {
161 i.key()->setVisible(false);
162 }
163 }
164 }
165 }
166
141 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
167 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
142 {
168 {
143 if(m_chartTheme && m_chartTheme->id() == theme) return;
169 if(m_chartTheme && m_chartTheme->id() == theme) return;
144 delete m_chartTheme;
170 delete m_chartTheme;
145 m_chartTheme = ChartTheme::createTheme(theme);
171 m_chartTheme = ChartTheme::createTheme(theme);
146 m_chartTheme->setForced(force);
172 m_chartTheme->setForced(force);
147 m_chartTheme->decorate(m_chart);
173 m_chartTheme->decorate(m_chart);
148 m_chartTheme->decorate(m_chart->legend());
174 m_chartTheme->decorate(m_chart->legend());
149 resetAllElements();
175 resetAllElements();
150
176
151 // We do not want "force" to stay on.
177 // We do not want "force" to stay on.
152 // Bar/pie are calling decorate when adding/removing slices/bars which means
178 // Bar/pie are calling decorate when adding/removing slices/bars which means
153 // that to preserve users colors "force" must not be on.
179 // that to preserve users colors "force" must not be on.
154 m_chartTheme->setForced(false);
180 m_chartTheme->setForced(false);
155 }
181 }
156
182
157 QChart::ChartTheme ChartPresenter::theme()
183 QChart::ChartTheme ChartPresenter::theme()
158 {
184 {
159 return m_chartTheme->id();
185 return m_chartTheme->id();
160 }
186 }
161
187
162 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
188 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
163 {
189 {
164 if(m_options!=options) {
190 if(m_options!=options) {
165
191
166 m_options=options;
192 m_options=options;
167
193
168 if(m_options!=QChart::NoAnimation && !m_animator) {
194 if(m_options!=QChart::NoAnimation && !m_animator) {
169 m_animator= new ChartAnimator(this);
195 m_animator= new ChartAnimator(this);
170 }
196 }
171 resetAllElements();
197 resetAllElements();
172 }
198 }
173
199
174 }
200 }
175
201
176 void ChartPresenter::resetAllElements()
202 void ChartPresenter::resetAllElements()
177 {
203 {
178 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
204 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
179 while (i.hasNext()) {
205 while (i.hasNext()) {
180 i.next();
206 i.next();
181 Domain* domain = i.value()->domain();
207 Domain* domain = i.value()->domain();
182 QAbstractAxis* axis = i.key();
208 QAbstractAxis* axis = i.key();
183 handleAxisRemoved(axis);
209 handleAxisRemoved(axis);
184 handleAxisAdded(axis,domain);
210 handleAxisAdded(axis,domain);
185 }
211 }
186
212
187 QMapIterator<QAbstractSeries*, Chart*> j(m_chartItems);
213 QMapIterator<QAbstractSeries*, Chart*> j(m_chartItems);
188 while (j.hasNext()) {
214 while (j.hasNext()) {
189 j.next();
215 j.next();
190 Domain* domain = j.value()->domain();
216 Domain* domain = j.value()->domain();
191 QAbstractSeries* series = j.key();
217 QAbstractSeries* series = j.key();
192 handleSeriesRemoved(series);
218 handleSeriesRemoved(series);
193 handleSeriesAdded(series,domain);
219 handleSeriesAdded(series,domain);
194 }
220 }
195 }
221 }
196
222
197 void ChartPresenter::zoomIn(qreal factor)
223 void ChartPresenter::zoomIn(qreal factor)
198 {
224 {
199 QRectF rect = geometry();
225 QRectF rect = geometry();
200 rect.setWidth(rect.width()/factor);
226 rect.setWidth(rect.width()/factor);
201 rect.setHeight(rect.height()/factor);
227 rect.setHeight(rect.height()/factor);
202 rect.moveCenter(geometry().center());
228 rect.moveCenter(geometry().center());
203 zoomIn(rect);
229 zoomIn(rect);
204 }
230 }
205
231
206 void ChartPresenter::zoomIn(const QRectF& rect)
232 void ChartPresenter::zoomIn(const QRectF& rect)
207 {
233 {
208 QRectF r = rect.normalized();
234 QRectF r = rect.normalized();
209 r.translate(-geometry().topLeft());
235 r.translate(-geometry().topLeft());
210 if (!r.isValid())
236 if (!r.isValid())
211 return;
237 return;
212
238
213 m_state = ZoomInState;
239 m_state = ZoomInState;
214 m_statePoint = QPointF(r.center().x()/geometry().width(),r.center().y()/geometry().height());
240 m_statePoint = QPointF(r.center().x()/geometry().width(),r.center().y()/geometry().height());
215 m_dataset->zoomInDomain(r,geometry().size());
241 m_dataset->zoomInDomain(r,geometry().size());
216 m_state = ShowState;
242 m_state = ShowState;
217 }
243 }
218
244
219 void ChartPresenter::zoomOut(qreal factor)
245 void ChartPresenter::zoomOut(qreal factor)
220 {
246 {
221 m_state = ZoomOutState;
247 m_state = ZoomOutState;
222
248
223 QRectF chartRect;
249 QRectF chartRect;
224 chartRect.setSize(geometry().size());
250 chartRect.setSize(geometry().size());
225
251
226 QRectF rect;
252 QRectF rect;
227 rect.setSize(chartRect.size()/factor);
253 rect.setSize(chartRect.size()/factor);
228 rect.moveCenter(chartRect.center());
254 rect.moveCenter(chartRect.center());
229 if (!rect.isValid())
255 if (!rect.isValid())
230 return;
256 return;
231 m_statePoint = QPointF(rect.center().x()/geometry().width(),rect.center().y()/geometry().height());
257 m_statePoint = QPointF(rect.center().x()/geometry().width(),rect.center().y()/geometry().height());
232 m_dataset->zoomOutDomain(rect, chartRect.size());
258 m_dataset->zoomOutDomain(rect, chartRect.size());
233 m_state = ShowState;
259 m_state = ShowState;
234 }
260 }
235
261
236 void ChartPresenter::scroll(qreal dx,qreal dy)
262 void ChartPresenter::scroll(qreal dx,qreal dy)
237 {
263 {
238 if(dx<0) m_state=ScrollLeftState;
264 if(dx<0) m_state=ScrollLeftState;
239 if(dx>0) m_state=ScrollRightState;
265 if(dx>0) m_state=ScrollRightState;
240 if(dy<0) m_state=ScrollUpState;
266 if(dy<0) m_state=ScrollUpState;
241 if(dy>0) m_state=ScrollDownState;
267 if(dy>0) m_state=ScrollDownState;
242
268
243 m_dataset->scrollDomain(dx,dy,geometry().size());
269 m_dataset->scrollDomain(dx,dy,geometry().size());
244 m_state = ShowState;
270 m_state = ShowState;
245 }
271 }
246
272
247 QChart::AnimationOptions ChartPresenter::animationOptions() const
273 QChart::AnimationOptions ChartPresenter::animationOptions() const
248 {
274 {
249 return m_options;
275 return m_options;
250 }
276 }
251
277
252 void ChartPresenter::createBackgroundItem()
278 void ChartPresenter::createBackgroundItem()
253 {
279 {
254 if (!m_backgroundItem) {
280 if (!m_backgroundItem) {
255 m_backgroundItem = new ChartBackground(rootItem());
281 m_backgroundItem = new ChartBackground(rootItem());
256 m_backgroundItem->setPen(Qt::NoPen);
282 m_backgroundItem->setPen(Qt::NoPen);
257 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
283 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
258 }
284 }
259 }
285 }
260
286
261 void ChartPresenter::createTitleItem()
287 void ChartPresenter::createTitleItem()
262 {
288 {
263 if (!m_titleItem) {
289 if (!m_titleItem) {
264 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
290 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
265 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
291 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
266 }
292 }
267 }
293 }
268
294
269
295
270 void ChartPresenter::handleAnimationFinished()
296 void ChartPresenter::handleAnimationFinished()
271 {
297 {
272 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
298 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
273 if(m_animations.empty()) emit animationsFinished();
299 if(m_animations.empty()) emit animationsFinished();
274 }
300 }
275
301
276 void ChartPresenter::startAnimation(ChartAnimation* animation)
302 void ChartPresenter::startAnimation(ChartAnimation* animation)
277 {
303 {
278 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
304 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
279 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
305 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
280 if(!m_animations.isEmpty()){
306 if(!m_animations.isEmpty()){
281 m_animations.append(animation);
307 m_animations.append(animation);
282 }
308 }
283 QTimer::singleShot(0, animation, SLOT(start()));
309 QTimer::singleShot(0, animation, SLOT(start()));
284 }
310 }
285
311
286 QGraphicsRectItem* ChartPresenter::backgroundItem()
312 QGraphicsRectItem* ChartPresenter::backgroundItem()
287 {
313 {
288 return m_backgroundItem;
314 return m_backgroundItem;
289 }
315 }
290
316
291 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
317 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
292 {
318 {
293 createBackgroundItem();
319 createBackgroundItem();
294 m_backgroundItem->setBrush(brush);
320 m_backgroundItem->setBrush(brush);
295 m_layout->invalidate();
321 m_layout->invalidate();
296 }
322 }
297
323
298 QBrush ChartPresenter::backgroundBrush() const
324 QBrush ChartPresenter::backgroundBrush() const
299 {
325 {
300 if (!m_backgroundItem) return QBrush();
326 if (!m_backgroundItem) return QBrush();
301 return m_backgroundItem->brush();
327 return m_backgroundItem->brush();
302 }
328 }
303
329
304 void ChartPresenter::setBackgroundPen(const QPen& pen)
330 void ChartPresenter::setBackgroundPen(const QPen& pen)
305 {
331 {
306 createBackgroundItem();
332 createBackgroundItem();
307 m_backgroundItem->setPen(pen);
333 m_backgroundItem->setPen(pen);
308 m_layout->invalidate();
334 m_layout->invalidate();
309 }
335 }
310
336
311 QPen ChartPresenter::backgroundPen() const
337 QPen ChartPresenter::backgroundPen() const
312 {
338 {
313 if (!m_backgroundItem) return QPen();
339 if (!m_backgroundItem) return QPen();
314 return m_backgroundItem->pen();
340 return m_backgroundItem->pen();
315 }
341 }
316
342
317 QGraphicsItem* ChartPresenter::titleItem()
343 QGraphicsItem* ChartPresenter::titleItem()
318 {
344 {
319 return m_titleItem;
345 return m_titleItem;
320 }
346 }
321
347
322 void ChartPresenter::setTitle(const QString& title)
348 void ChartPresenter::setTitle(const QString& title)
323 {
349 {
324 createTitleItem();
350 createTitleItem();
325 m_titleItem->setText(title);
351 m_titleItem->setText(title);
326 m_layout->invalidate();
352 m_layout->invalidate();
327 }
353 }
328
354
329 QString ChartPresenter::title() const
355 QString ChartPresenter::title() const
330 {
356 {
331 if (!m_titleItem) return QString();
357 if (!m_titleItem) return QString();
332 return m_titleItem->text();
358 return m_titleItem->text();
333 }
359 }
334
360
335 void ChartPresenter::setTitleFont(const QFont& font)
361 void ChartPresenter::setTitleFont(const QFont& font)
336 {
362 {
337 createTitleItem();
363 createTitleItem();
338 m_titleItem->setFont(font);
364 m_titleItem->setFont(font);
339 m_layout->invalidate();
365 m_layout->invalidate();
340 }
366 }
341
367
342 QFont ChartPresenter::titleFont() const
368 QFont ChartPresenter::titleFont() const
343 {
369 {
344 if (!m_titleItem) return QFont();
370 if (!m_titleItem) return QFont();
345 return m_titleItem->font();
371 return m_titleItem->font();
346 }
372 }
347
373
348 void ChartPresenter::setTitleBrush(const QBrush &brush)
374 void ChartPresenter::setTitleBrush(const QBrush &brush)
349 {
375 {
350 createTitleItem();
376 createTitleItem();
351 m_titleItem->setBrush(brush);
377 m_titleItem->setBrush(brush);
352 m_layout->invalidate();
378 m_layout->invalidate();
353 }
379 }
354
380
355 QBrush ChartPresenter::titleBrush() const
381 QBrush ChartPresenter::titleBrush() const
356 {
382 {
357 if (!m_titleItem) return QBrush();
383 if (!m_titleItem) return QBrush();
358 return m_titleItem->brush();
384 return m_titleItem->brush();
359 }
385 }
360
386
361 void ChartPresenter::setBackgroundVisible(bool visible)
387 void ChartPresenter::setBackgroundVisible(bool visible)
362 {
388 {
363 createBackgroundItem();
389 createBackgroundItem();
364 m_backgroundItem->setVisible(visible);
390 m_backgroundItem->setVisible(visible);
365 }
391 }
366
392
367
393
368 bool ChartPresenter::isBackgroundVisible() const
394 bool ChartPresenter::isBackgroundVisible() const
369 {
395 {
370 if (!m_backgroundItem) return false;
396 if (!m_backgroundItem) return false;
371 return m_backgroundItem->isVisible();
397 return m_backgroundItem->isVisible();
372 }
398 }
373
399
374 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
400 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
375 {
401 {
376 createBackgroundItem();
402 createBackgroundItem();
377 m_backgroundItem->setDropShadowEnabled(enabled);
403 m_backgroundItem->setDropShadowEnabled(enabled);
378 }
404 }
379
405
380 bool ChartPresenter::isBackgroundDropShadowEnabled() const
406 bool ChartPresenter::isBackgroundDropShadowEnabled() const
381 {
407 {
382 if (!m_backgroundItem) return false;
408 if (!m_backgroundItem) return false;
383 return m_backgroundItem->isDropShadowEnabled();
409 return m_backgroundItem->isDropShadowEnabled();
384 }
410 }
385
411
386
412
387 QGraphicsLayout* ChartPresenter::layout()
413 QGraphicsLayout* ChartPresenter::layout()
388 {
414 {
389 return m_layout;
415 return m_layout;
390 }
416 }
391
417
392 void ChartPresenter::setMarginsMinimum(const QRectF& margins)
418 void ChartPresenter::setMarginsMinimum(const QRectF& margins)
393 {
419 {
394 Q_UNUSED(margins);
420 Q_UNUSED(margins);
395 // m_layout->setMarginsMinimum(margins);
421 // m_layout->setMarginsMinimum(margins);
396 }
422 }
397
423
398 QRectF ChartPresenter::margins() const
424 QRectF ChartPresenter::margins() const
399 {
425 {
400 return QRectF();//m_layout->margins();
426 return QRectF();//m_layout->margins();
401 }
427 }
402
428
403 QLegend* ChartPresenter::legend()
429 QLegend* ChartPresenter::legend()
404 {
430 {
405 return m_chart->legend();
431 return m_chart->legend();
406 }
432 }
407
433
408 QList<ChartAxis*> ChartPresenter::axisItems() const
434 QList<ChartAxis*> ChartPresenter::axisItems() const
409 {
435 {
410 return m_axisItems.values();
436 return m_axisItems.values();
411 }
437 }
412
438
413 void ChartPresenter::setVisible(bool visible)
439 void ChartPresenter::setVisible(bool visible)
414 {
440 {
415 m_chart->setVisible(visible);
441 m_chart->setVisible(visible);
416 }
442 }
417
443
418 #include "moc_chartpresenter_p.cpp"
444 #include "moc_chartpresenter_p.cpp"
419
445
420 QTCOMMERCIALCHART_END_NAMESPACE
446 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,174 +1,175
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTPRESENTER_H
30 #ifndef CHARTPRESENTER_H
31 #define CHARTPRESENTER_H
31 #define CHARTPRESENTER_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
34 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
35 #include <QRectF>
35 #include <QRectF>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class Chart;
39 class Chart;
40 class QAbstractSeries;
40 class QAbstractSeries;
41 class ChartDataSet;
41 class ChartDataSet;
42 class Domain;
42 class Domain;
43 class ChartAxis;
43 class ChartAxis;
44 class ChartTheme;
44 class ChartTheme;
45 class ChartAnimator;
45 class ChartAnimator;
46 class ChartBackground;
46 class ChartBackground;
47 class ChartAnimation;
47 class ChartAnimation;
48 class ChartLayout;
48 class ChartLayout;
49
49
50 class ChartPresenter: public QObject
50 class ChartPresenter: public QObject
51 {
51 {
52 Q_OBJECT
52 Q_OBJECT
53 public:
53 public:
54 enum ZValues {
54 enum ZValues {
55 BackgroundZValue = -1,
55 BackgroundZValue = -1,
56 ShadesZValue,
56 ShadesZValue,
57 GridZValue,
57 GridZValue,
58 SeriesZValue,
58 SeriesZValue,
59 LineChartZValue = SeriesZValue,
59 LineChartZValue = SeriesZValue,
60 BarSeriesZValue = SeriesZValue,
60 BarSeriesZValue = SeriesZValue,
61 ScatterSeriesZValue = SeriesZValue,
61 ScatterSeriesZValue = SeriesZValue,
62 PieSeriesZValue = SeriesZValue,
62 PieSeriesZValue = SeriesZValue,
63 AxisZValue,
63 AxisZValue,
64 LegendZValue
64 LegendZValue
65 };
65 };
66
66
67 enum State {
67 enum State {
68 ShowState,
68 ShowState,
69 ScrollUpState,
69 ScrollUpState,
70 ScrollDownState,
70 ScrollDownState,
71 ScrollLeftState,
71 ScrollLeftState,
72 ScrollRightState,
72 ScrollRightState,
73 ZoomInState,
73 ZoomInState,
74 ZoomOutState
74 ZoomOutState
75 };
75 };
76
76
77 ChartPresenter(QChart* chart,ChartDataSet *dataset);
77 ChartPresenter(QChart* chart,ChartDataSet *dataset);
78 virtual ~ChartPresenter();
78 virtual ~ChartPresenter();
79
79
80 ChartAnimator* animator() const { return m_animator; }
80 ChartAnimator* animator() const { return m_animator; }
81 ChartTheme *chartTheme() const { return m_chartTheme; }
81 ChartTheme *chartTheme() const { return m_chartTheme; }
82 ChartDataSet *dataSet() const { return m_dataset; }
82 ChartDataSet *dataSet() const { return m_dataset; }
83 QGraphicsItem* rootItem() const { return m_chart; }
83 QGraphicsItem* rootItem() const { return m_chart; }
84 QGraphicsRectItem* backgroundItem();
84 QGraphicsRectItem* backgroundItem();
85 QGraphicsItem* titleItem();
85 QGraphicsItem* titleItem();
86 QList<ChartAxis*> axisItems() const;
86 QList<ChartAxis*> axisItems() const;
87
87
88 QLegend* legend();
88 QLegend* legend();
89
89
90 void setBackgroundBrush(const QBrush& brush);
90 void setBackgroundBrush(const QBrush& brush);
91 QBrush backgroundBrush() const;
91 QBrush backgroundBrush() const;
92
92
93 void setBackgroundPen(const QPen& pen);
93 void setBackgroundPen(const QPen& pen);
94 QPen backgroundPen() const;
94 QPen backgroundPen() const;
95
95
96 void setTitle(const QString& title);
96 void setTitle(const QString& title);
97 QString title() const;
97 QString title() const;
98
98
99 void setTitleFont(const QFont& font);
99 void setTitleFont(const QFont& font);
100 QFont titleFont() const;
100 QFont titleFont() const;
101
101
102 void setTitleBrush(const QBrush &brush);
102 void setTitleBrush(const QBrush &brush);
103 QBrush titleBrush() const;
103 QBrush titleBrush() const;
104
104
105 void setBackgroundVisible(bool visible);
105 void setBackgroundVisible(bool visible);
106 bool isBackgroundVisible() const;
106 bool isBackgroundVisible() const;
107
107
108 void setBackgroundDropShadowEnabled(bool enabled);
108 void setBackgroundDropShadowEnabled(bool enabled);
109 bool isBackgroundDropShadowEnabled() const;
109 bool isBackgroundDropShadowEnabled() const;
110
110
111 void setVisible(bool visible);
111 void setVisible(bool visible);
112
112
113 void setTheme(QChart::ChartTheme theme,bool force = true);
113 void setTheme(QChart::ChartTheme theme,bool force = true);
114 QChart::ChartTheme theme();
114 QChart::ChartTheme theme();
115
115
116 void setAnimationOptions(QChart::AnimationOptions options);
116 void setAnimationOptions(QChart::AnimationOptions options);
117 QChart::AnimationOptions animationOptions() const;
117 QChart::AnimationOptions animationOptions() const;
118
118
119 void zoomIn(qreal factor);
119 void zoomIn(qreal factor);
120 void zoomIn(const QRectF& rect);
120 void zoomIn(const QRectF& rect);
121 void zoomOut(qreal factor);
121 void zoomOut(qreal factor);
122 void scroll(qreal dx,qreal dy);
122 void scroll(qreal dx,qreal dy);
123
123
124 void setGeometry(const QRectF& rect);
124 void setGeometry(const QRectF& rect);
125 QRectF geometry() { return m_rect; }
125 QRectF geometry() { return m_rect; }
126
126
127 void startAnimation(ChartAnimation* animation);
127 void startAnimation(ChartAnimation* animation);
128 State state() const { return m_state; }
128 State state() const { return m_state; }
129 QPointF statePoint() const { return m_statePoint; }
129 QPointF statePoint() const { return m_statePoint; }
130
130
131 void resetAllElements();
131 void resetAllElements();
132
132
133 void setMarginsMinimum(const QRectF& margins);
133 void setMarginsMinimum(const QRectF& margins);
134 QRectF margins() const;
134 QRectF margins() const;
135 QGraphicsLayout* layout();
135 QGraphicsLayout* layout();
136
136
137 private:
137 private:
138 void createBackgroundItem();
138 void createBackgroundItem();
139 void createTitleItem();
139 void createTitleItem();
140
140
141 public Q_SLOTS:
141 public Q_SLOTS:
142 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
142 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
143 void handleSeriesRemoved(QAbstractSeries* series);
143 void handleSeriesRemoved(QAbstractSeries* series);
144 void handleAxisAdded(QAbstractAxis* axis,Domain* domain);
144 void handleAxisAdded(QAbstractAxis* axis,Domain* domain);
145 void handleAxisRemoved(QAbstractAxis* axis);
145 void handleAxisRemoved(QAbstractAxis* axis);
146 void handleAxisVisibleChanged(bool visible);
146
147
147 private Q_SLOTS:
148 private Q_SLOTS:
148 void handleAnimationFinished();
149 void handleAnimationFinished();
149
150
150 Q_SIGNALS:
151 Q_SIGNALS:
151 void geometryChanged(const QRectF& rect);
152 void geometryChanged(const QRectF& rect);
152 void animationsFinished();
153 void animationsFinished();
153 void marginsChanged(QRectF margins);
154 void marginsChanged(QRectF margins);
154
155
155 private:
156 private:
156 QChart* m_chart;
157 QChart* m_chart;
157 ChartAnimator* m_animator;
158 ChartAnimator* m_animator;
158 ChartDataSet* m_dataset;
159 ChartDataSet* m_dataset;
159 ChartTheme *m_chartTheme;
160 ChartTheme *m_chartTheme;
160 QMap<QAbstractSeries*, Chart*> m_chartItems;
161 QMap<QAbstractSeries*, Chart*> m_chartItems;
161 QMap<QAbstractAxis*, ChartAxis*> m_axisItems;
162 QMap<QAbstractAxis*, ChartAxis*> m_axisItems;
162 QRectF m_rect;
163 QRectF m_rect;
163 QChart::AnimationOptions m_options;
164 QChart::AnimationOptions m_options;
164 State m_state;
165 State m_state;
165 QPointF m_statePoint;
166 QPointF m_statePoint;
166 QList<ChartAnimation*> m_animations;
167 QList<ChartAnimation*> m_animations;
167 ChartLayout* m_layout;
168 ChartLayout* m_layout;
168 ChartBackground* m_backgroundItem;
169 ChartBackground* m_backgroundItem;
169 QGraphicsSimpleTextItem* m_titleItem;
170 QGraphicsSimpleTextItem* m_titleItem;
170 };
171 };
171
172
172 QTCOMMERCIALCHART_END_NAMESPACE
173 QTCOMMERCIALCHART_END_NAMESPACE
173
174
174 #endif /* CHARTPRESENTER_H_ */
175 #endif /* CHARTPRESENTER_H_ */
General Comments 0
You need to be logged in to leave comments. Login now