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