##// END OF EJS Templates
Doc fixes for pie
Jani Honkonen -
r809:ca452bc5066a
parent child
Show More
@@ -1,660 +1,693
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 "qpiesliceprivate_p.h"
23 23 #include "qpieseriesprivate_p.h"
24 24 #include <QDebug>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent)
29 29 :QObject(parent),
30 30 q_ptr(parent),
31 31 m_pieRelativeHorPos(0.5),
32 32 m_pieRelativeVerPos(0.5),
33 33 m_pieRelativeSize(0.7),
34 34 m_pieStartAngle(0),
35 35 m_pieEndAngle(360),
36 36 m_total(0),
37 37 m_mapValues(0),
38 38 m_mapLabels(0),
39 39 m_mapOrientation(Qt::Horizontal)
40 40 {
41 41
42 42 }
43 43
44 44 QPieSeriesPrivate::~QPieSeriesPrivate()
45 45 {
46 46
47 47 }
48 48
49 49 void QPieSeriesPrivate::updateDerivativeData()
50 50 {
51 51 m_total = 0;
52 52
53 53 // nothing to do?
54 54 if (m_slices.count() == 0)
55 55 return;
56 56
57 57 // calculate total
58 58 foreach (QPieSlice* s, m_slices)
59 59 m_total += s->value();
60 60
61 61 // nothing to show..
62 62 if (qFuzzyIsNull(m_total))
63 63 return;
64 64
65 65 // update slice attributes
66 66 qreal sliceAngle = m_pieStartAngle;
67 67 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
68 68 QVector<QPieSlice*> changed;
69 69 foreach (QPieSlice* s, m_slices) {
70 70
71 71 PieSliceData data = s->data_ptr()->m_data;
72 72 data.m_percentage = s->value() / m_total;
73 73 data.m_angleSpan = pieSpan * data.m_percentage;
74 74 data.m_startAngle = sliceAngle;
75 75 sliceAngle += data.m_angleSpan;
76 76
77 77 if (s->data_ptr()->m_data != data) {
78 78 s->data_ptr()->m_data = data;
79 79 changed << s;
80 80 }
81 81 }
82 82
83 83 // emit signals
84 84 foreach (QPieSlice* s, changed)
85 85 emit s->data_ptr()->changed();
86 86 }
87 87
88 88 void QPieSeriesPrivate::sliceChanged()
89 89 {
90 90 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
91 91 updateDerivativeData();
92 92 }
93 93
94 94 void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons)
95 95 {
96 96 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
97 97 Q_ASSERT(m_slices.contains(slice));
98 98 Q_Q(QPieSeries);
99 99 emit q->clicked(slice, buttons);
100 100 }
101 101
102 102 void QPieSeriesPrivate::sliceHoverEnter()
103 103 {
104 104 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
105 105 Q_ASSERT(m_slices.contains(slice));
106 106 Q_Q(QPieSeries);
107 107 emit q->hoverEnter(slice);
108 108 }
109 109
110 110 void QPieSeriesPrivate::sliceHoverLeave()
111 111 {
112 112 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
113 113 Q_ASSERT(m_slices.contains(slice));
114 114 Q_Q(QPieSeries);
115 115 emit q->hoverLeave(slice);
116 116 }
117 117
118 118 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
119 119 {
120 120 Q_UNUSED(bottomRight)
121 121 Q_Q(QPieSeries);
122 122
123 123 if (m_mapOrientation == Qt::Vertical)
124 124 {
125 125 // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
126 126 if (topLeft.column() == m_mapValues)
127 127 if (m_mapValues == m_mapLabels)
128 128 {
129 129 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
130 130 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
131 131 }
132 132 else
133 133 {
134 134 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
135 135 }
136 136 else if (topLeft.column() == m_mapLabels)
137 137 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
138 138 }
139 139 else
140 140 {
141 141 // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
142 142 if (topLeft.row() == m_mapValues)
143 143 if (m_mapValues == m_mapLabels)
144 144 {
145 145 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
146 146 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
147 147 }
148 148 else
149 149 {
150 150 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
151 151 }
152 152 else if (topLeft.row() == m_mapLabels)
153 153 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
154 154 }
155 155 }
156 156
157 157 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
158 158 {
159 159 Q_UNUSED(parent)
160 160 Q_UNUSED(end)
161 161 Q_Q(QPieSeries);
162 162
163 163 QPieSlice* newSlice = new QPieSlice;
164 164 newSlice->setLabelVisible(true);
165 165 if (m_mapOrientation == Qt::Vertical)
166 166 {
167 167 newSlice->setValue(q->m_model->data(q->m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
168 168 newSlice->setLabel(q->m_model->data(q->m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
169 169 }
170 170 else
171 171 {
172 172 newSlice->setValue(q->m_model->data(q->m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
173 173 newSlice->setLabel(q->m_model->data(q->m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
174 174 }
175 175
176 176 q->insert(start, newSlice);
177 177 }
178 178
179 179 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
180 180 {
181 181 Q_UNUSED(parent)
182 182 Q_UNUSED(end)
183 183 Q_Q(QPieSeries);
184 184 q->remove(m_slices.at(start));
185 185 }
186 186
187 187
188 188
189 189 /*!
190 190 \class QPieSeries
191 191 \brief Pie series API for QtCommercial Charts
192 192
193 193 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
194 194 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
195 195 The actual slice size is determined by that relative value.
196 196
197 197 By default the pie is defined as a full pie but it can be a partial pie.
198 198 This can be done by setting a starting angle and angle span to the series.
199 199 */
200 200
201 201 /*!
202 202 Constructs a series object which is a child of \a parent.
203 203 */
204 204 QPieSeries::QPieSeries(QObject *parent) :
205 205 QSeries(parent),
206 206 d_ptr(new QPieSeriesPrivate(this))
207 207 {
208 208
209 209 }
210 210
211 211 /*!
212 212 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
213 213 */
214 214 QPieSeries::~QPieSeries()
215 215 {
216 216 // NOTE: d_prt destroyed by QObject
217 217 }
218 218
219 219 /*!
220 220 Returns QChartSeries::SeriesTypePie.
221 221 */
222 222 QSeries::QSeriesType QPieSeries::type() const
223 223 {
224 224 return QSeries::SeriesTypePie;
225 225 }
226 226
227 227 /*!
228 228 Sets an array of \a slices to the series replacing the existing slices.
229 229 Slice ownership is passed to the series.
230 230 */
231 231 void QPieSeries::replace(QList<QPieSlice*> slices)
232 232 {
233 233 clear();
234 234 append(slices);
235 235 }
236 236
237 237 /*!
238 238 Adds an array of \a slices to the series.
239 239 Slice ownership is passed to the series.
240 240 */
241 241 void QPieSeries::append(QList<QPieSlice*> slices)
242 242 {
243 243 Q_D(QPieSeries);
244 244
245 245 foreach (QPieSlice* s, slices) {
246 246 s->setParent(this);
247 247 d->m_slices << s;
248 248 }
249 249
250 250 d->updateDerivativeData();
251 251
252 252 foreach (QPieSlice* s, slices) {
253 253 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
254 254 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
255 255 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
256 256 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
257 257 }
258 258
259 259 emit added(slices);
260 260 }
261 261
262 262 /*!
263 263 Adds a single \a slice to the series.
264 264 Slice ownership is passed to the series.
265 265 */
266 266 void QPieSeries::append(QPieSlice* slice)
267 267 {
268 268 append(QList<QPieSlice*>() << slice);
269 269 }
270 270
271 271 /*!
272 272 Adds a single \a slice to the series and returns a reference to the series.
273 273 Slice ownership is passed to the series.
274 274 */
275 275 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
276 276 {
277 277 append(slice);
278 278 return *this;
279 279 }
280 280
281 281
282 282 /*!
283 Adds a single slice to the series with give \a value and \a name.
283 Appends a single slice to the series with give \a value and \a name.
284 284 Slice ownership is passed to the series.
285 285 */
286 286 QPieSlice* QPieSeries::append(qreal value, QString name)
287 287 {
288 288 QPieSlice* slice = new QPieSlice(value, name);
289 289 append(slice);
290 290 return slice;
291 291 }
292 292
293 void QPieSeries::insert(int i, QPieSlice* slice)
293 /*!
294 Inserts a single \a slice to the series before the slice at \a index position.
295 Slice ownership is passed to the series.
296 */
297 void QPieSeries::insert(int index, QPieSlice* slice)
294 298 {
295 299 Q_D(QPieSeries);
296 Q_ASSERT(i <= d->m_slices.count());
300 Q_ASSERT(index <= d->m_slices.count());
297 301 slice->setParent(this);
298 d->m_slices.insert(i, slice);
302 d->m_slices.insert(index, slice);
299 303
300 304 d->updateDerivativeData();
301 305
302 306 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
303 307 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
304 308 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
305 309 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
306 310
307 311 emit added(QList<QPieSlice*>() << slice);
308 312 }
309 313
310 314 /*!
311 315 Removes a single \a slice from the series and deletes the slice.
312 316
313 317 Do not reference this pointer after this call.
314 318 */
315 319 void QPieSeries::remove(QPieSlice* slice)
316 320 {
317 321 Q_D(QPieSeries);
318 322 if (!d->m_slices.removeOne(slice)) {
319 323 Q_ASSERT(0); // TODO: how should this be reported?
320 324 return;
321 325 }
322 326
323 327 d->updateDerivativeData();
324 328
325 329 emit removed(QList<QPieSlice*>() << slice);
326 330
327 331 delete slice;
328 332 slice = NULL;
329 333 }
330 334
331 335 /*!
332 336 Clears all slices from the series.
333 337 */
334 338 void QPieSeries::clear()
335 339 {
336 340 Q_D(QPieSeries);
337 341 if (d->m_slices.count() == 0)
338 342 return;
339 343
340 344 QList<QPieSlice*> slices = d->m_slices;
341 345 foreach (QPieSlice* s, d->m_slices) {
342 346 d->m_slices.removeOne(s);
343 347 delete s;
344 348 }
345 349
346 350 d->updateDerivativeData();
347 351
348 352 emit removed(slices);
349 353 }
350 354
351 355 /*!
352 356 Counts the number of the slices in this series.
353 357 */
354 358 int QPieSeries::count() const
355 359 {
356 360 Q_D(const QPieSeries);
357 361 return d->m_slices.count();
358 362 }
359 363
360 364 /*!
361 365 Returns true is the series is empty.
362 366 */
363 367 bool QPieSeries::isEmpty() const
364 368 {
365 369 Q_D(const QPieSeries);
366 370 return d->m_slices.isEmpty();
367 371 }
368 372
369 373 /*!
370 374 Returns a list of slices that belong to this series.
371 375 */
372 376 QList<QPieSlice*> QPieSeries::slices() const
373 377 {
374 378 Q_D(const QPieSeries);
375 379 return d->m_slices;
376 380 }
377 381
378 382 /*!
379 383 Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition.
380 384
381 385 The factors are relative to the chart rectangle where:
382 386
383 387 \a relativeHorizontalPosition 0.0 means the absolute left.
384 388 \a relativeHorizontalPosition 1.0 means the absolute right.
385 389 \a relativeVerticalPosition 0.0 means the absolute top.
386 390 \a relativeVerticalPosition 1.0 means the absolute bottom.
387 391
388 392 By default both values are 0.5 which puts the pie in the middle of the chart rectangle.
389 393
390 394 \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize()
391 395 */
392 396 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition)
393 397 {
394 398 Q_D(QPieSeries);
395 399 if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 ||
396 400 relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0)
397 401 return;
398 402
399 403 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativeHorizontalPosition) ||
400 404 !qFuzzyIsNull(d->m_pieRelativeVerPos - relativeVerticalPosition)) {
401 405 d->m_pieRelativeHorPos = relativeHorizontalPosition;
402 406 d->m_pieRelativeVerPos = relativeVerticalPosition;
403 407 emit piePositionChanged();
404 408 }
405 409 }
406 410
407 411 /*!
408 412 Gets the horizontal position of the pie.
409 413
410 414 The returned value is relative to the chart rectangle where:
411 415
412 416 0.0 means the absolute left.
413 417 1.0 means the absolute right.
414 418
415 419 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
416 420
417 421 \sa setPiePosition(), pieVerticalPosition(), setPieSize()
418 422 */
419 423 qreal QPieSeries::pieHorizontalPosition() const
420 424 {
421 425 Q_D(const QPieSeries);
422 426 return d->m_pieRelativeHorPos;
423 427 }
424 428
425 429 /*!
426 430 Gets the vertical position position of the pie.
427 431
428 432 The returned value is relative to the chart rectangle where:
429 433
430 434 0.0 means the absolute top.
431 435 1.0 means the absolute bottom.
432 436
433 437 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
434 438
435 439 \sa setPiePosition(), pieHorizontalPosition(), setPieSize()
436 440 */
437 441 qreal QPieSeries::pieVerticalPosition() const
438 442 {
439 443 Q_D(const QPieSeries);
440 444 return d->m_pieRelativeVerPos;
441 445 }
442 446
443 447 /*!
444 448 Sets the relative size of the pie.
445 449
446 450 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
447 451
448 452 Default value is 0.7.
449 453
450 454 \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
451 455 */
452 456 void QPieSeries::setPieSize(qreal relativeSize)
453 457 {
454 458 Q_D(QPieSeries);
455 459 if (relativeSize < 0.0 || relativeSize > 1.0)
456 460 return;
457 461
458 462 if (!qFuzzyIsNull(d->m_pieRelativeSize- relativeSize)) {
459 463 d->m_pieRelativeSize = relativeSize;
460 464 emit pieSizeChanged();
461 465 }
462 466 }
463 467
464 468 /*!
465 469 Gets the relative size of the pie.
466 470
467 471 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
468 472
469 473 Default value is 0.7.
470 474
471 475 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
472 476 */
473 477 qreal QPieSeries::pieSize() const
474 478 {
475 479 Q_D(const QPieSeries);
476 480 return d->m_pieRelativeSize;
477 481 }
478 482
479 483
480 484 /*!
481 485 Sets the end angle of the pie.
482 486
483 487 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
484 488
485 489 \a angle must be less than pie end angle. Default value is 0.
486 490
487 491 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
488 492 */
489 493 void QPieSeries::setPieStartAngle(qreal angle)
490 494 {
491 495 Q_D(QPieSeries);
492 496
493 497 if (angle < 0 || angle > 360 || angle > d->m_pieEndAngle)
494 498 return;
495 499
496 500 if (!qFuzzyIsNull(angle - d->m_pieStartAngle)) {
497 501 d->m_pieStartAngle = angle;
498 502 d->updateDerivativeData();
499 503 }
500 504 }
501 505
502 506 /*!
503 507 Gets the start angle of the pie.
504 508
505 509 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
506 510
507 511 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
508 512 */
509 513 qreal QPieSeries::pieStartAngle() const
510 514 {
511 515 Q_D(const QPieSeries);
512 516 return d->m_pieStartAngle;
513 517 }
514 518
515 519 /*!
516 520 Sets the end angle of the pie.
517 521
518 522 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
519 523
520 524 \a angle must be greater than start angle.
521 525
522 526 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
523 527 */
524 528 void QPieSeries::setPieEndAngle(qreal angle)
525 529 {
526 530 Q_D(QPieSeries);
527 531
528 532 if (angle < 0 || angle > 360 || angle < d->m_pieStartAngle)
529 533 return;
530 534
531 535 if (!qFuzzyIsNull(angle - d->m_pieEndAngle)) {
532 536 d->m_pieEndAngle = angle;
533 537 d->updateDerivativeData();
534 538 }
535 539 }
536 540
537 541 /*!
538 542 Returns the end angle of the pie.
539 543
540 544 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
541 545
542 546 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
543 547 */
544 548 qreal QPieSeries::pieEndAngle() const
545 549 {
546 550 Q_D(const QPieSeries);
547 551 return d->m_pieEndAngle;
548 552 }
549 553
550 554 /*!
551 555 Sets the all the slice labels \a visible or invisible.
552 556
553 557 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
554 558 */
555 559 void QPieSeries::setLabelsVisible(bool visible)
556 560 {
557 561 Q_D(QPieSeries);
558 562 foreach (QPieSlice* s, d->m_slices)
559 563 s->setLabelVisible(visible);
560 564 }
561 565
562 566 /*!
563 567 Returns the sum of all slice values in this series.
564 568
565 569 \sa QPieSlice::value(), QPieSlice::setValue()
566 570 */
567 571 qreal QPieSeries::total() const
568 572 {
569 573 Q_D(const QPieSeries);
570 574 return d->m_total;
571 575 }
572 576
573 577 /*!
574 \fn void QPieSeries::clicked(QPieSlice* slice)
578 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
575 579
576 This signal is emitted when a \a slice has been clicked.
580 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
577 581
578 582 \sa QPieSlice::clicked()
579 583 */
580 584
581 585 /*!
582 586 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
583 587
584 588 This signal is emitted when user has hovered over a \a slice.
585 589
586 590 \sa QPieSlice::hoverEnter()
587 591 */
588 592
589 593 /*!
590 594 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
591 595
592 596 This signal is emitted when user has hovered away from a \a slice.
593 597
594 598 \sa QPieSlice::hoverLeave()
595 599 */
596 600
601 /*!
602 \fn void QPieSeries::added(QList<QPieSlice*> slices)
603
604 This signal is emitted when \a slices has been added to the series.
605
606 \sa append(), insert()
607 */
608
609 /*!
610 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
611
612 This signal is emitted when \a slices has been removed from the series.
613
614 \sa remove(), clear()
615 */
616
617 /*!
618 \fn void QPieSeries::piePositionChanged()
619
620 This signal is emitted when pie position has changed.
621
622 \sa setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
623 */
624
625 /*!
626 \fn void QPieSeries::pieSizeChanged()
597 627
628 This signal is emitted when pie size has changed.
598 629
630 \sa pieSize(), setPieSize()
631 */
599 632
600 633 bool QPieSeries::setModel(QAbstractItemModel* model)
601 634 {
602 635 Q_D(QPieSeries);
603 636 // disconnect signals from old model
604 637 if(m_model)
605 638 {
606 639 disconnect(m_model, 0, this, 0);
607 640 d->m_mapValues = -1;
608 641 d->m_mapLabels = -1;
609 642 d->m_mapOrientation = Qt::Vertical;
610 643 }
611 644
612 645 // set new model
613 646 if(model)
614 647 {
615 648 m_model = model;
616 649 return true;
617 650 }
618 651 else
619 652 {
620 653 m_model = NULL;
621 654 return false;
622 655 }
623 656 }
624 657
625 658 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
626 659 {
627 660 Q_D(QPieSeries);
628 661
629 662 if (m_model == NULL)
630 663 return;
631 664
632 665 d->m_mapValues = modelValuesLine;
633 666 d->m_mapLabels = modelLabelsLine;
634 667 d->m_mapOrientation = orientation;
635 668
636 669 // connect the signals
637 670 if (d->m_mapOrientation == Qt::Vertical) {
638 671 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
639 672 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
640 673 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
641 674 } else {
642 675 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
643 676 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
644 677 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
645 678 }
646 679
647 680 // create the initial slices set
648 681 if (d->m_mapOrientation == Qt::Vertical) {
649 682 for (int i = 0; i < m_model->rowCount(); i++)
650 683 append(m_model->data(m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
651 684 } else {
652 685 for (int i = 0; i < m_model->columnCount(); i++)
653 686 append(m_model->data(m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
654 687 }
655 688 }
656 689
657 690 #include "moc_qpieseries.cpp"
658 691 #include "moc_qpieseriesprivate_p.cpp"
659 692
660 693 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,100 +1,100
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 #ifndef PIESERIES_H
22 22 #define PIESERIES_H
23 23
24 24 #include <qseries.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27 class QPieSeriesPrivate;
28 28 class QPieSlice;
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
31 31 {
32 32 Q_OBJECT
33 33
34 34 public:
35 35 QPieSeries(QObject *parent = 0);
36 36 virtual ~QPieSeries();
37 37
38 38 public: // from QChartSeries
39 39 QSeriesType type() const;
40 40
41 41 public:
42 42
43 43 // slice setters
44 44 void append(QPieSlice* slice);
45 45 void append(QList<QPieSlice*> slices);
46 void insert(int i, QPieSlice* slice);
46 void insert(int index, QPieSlice* slice);
47 47 void replace(QList<QPieSlice*> slices);
48 48 void remove(QPieSlice* slice);
49 49 void clear();
50 50
51 51 // slice getters
52 52 QList<QPieSlice*> slices() const;
53 53
54 54 // calculated data
55 55 int count() const;
56 56 bool isEmpty() const;
57 57 qreal total() const;
58 58
59 59 // pie customization
60 60 void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition);
61 61 qreal pieHorizontalPosition() const;
62 62 qreal pieVerticalPosition() const;
63 63 void setPieSize(qreal relativeSize);
64 64 qreal pieSize() const;
65 65 void setPieStartAngle(qreal startAngle);
66 66 qreal pieStartAngle() const;
67 67 void setPieEndAngle(qreal endAngle);
68 68 qreal pieEndAngle() const;
69 69
70 70 // convenience function
71 71 QPieSeries& operator << (QPieSlice* slice);
72 72 QPieSlice* append(qreal value, QString name);
73 73 void setLabelsVisible(bool visible = true);
74 74
75 75 // data from model
76 76 bool setModel(QAbstractItemModel* model);
77 77 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
78 78
79 79 Q_SIGNALS:
80 80 void clicked(QPieSlice* slice, Qt::MouseButtons buttons);
81 81 void hoverEnter(QPieSlice* slice);
82 82 void hoverLeave(QPieSlice* slice);
83 83 void added(QList<QPieSlice*> slices);
84 84 void removed(QList<QPieSlice*> slices);
85 85 void piePositionChanged();
86 86 void pieSizeChanged();
87 87
88 88 private:
89 89 QPieSeriesPrivate * const d_ptr;
90 90 Q_DECLARE_PRIVATE(QPieSeries)
91 91 Q_DISABLE_COPY(QPieSeries)
92 92
93 93 public:
94 94 typedef QPieSeriesPrivate * const DataPtr;
95 95 inline DataPtr &data_ptr() { return d_ptr; }
96 96 };
97 97
98 98 QTCOMMERCIALCHART_END_NAMESPACE
99 99
100 100 #endif // PIESERIES_H
@@ -1,433 +1,433
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 "qpiesliceprivate_p.h"
23 23 #include "qpieseries.h"
24 24 #include "qpieseriesprivate_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \class QPieSlice
30 30 \brief Defines a slice in pie series.
31 31
32 32 Holds all the data of a single slice in a QPieSeries and provides the means
33 33 to modify slice data and customize the visual appearance of the slice.
34 34
35 35 It also provides the means to customize user interaction with the slice by
36 36 providing signals for clicking and hover events.
37 37 */
38 38
39 39 /*!
40 40 \property QPieSlice::label
41 41
42 42 Label of the slice.
43 43 */
44 44
45 45 /*!
46 46 \property QPieSlice::value
47 47
48 48 Value of the slice.
49 49 */
50 50
51 51 /*!
52 52 Constructs an empty slice with a \a parent.
53 53
54 54 Note that QPieSeries takes ownership of the slice when it is set/added.
55 55
56 56 \sa QPieSeries::replace(), QPieSeries::append()
57 57 */
58 58 QPieSlice::QPieSlice(QObject *parent)
59 59 :QObject(parent),
60 60 d_ptr(new QPieSlicePrivate(this))
61 61 {
62 62
63 63 }
64 64
65 65 /*!
66 66 Constructs an empty slice with given \a value, \a label and a \a parent.
67 67 Note that QPieSeries takes ownership of the slice when it is set/added.
68 68 \sa QPieSeries::replace(), QPieSeries::append()
69 69 */
70 70 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
71 71 :QObject(parent),
72 72 d_ptr(new QPieSlicePrivate(this))
73 73 {
74 74 Q_D(QPieSlice);
75 75 d->m_data.m_value = value;
76 76 d->m_data.m_labelText = label;
77 77 }
78 78
79 79 /*!
80 80 Destroys the slice.
81 81 User should not delete the slice if it has been added to the series.
82 82 */
83 83 QPieSlice::~QPieSlice()
84 84 {
85 85 delete d_ptr;
86 86 }
87 87
88 88 /*!
89 89 Gets the value of the slice.
90 90 Note that all values in the series
91 91 \sa setValue()
92 92 */
93 93 qreal QPieSlice::value() const
94 94 {
95 95 Q_D(const QPieSlice);
96 96 return d->m_data.m_value;
97 97 }
98 98
99 99 /*!
100 100 Gets the label of the slice.
101 101 \sa setLabel()
102 102 */
103 103 QString QPieSlice::label() const
104 104 {
105 105 Q_D(const QPieSlice);
106 106 return d->m_data.m_labelText;
107 107 }
108 108
109 109 /*!
110 110 Returns true if label is set as visible.
111 111 \sa setLabelVisible()
112 112 */
113 113 bool QPieSlice::isLabelVisible() const
114 114 {
115 115 Q_D(const QPieSlice);
116 116 return d->m_data.m_isLabelVisible;
117 117 }
118 118
119 119 /*!
120 120 Returns true if slice is exloded from the pie.
121 121 \sa setExploded(), setExplodeDistanceFactor()
122 122 */
123 123 bool QPieSlice::isExploded() const
124 124 {
125 125 Q_D(const QPieSlice);
126 126 return d->m_data.m_isExploded;
127 127 }
128 128
129 129 /*!
130 130 Returns the explode distance factor.
131 131
132 132 The factor is relative to pie radius. For example:
133 133 1.0 means the distance is the same as the radius.
134 134 0.5 means the distance is half of the radius.
135 135
136 136 Default value is 0.15.
137 137
138 138 \sa setExplodeDistanceFactor()
139 139 */
140 140 qreal QPieSlice::explodeDistanceFactor() const
141 141 {
142 142 Q_D(const QPieSlice);
143 143 return d->m_data.m_explodeDistanceFactor;
144 144 }
145 145
146 146 /*!
147 147 Returns the percentage of this slice compared to all slices in the same series.
148 148 The returned value ranges from 0 to 1.0.
149 149
150 150 Updated internally after the slice is added to the series.
151 151 */
152 152 qreal QPieSlice::percentage() const
153 153 {
154 154 Q_D(const QPieSlice);
155 155 return d->m_data.m_percentage;
156 156 }
157 157
158 158 /*!
159 159 Returns the starting angle of this slice in the series it belongs to.
160 160
161 161 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
162 162
163 163 Updated internally after the slice is added to the series.
164 164 */
165 165 qreal QPieSlice::startAngle() const
166 166 {
167 167 Q_D(const QPieSlice);
168 168 return d->m_data.m_startAngle;
169 169 }
170 170
171 171 /*!
172 172 Returns the end angle of this slice in the series it belongs to.
173 173
174 174 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
175 175
176 176 Updated internally after the slice is added to the series.
177 177 */
178 178 qreal QPieSlice::endAngle() const
179 179 {
180 180 Q_D(const QPieSlice);
181 181 return d->m_data.m_startAngle + d->m_data.m_angleSpan;
182 182 }
183 183
184 184 /*!
185 185 Returns the pen used to draw this slice.
186 \sa setSlicePen()
186 \sa setPen()
187 187 */
188 188 QPen QPieSlice::pen() const
189 189 {
190 190 Q_D(const QPieSlice);
191 191 return d->m_data.m_slicePen;
192 192 }
193 193
194 194 /*!
195 195 Returns the brush used to draw this slice.
196 \sa setSliceBrush()
196 \sa setBrush()
197 197 */
198 198 QBrush QPieSlice::brush() const
199 199 {
200 200 Q_D(const QPieSlice);
201 201 return d->m_data.m_sliceBrush;
202 202 }
203 203
204 204 /*!
205 205 Returns the pen used to draw the label in this slice.
206 \sa setLabelArmPen()
206 \sa setLabelPen()
207 207 */
208 208 QPen QPieSlice::labelPen() const
209 209 {
210 210 Q_D(const QPieSlice);
211 211 return d->m_data.m_labelPen;
212 212 }
213 213
214 214 /*!
215 215 Returns the font used to draw label in this slice.
216 216 \sa setLabelFont()
217 217 */
218 218 QFont QPieSlice::labelFont() const
219 219 {
220 220 Q_D(const QPieSlice);
221 221 return d->m_data.m_labelFont;
222 222 }
223 223
224 224 /*!
225 225 Gets the label arm length factor.
226 226
227 227 The factor is relative to pie radius. For example:
228 228 1.0 means the length is the same as the radius.
229 229 0.5 means the length is half of the radius.
230 230
231 231 Default value is 0.15
232 232
233 233 \sa setLabelArmLengthFactor()
234 234 */
235 235 qreal QPieSlice::labelArmLengthFactor() const
236 236 {
237 237 Q_D(const QPieSlice);
238 238 return d->m_data.m_labelArmLengthFactor;
239 239 }
240 240
241 241 /*!
242 \fn void QPieSlice::clicked()
242 \fn void QPieSlice::clicked(Qt::MouseButtons buttons)
243 243
244 244 This signal is emitted when user has clicked the slice.
245 245
246 246 \sa QPieSeries::clicked()
247 247 */
248 248
249 249 /*!
250 250 \fn void QPieSlice::hoverEnter()
251 251
252 252 This signal is emitted when user has hovered over the slice.
253 253
254 254 \sa QPieSeries::hoverEnter()
255 255 */
256 256
257 257 /*!
258 258 \fn void QPieSlice::hoverLeave()
259 259
260 260 This signal is emitted when user has hovered away from the slice.
261 261
262 262 \sa QPieSeries::hoverLeave()
263 263 */
264 264
265 265 /*!
266 266 \fn void QPieSlice::changed()
267 267
268 268 This signal emitted when something has changed in the slice.
269 269
270 270 \sa QPieSeries::changed()
271 271 */
272 272
273 273 /*!
274 274 Sets the \a value of this slice.
275 275 \sa value()
276 276 */
277 277 void QPieSlice::setValue(qreal value)
278 278 {
279 279 Q_D(QPieSlice);
280 280 if (!qFuzzyIsNull(d->m_data.m_value - value)) {
281 281 d->m_data.m_value = value;
282 282
283 283 QPieSeries *series = qobject_cast<QPieSeries*>(parent());
284 284 if (series)
285 285 series->data_ptr()->updateDerivativeData(); // will emit changed()
286 286 else
287 287 emit changed();
288 288 }
289 289 }
290 290
291 291 /*!
292 292 Sets the \a label of the slice.
293 293 \sa label()
294 294 */
295 295 void QPieSlice::setLabel(QString label)
296 296 {
297 297 Q_D(QPieSlice);
298 298 if (d->m_data.m_labelText != label) {
299 299 d->m_data.m_labelText = label;
300 300 emit changed();
301 301 }
302 302 }
303 303
304 304 /*!
305 305 Sets the label \a visible in this slice.
306 306 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
307 307 */
308 308 void QPieSlice::setLabelVisible(bool visible)
309 309 {
310 310 Q_D(QPieSlice);
311 311 if (d->m_data.m_isLabelVisible != visible) {
312 312 d->m_data.m_isLabelVisible = visible;
313 313 emit changed();
314 314 }
315 315 }
316 316
317 317 /*!
318 318 Sets this slice \a exploded.
319 319 \sa isExploded(), explodeDistanceFactor()
320 320 */
321 321 void QPieSlice::setExploded(bool exploded)
322 322 {
323 323 Q_D(QPieSlice);
324 324 if (d->m_data.m_isExploded != exploded) {
325 325 d->m_data.m_isExploded = exploded;
326 326 emit changed();
327 327 }
328 328 }
329 329
330 330 /*!
331 331 Sets the explode distance \a factor.
332 332
333 333 The factor is relative to pie radius. For example:
334 334 1.0 means the distance is the same as the radius.
335 335 0.5 means the distance is half of the radius.
336 336
337 337 Default value is 0.15
338 338
339 339 \sa explodeDistanceFactor()
340 340 */
341 341 void QPieSlice::setExplodeDistanceFactor(qreal factor)
342 342 {
343 343 Q_D(QPieSlice);
344 344 if (!qFuzzyIsNull(d->m_data.m_explodeDistanceFactor - factor)) {
345 345 d->m_data.m_explodeDistanceFactor = factor;
346 346 emit changed();
347 347 }
348 348 }
349 349
350 350 /*!
351 351 Sets the \a pen used to draw this slice.
352 352 Note that applying a theme will override this.
353 \sa slicePen()
353 \sa pen()
354 354 */
355 355 void QPieSlice::setPen(const QPen &pen)
356 356 {
357 357 Q_D(QPieSlice);
358 358 if (d->m_data.m_slicePen != pen) {
359 359 d->m_data.m_slicePen = pen;
360 360 d->m_data.m_slicePen.setThemed(false);
361 361 emit changed();
362 362 }
363 363 }
364 364
365 365 /*!
366 366 Sets the \a brush used to draw this slice.
367 367 Note that applying a theme will override this.
368 \sa sliceBrush()
368 \sa brush()
369 369 */
370 370 void QPieSlice::setBrush(const QBrush &brush)
371 371 {
372 372 Q_D(QPieSlice);
373 373 if (d->m_data.m_sliceBrush != brush) {
374 374 d->m_data.m_sliceBrush = brush;
375 375 d->m_data.m_sliceBrush.setThemed(false);
376 376 emit changed();
377 377 }
378 378 }
379 379
380 380 /*!
381 381 Sets the \a pen used to draw the label in this slice.
382 382 Note that applying a theme will override this.
383 \sa labelArmPen()
383 \sa labelPen()
384 384 */
385 385 void QPieSlice::setLabelPen(const QPen &pen)
386 386 {
387 387 Q_D(QPieSlice);
388 388 if (d->m_data.m_labelPen != pen) {
389 389 d->m_data.m_labelPen = pen;
390 390 d->m_data.m_labelPen.setThemed(false);
391 391 emit changed();
392 392 }
393 393 }
394 394
395 395 /*!
396 396 Sets the \a font used to draw the label in this slice.
397 397 Note that applying a theme will override this.
398 398 \sa labelFont()
399 399 */
400 400 void QPieSlice::setLabelFont(const QFont &font)
401 401 {
402 402 Q_D(QPieSlice);
403 403 if (d->m_data.m_labelFont != font) {
404 404 d->m_data.m_labelFont = font;
405 405 d->m_data.m_labelFont.setThemed(false);
406 406 emit changed();
407 407 }
408 408 }
409 409
410 410 /*!
411 411 Sets the label arm length \a factor.
412 412
413 413 The factor is relative to pie radius. For example:
414 414 1.0 means the length is the same as the radius.
415 415 0.5 means the length is half of the radius.
416 416
417 417 Default value is 0.15
418 418
419 419 \sa labelArmLengthFactor()
420 420 */
421 421 void QPieSlice::setLabelArmLengthFactor(qreal factor)
422 422 {
423 423 Q_D(QPieSlice);
424 424 if (!qFuzzyIsNull(d->m_data.m_labelArmLengthFactor - factor)) {
425 425 d->m_data.m_labelArmLengthFactor = factor;
426 426 emit changed();
427 427 }
428 428 }
429 429
430 430 #include "moc_qpieslice.cpp"
431 431 #include "moc_qpiesliceprivate_p.cpp"
432 432
433 433 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,87
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 "qseries.h"
22 22
23 23 /*!
24 24 \class QSeries
25 25 \brief Base class for all QtCommercial Chart series.
26 26 \mainclass
27 27
28 28 Usually you use the series type specific inherited classes instead of the base class.
29 29 \sa QScatterSeries
30 30 */
31 31
32 32 /*!
33 33 \enum QSeries::QSeriesType
34 34
35 35 The type of the series object.
36 36
37 37 \value SeriesTypeLine
38 38 \value SeriesTypeArea
39 39 \value SeriesTypeBar
40 40 \value SeriesTypeStackedBar
41 41 \value SeriesTypePercentBar
42 42 \value SeriesTypePie
43 43 \value SeriesTypeScatter
44 44 \value SeriesTypeSpline
45 45 */
46 46
47 47 /*!
48 48 \fn QSeries::QSeries(QObject *parent)
49 49 \brief Constructs ChartSeries object with \a parent.
50 50 */
51 51
52 52 /*!
53 53 \fn QSeries::~QSeries()
54 54 \brief Virtual destructor for the chart series.
55 55 */
56 56
57 57 /*!
58 58 \fn QSeriesType QSeries::type() const
59 59 \brief The type of the series.
60 60 */
61 61
62 62 /*!
63 63 \fn bool QSeries::setModel(QAbstractItemModel *model)
64 64 \brief Use the \a model to provide data for the series. The model overrides possible user data
65 65 set with QChartSeries type specific data setters. For example if you call both
66 66 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
67 67 used by the series. Returns true if the model is valid for the series.
68 68 */
69 69
70 70 /*!
71 71 \fn void QSeries::setName(QString name)
72 72 \brief Sets a \a name for the series.
73 73
74 74 The name of a series is shown in the legend for QXYSeries.
75 75 \sa QChart::setTitle()
76 \sa QPieSlice::setName()
76 \sa QPieSlice::setLabel()
77 77 \sa QBarSet::setName()
78 78 */
79 79
80 80 /*!
81 81 \fn QString QSeries::name()
82 82 \brief Returns the name of the series.
83 83 */
84 84
85 85 QTCOMMERCIALCHART_BEGIN_NAMESPACE
86 86 #include "moc_qseries.cpp"
87 87 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now