##// END OF EJS Templates
Fixed documentation of signals of PieSeries...
Titta Heikkala -
r2797:e373ed9f8f65
parent child
Show More
@@ -1,1002 +1,1012
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtCharts/QPieSeries>
20 20 #include <private/qpieseries_p.h>
21 21 #include <QtCharts/QPieSlice>
22 22 #include <private/qpieslice_p.h>
23 23 #include <private/pieslicedata_p.h>
24 24 #include <private/chartdataset_p.h>
25 25 #include <private/charttheme_p.h>
26 26 #include <QtCharts/QAbstractAxis>
27 27 #include <private/pieanimation_p.h>
28 28 #include <private/charthelpers_p.h>
29 29
30 30 #include <QtCharts/QPieLegendMarker>
31 31
32 32 QT_CHARTS_BEGIN_NAMESPACE
33 33
34 34 /*!
35 35 \class QPieSeries
36 36 \inmodule Qt Charts
37 37 \brief Pie series API for Qt Charts.
38 38
39 39 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
40 40 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
41 41 The actual slice size is determined by that relative value.
42 42
43 43 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0.
44 44 These relate to the actual chart rectangle.
45 45
46 46 By default the pie is defined as a full pie but it can also be a partial pie.
47 47 This can be done by setting a starting angle and angle span to the series.
48 48 Full pie is 360 degrees where 0 is at 12 a'clock.
49 49
50 50 See the \l {PieChart Example} {pie chart example} or \l {DonutChart Example} {donut chart example} to learn how to use QPieSeries.
51 51 \image examples_piechart.png
52 52 \image examples_donutchart.png
53 53 */
54 54 /*!
55 55 \qmltype PieSeries
56 56 \instantiates QPieSeries
57 57 \inqmlmodule QtCharts
58 58
59 59 \inherits AbstractSeries
60 60
61 61 \brief The PieSeries type is used for making pie charts.
62 62
63 63 The following QML shows how to create a simple pie chart.
64 64
65 65 \snippet qmlchart/qml/qmlchart/View1.qml 1
66 66
67 67 \beginfloatleft
68 68 \image examples_qmlchart1.png
69 69 \endfloat
70 70 \clearfloat
71 71 */
72 72
73 73 /*!
74 74 \property QPieSeries::horizontalPosition
75 75 \brief Defines the horizontal position of the pie.
76 76
77 77 The value is a relative value to the chart rectangle where:
78 78
79 79 \list
80 80 \li 0.0 is the absolute left.
81 81 \li 1.0 is the absolute right.
82 82 \endlist
83 83 Default value is 0.5 (center).
84 84 \sa verticalPosition
85 85 */
86 86
87 87 /*!
88 88 \qmlproperty real PieSeries::horizontalPosition
89 89
90 90 Defines the horizontal position of the pie.
91 91
92 92 The value is a relative value to the chart rectangle where:
93 93
94 94 \list
95 95 \li 0.0 is the absolute left.
96 96 \li 1.0 is the absolute right.
97 97 \endlist
98 98 Default value is 0.5 (center).
99 99 \sa verticalPosition
100 100 */
101 101
102 102 /*!
103 103 \property QPieSeries::verticalPosition
104 104 \brief Defines the vertical position of the pie.
105 105
106 106 The value is a relative value to the chart rectangle where:
107 107
108 108 \list
109 109 \li 0.0 is the absolute top.
110 110 \li 1.0 is the absolute bottom.
111 111 \endlist
112 112 Default value is 0.5 (center).
113 113 \sa horizontalPosition
114 114 */
115 115
116 116 /*!
117 117 \qmlproperty real PieSeries::verticalPosition
118 118
119 119 Defines the vertical position of the pie.
120 120
121 121 The value is a relative value to the chart rectangle where:
122 122
123 123 \list
124 124 \li 0.0 is the absolute top.
125 125 \li 1.0 is the absolute bottom.
126 126 \endlist
127 127 Default value is 0.5 (center).
128 128 \sa horizontalPosition
129 129 */
130 130
131 131 /*!
132 132 \property QPieSeries::size
133 133 \brief Defines the pie size.
134 134
135 135 The value is a relative value to the chart rectangle where:
136 136
137 137 \list
138 138 \li 0.0 is the minimum size (pie not drawn).
139 139 \li 1.0 is the maximum size that can fit the chart.
140 140 \endlist
141 141
142 142 When setting this property the holeSize property is adjusted if necessary, to ensure that the hole size is not greater than the outer size.
143 143
144 144 Default value is 0.7.
145 145 */
146 146
147 147 /*!
148 148 \qmlproperty real PieSeries::size
149 149
150 150 Defines the pie size.
151 151
152 152 The value is a relative value to the chart rectangle where:
153 153
154 154 \list
155 155 \li 0.0 is the minimum size (pie not drawn).
156 156 \li 1.0 is the maximum size that can fit the chart.
157 157 \endlist
158 158
159 159 Default value is 0.7.
160 160 */
161 161
162 162 /*!
163 163 \property QPieSeries::holeSize
164 164 \brief Defines the donut hole size.
165 165
166 166 The value is a relative value to the chart rectangle where:
167 167
168 168 \list
169 169 \li 0.0 is the minimum size (full pie drawn, without any hole inside).
170 170 \li 1.0 is the maximum size that can fit the chart. (donut has no width)
171 171 \endlist
172 172
173 173 The value is never greater then size property.
174 174 Default value is 0.0.
175 175 */
176 176
177 177 /*!
178 178 \qmlproperty real PieSeries::holeSize
179 179
180 180 Defines the donut hole size.
181 181
182 182 The value is a relative value to the chart rectangle where:
183 183
184 184 \list
185 185 \li 0.0 is the minimum size (full pie drawn, without any hole inside).
186 186 \li 1.0 is the maximum size that can fit the chart. (donut has no width)
187 187 \endlist
188 188
189 189 When setting this property the size property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
190 190
191 191 Default value is 0.0.
192 192 */
193 193
194 194 /*!
195 195 \property QPieSeries::startAngle
196 196 \brief Defines the starting angle of the pie.
197 197
198 198 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
199 199
200 200 Default is value is 0.
201 201 */
202 202
203 203 /*!
204 204 \qmlproperty real PieSeries::startAngle
205 205
206 206 Defines the starting angle of the pie.
207 207
208 208 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
209 209
210 210 Default is value is 0.
211 211 */
212 212
213 213 /*!
214 214 \property QPieSeries::endAngle
215 215 \brief Defines the ending angle of the pie.
216 216
217 217 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
218 218
219 219 Default is value is 360.
220 220 */
221 221
222 222 /*!
223 223 \qmlproperty real PieSeries::endAngle
224 224
225 225 Defines the ending angle of the pie.
226 226
227 227 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
228 228
229 229 Default is value is 360.
230 230 */
231 231
232 232 /*!
233 233 \property QPieSeries::count
234 234
235 235 Number of slices in the series.
236 236 */
237 237
238 238 /*!
239 239 \qmlproperty int PieSeries::count
240 240
241 241 Number of slices in the series.
242 242 */
243 243
244 244 /*!
245 245 \fn void QPieSeries::countChanged()
246 246 Emitted when the slice count has changed.
247 247 \sa count
248 248 */
249 249 /*!
250 250 \qmlsignal PieSeries::onCountChanged()
251 251 Emitted when the slice count has changed.
252 252 */
253 253
254 254 /*!
255 255 \property QPieSeries::sum
256 256
257 257 Sum of all slices.
258 258
259 259 The series keeps track of the sum of all slices it holds.
260 260 */
261 261
262 262 /*!
263 263 \qmlproperty real PieSeries::sum
264 264
265 265 Sum of all slices.
266 266
267 267 The series keeps track of the sum of all slices it holds.
268 268 */
269 269
270 270 /*!
271 271 \fn void QPieSeries::sumChanged()
272 272 Emitted when the sum of all slices has changed.
273 273 \sa sum
274 274 */
275 275 /*!
276 276 \qmlsignal PieSeries::onSumChanged()
277 277 Emitted when the sum of all slices has changed. This may happen for example if you add or remove slices, or if you
278 278 change value of a slice.
279 279 */
280 280
281 281 /*!
282 282 \fn void QPieSeries::added(QList<QPieSlice*> slices)
283 283
284 284 This signal is emitted when \a slices have been added to the series.
285 285
286 286 \sa append(), insert()
287 287 */
288 288 /*!
289 \qmlsignal PieSeries::onAdded(PieSlice slice)
290 Emitted when \a slice has been added to the series.
289 \qmlsignal PieSeries::onAdded(list<PieSlice> slices)
290 Emitted when \a slices have been added to the series.
291 291 */
292 292
293 293 /*!
294 294 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
295 295 This signal is emitted when \a slices have been removed from the series.
296 296 \sa remove()
297 297 */
298 298 /*!
299 \qmlsignal PieSeries::onRemoved(PieSlice slice)
299 \qmlsignal PieSeries::onRemoved(list<PieSlice> slices)
300 Emitted when \a slices have been removed from the series.
301 */
302
303 /*!
304 \qmlsignal PieSeries::onSliceAdded(PieSlice slice)
305 Emitted when \a slice has been added to the series.
306 */
307
308 /*!
309 \qmlsignal PieSeries::onSliceRemoved(PieSlice slice)
300 310 Emitted when \a slice has been removed from the series.
301 311 */
302 312
303 313 /*!
304 314 \fn void QPieSeries::clicked(QPieSlice *slice)
305 315 This signal is emitted when a \a slice has been clicked.
306 316 \sa QPieSlice::clicked()
307 317 */
308 318 /*!
309 319 \qmlsignal PieSeries::onClicked(PieSlice slice)
310 320 This signal is emitted when a \a slice has been clicked.
311 321 */
312 322
313 323 /*!
314 324 \fn void QPieSeries::pressed(QPieSlice *slice)
315 325 This signal is emitted when a \a slice has been pressed.
316 326 \sa QPieSlice::pressed()
317 327 */
318 328 /*!
319 329 \qmlsignal PieSeries::onPressed(PieSlice slice)
320 330 This signal is emitted when a \a slice has been pressed.
321 331 */
322 332
323 333 /*!
324 334 \fn void QPieSeries::released(QPieSlice *slice)
325 335 This signal is emitted when a \a slice has been released.
326 336 \sa QPieSlice::released()
327 337 */
328 338 /*!
329 339 \qmlsignal PieSeries::onReleased(PieSlice slice)
330 340 This signal is emitted when a \a slice has been released.
331 341 */
332 342
333 343 /*!
334 344 \fn void QPieSeries::doubleClicked(QPieSlice *slice)
335 345 This signal is emitted when a \a slice has been doubleClicked.
336 346 \sa QPieSlice::doubleClicked()
337 347 */
338 348 /*!
339 349 \qmlsignal PieSeries::onDoubleClicked(PieSlice slice)
340 350 This signal is emitted when a \a slice has been doubleClicked.
341 351 */
342 352
343 353 /*!
344 354 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
345 355 This signal is emitted when user has hovered over or away from the \a slice.
346 356 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
347 357 \sa QPieSlice::hovered()
348 358 */
349 359 /*!
350 360 \qmlsignal PieSeries::onHovered(PieSlice slice, bool state)
351 361 This signal is emitted when user has hovered over or away from the \a slice. \a state is true when user has hovered
352 362 over the slice and false when hover has moved away from the slice.
353 363 */
354 364
355 365 /*!
356 366 \qmlmethod PieSlice PieSeries::at(int index)
357 367 Returns slice at \a index. Returns null if the index is not valid.
358 368 */
359 369
360 370 /*!
361 371 \qmlmethod PieSlice PieSeries::find(string label)
362 372 Returns the first slice with \a label. Returns null if the index is not valid.
363 373 */
364 374
365 375 /*!
366 376 \qmlmethod PieSlice PieSeries::append(string label, real value)
367 377 Adds a new slice with \a label and \a value to the pie.
368 378 */
369 379
370 380 /*!
371 381 \qmlmethod bool PieSeries::remove(PieSlice slice)
372 382 Removes the \a slice from the pie. Returns true if the removal was successful, false otherwise.
373 383 */
374 384
375 385 /*!
376 386 \qmlmethod PieSeries::clear()
377 387 Removes all slices from the pie.
378 388 */
379 389
380 390 /*!
381 391 Constructs a series object which is a child of \a parent.
382 392 */
383 393 QPieSeries::QPieSeries(QObject *parent)
384 394 : QAbstractSeries(*new QPieSeriesPrivate(this), parent)
385 395 {
386 396 Q_D(QPieSeries);
387 397 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
388 398 }
389 399
390 400 /*!
391 401 Destroys the series and its slices.
392 402 */
393 403 QPieSeries::~QPieSeries()
394 404 {
395 405 // NOTE: d_prt destroyed by QObject
396 406 clear();
397 407 }
398 408
399 409 /*!
400 410 Returns QAbstractSeries::SeriesTypePie.
401 411 */
402 412 QAbstractSeries::SeriesType QPieSeries::type() const
403 413 {
404 414 return QAbstractSeries::SeriesTypePie;
405 415 }
406 416
407 417 /*!
408 418 Appends a single \a slice to the series.
409 419 Slice ownership is passed to the series.
410 420
411 421 Returns true if append was succesfull.
412 422 */
413 423 bool QPieSeries::append(QPieSlice *slice)
414 424 {
415 425 return append(QList<QPieSlice *>() << slice);
416 426 }
417 427
418 428 /*!
419 429 Appends an array of \a slices to the series.
420 430 Slice ownership is passed to the series.
421 431
422 432 Returns true if append was successful.
423 433 */
424 434 bool QPieSeries::append(QList<QPieSlice *> slices)
425 435 {
426 436 Q_D(QPieSeries);
427 437
428 438 if (slices.count() == 0)
429 439 return false;
430 440
431 441 foreach (QPieSlice *s, slices) {
432 442 if (!s || d->m_slices.contains(s))
433 443 return false;
434 444 if (s->series()) // already added to some series
435 445 return false;
436 446 if (!isValidValue(s->value()))
437 447 return false;
438 448 }
439 449
440 450 foreach (QPieSlice *s, slices) {
441 451 s->setParent(this);
442 452 QPieSlicePrivate::fromSlice(s)->m_series = this;
443 453 d->m_slices << s;
444 454 }
445 455
446 456 d->updateDerivativeData();
447 457
448 458 foreach(QPieSlice * s, slices) {
449 459 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
450 460 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
451 461 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
452 462 connect(s, SIGNAL(pressed()), d, SLOT(slicePressed()));
453 463 connect(s, SIGNAL(released()), d, SLOT(sliceReleased()));
454 464 connect(s, SIGNAL(doubleClicked()), d, SLOT(sliceDoubleClicked()));
455 465 }
456 466
457 467 emit added(slices);
458 468 emit countChanged();
459 469
460 470 return true;
461 471 }
462 472
463 473 /*!
464 474 Appends a single \a slice to the series and returns a reference to the series.
465 475 Slice ownership is passed to the series.
466 476 */
467 477 QPieSeries &QPieSeries::operator << (QPieSlice *slice)
468 478 {
469 479 append(slice);
470 480 return *this;
471 481 }
472 482
473 483
474 484 /*!
475 485 Appends a single slice to the series with give \a value and \a label.
476 486 Slice ownership is passed to the series.
477 487 Returns NULL if value is NaN, Inf or -Inf and no slice is added to the series.
478 488 */
479 489 QPieSlice *QPieSeries::append(QString label, qreal value)
480 490 {
481 491 if (isValidValue(value)) {
482 492 QPieSlice *slice = new QPieSlice(label, value);
483 493 append(slice);
484 494 return slice;
485 495 } else {
486 496 return 0;
487 497 }
488 498 }
489 499
490 500 /*!
491 501 Inserts a single \a slice to the series before the slice at \a index position.
492 502 Slice ownership is passed to the series.
493 503
494 504 Returns true if insert was successful.
495 505 */
496 506 bool QPieSeries::insert(int index, QPieSlice *slice)
497 507 {
498 508 Q_D(QPieSeries);
499 509
500 510 if (index < 0 || index > d->m_slices.count())
501 511 return false;
502 512
503 513 if (!slice || d->m_slices.contains(slice))
504 514 return false;
505 515
506 516 if (slice->series()) // already added to some series
507 517 return false;
508 518
509 519 if (!isValidValue(slice->value()))
510 520 return false;
511 521
512 522 slice->setParent(this);
513 523 QPieSlicePrivate::fromSlice(slice)->m_series = this;
514 524 d->m_slices.insert(index, slice);
515 525
516 526 d->updateDerivativeData();
517 527
518 528 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
519 529 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
520 530 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
521 531 connect(slice, SIGNAL(pressed()), d, SLOT(slicePressed()));
522 532 connect(slice, SIGNAL(released()), d, SLOT(sliceReleased()));
523 533 connect(slice, SIGNAL(doubleClicked()), d, SLOT(sliceDoubleClicked()));
524 534
525 535 emit added(QList<QPieSlice *>() << slice);
526 536 emit countChanged();
527 537
528 538 return true;
529 539 }
530 540
531 541 /*!
532 542 Removes a single \a slice from the series and deletes the slice.
533 543
534 544 Do not reference the pointer after this call.
535 545
536 546 Returns true if remove was successful.
537 547 */
538 548 bool QPieSeries::remove(QPieSlice *slice)
539 549 {
540 550 Q_D(QPieSeries);
541 551
542 552 if (!d->m_slices.removeOne(slice))
543 553 return false;
544 554
545 555 d->updateDerivativeData();
546 556
547 557 emit removed(QList<QPieSlice *>() << slice);
548 558 emit countChanged();
549 559
550 560 delete slice;
551 561 slice = 0;
552 562
553 563 return true;
554 564 }
555 565
556 566 /*!
557 567 Takes a single \a slice from the series. Does not destroy the slice object.
558 568
559 569 \note The series remains as the slice's parent object. You must set the
560 570 parent object to take full ownership.
561 571
562 572 Returns true if take was successful.
563 573 */
564 574 bool QPieSeries::take(QPieSlice *slice)
565 575 {
566 576 Q_D(QPieSeries);
567 577
568 578 if (!d->m_slices.removeOne(slice))
569 579 return false;
570 580
571 581 QPieSlicePrivate::fromSlice(slice)->m_series = 0;
572 582 slice->disconnect(d);
573 583
574 584 d->updateDerivativeData();
575 585
576 586 emit removed(QList<QPieSlice *>() << slice);
577 587 emit countChanged();
578 588
579 589 return true;
580 590 }
581 591
582 592 /*!
583 593 Clears all slices from the series.
584 594 */
585 595 void QPieSeries::clear()
586 596 {
587 597 Q_D(QPieSeries);
588 598 if (d->m_slices.count() == 0)
589 599 return;
590 600
591 601 QList<QPieSlice *> slices = d->m_slices;
592 602 foreach (QPieSlice *s, d->m_slices)
593 603 d->m_slices.removeOne(s);
594 604
595 605 d->updateDerivativeData();
596 606
597 607 emit removed(slices);
598 608 emit countChanged();
599 609
600 610 foreach (QPieSlice *s, slices)
601 611 delete s;
602 612 }
603 613
604 614 /*!
605 615 Returns a list of slices that belong to this series.
606 616 */
607 617 QList<QPieSlice *> QPieSeries::slices() const
608 618 {
609 619 Q_D(const QPieSeries);
610 620 return d->m_slices;
611 621 }
612 622
613 623 /*!
614 624 returns the number of the slices in this series.
615 625 */
616 626 int QPieSeries::count() const
617 627 {
618 628 Q_D(const QPieSeries);
619 629 return d->m_slices.count();
620 630 }
621 631
622 632 /*!
623 633 Returns true is the series is empty.
624 634 */
625 635 bool QPieSeries::isEmpty() const
626 636 {
627 637 Q_D(const QPieSeries);
628 638 return d->m_slices.isEmpty();
629 639 }
630 640
631 641 /*!
632 642 Returns the sum of all slice values in this series.
633 643
634 644 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
635 645 */
636 646 qreal QPieSeries::sum() const
637 647 {
638 648 Q_D(const QPieSeries);
639 649 return d->m_sum;
640 650 }
641 651
642 652 void QPieSeries::setHoleSize(qreal holeSize)
643 653 {
644 654 Q_D(QPieSeries);
645 655 holeSize = qBound((qreal)0.0, holeSize, (qreal)1.0);
646 656 d->setSizes(holeSize, qMax(d->m_pieRelativeSize, holeSize));
647 657 }
648 658
649 659 qreal QPieSeries::holeSize() const
650 660 {
651 661 Q_D(const QPieSeries);
652 662 return d->m_holeRelativeSize;
653 663 }
654 664
655 665 void QPieSeries::setHorizontalPosition(qreal relativePosition)
656 666 {
657 667 Q_D(QPieSeries);
658 668
659 669 if (relativePosition < 0.0)
660 670 relativePosition = 0.0;
661 671 if (relativePosition > 1.0)
662 672 relativePosition = 1.0;
663 673
664 674 if (!qFuzzyCompare(d->m_pieRelativeHorPos, relativePosition)) {
665 675 d->m_pieRelativeHorPos = relativePosition;
666 676 emit d->horizontalPositionChanged();
667 677 }
668 678 }
669 679
670 680 qreal QPieSeries::horizontalPosition() const
671 681 {
672 682 Q_D(const QPieSeries);
673 683 return d->m_pieRelativeHorPos;
674 684 }
675 685
676 686 void QPieSeries::setVerticalPosition(qreal relativePosition)
677 687 {
678 688 Q_D(QPieSeries);
679 689
680 690 if (relativePosition < 0.0)
681 691 relativePosition = 0.0;
682 692 if (relativePosition > 1.0)
683 693 relativePosition = 1.0;
684 694
685 695 if (!qFuzzyCompare(d->m_pieRelativeVerPos, relativePosition)) {
686 696 d->m_pieRelativeVerPos = relativePosition;
687 697 emit d->verticalPositionChanged();
688 698 }
689 699 }
690 700
691 701 qreal QPieSeries::verticalPosition() const
692 702 {
693 703 Q_D(const QPieSeries);
694 704 return d->m_pieRelativeVerPos;
695 705 }
696 706
697 707 void QPieSeries::setPieSize(qreal relativeSize)
698 708 {
699 709 Q_D(QPieSeries);
700 710 relativeSize = qBound((qreal)0.0, relativeSize, (qreal)1.0);
701 711 d->setSizes(qMin(d->m_holeRelativeSize, relativeSize), relativeSize);
702 712
703 713 }
704 714
705 715 qreal QPieSeries::pieSize() const
706 716 {
707 717 Q_D(const QPieSeries);
708 718 return d->m_pieRelativeSize;
709 719 }
710 720
711 721
712 722 void QPieSeries::setPieStartAngle(qreal angle)
713 723 {
714 724 Q_D(QPieSeries);
715 725 if (qFuzzyCompare(d->m_pieStartAngle, angle))
716 726 return;
717 727 d->m_pieStartAngle = angle;
718 728 d->updateDerivativeData();
719 729 emit d->pieStartAngleChanged();
720 730 }
721 731
722 732 qreal QPieSeries::pieStartAngle() const
723 733 {
724 734 Q_D(const QPieSeries);
725 735 return d->m_pieStartAngle;
726 736 }
727 737
728 738 /*!
729 739 Sets the end angle of the pie.
730 740
731 741 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
732 742
733 743 \a angle must be greater than start angle.
734 744
735 745 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
736 746 */
737 747 void QPieSeries::setPieEndAngle(qreal angle)
738 748 {
739 749 Q_D(QPieSeries);
740 750 if (qFuzzyCompare(d->m_pieEndAngle, angle))
741 751 return;
742 752 d->m_pieEndAngle = angle;
743 753 d->updateDerivativeData();
744 754 emit d->pieEndAngleChanged();
745 755 }
746 756
747 757 /*!
748 758 Returns the end angle of the pie.
749 759
750 760 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
751 761
752 762 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
753 763 */
754 764 qreal QPieSeries::pieEndAngle() const
755 765 {
756 766 Q_D(const QPieSeries);
757 767 return d->m_pieEndAngle;
758 768 }
759 769
760 770 /*!
761 771 Sets the all the slice labels \a visible or invisible.
762 772
763 773 Note that this affects only the current slices in the series.
764 774 If user adds a new slice the default label visibility is false.
765 775
766 776 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
767 777 */
768 778 void QPieSeries::setLabelsVisible(bool visible)
769 779 {
770 780 Q_D(QPieSeries);
771 781 foreach (QPieSlice *s, d->m_slices)
772 782 s->setLabelVisible(visible);
773 783 }
774 784
775 785 /*!
776 786 Sets the all the slice labels \a position
777 787
778 788 Note that this affects only the current slices in the series.
779 789 If user adds a new slice the default label position is LabelOutside
780 790
781 791 \sa QPieSlice::labelPosition(), QPieSlice::setLabelPosition()
782 792 */
783 793 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
784 794 {
785 795 Q_D(QPieSeries);
786 796 foreach (QPieSlice *s, d->m_slices)
787 797 s->setLabelPosition(position);
788 798 }
789 799
790 800 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
791 801
792 802
793 803 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
794 804 QAbstractSeriesPrivate(parent),
795 805 m_pieRelativeHorPos(0.5),
796 806 m_pieRelativeVerPos(0.5),
797 807 m_pieRelativeSize(0.7),
798 808 m_pieStartAngle(0),
799 809 m_pieEndAngle(360),
800 810 m_sum(0),
801 811 m_holeRelativeSize(0.0)
802 812 {
803 813 }
804 814
805 815 QPieSeriesPrivate::~QPieSeriesPrivate()
806 816 {
807 817 }
808 818
809 819 void QPieSeriesPrivate::updateDerivativeData()
810 820 {
811 821 // calculate sum of all slices
812 822 qreal sum = 0;
813 823 foreach (QPieSlice *s, m_slices)
814 824 sum += s->value();
815 825
816 826 if (!qFuzzyCompare(m_sum, sum)) {
817 827 m_sum = sum;
818 828 emit q_func()->sumChanged();
819 829 }
820 830
821 831 // nothing to show..
822 832 if (qFuzzyCompare(m_sum, 0))
823 833 return;
824 834
825 835 // update slice attributes
826 836 qreal sliceAngle = m_pieStartAngle;
827 837 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
828 838 QVector<QPieSlice *> changed;
829 839 foreach (QPieSlice *s, m_slices) {
830 840 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
831 841 d->setPercentage(s->value() / m_sum);
832 842 d->setStartAngle(sliceAngle);
833 843 d->setAngleSpan(pieSpan * s->percentage());
834 844 sliceAngle += s->angleSpan();
835 845 }
836 846
837 847
838 848 emit calculatedDataChanged();
839 849 }
840 850
841 851 void QPieSeriesPrivate::setSizes(qreal innerSize, qreal outerSize)
842 852 {
843 853 bool changed = false;
844 854
845 855 if (!qFuzzyCompare(m_holeRelativeSize, innerSize)) {
846 856 m_holeRelativeSize = innerSize;
847 857 changed = true;
848 858 }
849 859
850 860 if (!qFuzzyCompare(m_pieRelativeSize, outerSize)) {
851 861 m_pieRelativeSize = outerSize;
852 862 changed = true;
853 863 }
854 864
855 865 if (changed)
856 866 emit pieSizeChanged();
857 867 }
858 868
859 869 QPieSeriesPrivate *QPieSeriesPrivate::fromSeries(QPieSeries *series)
860 870 {
861 871 return series->d_func();
862 872 }
863 873
864 874 void QPieSeriesPrivate::sliceValueChanged()
865 875 {
866 876 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
867 877 updateDerivativeData();
868 878 }
869 879
870 880 void QPieSeriesPrivate::sliceClicked()
871 881 {
872 882 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
873 883 Q_ASSERT(m_slices.contains(slice));
874 884 Q_Q(QPieSeries);
875 885 emit q->clicked(slice);
876 886 }
877 887
878 888 void QPieSeriesPrivate::sliceHovered(bool state)
879 889 {
880 890 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
881 891 if (!m_slices.isEmpty()) {
882 892 Q_ASSERT(m_slices.contains(slice));
883 893 Q_Q(QPieSeries);
884 894 emit q->hovered(slice, state);
885 895 }
886 896 }
887 897
888 898 void QPieSeriesPrivate::slicePressed()
889 899 {
890 900 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
891 901 Q_ASSERT(m_slices.contains(slice));
892 902 Q_Q(QPieSeries);
893 903 emit q->pressed(slice);
894 904 }
895 905
896 906 void QPieSeriesPrivate::sliceReleased()
897 907 {
898 908 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
899 909 Q_ASSERT(m_slices.contains(slice));
900 910 Q_Q(QPieSeries);
901 911 emit q->released(slice);
902 912 }
903 913
904 914 void QPieSeriesPrivate::sliceDoubleClicked()
905 915 {
906 916 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
907 917 Q_ASSERT(m_slices.contains(slice));
908 918 Q_Q(QPieSeries);
909 919 emit q->doubleClicked(slice);
910 920 }
911 921
912 922 void QPieSeriesPrivate::initializeDomain()
913 923 {
914 924 // does not apply to pie
915 925 }
916 926
917 927 void QPieSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
918 928 {
919 929 Q_Q(QPieSeries);
920 930 PieChartItem *pie = new PieChartItem(q,parent);
921 931 m_item.reset(pie);
922 932 QAbstractSeriesPrivate::initializeGraphics(parent);
923 933 }
924 934
925 935 void QPieSeriesPrivate::initializeAnimations(QtCharts::QChart::AnimationOptions options)
926 936 {
927 937 PieChartItem *item = static_cast<PieChartItem *>(m_item.data());
928 938 Q_ASSERT(item);
929 939 if (item->animation())
930 940 item->animation()->stopAndDestroyLater();
931 941
932 942 if (options.testFlag(QChart::SeriesAnimations))
933 943 item->setAnimation(new PieAnimation(item));
934 944 else
935 945 item->setAnimation(0);
936 946 QAbstractSeriesPrivate::initializeAnimations(options);
937 947 }
938 948
939 949 QList<QLegendMarker*> QPieSeriesPrivate::createLegendMarkers(QLegend* legend)
940 950 {
941 951 Q_Q(QPieSeries);
942 952 QList<QLegendMarker*> markers;
943 953 foreach(QPieSlice* slice, q->slices()) {
944 954 QPieLegendMarker* marker = new QPieLegendMarker(q,slice,legend);
945 955 markers << marker;
946 956 }
947 957 return markers;
948 958 }
949 959
950 960 void QPieSeriesPrivate::initializeAxes()
951 961 {
952 962
953 963 }
954 964
955 965 QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
956 966 {
957 967 Q_UNUSED(orientation);
958 968 return QAbstractAxis::AxisTypeNoAxis;
959 969 }
960 970
961 971 QAbstractAxis* QPieSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
962 972 {
963 973 Q_UNUSED(orientation);
964 974 return 0;
965 975 }
966 976
967 977 void QPieSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
968 978 {
969 979 //Q_Q(QPieSeries);
970 980 //const QList<QColor>& colors = theme->seriesColors();
971 981 const QList<QGradient>& gradients = theme->seriesGradients();
972 982
973 983 for (int i(0); i < m_slices.count(); i++) {
974 984
975 985 QColor penColor = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
976 986
977 987 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
978 988 qreal pos = (qreal)(i + 1) / (qreal) m_slices.count();
979 989 QColor brushColor = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), pos);
980 990
981 991 QPieSlice *s = m_slices.at(i);
982 992 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
983 993
984 994 if (forced || d->m_data.m_slicePen.isThemed())
985 995 d->setPen(penColor, true);
986 996
987 997 if (forced || d->m_data.m_sliceBrush.isThemed())
988 998 d->setBrush(brushColor, true);
989 999
990 1000 if (forced || d->m_data.m_labelBrush.isThemed())
991 1001 d->setLabelBrush(theme->labelBrush().color(), true);
992 1002
993 1003 if (forced || d->m_data.m_labelFont.isThemed())
994 1004 d->setLabelFont(theme->labelFont(), true);
995 1005 }
996 1006 }
997 1007
998 1008
999 1009 #include "moc_qpieseries.cpp"
1000 1010 #include "moc_qpieseries_p.cpp"
1001 1011
1002 1012 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now