##// END OF EJS Templates
Embedded compile fix
Marek Rosa -
r1779:c1cdb21ee47d
parent child
Show More
@@ -1,872 +1,872
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 "qpieseries.h"
22 22 #include "qpieseries_p.h"
23 23 #include "qpieslice.h"
24 24 #include "qpieslice_p.h"
25 25 #include "pieslicedata_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include "charttheme_p.h"
28 28 #include "legendmarker_p.h"
29 29 #include "qabstractaxis.h"
30 30 #include "pieanimation_p.h"
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 /*!
35 35 \class QPieSeries
36 36 \brief Pie series API for QtCommercial Charts
37 37
38 38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
39 39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
40 40 The actual slice size is determined by that relative value.
41 41
42 42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
43 43 These relate to the actual chart rectangle.
44 44
45 45 By default the pie is defined as a full pie but it can also be a partial pie.
46 46 This can be done by setting a starting angle and angle span to the series.
47 47 Full pie is 360 degrees where 0 is at 12 a'clock.
48 48
49 49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
50 50 \image examples_piechart.png
51 51 */
52 52 /*!
53 53 \qmlclass PieSeries QPieSeries
54 54 \inherits AbstractSeries
55 55
56 56 The following QML shows how to create a simple pie chart.
57 57
58 58 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
59 59
60 60 \beginfloatleft
61 61 \image demos_qmlchart1.png
62 62 \endfloat
63 63 \clearfloat
64 64 */
65 65
66 66 /*!
67 67 \property QPieSeries::horizontalPosition
68 68 \brief Defines the horizontal position of the pie.
69 69
70 70 The value is a relative value to the chart rectangle where:
71 71
72 72 \list
73 73 \o 0.0 is the absolute left.
74 74 \o 1.0 is the absolute right.
75 75 \endlist
76 76 Default value is 0.5 (center).
77 77 \sa verticalPosition
78 78 */
79 79
80 80 /*!
81 81 \qmlproperty real PieSeries::horizontalPosition
82 82
83 83 Defines the horizontal position of the pie.
84 84
85 85 The value is a relative value to the chart rectangle where:
86 86
87 87 \list
88 88 \o 0.0 is the absolute left.
89 89 \o 1.0 is the absolute right.
90 90 \endlist
91 91 Default value is 0.5 (center).
92 92 \sa verticalPosition
93 93 */
94 94
95 95 /*!
96 96 \property QPieSeries::verticalPosition
97 97 \brief Defines the vertical position of the pie.
98 98
99 99 The value is a relative value to the chart rectangle where:
100 100
101 101 \list
102 102 \o 0.0 is the absolute top.
103 103 \o 1.0 is the absolute bottom.
104 104 \endlist
105 105 Default value is 0.5 (center).
106 106 \sa horizontalPosition
107 107 */
108 108
109 109 /*!
110 110 \qmlproperty real PieSeries::verticalPosition
111 111
112 112 Defines the vertical position of the pie.
113 113
114 114 The value is a relative value to the chart rectangle where:
115 115
116 116 \list
117 117 \o 0.0 is the absolute top.
118 118 \o 1.0 is the absolute bottom.
119 119 \endlist
120 120 Default value is 0.5 (center).
121 121 \sa horizontalPosition
122 122 */
123 123
124 124 /*!
125 125 \property QPieSeries::size
126 126 \brief Defines the pie size.
127 127
128 128 The value is a relative value to the chart rectangle where:
129 129
130 130 \list
131 131 \o 0.0 is the minimum size (pie not drawn).
132 132 \o 1.0 is the maximum size that can fit the chart.
133 133 \endlist
134 134
135 135 When setting this property the donutInnerSize property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
136 136
137 137 Default value is 0.7.
138 138 */
139 139
140 140 /*!
141 141 \qmlproperty real PieSeries::size
142 142
143 143 Defines the pie size.
144 144
145 145 The value is a relative value to the chart rectangle where:
146 146
147 147 \list
148 148 \o 0.0 is the minimum size (pie not drawn).
149 149 \o 1.0 is the maximum size that can fit the chart.
150 150 \endlist
151 151
152 152 Default value is 0.7.
153 153 */
154 154
155 155 /*!
156 156 \property QPieSeries::donutInnerSize
157 157 \brief Defines the donut inner size.
158 158
159 159 The value is a relative value to the chart rectangle where:
160 160
161 161 \list
162 162 \o 0.0 is the minimum size (pie not drawn).
163 163 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
164 164 \endlist
165 165
166 166 The value is never greater then size property.
167 167 Default value is 0.5.
168 168 */
169 169
170 170 /*!
171 171 \qmlproperty real PieSeries::donutInnerSize
172 172
173 173 Defines the donut inner size.
174 174
175 175 The value is a relative value to the chart rectangle where:
176 176
177 177 \list
178 178 \o 0.0 is the minimum size (donut is a pie).
179 179 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
180 180 \endlist
181 181
182 182 When setting this property the size property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
183 183
184 184 Default value is 0.5.
185 185 */
186 186
187 187 /*!
188 188 \property QPieSeries::startAngle
189 189 \brief Defines the starting angle of the pie.
190 190
191 191 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
192 192
193 193 Default is value is 0.
194 194 */
195 195
196 196 /*!
197 197 \qmlproperty real PieSeries::startAngle
198 198
199 199 Defines the starting angle of the pie.
200 200
201 201 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
202 202
203 203 Default is value is 0.
204 204 */
205 205
206 206 /*!
207 207 \property QPieSeries::endAngle
208 208 \brief Defines the ending angle of the pie.
209 209
210 210 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
211 211
212 212 Default is value is 360.
213 213 */
214 214
215 215 /*!
216 216 \qmlproperty real PieSeries::endAngle
217 217
218 218 Defines the ending angle of the pie.
219 219
220 220 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
221 221
222 222 Default is value is 360.
223 223 */
224 224
225 225 /*!
226 226 \property QPieSeries::count
227 227
228 228 Number of slices in the series.
229 229 */
230 230
231 231 /*!
232 232 \qmlproperty int PieSeries::count
233 233
234 234 Number of slices in the series.
235 235 */
236 236
237 237 /*!
238 238 \property QPieSeries::donut
239 239
240 240 Defines whether the series should be drawn as a donut
241 241 */
242 242
243 243 /*!
244 244 \qmlproperty int PieSeries::donut
245 245
246 246 Defines whether the series should be drawn as a donut
247 247 */
248 248
249 249 /*!
250 250 \fn void QPieSeries::countChanged()
251 251 Emitted when the slice count has changed.
252 252 \sa count
253 253 */
254 254 /*!
255 255 \qmlsignal PieSeries::onCountChanged()
256 256 Emitted when the slice count has changed.
257 257 */
258 258
259 259 /*!
260 260 \property QPieSeries::sum
261 261
262 262 Sum of all slices.
263 263
264 264 The series keeps track of the sum of all slices it holds.
265 265 */
266 266
267 267 /*!
268 268 \qmlproperty real PieSeries::sum
269 269
270 270 Sum of all slices.
271 271
272 272 The series keeps track of the sum of all slices it holds.
273 273 */
274 274
275 275 /*!
276 276 \fn void QPieSeries::sumChanged()
277 277 Emitted when the sum of all slices has changed.
278 278 \sa sum
279 279 */
280 280 /*!
281 281 \qmlsignal PieSeries::onSumChanged()
282 282 Emitted when the sum of all slices has changed. This may happen for example if you add or remove slices, or if you
283 283 change value of a slice.
284 284 */
285 285
286 286 /*!
287 287 \fn void QPieSeries::added(QList<QPieSlice*> slices)
288 288
289 289 This signal is emitted when \a slices have been added to the series.
290 290
291 291 \sa append(), insert()
292 292 */
293 293 /*!
294 294 \qmlsignal PieSeries::onAdded(PieSlice slice)
295 295 Emitted when \a slice has been added to the series.
296 296 */
297 297
298 298 /*!
299 299 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
300 300 This signal is emitted when \a slices have been removed from the series.
301 301 \sa remove()
302 302 */
303 303 /*!
304 304 \qmlsignal PieSeries::onRemoved(PieSlice slice)
305 305 Emitted when \a slice has been removed from the series.
306 306 */
307 307
308 308 /*!
309 309 \fn void QPieSeries::clicked(QPieSlice* slice)
310 310 This signal is emitted when a \a slice has been clicked.
311 311 \sa QPieSlice::clicked()
312 312 */
313 313 /*!
314 314 \qmlsignal PieSeries::onClicked(PieSlice slice)
315 315 This signal is emitted when a \a slice has been clicked.
316 316 */
317 317
318 318 /*!
319 319 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
320 320 This signal is emitted when user has hovered over or away from the \a slice.
321 321 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
322 322 \sa QPieSlice::hovered()
323 323 */
324 324 /*!
325 325 \qmlsignal PieSeries::onHovered(PieSlice slice, bool state)
326 326 This signal is emitted when user has hovered over or away from the \a slice. \a state is true when user has hovered
327 327 over the slice and false when hover has moved away from the slice.
328 328 */
329 329
330 330 /*!
331 331 \qmlmethod PieSlice PieSeries::at(int index)
332 332 Returns slice at \a index. Returns null if the index is not valid.
333 333 */
334 334
335 335 /*!
336 336 \qmlmethod PieSlice PieSeries::find(string label)
337 337 Returns the first slice with \a label. Returns null if the index is not valid.
338 338 */
339 339
340 340 /*!
341 341 \qmlmethod PieSlice PieSeries::append(string label, real value)
342 342 Adds a new slice with \a label and \a value to the pie.
343 343 */
344 344
345 345 /*!
346 346 \qmlmethod bool PieSeries::remove(PieSlice slice)
347 347 Removes the \a slice from the pie. Returns true if the removal was successfull, false otherwise.
348 348 */
349 349
350 350 /*!
351 351 \qmlmethod PieSeries::clear()
352 352 Removes all slices from the pie.
353 353 */
354 354
355 355 /*!
356 356 Constructs a series object which is a child of \a parent.
357 357 */
358 358 QPieSeries::QPieSeries(QObject *parent) :
359 359 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
360 360 {
361 361
362 362 }
363 363
364 364 /*!
365 365 Destroys the series and its slices.
366 366 */
367 367 QPieSeries::~QPieSeries()
368 368 {
369 369 // NOTE: d_prt destroyed by QObject
370 370 }
371 371
372 372 /*!
373 373 Returns QChartSeries::SeriesTypePie.
374 374 */
375 375 QAbstractSeries::SeriesType QPieSeries::type() const
376 376 {
377 377 return QAbstractSeries::SeriesTypePie;
378 378 }
379 379
380 380 /*!
381 381 Appends a single \a slice to the series.
382 382 Slice ownership is passed to the series.
383 383
384 384 Returns true if append was succesfull.
385 385 */
386 386 bool QPieSeries::append(QPieSlice* slice)
387 387 {
388 388 return append(QList<QPieSlice*>() << slice);
389 389 }
390 390
391 391 /*!
392 392 Appends an array of \a slices to the series.
393 393 Slice ownership is passed to the series.
394 394
395 395 Returns true if append was successfull.
396 396 */
397 397 bool QPieSeries::append(QList<QPieSlice*> slices)
398 398 {
399 399 Q_D(QPieSeries);
400 400
401 401 if (slices.count() == 0)
402 402 return false;
403 403
404 404 foreach (QPieSlice* s, slices) {
405 405 if (!s || d->m_slices.contains(s))
406 406 return false;
407 407 if (s->series()) // already added to some series
408 408 return false;
409 409 }
410 410
411 411 foreach (QPieSlice* s, slices) {
412 412 s->setParent(this);
413 413 QPieSlicePrivate::fromSlice(s)->m_series = this;
414 414 d->m_slices << s;
415 415 }
416 416
417 417 d->updateDerivativeData();
418 418
419 419 foreach (QPieSlice* s, slices) {
420 420 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
421 421 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
422 422 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
423 423 }
424 424
425 425 emit added(slices);
426 426 emit countChanged();
427 427
428 428 return true;
429 429 }
430 430
431 431 /*!
432 432 Appends a single \a slice to the series and returns a reference to the series.
433 433 Slice ownership is passed to the series.
434 434 */
435 435 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
436 436 {
437 437 append(slice);
438 438 return *this;
439 439 }
440 440
441 441
442 442 /*!
443 443 Appends a single slice to the series with give \a value and \a label.
444 444 Slice ownership is passed to the series.
445 445 */
446 446 QPieSlice* QPieSeries::append(QString label, qreal value)
447 447 {
448 448 QPieSlice* slice = new QPieSlice(label, value);
449 449 append(slice);
450 450 return slice;
451 451 }
452 452
453 453 /*!
454 454 Inserts a single \a slice to the series before the slice at \a index position.
455 455 Slice ownership is passed to the series.
456 456
457 457 Returns true if insert was successfull.
458 458 */
459 459 bool QPieSeries::insert(int index, QPieSlice* slice)
460 460 {
461 461 Q_D(QPieSeries);
462 462
463 463 if (index < 0 || index > d->m_slices.count())
464 464 return false;
465 465
466 466 if (!slice || d->m_slices.contains(slice))
467 467 return false;
468 468
469 469 if (slice->series()) // already added to some series
470 470 return false;
471 471
472 472 slice->setParent(this);
473 473 QPieSlicePrivate::fromSlice(slice)->m_series = this;
474 474 d->m_slices.insert(index, slice);
475 475
476 476 d->updateDerivativeData();
477 477
478 478 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
479 479 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
480 480 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
481 481
482 482 emit added(QList<QPieSlice*>() << slice);
483 483 emit countChanged();
484 484
485 485 return true;
486 486 }
487 487
488 488 /*!
489 489 Removes a single \a slice from the series and deletes the slice.
490 490
491 491 Do not reference the pointer after this call.
492 492
493 493 Returns true if remove was successfull.
494 494 */
495 495 bool QPieSeries::remove(QPieSlice* slice)
496 496 {
497 497 Q_D(QPieSeries);
498 498
499 499 if (!d->m_slices.removeOne(slice))
500 500 return false;
501 501
502 502 d->updateDerivativeData();
503 503
504 504 emit removed(QList<QPieSlice*>() << slice);
505 505 emit countChanged();
506 506
507 507 delete slice;
508 508 slice = 0;
509 509
510 510 return true;
511 511 }
512 512
513 513 /*!
514 514 Clears all slices from the series.
515 515 */
516 516 void QPieSeries::clear()
517 517 {
518 518 Q_D(QPieSeries);
519 519 if (d->m_slices.count() == 0)
520 520 return;
521 521
522 522 QList<QPieSlice*> slices = d->m_slices;
523 523 foreach (QPieSlice* s, d->m_slices) {
524 524 d->m_slices.removeOne(s);
525 525 delete s;
526 526 }
527 527
528 528 d->updateDerivativeData();
529 529
530 530 emit removed(slices);
531 531 emit countChanged();
532 532 }
533 533
534 534 /*!
535 535 Returns a list of slices that belong to this series.
536 536 */
537 537 QList<QPieSlice*> QPieSeries::slices() const
538 538 {
539 539 Q_D(const QPieSeries);
540 540 return d->m_slices;
541 541 }
542 542
543 543 /*!
544 544 returns the number of the slices in this series.
545 545 */
546 546 int QPieSeries::count() const
547 547 {
548 548 Q_D(const QPieSeries);
549 549 return d->m_slices.count();
550 550 }
551 551
552 552 /*!
553 553 Returns true is the series is empty.
554 554 */
555 555 bool QPieSeries::isEmpty() const
556 556 {
557 557 Q_D(const QPieSeries);
558 558 return d->m_slices.isEmpty();
559 559 }
560 560
561 561 /*!
562 562 Returns the sum of all slice values in this series.
563 563
564 564 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
565 565 */
566 566 qreal QPieSeries::sum() const
567 567 {
568 568 Q_D(const QPieSeries);
569 569 return d->m_sum;
570 570 }
571 571
572 572 void QPieSeries::setDonut(bool donut)
573 573 {
574 574 Q_D(QPieSeries);
575 575 d->m_donutChart = donut;
576 576 d->updateDerivativeData();
577 577 }
578 578
579 579 bool QPieSeries::donut() const
580 580 {
581 581 Q_D(const QPieSeries);
582 582 return d->m_donutChart;
583 583 }
584 584
585 585 void QPieSeries::setDonutInnerSize(qreal innerSize)
586 586 {
587 587 Q_D(QPieSeries);
588 innerSize = qBound(0.0, innerSize, 1.0);
588 innerSize = qBound((qreal)0.0, innerSize, (qreal)1.0);
589 589 d->setSizes(innerSize, qMax(d->m_pieRelativeSize, innerSize));
590 590 }
591 591
592 592 qreal QPieSeries::donutInnerSize() const
593 593 {
594 594 Q_D(const QPieSeries);
595 595 return d->m_donutRelativeInnerSize;
596 596 }
597 597
598 598 void QPieSeries::setHorizontalPosition(qreal relativePosition)
599 599 {
600 600 Q_D(QPieSeries);
601 601
602 602 if (relativePosition < 0.0)
603 603 relativePosition = 0.0;
604 604 if (relativePosition > 1.0)
605 605 relativePosition = 1.0;
606 606
607 607 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) {
608 608 d->m_pieRelativeHorPos = relativePosition;
609 609 emit d->horizontalPositionChanged();
610 610 }
611 611 }
612 612
613 613 qreal QPieSeries::horizontalPosition() const
614 614 {
615 615 Q_D(const QPieSeries);
616 616 return d->m_pieRelativeHorPos;
617 617 }
618 618
619 619 void QPieSeries::setVerticalPosition(qreal relativePosition)
620 620 {
621 621 Q_D(QPieSeries);
622 622
623 623 if (relativePosition < 0.0)
624 624 relativePosition = 0.0;
625 625 if (relativePosition > 1.0)
626 626 relativePosition = 1.0;
627 627
628 628 if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) {
629 629 d->m_pieRelativeVerPos = relativePosition;
630 630 emit d->verticalPositionChanged();
631 631 }
632 632 }
633 633
634 634 qreal QPieSeries::verticalPosition() const
635 635 {
636 636 Q_D(const QPieSeries);
637 637 return d->m_pieRelativeVerPos;
638 638 }
639 639
640 640 void QPieSeries::setPieSize(qreal relativeSize)
641 641 {
642 642 Q_D(QPieSeries);
643 relativeSize = qBound(0.0, relativeSize, 1.0);
643 relativeSize = qBound((qreal)0.0, relativeSize, (qreal)1.0);
644 644 d->setSizes(qMin(d->m_donutRelativeInnerSize, relativeSize), relativeSize);
645 645
646 646 }
647 647
648 648 qreal QPieSeries::pieSize() const
649 649 {
650 650 Q_D(const QPieSeries);
651 651 return d->m_pieRelativeSize;
652 652 }
653 653
654 654
655 655 void QPieSeries::setPieStartAngle(qreal angle)
656 656 {
657 657 Q_D(QPieSeries);
658 658 if (qFuzzyIsNull(d->m_pieStartAngle - angle))
659 659 return;
660 660 d->m_pieStartAngle = angle;
661 661 d->updateDerivativeData();
662 662 emit d->pieStartAngleChanged();
663 663 }
664 664
665 665 qreal QPieSeries::pieStartAngle() const
666 666 {
667 667 Q_D(const QPieSeries);
668 668 return d->m_pieStartAngle;
669 669 }
670 670
671 671 /*!
672 672 Sets the end angle of the pie.
673 673
674 674 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
675 675
676 676 \a angle must be greater than start angle.
677 677
678 678 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
679 679 */
680 680 void QPieSeries::setPieEndAngle(qreal angle)
681 681 {
682 682 Q_D(QPieSeries);
683 683 if (qFuzzyIsNull(d->m_pieEndAngle - angle))
684 684 return;
685 685 d->m_pieEndAngle = angle;
686 686 d->updateDerivativeData();
687 687 emit d->pieEndAngleChanged();
688 688 }
689 689
690 690 /*!
691 691 Returns the end angle of the pie.
692 692
693 693 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
694 694
695 695 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
696 696 */
697 697 qreal QPieSeries::pieEndAngle() const
698 698 {
699 699 Q_D(const QPieSeries);
700 700 return d->m_pieEndAngle;
701 701 }
702 702
703 703 /*!
704 704 Sets the all the slice labels \a visible or invisible.
705 705
706 706 Note that this affects only the current slices in the series.
707 707 If user adds a new slice the default label visibility is false.
708 708
709 709 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
710 710 */
711 711 void QPieSeries::setLabelsVisible(bool visible)
712 712 {
713 713 Q_D(QPieSeries);
714 714 foreach (QPieSlice* s, d->m_slices)
715 715 s->setLabelVisible(visible);
716 716 }
717 717
718 718 /*!
719 719 Sets the all the slice labels \a position
720 720
721 721 Note that this affects only the current slices in the series.
722 722 If user adds a new slice the default label position is LabelOutside
723 723
724 724 \sa QPieSlice::labelPosition(), QPieSlice::setLabelPosition()
725 725 */
726 726 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
727 727 {
728 728 Q_D(QPieSeries);
729 729 foreach (QPieSlice* s, d->m_slices)
730 730 s->setLabelPosition(position);
731 731 }
732 732
733 733 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
734 734
735 735
736 736 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
737 737 QAbstractSeriesPrivate(parent),
738 738 m_pieRelativeHorPos(0.5),
739 739 m_pieRelativeVerPos(0.5),
740 740 m_pieRelativeSize(0.7),
741 741 m_pieStartAngle(0),
742 742 m_pieEndAngle(360),
743 743 m_sum(0),
744 744 m_donutChart(false),
745 745 m_donutRelativeInnerSize(0.5)
746 746 {
747 747 }
748 748
749 749 QPieSeriesPrivate::~QPieSeriesPrivate()
750 750 {
751 751 }
752 752
753 753 void QPieSeriesPrivate::updateDerivativeData()
754 754 {
755 755 // calculate sum of all slices
756 756 qreal sum = 0;
757 757 foreach (QPieSlice* s, m_slices)
758 758 sum += s->value();
759 759
760 760 if (!qFuzzyIsNull(m_sum - sum)) {
761 761 m_sum = sum;
762 762 emit q_func()->sumChanged();
763 763 }
764 764
765 765 // nothing to show..
766 766 if (qFuzzyIsNull(m_sum))
767 767 return;
768 768
769 769 // update slice attributes
770 770 qreal sliceAngle = m_pieStartAngle;
771 771 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
772 772 QVector<QPieSlice*> changed;
773 773 foreach (QPieSlice* s, m_slices) {
774 774 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
775 775 d->setPercentage(s->value() / m_sum);
776 776 d->setStartAngle(sliceAngle);
777 777 d->setAngleSpan(pieSpan * s->percentage());
778 778 sliceAngle += s->angleSpan();
779 779 }
780 780
781 781
782 782 emit calculatedDataChanged();
783 783 }
784 784
785 785 void QPieSeriesPrivate::setSizes(qreal innerSize, qreal outerSize)
786 786 {
787 787 bool changed = false;
788 788
789 789 if (!qFuzzyIsNull(m_donutRelativeInnerSize - innerSize)) {
790 790 m_donutRelativeInnerSize = innerSize;
791 791 changed = true;
792 792 }
793 793
794 794 if (!qFuzzyIsNull(m_pieRelativeSize - outerSize)) {
795 795 m_pieRelativeSize = outerSize;
796 796 changed = true;
797 797 }
798 798
799 799 if (changed)
800 800 emit pieSizeChanged();
801 801 }
802 802
803 803 QPieSeriesPrivate* QPieSeriesPrivate::fromSeries(QPieSeries *series)
804 804 {
805 805 return series->d_func();
806 806 }
807 807
808 808 void QPieSeriesPrivate::sliceValueChanged()
809 809 {
810 810 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
811 811 updateDerivativeData();
812 812 }
813 813
814 814 void QPieSeriesPrivate::sliceClicked()
815 815 {
816 816 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
817 817 Q_ASSERT(m_slices.contains(slice));
818 818 Q_Q(QPieSeries);
819 819 emit q->clicked(slice);
820 820 }
821 821
822 822 void QPieSeriesPrivate::sliceHovered(bool state)
823 823 {
824 824 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
825 825 Q_ASSERT(m_slices.contains(slice));
826 826 Q_Q(QPieSeries);
827 827 emit q->hovered(slice, state);
828 828 }
829 829
830 830 void QPieSeriesPrivate::scaleDomain(Domain& domain)
831 831 {
832 832 Q_UNUSED(domain);
833 833 // does not apply to pie
834 834 }
835 835
836 836 ChartElement* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
837 837 {
838 838 Q_Q(QPieSeries);
839 839 PieChartItem* pie = new PieChartItem(q,presenter);
840 840 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
841 841 pie->setAnimation(new PieAnimation(pie));
842 842 }
843 843 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
844 844 return pie;
845 845 }
846 846
847 847 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
848 848 {
849 849 Q_Q(QPieSeries);
850 850 QList<LegendMarker*> markers;
851 851 foreach(QPieSlice* slice, q->slices()) {
852 852 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
853 853 markers << marker;
854 854 }
855 855 return markers;
856 856 }
857 857
858 858 void QPieSeriesPrivate::initializeAxis(QAbstractAxis* axis)
859 859 {
860 860 Q_UNUSED(axis);
861 861 }
862 862
863 863 QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
864 864 {
865 865 Q_UNUSED(orientation);
866 866 return QAbstractAxis::AxisTypeNoAxis;
867 867 }
868 868
869 869 #include "moc_qpieseries.cpp"
870 870 #include "moc_qpieseries_p.cpp"
871 871
872 872 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,299 +1,304
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 "../qabstractaxis/tst_qabstractaxis.h"
22 22 #include "qintervalsaxis.h"
23 23 #include <qlineseries.h>
24 24
25 25 class tst_QIntervalsAxis: public tst_QAbstractAxis
26 26 {
27 27 Q_OBJECT
28 28
29 29 public slots:
30 30 void initTestCase();
31 31 void cleanupTestCase();
32 32 void init();
33 33 void cleanup();
34 34
35 35 private slots:
36 36 void qintervalsaxis_data();
37 37 void qintervalsaxis();
38 38
39 39 void max_raw_data();
40 40 void max_raw();
41 41 void max_data();
42 42 void max();
43 43 void max_animation_data();
44 44 void max_animation();
45 45 void min_raw_data();
46 46 void min_raw();
47 47 void min_data();
48 48 void min();
49 49 void min_animation_data();
50 50 void min_animation();
51 51 void range_raw_data();
52 52 void range_raw();
53 53 void range_data();
54 54 void range();
55 55 void range_animation_data();
56 56 void range_animation();
57 57
58 58 void interval_data();
59 59 void interval();
60 60
61 61 private:
62 62 QIntervalsAxis* m_intervalsaxis;
63 63 QLineSeries* m_series;
64 64 };
65 65
66 66 void tst_QIntervalsAxis::initTestCase()
67 67 {
68 68 }
69 69
70 70 void tst_QIntervalsAxis::cleanupTestCase()
71 71 {
72 72 }
73 73
74 74 void tst_QIntervalsAxis::init()
75 75 {
76 76 m_intervalsaxis = new QIntervalsAxis();
77 77 m_series = new QLineSeries();
78 78 *m_series << QPointF(-100, -100) << QPointF(0, 0) << QPointF(100, 100);
79 79 tst_QAbstractAxis::init(m_intervalsaxis, m_series);
80 80 m_chart->addSeries(m_series);
81 81 m_chart->createDefaultAxes();
82 82 }
83 83
84 84 void tst_QIntervalsAxis::cleanup()
85 85 {
86 86 delete m_series;
87 87 delete m_intervalsaxis;
88 88 m_series = 0;
89 89 m_intervalsaxis = 0;
90 90 tst_QAbstractAxis::cleanup();
91 91 }
92 92
93 93 void tst_QIntervalsAxis::qintervalsaxis_data()
94 94 {
95 95 }
96 96
97 97 void tst_QIntervalsAxis::qintervalsaxis()
98 98 {
99 99 qabstractaxis();
100 100
101 101 QVERIFY(qFuzzyIsNull(m_intervalsaxis->max()));
102 102 QVERIFY(qFuzzyIsNull(m_intervalsaxis->min()));
103 103 QCOMPARE(m_intervalsaxis->type(), QAbstractAxis::AxisTypeIntervals);
104 104
105 105 m_chart->setAxisX(m_intervalsaxis, m_series);
106 106 m_view->show();
107 107 QTest::qWaitForWindowShown(m_view);
108 108
109 109 QVERIFY(!qFuzzyIsNull(m_intervalsaxis->max()));
110 110 QVERIFY(!qFuzzyIsNull(m_intervalsaxis->min()));
111 111 }
112 112
113 113 void tst_QIntervalsAxis::max_raw_data()
114 114 {
115 115 QTest::addColumn<qreal>("max");
116 116 QTest::newRow("1.0") << 1.0;
117 117 QTest::newRow("50.0") << 50.0;
118 118 QTest::newRow("101.0") << 101.0;
119 119 }
120 120
121 121 void tst_QIntervalsAxis::max_raw()
122 122 {
123 123 QFETCH(qreal, max);
124 124
125 125 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
126 126 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
127 127 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
128 128
129 129 m_intervalsaxis->setMax(max);
130 130 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Not equal");
131 131
132 132 QCOMPARE(spy0.count(), 1);
133 133 QCOMPARE(spy1.count(), 0);
134 134 QCOMPARE(spy2.count(), 1);
135 135 }
136 136
137 137 void tst_QIntervalsAxis::max_data()
138 138 {
139 139 max_raw_data();
140 140 }
141 141
142 142 void tst_QIntervalsAxis::max()
143 143 {
144 144 m_chart->setAxisX(m_intervalsaxis, m_series);
145 145 m_view->show();
146 146 QTest::qWaitForWindowShown(m_view);
147 147 max_raw();
148 148 }
149 149
150 150 void tst_QIntervalsAxis::max_animation_data()
151 151 {
152 152 max_data();
153 153 }
154 154
155 155 void tst_QIntervalsAxis::max_animation()
156 156 {
157 157 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
158 158 max();
159 159 }
160 160
161 161 void tst_QIntervalsAxis::min_raw_data()
162 162 {
163 163 QTest::addColumn<qreal>("min");
164 164 QTest::newRow("-1.0") << -1.0;
165 165 QTest::newRow("-50.0") << -50.0;
166 166 QTest::newRow("-101.0") << -101.0;
167 167 }
168 168
169 169 void tst_QIntervalsAxis::min_raw()
170 170 {
171 171 QFETCH(qreal, min);
172 172
173 173 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
174 174 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
175 175 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
176 176
177 177 m_intervalsaxis->setMin(min);
178 178 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Not equal");
179 179
180 180 QCOMPARE(spy0.count(), 0);
181 181 QCOMPARE(spy1.count(), 1);
182 182 QCOMPARE(spy2.count(), 1);
183 183 }
184 184
185 185 void tst_QIntervalsAxis::min_data()
186 186 {
187 187 min_raw_data();
188 188 }
189 189
190 190 void tst_QIntervalsAxis::min()
191 191 {
192 192 m_chart->setAxisX(m_intervalsaxis, m_series);
193 193 m_view->show();
194 194 QTest::qWaitForWindowShown(m_view);
195 195 min_raw();
196 196 }
197 197
198 198 void tst_QIntervalsAxis::min_animation_data()
199 199 {
200 200 min_data();
201 201 }
202 202
203 203 void tst_QIntervalsAxis::min_animation()
204 204 {
205 205 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
206 206 min();
207 207 }
208 208
209 209 void tst_QIntervalsAxis::range_raw_data()
210 210 {
211 211 QTest::addColumn<qreal>("min");
212 212 QTest::addColumn<qreal>("max");
213 213 QTest::newRow("1.0 - 101.0") << -1.0 << 101.0;
214 214 QTest::newRow("25.0 - 75.0") << 25.0 << 75.0;
215 215 QTest::newRow("101.0") << 40.0 << 60.0;
216 216 }
217 217
218 218 void tst_QIntervalsAxis::range_raw()
219 219 {
220 220 QFETCH(qreal, min);
221 221 QFETCH(qreal, max);
222 222
223 223 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
224 224 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
225 225 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
226 226
227 227 m_intervalsaxis->setRange(min, max);
228 228 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Min not equal");
229 229 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Max not equal");
230 230
231 231 QCOMPARE(spy0.count(), 1);
232 232 QCOMPARE(spy1.count(), 1);
233 233 QCOMPARE(spy2.count(), 1);
234 234 }
235 235
236 236 void tst_QIntervalsAxis::range_data()
237 237 {
238 238 range_raw_data();
239 239 }
240 240
241 241 void tst_QIntervalsAxis::range()
242 242 {
243 243 m_chart->setAxisX(m_intervalsaxis, m_series);
244 244 m_view->show();
245 245 QTest::qWaitForWindowShown(m_view);
246 246 range_raw();
247 247 }
248 248
249 249 void tst_QIntervalsAxis::range_animation_data()
250 250 {
251 251 range_data();
252 252 }
253 253
254 254 void tst_QIntervalsAxis::range_animation()
255 255 {
256 256 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
257 257 range();
258 258 }
259 259
260 260 void tst_QIntervalsAxis::interval_data()
261 261 {
262 262 //
263 263 }
264 264
265 265 void tst_QIntervalsAxis::interval()
266 266 {
267 267 // append one correct interval
268 268 m_intervalsaxis->append("first", (qreal)45);
269 269 QCOMPARE(m_intervalsaxis->intervalMin("first"), (qreal)0);
270 270 QCOMPARE(m_intervalsaxis->intervalMax("first"), (qreal)45);
271 271
272 272 // append one more correct interval
273 273 m_intervalsaxis->append("second", (qreal)75);
274 274 QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)45);
275 275 QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75);
276 276
277 277 // append one incorrect interval
278 278 m_intervalsaxis->append("third", (qreal)15);
279 279 QCOMPARE(m_intervalsaxis->count(), 2);
280 280 QCOMPARE(m_intervalsaxis->intervalMax(m_intervalsaxis->intervalsLabels().last()), (qreal)75);
281 281 // QCOMPARE(intervalMax("first"), (qreal)75);
282 282
283 283 // append one more correct interval
284 284 m_intervalsaxis->append("third", (qreal)100);
285 285 QCOMPARE(m_intervalsaxis->count(), 3);
286 286 QCOMPARE(m_intervalsaxis->intervalMin("third"), (qreal)75);
287 287 QCOMPARE(m_intervalsaxis->intervalMax("third"), (qreal)100);
288 288
289 289 // remove one interval
290 290 m_intervalsaxis->remove("first");
291 291 QCOMPARE(m_intervalsaxis->count(), 2);
292 292 QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)0); // second interval should extend to firstInterval minimum
293 293 QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75);
294 294
295 // remove one interval
296 m_intervalsaxis->replace("second", "replaced");
297 QCOMPARE(m_intervalsaxis->count(), 2);
298 QCOMPARE(m_intervalsaxis->intervalMin("replaced"), (qreal)0); // second interval should extend to firstInterval minimum
299 QCOMPARE(m_intervalsaxis->intervalMax("replaced"), (qreal)75);
295 300 }
296 301
297 302 QTEST_MAIN(tst_QIntervalsAxis)
298 303 #include "tst_qintervalsaxis.moc"
299 304
General Comments 0
You need to be logged in to leave comments. Login now