##// END OF EJS Templates
pie: make it impossible to add negative slice values
Jani Honkonen -
r1280:dde538c774a3
parent child
Show More
@@ -1,533 +1,536
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 "qpieslice.h"
21 #include "qpieslice.h"
22 #include "qpieslice_p.h"
22 #include "qpieslice_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QPieSlice
27 \class QPieSlice
28 \brief Defines a slice in pie series.
28 \brief Defines a slice in pie series.
29
29
30 This object defines the properties of a single slice in a QPieSeries.
30 This object defines the properties of a single slice in a QPieSeries.
31
31
32 In addition to the obvious value and label properties the user can also control
32 In addition to the obvious value and label properties the user can also control
33 the visual appearance of a slice. By modifying the visual appearance also means that
33 the visual appearance of a slice. By modifying the visual appearance also means that
34 the user is overriding the default appearance set by the theme.
34 the user is overriding the default appearance set by the theme.
35
35
36 Note that if the user has customized slices and theme is changed all customizations will be lost.
36 Note that if the user has customized slices and theme is changed all customizations will be lost.
37
37
38 To enable user interaction with the pie some basic signals are provided about clicking and hovering.
38 To enable user interaction with the pie some basic signals are provided about clicking and hovering.
39 */
39 */
40
40
41 /*!
41 /*!
42 \property QPieSlice::label
42 \property QPieSlice::label
43
43
44 Label of the slice.
44 Label of the slice.
45
45
46 \sa labelVisible, labelPen, labelFont, labelArmLengthFactor
46 \sa labelVisible, labelPen, labelFont, labelArmLengthFactor
47 */
47 */
48
48
49 /*!
49 /*!
50 \fn void QPieSlice::labelChanged()
50 \fn void QPieSlice::labelChanged()
51
51
52 This signal emitted when the slice label has been changed.
52 This signal emitted when the slice label has been changed.
53
53
54 \sa label
54 \sa label
55 */
55 */
56
56
57 /*!
57 /*!
58 \property QPieSlice::value
58 \property QPieSlice::value
59
59
60 Value of the slice.
60 Value of the slice.
61
61
62 Note that if users sets a negative value it is converted to a positive value.
63
62 \sa percentage(), QPieSeries::sum()
64 \sa percentage(), QPieSeries::sum()
63 */
65 */
64
66
65 /*!
67 /*!
66 \fn void QPieSlice::valueChanged()
68 \fn void QPieSlice::valueChanged()
67
69
68 This signal is emitted when the slice value changes.
70 This signal is emitted when the slice value changes.
69
71
70 \sa value
72 \sa value
71 */
73 */
72
74
73 /*!
75 /*!
74 \property QPieSlice::labelVisible
76 \property QPieSlice::labelVisible
75
77
76 Defienes the visibility of the slice label.
78 Defienes the visibility of the slice label.
77
79
78 Default is not visible.
80 Default is not visible.
79
81
80 \sa label, labelPen, labelFont, labelArmLengthFactor
82 \sa label, labelPen, labelFont, labelArmLengthFactor
81 */
83 */
82
84
83 /*!
85 /*!
84 \fn void QPieSlice::labelVisibleChanged()
86 \fn void QPieSlice::labelVisibleChanged()
85
87
86 This signal emitted when visibility of the slice label has changed.
88 This signal emitted when visibility of the slice label has changed.
87
89
88 \sa labelVisible
90 \sa labelVisible
89 */
91 */
90
92
91 /*!
93 /*!
92 \property QPieSlice::exploded
94 \property QPieSlice::exploded
93
95
94 Defines if the slice is exploded from the pie.
96 Defines if the slice is exploded from the pie.
95
97
96 \sa explodeDistanceFactor
98 \sa explodeDistanceFactor
97 */
99 */
98
100
99 /*!
101 /*!
100 \fn void QPieSlice::explodedChanged()
102 \fn void QPieSlice::explodedChanged()
101
103
102 This signal is emitted the the slice has been exploded from the pie or is returned back to the pie.
104 This signal is emitted the the slice has been exploded from the pie or is returned back to the pie.
103
105
104 \sa exploded
106 \sa exploded
105 */
107 */
106
108
107 /*!
109 /*!
108 \property QPieSlice::pen
110 \property QPieSlice::pen
109
111
110 Pen used to draw the slice border.
112 Pen used to draw the slice border.
111 */
113 */
112
114
113 /*!
115 /*!
114 \fn void QPieSlice::penChanged()
116 \fn void QPieSlice::penChanged()
115
117
116 This signal is emitted when the pen of the slice has changed.
118 This signal is emitted when the pen of the slice has changed.
117
119
118 \sa pen
120 \sa pen
119 */
121 */
120
122
121 /*!
123 /*!
122 \property QPieSlice::brush
124 \property QPieSlice::brush
123
125
124 Brush used to draw the slice.
126 Brush used to draw the slice.
125 */
127 */
126
128
127 /*!
129 /*!
128 \fn void QPieSlice::brushChanged()
130 \fn void QPieSlice::brushChanged()
129
131
130 This signal is emitted when the brush of the slice has changed.
132 This signal is emitted when the brush of the slice has changed.
131
133
132 \sa brush
134 \sa brush
133 */
135 */
134
136
135 /*!
137 /*!
136 \property QPieSlice::labelPen
138 \property QPieSlice::labelPen
137
139
138 Pen used to draw label and label arm of the slice.
140 Pen used to draw label and label arm of the slice.
139
141
140 \sa label, labelVisible, labelFont, labelArmLengthFactor
142 \sa label, labelVisible, labelFont, labelArmLengthFactor
141 */
143 */
142
144
143 /*!
145 /*!
144 \fn void QPieSlice::labelPenChanged()
146 \fn void QPieSlice::labelPenChanged()
145
147
146 This signal is emitted when the label pen of the slice has changed.
148 This signal is emitted when the label pen of the slice has changed.
147
149
148 \sa labelPen
150 \sa labelPen
149 */
151 */
150
152
151 /*!
153 /*!
152 \property QPieSlice::labelFont
154 \property QPieSlice::labelFont
153
155
154 Font used for drawing label text.
156 Font used for drawing label text.
155
157
156 \sa label, labelVisible, labelArmLengthFactor
158 \sa label, labelVisible, labelArmLengthFactor
157 */
159 */
158
160
159 /*!
161 /*!
160 \fn void QPieSlice::labelFontChanged()
162 \fn void QPieSlice::labelFontChanged()
161
163
162 This signal is emitted when the label font of the slice has changed.
164 This signal is emitted when the label font of the slice has changed.
163
165
164 \sa labelFont
166 \sa labelFont
165 */
167 */
166
168
167 /*!
169 /*!
168 \property QPieSlice::labelArmLengthFactor
170 \property QPieSlice::labelArmLengthFactor
169
171
170 Defines the length of the label arm.
172 Defines the length of the label arm.
171
173
172 The factor is relative to pie radius. For example:
174 The factor is relative to pie radius. For example:
173 1.0 means the length is the same as the radius.
175 1.0 means the length is the same as the radius.
174 0.5 means the length is half of the radius.
176 0.5 means the length is half of the radius.
175
177
176 Default value is 0.15
178 Default value is 0.15
177
179
178 \sa label, labelVisible, labelPen, labelFont
180 \sa label, labelVisible, labelPen, labelFont
179 */
181 */
180
182
181 /*!
183 /*!
182 \fn void QPieSlice::labelArmLengthFactorChanged()
184 \fn void QPieSlice::labelArmLengthFactorChanged()
183
185
184 This signal is emitted when the label arm factor of the slice has changed.
186 This signal is emitted when the label arm factor of the slice has changed.
185
187
186 \sa labelArmLengthFactor
188 \sa labelArmLengthFactor
187 */
189 */
188
190
189 /*!
191 /*!
190 \property QPieSlice::explodeDistanceFactor
192 \property QPieSlice::explodeDistanceFactor
191
193
192 When the slice is exploded this factor defines how far the slice is exploded away from the pie.
194 When the slice is exploded this factor defines how far the slice is exploded away from the pie.
193
195
194 The factor is relative to pie radius. For example:
196 The factor is relative to pie radius. For example:
195 1.0 means the distance is the same as the radius.
197 1.0 means the distance is the same as the radius.
196 0.5 means the distance is half of the radius.
198 0.5 means the distance is half of the radius.
197
199
198 Default value is 0.15
200 Default value is 0.15
199
201
200 \sa exploded
202 \sa exploded
201 */
203 */
202
204
203 /*!
205 /*!
204 \fn void QPieSlice::explodeDistanceFactorChanged()
206 \fn void QPieSlice::explodeDistanceFactorChanged()
205
207
206 This signal is emitted when the explode distance factor of the slice has changed.
208 This signal is emitted when the explode distance factor of the slice has changed.
207
209
208 \sa explodeDistanceFactor
210 \sa explodeDistanceFactor
209 */
211 */
210
212
211 /*!
213 /*!
212 \property QPieSlice::percentage
214 \property QPieSlice::percentage
213
215
214 Percentage of the slice compared to the sum of all slices in the series.
216 Percentage of the slice compared to the sum of all slices in the series.
215
217
216 The actual value ranges from 0.0 to 1.0.
218 The actual value ranges from 0.0 to 1.0.
217
219
218 Updated automatically once the slice is added to the series.
220 Updated automatically once the slice is added to the series.
219
221
220 \sa value, QPieSeries::sum
222 \sa value, QPieSeries::sum
221 */
223 */
222
224
223 /*!
225 /*!
224 \fn void QPieSlice::percentageChanged()
226 \fn void QPieSlice::percentageChanged()
225
227
226 This signal is emitted when the percentage of the slice has changed.
228 This signal is emitted when the percentage of the slice has changed.
227
229
228 \sa percentage
230 \sa percentage
229 */
231 */
230
232
231 /*!
233 /*!
232 \property QPieSlice::startAngle
234 \property QPieSlice::startAngle
233
235
234 Defines the starting angle of this slice in the series it belongs to.
236 Defines the starting angle of this slice in the series it belongs to.
235
237
236 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
238 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
237
239
238 Updated automatically once the slice is added to the series.
240 Updated automatically once the slice is added to the series.
239 */
241 */
240
242
241 /*!
243 /*!
242 \fn void QPieSlice::startAngleChanged()
244 \fn void QPieSlice::startAngleChanged()
243
245
244 This signal is emitted when the starting angle f the slice has changed.
246 This signal is emitted when the starting angle f the slice has changed.
245
247
246 \sa startAngle
248 \sa startAngle
247 */
249 */
248
250
249 /*!
251 /*!
250 \property QPieSlice::angleSpan
252 \property QPieSlice::angleSpan
251
253
252 Span of the slice in degrees.
254 Span of the slice in degrees.
253
255
254 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
256 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
255
257
256 Updated automatically once the slice is added to the series.
258 Updated automatically once the slice is added to the series.
257 */
259 */
258
260
259 /*!
261 /*!
260 \fn void QPieSlice::angleSpanChanged()
262 \fn void QPieSlice::angleSpanChanged()
261
263
262 This signal is emitted when the angle span of the slice has changed.
264 This signal is emitted when the angle span of the slice has changed.
263
265
264 \sa angleSpan
266 \sa angleSpan
265 */
267 */
266
268
267
269
268 /*!
270 /*!
269 \fn void QPieSlice::clicked()
271 \fn void QPieSlice::clicked()
270
272
271 This signal is emitted when user has clicked the slice.
273 This signal is emitted when user has clicked the slice.
272
274
273 \sa QPieSeries::clicked()
275 \sa QPieSeries::clicked()
274 */
276 */
275
277
276 /*!
278 /*!
277 \fn void QPieSlice::hovered(bool state)
279 \fn void QPieSlice::hovered(bool state)
278
280
279 This signal is emitted when user has hovered over or away from the slice.
281 This signal is emitted when user has hovered over or away from the slice.
280
282
281 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
283 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
282
284
283 \sa QPieSeries::hovered()
285 \sa QPieSeries::hovered()
284 */
286 */
285
287
286 /*!
288 /*!
287 Constructs an empty slice with a \a parent.
289 Constructs an empty slice with a \a parent.
288
290
289 \sa QPieSeries::append(), QPieSeries::insert()
291 \sa QPieSeries::append(), QPieSeries::insert()
290 */
292 */
291 QPieSlice::QPieSlice(QObject *parent)
293 QPieSlice::QPieSlice(QObject *parent)
292 :QObject(parent),
294 :QObject(parent),
293 d_ptr(new QPieSlicePrivate(this))
295 d_ptr(new QPieSlicePrivate(this))
294 {
296 {
295
297
296 }
298 }
297
299
298 /*!
300 /*!
299 Constructs an empty slice with given \a value, \a label and a \a parent.
301 Constructs an empty slice with given \a value, \a label and a \a parent.
300 \sa QPieSeries::append(), QPieSeries::insert()
302 \sa QPieSeries::append(), QPieSeries::insert()
301 */
303 */
302 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
304 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
303 :QObject(parent),
305 :QObject(parent),
304 d_ptr(new QPieSlicePrivate(this))
306 d_ptr(new QPieSlicePrivate(this))
305 {
307 {
306 d_ptr->m_data.m_value = value;
308 setValue(value);
307 d_ptr->m_data.m_labelText = label;
309 setLabel(label);
308 }
310 }
309
311
310 /*!
312 /*!
311 Destroys the slice.
313 Destroys the slice.
312 User should not delete the slice if it has been added to the series.
314 User should not delete the slice if it has been added to the series.
313 */
315 */
314 QPieSlice::~QPieSlice()
316 QPieSlice::~QPieSlice()
315 {
317 {
316
318
317 }
319 }
318
320
319 void QPieSlice::setLabel(QString label)
321 void QPieSlice::setLabel(QString label)
320 {
322 {
321 if (d_ptr->m_data.m_labelText != label) {
323 if (d_ptr->m_data.m_labelText != label) {
322 d_ptr->m_data.m_labelText = label;
324 d_ptr->m_data.m_labelText = label;
323 emit labelChanged();
325 emit labelChanged();
324 }
326 }
325 }
327 }
326
328
327 QString QPieSlice::label() const
329 QString QPieSlice::label() const
328 {
330 {
329 return d_ptr->m_data.m_labelText;
331 return d_ptr->m_data.m_labelText;
330 }
332 }
331
333
332 void QPieSlice::setValue(qreal value)
334 void QPieSlice::setValue(qreal value)
333 {
335 {
336 value = qAbs(value); // negative values not allowed
334 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
337 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
335 d_ptr->m_data.m_value = value;
338 d_ptr->m_data.m_value = value;
336 emit valueChanged();
339 emit valueChanged();
337 }
340 }
338 }
341 }
339
342
340 qreal QPieSlice::value() const
343 qreal QPieSlice::value() const
341 {
344 {
342 return d_ptr->m_data.m_value;
345 return d_ptr->m_data.m_value;
343 }
346 }
344
347
345 void QPieSlice::setLabelVisible(bool visible)
348 void QPieSlice::setLabelVisible(bool visible)
346 {
349 {
347 if (d_ptr->m_data.m_isLabelVisible != visible) {
350 if (d_ptr->m_data.m_isLabelVisible != visible) {
348 d_ptr->m_data.m_isLabelVisible = visible;
351 d_ptr->m_data.m_isLabelVisible = visible;
349 emit labelVisibleChanged();
352 emit labelVisibleChanged();
350 }
353 }
351 }
354 }
352
355
353 bool QPieSlice::isLabelVisible() const
356 bool QPieSlice::isLabelVisible() const
354 {
357 {
355 return d_ptr->m_data.m_isLabelVisible;
358 return d_ptr->m_data.m_isLabelVisible;
356 }
359 }
357
360
358 void QPieSlice::setExploded(bool exploded)
361 void QPieSlice::setExploded(bool exploded)
359 {
362 {
360 if (d_ptr->m_data.m_isExploded != exploded) {
363 if (d_ptr->m_data.m_isExploded != exploded) {
361 d_ptr->m_data.m_isExploded = exploded;
364 d_ptr->m_data.m_isExploded = exploded;
362 emit explodedChanged();
365 emit explodedChanged();
363 }
366 }
364 }
367 }
365
368
366 bool QPieSlice::isExploded() const
369 bool QPieSlice::isExploded() const
367 {
370 {
368 return d_ptr->m_data.m_isExploded;
371 return d_ptr->m_data.m_isExploded;
369 }
372 }
370
373
371 void QPieSlice::setPen(const QPen &pen)
374 void QPieSlice::setPen(const QPen &pen)
372 {
375 {
373 d_ptr->setPen(pen, false);
376 d_ptr->setPen(pen, false);
374 }
377 }
375
378
376 QPen QPieSlice::pen() const
379 QPen QPieSlice::pen() const
377 {
380 {
378 return d_ptr->m_data.m_slicePen;
381 return d_ptr->m_data.m_slicePen;
379 }
382 }
380
383
381 void QPieSlice::setBrush(const QBrush &brush)
384 void QPieSlice::setBrush(const QBrush &brush)
382 {
385 {
383 d_ptr->setBrush(brush, false);
386 d_ptr->setBrush(brush, false);
384 }
387 }
385
388
386 QBrush QPieSlice::brush() const
389 QBrush QPieSlice::brush() const
387 {
390 {
388 return d_ptr->m_data.m_sliceBrush;
391 return d_ptr->m_data.m_sliceBrush;
389 }
392 }
390
393
391 void QPieSlice::setLabelPen(const QPen &pen)
394 void QPieSlice::setLabelPen(const QPen &pen)
392 {
395 {
393 d_ptr->setLabelPen(pen, false);
396 d_ptr->setLabelPen(pen, false);
394 }
397 }
395
398
396 QPen QPieSlice::labelPen() const
399 QPen QPieSlice::labelPen() const
397 {
400 {
398 return d_ptr->m_data.m_labelPen;
401 return d_ptr->m_data.m_labelPen;
399 }
402 }
400
403
401 void QPieSlice::setLabelFont(const QFont &font)
404 void QPieSlice::setLabelFont(const QFont &font)
402 {
405 {
403 d_ptr->setLabelFont(font, false);
406 d_ptr->setLabelFont(font, false);
404 }
407 }
405
408
406 QFont QPieSlice::labelFont() const
409 QFont QPieSlice::labelFont() const
407 {
410 {
408 return d_ptr->m_data.m_labelFont;
411 return d_ptr->m_data.m_labelFont;
409 }
412 }
410
413
411 void QPieSlice::setLabelArmLengthFactor(qreal factor)
414 void QPieSlice::setLabelArmLengthFactor(qreal factor)
412 {
415 {
413 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
416 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
414 d_ptr->m_data.m_labelArmLengthFactor = factor;
417 d_ptr->m_data.m_labelArmLengthFactor = factor;
415 emit labelArmLengthFactorChanged();
418 emit labelArmLengthFactorChanged();
416 }
419 }
417 }
420 }
418
421
419 qreal QPieSlice::labelArmLengthFactor() const
422 qreal QPieSlice::labelArmLengthFactor() const
420 {
423 {
421 return d_ptr->m_data.m_labelArmLengthFactor;
424 return d_ptr->m_data.m_labelArmLengthFactor;
422 }
425 }
423
426
424 void QPieSlice::setExplodeDistanceFactor(qreal factor)
427 void QPieSlice::setExplodeDistanceFactor(qreal factor)
425 {
428 {
426 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
429 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
427 d_ptr->m_data.m_explodeDistanceFactor = factor;
430 d_ptr->m_data.m_explodeDistanceFactor = factor;
428 emit explodeDistanceFactorChanged();
431 emit explodeDistanceFactorChanged();
429 }
432 }
430 }
433 }
431
434
432 qreal QPieSlice::explodeDistanceFactor() const
435 qreal QPieSlice::explodeDistanceFactor() const
433 {
436 {
434 return d_ptr->m_data.m_explodeDistanceFactor;
437 return d_ptr->m_data.m_explodeDistanceFactor;
435 }
438 }
436
439
437 qreal QPieSlice::percentage() const
440 qreal QPieSlice::percentage() const
438 {
441 {
439 return d_ptr->m_data.m_percentage;
442 return d_ptr->m_data.m_percentage;
440 }
443 }
441
444
442 qreal QPieSlice::startAngle() const
445 qreal QPieSlice::startAngle() const
443 {
446 {
444 return d_ptr->m_data.m_startAngle;
447 return d_ptr->m_data.m_startAngle;
445 }
448 }
446
449
447 qreal QPieSlice::angleSpan() const
450 qreal QPieSlice::angleSpan() const
448 {
451 {
449 return d_ptr->m_data.m_angleSpan;
452 return d_ptr->m_data.m_angleSpan;
450 }
453 }
451
454
452 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
455 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
453 :QObject(parent),
456 :QObject(parent),
454 q_ptr(parent)
457 q_ptr(parent)
455 {
458 {
456
459
457 }
460 }
458
461
459 QPieSlicePrivate::~QPieSlicePrivate()
462 QPieSlicePrivate::~QPieSlicePrivate()
460 {
463 {
461
464
462 }
465 }
463
466
464 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
467 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
465 {
468 {
466 return slice->d_func();
469 return slice->d_func();
467 }
470 }
468
471
469 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
472 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
470 {
473 {
471 if (m_data.m_slicePen != pen) {
474 if (m_data.m_slicePen != pen) {
472 m_data.m_slicePen = pen;
475 m_data.m_slicePen = pen;
473 m_data.m_slicePen.setThemed(themed);
476 m_data.m_slicePen.setThemed(themed);
474 emit q_ptr->penChanged();
477 emit q_ptr->penChanged();
475 }
478 }
476 }
479 }
477
480
478 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
481 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
479 {
482 {
480 if (m_data.m_sliceBrush != brush) {
483 if (m_data.m_sliceBrush != brush) {
481 m_data.m_sliceBrush = brush;
484 m_data.m_sliceBrush = brush;
482 m_data.m_sliceBrush.setThemed(themed);
485 m_data.m_sliceBrush.setThemed(themed);
483 emit q_ptr->brushChanged();
486 emit q_ptr->brushChanged();
484 }
487 }
485 }
488 }
486
489
487 void QPieSlicePrivate::setLabelPen(const QPen &pen, bool themed)
490 void QPieSlicePrivate::setLabelPen(const QPen &pen, bool themed)
488 {
491 {
489 if (m_data.m_labelPen != pen) {
492 if (m_data.m_labelPen != pen) {
490 m_data.m_labelPen = pen;
493 m_data.m_labelPen = pen;
491 m_data.m_labelPen.setThemed(themed);
494 m_data.m_labelPen.setThemed(themed);
492 emit q_ptr->labelPenChanged();
495 emit q_ptr->labelPenChanged();
493 }
496 }
494 }
497 }
495
498
496 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
499 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
497 {
500 {
498 if (m_data.m_labelFont != font) {
501 if (m_data.m_labelFont != font) {
499 m_data.m_labelFont = font;
502 m_data.m_labelFont = font;
500 m_data.m_labelFont.setThemed(themed);
503 m_data.m_labelFont.setThemed(themed);
501 emit q_ptr->labelFontChanged();
504 emit q_ptr->labelFontChanged();
502 }
505 }
503 }
506 }
504
507
505 void QPieSlicePrivate::setPercentage(qreal percentage)
508 void QPieSlicePrivate::setPercentage(qreal percentage)
506 {
509 {
507 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
510 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
508 m_data.m_percentage = percentage;
511 m_data.m_percentage = percentage;
509 emit q_ptr->percentageChanged();
512 emit q_ptr->percentageChanged();
510 }
513 }
511 }
514 }
512
515
513 void QPieSlicePrivate::setStartAngle(qreal angle)
516 void QPieSlicePrivate::setStartAngle(qreal angle)
514 {
517 {
515 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
518 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
516 m_data.m_startAngle = angle;
519 m_data.m_startAngle = angle;
517 emit q_ptr->startAngleChanged();
520 emit q_ptr->startAngleChanged();
518 }
521 }
519 }
522 }
520
523
521 void QPieSlicePrivate::setAngleSpan(qreal span)
524 void QPieSlicePrivate::setAngleSpan(qreal span)
522 {
525 {
523 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
526 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
524 m_data.m_angleSpan = span;
527 m_data.m_angleSpan = span;
525 emit q_ptr->angleSpanChanged();
528 emit q_ptr->angleSpanChanged();
526 }
529 }
527 }
530 }
528
531
529 QTCOMMERCIALCHART_END_NAMESPACE
532 QTCOMMERCIALCHART_END_NAMESPACE
530
533
531 QTCOMMERCIALCHART_USE_NAMESPACE
534 QTCOMMERCIALCHART_USE_NAMESPACE
532 #include "moc_qpieslice.cpp"
535 #include "moc_qpieslice.cpp"
533 #include "moc_qpieslice_p.cpp"
536 #include "moc_qpieslice_p.cpp"
@@ -1,291 +1,292
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <tst_definitions.h>
22 #include <tst_definitions.h>
23 #include <qchartview.h>
23 #include <qchartview.h>
24 #include <qchart.h>
24 #include <qchart.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpieseries.h>
26 #include <qpieseries.h>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 class tst_qpieslice : public QObject
30 class tst_qpieslice : public QObject
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33
33
34 public slots:
34 public slots:
35 void initTestCase();
35 void initTestCase();
36 void cleanupTestCase();
36 void cleanupTestCase();
37 void init();
37 void init();
38 void cleanup();
38 void cleanup();
39
39
40 private slots:
40 private slots:
41 void construction();
41 void construction();
42 void changedSignals();
42 void changedSignals();
43 void customize();
43 void customize();
44 void mouseClick();
44 void mouseClick();
45 void mouseHover();
45 void mouseHover();
46
46
47 private:
47 private:
48
48
49
49
50 private:
50 private:
51
51
52 };
52 };
53
53
54 void tst_qpieslice::initTestCase()
54 void tst_qpieslice::initTestCase()
55 {
55 {
56 }
56 }
57
57
58 void tst_qpieslice::cleanupTestCase()
58 void tst_qpieslice::cleanupTestCase()
59 {
59 {
60 }
60 }
61
61
62 void tst_qpieslice::init()
62 void tst_qpieslice::init()
63 {
63 {
64
64
65 }
65 }
66
66
67 void tst_qpieslice::cleanup()
67 void tst_qpieslice::cleanup()
68 {
68 {
69
69
70 }
70 }
71
71
72 void tst_qpieslice::construction()
72 void tst_qpieslice::construction()
73 {
73 {
74 // no params
74 // no params
75 QPieSlice slice1;
75 QPieSlice slice1;
76 QCOMPARE(slice1.value(), 0.0);
76 QCOMPARE(slice1.value(), 0.0);
77 QVERIFY(slice1.label().isEmpty());
77 QVERIFY(slice1.label().isEmpty());
78 QVERIFY(!slice1.isLabelVisible());
78 QVERIFY(!slice1.isLabelVisible());
79 QVERIFY(!slice1.isExploded());
79 QVERIFY(!slice1.isExploded());
80 QCOMPARE(slice1.pen(), QPen());
80 QCOMPARE(slice1.pen(), QPen());
81 QCOMPARE(slice1.brush(), QBrush());
81 QCOMPARE(slice1.brush(), QBrush());
82 QCOMPARE(slice1.labelPen(), QPen());
82 QCOMPARE(slice1.labelPen(), QPen());
83 QCOMPARE(slice1.labelFont(), QFont());
83 QCOMPARE(slice1.labelFont(), QFont());
84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
86 QCOMPARE(slice1.percentage(), 0.0);
86 QCOMPARE(slice1.percentage(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
88 QCOMPARE(slice1.angleSpan(), 0.0);
88 QCOMPARE(slice1.angleSpan(), 0.0);
89
89
90 // value and label params
90 // value and label params
91 QPieSlice slice2("foobar", 1.0);
91 QPieSlice slice2("foobar", 1.0);
92 QCOMPARE(slice2.value(), 1.0);
92 QCOMPARE(slice2.value(), 1.0);
93 QCOMPARE(slice2.label(), QString("foobar"));
93 QCOMPARE(slice2.label(), QString("foobar"));
94 QVERIFY(!slice2.isLabelVisible());
94 QVERIFY(!slice2.isLabelVisible());
95 QVERIFY(!slice2.isExploded());
95 QVERIFY(!slice2.isExploded());
96 QCOMPARE(slice2.pen(), QPen());
96 QCOMPARE(slice2.pen(), QPen());
97 QCOMPARE(slice2.brush(), QBrush());
97 QCOMPARE(slice2.brush(), QBrush());
98 QCOMPARE(slice2.labelPen(), QPen());
98 QCOMPARE(slice2.labelPen(), QPen());
99 QCOMPARE(slice2.labelFont(), QFont());
99 QCOMPARE(slice2.labelFont(), QFont());
100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
102 QCOMPARE(slice2.percentage(), 0.0);
102 QCOMPARE(slice2.percentage(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
104 QCOMPARE(slice2.angleSpan(), 0.0);
104 QCOMPARE(slice2.angleSpan(), 0.0);
105 }
105 }
106
106
107 void tst_qpieslice::changedSignals()
107 void tst_qpieslice::changedSignals()
108 {
108 {
109 QPieSlice slice;
109 QPieSlice slice;
110
110
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
116 QSignalSpy labelPenSpy(&slice, SIGNAL(labelPenChanged()));
116 QSignalSpy labelPenSpy(&slice, SIGNAL(labelPenChanged()));
117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
118 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
118 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
119 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
119 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
120
120
121 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
121 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
122
122
123 // set everything twice to see we do not get unnecessary signals
123 // set everything twice to see we do not get unnecessary signals
124 slice.setValue(1);
124 slice.setValue(1.0);
125 slice.setValue(1);
125 slice.setValue(-1.0);
126 QCOMPARE(slice.value(), 1.0);
126 slice.setLabel("foobar");
127 slice.setLabel("foobar");
127 slice.setLabel("foobar");
128 slice.setLabel("foobar");
128 slice.setLabelVisible();
129 slice.setLabelVisible();
129 slice.setLabelVisible();
130 slice.setLabelVisible();
130 slice.setExploded();
131 slice.setExploded();
131 slice.setExploded();
132 slice.setExploded();
132 slice.setPen(QPen(Qt::red));
133 slice.setPen(QPen(Qt::red));
133 slice.setPen(QPen(Qt::red));
134 slice.setPen(QPen(Qt::red));
134 slice.setBrush(QBrush(Qt::red));
135 slice.setBrush(QBrush(Qt::red));
135 slice.setBrush(QBrush(Qt::red));
136 slice.setBrush(QBrush(Qt::red));
136 slice.setLabelPen(QPen(Qt::green));
137 slice.setLabelPen(QPen(Qt::green));
137 slice.setLabelPen(QPen(Qt::green));
138 slice.setLabelPen(QPen(Qt::green));
138 slice.setLabelFont(QFont("Tahoma"));
139 slice.setLabelFont(QFont("Tahoma"));
139 slice.setLabelFont(QFont("Tahoma"));
140 slice.setLabelFont(QFont("Tahoma"));
140 slice.setLabelArmLengthFactor(0.1);
141 slice.setLabelArmLengthFactor(0.1);
141 slice.setLabelArmLengthFactor(0.1);
142 slice.setLabelArmLengthFactor(0.1);
142 slice.setExplodeDistanceFactor(0.1);
143 slice.setExplodeDistanceFactor(0.1);
143 slice.setExplodeDistanceFactor(0.1);
144 slice.setExplodeDistanceFactor(0.1);
144
145
145 TRY_COMPARE(valueSpy.count(), 1);
146 TRY_COMPARE(valueSpy.count(), 1);
146 TRY_COMPARE(labelSpy.count(), 1);
147 TRY_COMPARE(labelSpy.count(), 1);
147 TRY_COMPARE(explodedSpy.count(), 1);
148 TRY_COMPARE(explodedSpy.count(), 1);
148 TRY_COMPARE(penSpy.count(), 1);
149 TRY_COMPARE(penSpy.count(), 1);
149 TRY_COMPARE(brushSpy.count(), 1);
150 TRY_COMPARE(brushSpy.count(), 1);
150 TRY_COMPARE(labelPenSpy.count(), 1);
151 TRY_COMPARE(labelPenSpy.count(), 1);
151 TRY_COMPARE(labelFontSpy.count(), 1);
152 TRY_COMPARE(labelFontSpy.count(), 1);
152 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
153 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
153 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
154 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
154 }
155 }
155
156
156 void tst_qpieslice::customize()
157 void tst_qpieslice::customize()
157 {
158 {
158 // create a pie series
159 // create a pie series
159 QPieSeries *series = new QPieSeries();
160 QPieSeries *series = new QPieSeries();
160 QPieSlice *s1 = series->append("slice 1", 1);
161 QPieSlice *s1 = series->append("slice 1", 1);
161 QPieSlice *s2 = series->append("slice 2", 2);
162 QPieSlice *s2 = series->append("slice 2", 2);
162 series->append("slice 3", 3);
163 series->append("slice 3", 3);
163
164
164 // customize a slice
165 // customize a slice
165 QPen p1(Qt::red);
166 QPen p1(Qt::red);
166 s1->setPen(p1);
167 s1->setPen(p1);
167 QBrush b1(Qt::red);
168 QBrush b1(Qt::red);
168 s1->setBrush(b1);
169 s1->setBrush(b1);
169 s1->setLabelPen(p1);
170 s1->setLabelPen(p1);
170 QFont f1("Consolas");
171 QFont f1("Consolas");
171 s1->setLabelFont(f1);
172 s1->setLabelFont(f1);
172
173
173 // add series to the chart
174 // add series to the chart
174 QChartView view(new QChart());
175 QChartView view(new QChart());
175 view.resize(200, 200);
176 view.resize(200, 200);
176 view.chart()->addSeries(series);
177 view.chart()->addSeries(series);
177 view.show();
178 view.show();
178 QTest::qWaitForWindowShown(&view);
179 QTest::qWaitForWindowShown(&view);
179 //QTest::qWait(1000);
180 //QTest::qWait(1000);
180
181
181 // check that customizations persist
182 // check that customizations persist
182 QCOMPARE(s1->pen(), p1);
183 QCOMPARE(s1->pen(), p1);
183 QCOMPARE(s1->brush(), b1);
184 QCOMPARE(s1->brush(), b1);
184 QCOMPARE(s1->labelPen(), p1);
185 QCOMPARE(s1->labelPen(), p1);
185 QCOMPARE(s1->labelFont(), f1);
186 QCOMPARE(s1->labelFont(), f1);
186
187
187 // remove a slice
188 // remove a slice
188 series->remove(s2);
189 series->remove(s2);
189 QCOMPARE(s1->pen(), p1);
190 QCOMPARE(s1->pen(), p1);
190 QCOMPARE(s1->brush(), b1);
191 QCOMPARE(s1->brush(), b1);
191 QCOMPARE(s1->labelPen(), p1);
192 QCOMPARE(s1->labelPen(), p1);
192 QCOMPARE(s1->labelFont(), f1);
193 QCOMPARE(s1->labelFont(), f1);
193
194
194 // add a slice
195 // add a slice
195 series->append("slice 4", 4);
196 series->append("slice 4", 4);
196 QCOMPARE(s1->pen(), p1);
197 QCOMPARE(s1->pen(), p1);
197 QCOMPARE(s1->brush(), b1);
198 QCOMPARE(s1->brush(), b1);
198 QCOMPARE(s1->labelPen(), p1);
199 QCOMPARE(s1->labelPen(), p1);
199 QCOMPARE(s1->labelFont(), f1);
200 QCOMPARE(s1->labelFont(), f1);
200
201
201 // insert a slice
202 // insert a slice
202 series->insert(0, new QPieSlice("slice 5", 5));
203 series->insert(0, new QPieSlice("slice 5", 5));
203 QCOMPARE(s1->pen(), p1);
204 QCOMPARE(s1->pen(), p1);
204 QCOMPARE(s1->brush(), b1);
205 QCOMPARE(s1->brush(), b1);
205 QCOMPARE(s1->labelPen(), p1);
206 QCOMPARE(s1->labelPen(), p1);
206 QCOMPARE(s1->labelFont(), f1);
207 QCOMPARE(s1->labelFont(), f1);
207
208
208 // change theme
209 // change theme
209 // theme will overwrite customizations
210 // theme will overwrite customizations
210 view.chart()->setTheme(QChart::ChartThemeHighContrast);
211 view.chart()->setTheme(QChart::ChartThemeHighContrast);
211 QVERIFY(s1->pen() != p1);
212 QVERIFY(s1->pen() != p1);
212 QVERIFY(s1->brush() != b1);
213 QVERIFY(s1->brush() != b1);
213 QVERIFY(s1->labelPen() != p1);
214 QVERIFY(s1->labelPen() != p1);
214 QVERIFY(s1->labelFont() != f1);
215 QVERIFY(s1->labelFont() != f1);
215 }
216 }
216
217
217 void tst_qpieslice::mouseClick()
218 void tst_qpieslice::mouseClick()
218 {
219 {
219 // create a pie series
220 // create a pie series
220 QPieSeries *series = new QPieSeries();
221 QPieSeries *series = new QPieSeries();
221 series->setPieSize(1.0);
222 series->setPieSize(1.0);
222 QPieSlice *s1 = series->append("slice 1", 1);
223 QPieSlice *s1 = series->append("slice 1", 1);
223 QPieSlice *s2 = series->append("slice 2", 2);
224 QPieSlice *s2 = series->append("slice 2", 2);
224 QPieSlice *s3 = series->append("slice 3", 3);
225 QPieSlice *s3 = series->append("slice 3", 3);
225 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
226 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
226 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
227 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
227 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
228 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
228
229
229 // add series to the chart
230 // add series to the chart
230 QChartView view(new QChart());
231 QChartView view(new QChart());
231 view.chart()->legend()->setVisible(false);
232 view.chart()->legend()->setVisible(false);
232 view.resize(200, 200);
233 view.resize(200, 200);
233 view.chart()->addSeries(series);
234 view.chart()->addSeries(series);
234 view.show();
235 view.show();
235 QTest::qWaitForWindowShown(&view);
236 QTest::qWaitForWindowShown(&view);
236
237
237 // simulate clicks
238 // simulate clicks
238 // pie rectangle: QRectF(60,60 121x121)
239 // pie rectangle: QRectF(60,60 121x121)
239 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
240 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
240 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
241 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
241 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
242 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
242 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
243 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
243 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
244 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
244 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
245 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
245 QCOMPARE(clickSpy1.count(), 1);
246 QCOMPARE(clickSpy1.count(), 1);
246 QCOMPARE(clickSpy2.count(), 1);
247 QCOMPARE(clickSpy2.count(), 1);
247 QCOMPARE(clickSpy3.count(), 1);
248 QCOMPARE(clickSpy3.count(), 1);
248 }
249 }
249
250
250 void tst_qpieslice::mouseHover()
251 void tst_qpieslice::mouseHover()
251 {
252 {
252 // create a pie series
253 // create a pie series
253 QPieSeries *series = new QPieSeries();
254 QPieSeries *series = new QPieSeries();
254 series->setPieSize(1.0);
255 series->setPieSize(1.0);
255 QPieSlice *s1 = series->append("slice 1", 1);
256 QPieSlice *s1 = series->append("slice 1", 1);
256 series->append("slice 2", 2);
257 series->append("slice 2", 2);
257 series->append("slice 3", 3);
258 series->append("slice 3", 3);
258
259
259 // add series to the chart
260 // add series to the chart
260 QChartView view(new QChart());
261 QChartView view(new QChart());
261 view.chart()->legend()->setVisible(false);
262 view.chart()->legend()->setVisible(false);
262 view.resize(200, 200);
263 view.resize(200, 200);
263 view.chart()->addSeries(series);
264 view.chart()->addSeries(series);
264 view.show();
265 view.show();
265 QTest::qWaitForWindowShown(&view);
266 QTest::qWaitForWindowShown(&view);
266
267
267 // first move to right top corner
268 // first move to right top corner
268 QTest::mouseMove(view.viewport(), QPoint(200, 0));
269 QTest::mouseMove(view.viewport(), QPoint(200, 0));
269 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
270 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
270
271
271 // move inside slice rectangle but NOT the actual slice
272 // move inside slice rectangle but NOT the actual slice
272 // pie rectangle: QRectF(60,60 121x121)
273 // pie rectangle: QRectF(60,60 121x121)
273 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
274 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
274 QTest::mouseMove(view.viewport(), QPoint(170, 70));
275 QTest::mouseMove(view.viewport(), QPoint(170, 70));
275 TRY_COMPARE(hoverSpy.count(), 0);
276 TRY_COMPARE(hoverSpy.count(), 0);
276
277
277 // move inside the slice
278 // move inside the slice
278 QTest::mouseMove(view.viewport(), QPoint(139, 85));
279 QTest::mouseMove(view.viewport(), QPoint(139, 85));
279 TRY_COMPARE(hoverSpy.count(), 1);
280 TRY_COMPARE(hoverSpy.count(), 1);
280 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
281 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
281
282
282 // move outside the slice
283 // move outside the slice
283 QTest::mouseMove(view.viewport(), QPoint(200, 0));
284 QTest::mouseMove(view.viewport(), QPoint(200, 0));
284 TRY_COMPARE(hoverSpy.count(), 2);
285 TRY_COMPARE(hoverSpy.count(), 2);
285 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
286 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
286 }
287 }
287
288
288 QTEST_MAIN(tst_qpieslice)
289 QTEST_MAIN(tst_qpieslice)
289
290
290 #include "tst_qpieslice.moc"
291 #include "tst_qpieslice.moc"
291
292
General Comments 0
You need to be logged in to leave comments. Login now