##// END OF EJS Templates
Update pie doc
Jani Honkonen -
r345:d81114a9429d
parent child
Show More
@@ -1,33 +1,38
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qpieseries.h>
6 6 #include <qpieslice.h>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 int main(int argc, char *argv[])
11 11 {
12 12 QApplication a(argc, argv);
13 13
14 14 QMainWindow window;
15 15
16 16 QChartView* chartView = new QChartView(&window);
17 17
18 18 //! [1]
19 19 QPieSeries *series = new QPieSeries();
20 20 series->add(1, "Slice 1");
21 21 series->add(2, "Slice 2");
22 22 series->add(3, "Slice 3");
23 23 series->add(4, "Slice 4");
24 24 series->add(5, "Slice 5");
25 25 chartView->addSeries(series);
26 26 //! [1]
27 27
28 //! [2]
29 series->enableHoverHighlight(true);
30 series->enableClickExplodes(true);
31 //! [2]
32
28 33 window.setCentralWidget(chartView);
29 34 window.resize(600, 600);
30 35 window.show();
31 36
32 37 return a.exec();
33 38 }
@@ -1,531 +1,536
1 1 #include "qpieseries.h"
2 2 #include "qpieslice.h"
3 3 #include "piepresenter.h"
4 4 #include "pieslice.h"
5 5 #include <QFontMetrics>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10
11 11 /*!
12 12 \class QPieSeries::ChangeSet
13 13 \brief Defines the changes in the series.
14 14
15 15 Contains the changes that have occurred in the series. Lists of added, changed and removed slices.
16 16
17 17 \sa QPieSeries::changed()
18 18 */
19 19
20 20 /*!
21 21 \internal
22 22 */
23 23 void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice)
24 24 {
25 25 if (!m_added.contains(slice))
26 26 m_added << slice;
27 27 }
28 28
29 29 /*!
30 30 \internal
31 31 */
32 32 void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices)
33 33 {
34 34 foreach (QPieSlice* s, slices)
35 35 appendAdded(s);
36 36 }
37 37
38 38 /*!
39 39 \internal
40 40 */
41 41 void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice)
42 42 {
43 43 if (!m_changed.contains(slice))
44 44 m_changed << slice;
45 45 }
46 46
47 47 /*!
48 48 \internal
49 49 */
50 50 void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice)
51 51 {
52 52 if (!m_removed.contains(slice))
53 53 m_removed << slice;
54 54 }
55 55
56 56 /*!
57 57 Returns a list of slices that have been added to the series.
58 58 \sa QPieSeries::changed()
59 59 */
60 60 QList<QPieSlice*> QPieSeries::ChangeSet::added() const
61 61 {
62 62 return m_added;
63 63 }
64 64
65 65 /*!
66 66 Returns a list of slices that have been changed in the series.
67 67 \sa QPieSeries::changed()
68 68 */
69 69 QList<QPieSlice*> QPieSeries::ChangeSet::changed() const
70 70 {
71 71 return m_changed;
72 72 }
73 73
74 74 /*!
75 75 Returns a list of slices that have been removed from the series.
76 76 \sa QPieSeries::changed()
77 77 */
78 78 QList<QPieSlice*> QPieSeries::ChangeSet::removed() const
79 79 {
80 80 return m_removed;
81 81 }
82 82
83 83
84 84 /*!
85 85 Returns true if there are no added/changed or removed slices in the change set.
86 86 */
87 87 bool QPieSeries::ChangeSet::isEmpty() const
88 88 {
89 89 if (m_added.count() || m_changed.count() || m_removed.count())
90 90 return false;
91 91 return true;
92 92 }
93 93
94 94 /*!
95 95 \enum QPieSeries::PiePosition
96 96
97 97 This enum describes pie position within its bounding rectangle
98 98
99 99 \value PiePositionMaximized
100 100 \value PiePositionTopLeft
101 101 \value PiePositionTopRight
102 102 \value PiePositionBottomLeft
103 103 \value PiePositionBottomRight
104 104 */
105 105
106 106 /*!
107 107 \class QPieSeries
108 108 \brief Pie series API for QtCommercial Charts
109 109
110 110 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
111 111 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
112 112 The actual slice size (span) is determined by that relative value.
113 113
114 114 By default the pie is defined as full but it can be a partial pie.
115 115 This can be done by setting a starting angle and angle span to the series.
116 116
117 117 Example on how to create a chart with pie series:
118 118 \snippet ../example/piechart/main.cpp 1
119
120 To help with the most common user intercation scenarions there some convenience functions. Specifically
121 exploding and higlighting:
122 \snippet ../example/piechart/main.cpp 2
123
119 124 */
120 125
121 126 /*!
122 127 Constructs a series object which is a child of \a parent.
123 128 */
124 129 QPieSeries::QPieSeries(QObject *parent) :
125 130 QChartSeries(parent),
126 131 m_sizeFactor(1.0),
127 132 m_position(PiePositionMaximized),
128 133 m_pieStartAngle(0),
129 134 m_pieAngleSpan(360)
130 135 {
131 136
132 137 }
133 138
134 139 /*!
135 140 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
136 141 */
137 142 QPieSeries::~QPieSeries()
138 143 {
139 144
140 145 }
141 146
142 147 /*!
143 148 Returns QChartSeries::SeriesTypePie.
144 149 */
145 150 QChartSeries::QChartSeriesType QPieSeries::type() const
146 151 {
147 152 return QChartSeries::SeriesTypePie;
148 153 }
149 154
150 155 /*!
151 156 Sets an array of values to the series.
152 157 TO BE REMOVED
153 158 */
154 159 bool QPieSeries::setData(QList<qreal> data)
155 160 {
156 161 // TODO: remove this function
157 162 QList<QPieSlice*> slices;
158 163 foreach (qreal value, data)
159 164 slices << new QPieSlice(value, QString::number(value));
160 165 set(slices);
161 166 return true;
162 167 }
163 168
164 169 /*!
165 170 Sets an array of \a slices to the series.
166 171 Slice ownership is passed to the series.
167 172 */
168 173 void QPieSeries::set(QList<QPieSlice*> slices)
169 174 {
170 175 clear();
171 176 add(slices);
172 177 }
173 178
174 179 /*!
175 Adds an array of slices to the series.
180 Adds an array of \a slices to the series.
176 181 Slice ownership is passed to the series.
177 182 */
178 183 void QPieSeries::add(QList<QPieSlice*> slices)
179 184 {
180 185 ChangeSet changeSet;
181 186 foreach (QPieSlice* s, slices) {
182 187 s->setParent(this);
183 188 m_slices << s;
184 189 changeSet.appendAdded(s);
185 190 }
186 191
187 192 updateDerivativeData();
188 193
189 194 foreach (QPieSlice* s, slices) {
190 195 connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
191 196 connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
192 197 connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
193 198 connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
194 199 }
195 200
196 201 emit changed(changeSet);
197 202 }
198 203
199 204 /*!
200 205 Adds a single \a slice to the series.
201 206 Slice ownership is passed to the series.
202 207 */
203 208 void QPieSeries::add(QPieSlice* slice)
204 209 {
205 210 add(QList<QPieSlice*>() << slice);
206 211 }
207 212
208 213
209 214 /*!
210 215 Adds a single slice to the series with give \a value and \a name.
211 216 Slice ownership is passed to the series.
212 217 */
213 218 QPieSlice* QPieSeries::add(qreal value, QString name)
214 219 {
215 220 QPieSlice* slice = new QPieSlice(value, name);
216 221 add(slice);
217 222 return slice;
218 223 }
219 224
220 225 /*!
221 226 Removes a single \a slice from the series and deletes the slice.
222 227
223 228 Do not reference this pointer after this call.
224 229 */
225 230 void QPieSeries::remove(QPieSlice* slice)
226 231 {
227 232 if (!m_slices.removeOne(slice)) {
228 233 Q_ASSERT(0); // TODO: how should this be reported?
229 234 return;
230 235 }
231 236
232 237 ChangeSet changeSet;
233 238 changeSet.appendRemoved(slice);
234 239 emit changed(changeSet);
235 240
236 241 delete slice;
237 242 slice = NULL;
238 243
239 244 updateDerivativeData();
240 245 }
241 246
242 247 /*!
243 248 Clears all slices from the series.
244 249 */
245 250 void QPieSeries::clear()
246 251 {
247 252 if (m_slices.count() == 0)
248 253 return;
249 254
250 255 ChangeSet changeSet;
251 256 foreach (QPieSlice* s, m_slices) {
252 257 changeSet.appendRemoved(s);
253 258 m_slices.removeOne(s);
254 259 delete s;
255 260 }
256 261 emit changed(changeSet);
257 262 updateDerivativeData();
258 263 }
259 264
260 265 /*!
261 266 Counts the number of the slices in this series.
262 267 */
263 268 int QPieSeries::count() const
264 269 {
265 270 return m_slices.count();
266 271 }
267 272
268 273 /*!
269 274 Returns a list of slices that belong to this series.
270 275 */
271 276 QList<QPieSlice*> QPieSeries::slices() const
272 277 {
273 278 return m_slices;
274 279 }
275 280
276 281 /*!
277 282 Sets the size \a factor of the pie. 1.0 is the default value.
278 283 Note that the pie will not grow beyond its absolute maximum size.
279 284 In practice its use is to make the pie appear smaller.
280 285 \sa sizeFactor()
281 286 */
282 287 void QPieSeries::setSizeFactor(qreal factor)
283 288 {
284 289 if (factor < 0.0)
285 290 return;
286 291
287 292 if (m_sizeFactor != factor) {
288 293 m_sizeFactor = factor;
289 294 emit sizeFactorChanged();
290 295 }
291 296 }
292 297
293 298 /*!
294 299 Gets the size factor of the pie.
295 300 \sa setSizeFactor()
296 301 */
297 302 qreal QPieSeries::sizeFactor() const
298 303 {
299 304 return m_sizeFactor;
300 305 }
301 306
302 307 /*!
303 308 Sets the \a position of the pie within its bounding rectangle.
304 309 \sa PiePosition, position()
305 310 */
306 311 void QPieSeries::setPosition(PiePosition position)
307 312 {
308 313 if (m_position != position) {
309 314 m_position = position;
310 315 emit positionChanged();
311 316 }
312 317 }
313 318
314 319 /*!
315 320 Gets the position of the pie within its bounding rectangle.
316 321 \sa PiePosition, setPosition()
317 322 */
318 323 QPieSeries::PiePosition QPieSeries::position() const
319 324 {
320 325 return m_position;
321 326 }
322 327
323 328
324 329 /*!
325 330 Sets the \a startAngle and \a angleSpan of this series.
326 331
327 332 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
328 333 */
329 334 void QPieSeries::setSpan(qreal startAngle, qreal angleSpan)
330 335 {
331 336 if (startAngle >= 0 && startAngle < 360 &&
332 337 angleSpan > 0 && angleSpan <= 360) {
333 338 m_pieStartAngle = startAngle;
334 339 m_pieAngleSpan = angleSpan;
335 340 updateDerivativeData();
336 341 }
337 342 }
338 343
339 344 /*!
340 345 Sets the all the slice labels \a visible or invisible.
341 346
342 347 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
343 348 */
344 349 void QPieSeries::setLabelsVisible(bool visible)
345 350 {
346 351 foreach (QPieSlice* s, m_slices)
347 352 s->setLabelVisible(visible);
348 353 }
349 354
350 355 /*!
351 356 Convenience method for exploding a slice when user clicks the pie.
352 357
353 \sa QPieSlice::isExploded(), QPieSlice::setExploded()
358 \sa QPieSlice::isExploded(), QPieSlice::setExploded(), QPieSlice::setExplodeDistance()
354 359 */
355 360 void QPieSeries::enableClickExplodes(bool enable)
356 361 {
357 362 if (enable)
358 363 connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*)));
359 364 else
360 365 disconnect(this, SLOT(toggleExploded(QPieSlice*)));
361 366 }
362 367
363 368 /*!
364 369 Convenience method for highlighting a slice when user hovers over the slice.
365 370 It changes the slice color to be lighter and shows the label of the slice.
366 371
367 372 \sa QPieSlice::isExploded(), QPieSlice::setExploded()
368 373 */
369 374
370 375 void QPieSeries::enableHoverHighlight(bool enable)
371 376 {
372 377 if (enable) {
373 378 connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*)));
374 379 connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*)));
375 380 } else {
376 381 disconnect(this, SLOT(hoverEnter(QPieSlice*)));
377 382 disconnect(this, SLOT(hoverLeave(QPieSlice*)));
378 383 }
379 384 }
380 385
381 386 /*!
382 387 \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet)
383 388
384 389 This signal emitted when something has changed in the series.
385 390 The \a changeSet contains the details of which slices have been added, changed or removed.
386 391
387 392 \sa QPieSeries::ChangeSet, QPieSlice::changed()
388 393 */
389 394
390 395 /*!
391 396 \fn void QPieSeries::clicked(QPieSlice* slice)
392 397
393 398 This signal is emitted when a \a slice has been clicked.
394 399
395 400 \sa QPieSlice::clicked()
396 401 */
397 402
398 403 /*!
399 404 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
400 405
401 406 This signal is emitted when user has hovered over a \a slice.
402 407
403 408 \sa QPieSlice::hoverEnter()
404 409 */
405 410
406 411 /*!
407 412 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
408 413
409 414 This signal is emitted when user has hovered away from a \a slice.
410 415
411 416 \sa QPieSlice::hoverLeave()
412 417 */
413 418
414 419 /*!
415 420 \fn void QPieSeries::sizeFactorChanged()
416 421
417 422 This signal is emitted when size factor has been changed.
418 423
419 424 \sa sizeFactor(), setSizeFactor()
420 425 */
421 426
422 427 /*!
423 428 \fn void QPieSeries::positionChanged()
424 429
425 430 This signal is emitted when position of the pie has been changed.
426 431
427 432 \sa position(), setPosition()
428 433 */
429 434
430 435 void QPieSeries::sliceChanged()
431 436 {
432 437 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
433 438 Q_ASSERT(m_slices.contains(slice));
434 439
435 440 ChangeSet changeSet;
436 441 changeSet.appendChanged(slice);
437 442 emit changed(changeSet);
438 443
439 444 updateDerivativeData();
440 445 }
441 446
442 447 void QPieSeries::sliceClicked()
443 448 {
444 449 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
445 450 Q_ASSERT(m_slices.contains(slice));
446 451 emit clicked(slice);
447 452 }
448 453
449 454 void QPieSeries::sliceHoverEnter()
450 455 {
451 456 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
452 457 Q_ASSERT(m_slices.contains(slice));
453 458 emit hoverEnter(slice);
454 459 }
455 460
456 461 void QPieSeries::sliceHoverLeave()
457 462 {
458 463 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
459 464 Q_ASSERT(m_slices.contains(slice));
460 465 emit hoverLeave(slice);
461 466 }
462 467
463 468 void QPieSeries::toggleExploded(QPieSlice* slice)
464 469 {
465 470 Q_ASSERT(slice);
466 471 slice->setExploded(!slice->isExploded());
467 472 }
468 473
469 474 void QPieSeries::highlightOn(QPieSlice* slice)
470 475 {
471 476 Q_ASSERT(slice);
472 477 QColor c = slice->brush().color().lighter();
473 478 slice->setBrush(c);
474 479 slice->setLabelVisible(true);
475 480 }
476 481
477 482 void QPieSeries::highlightOff(QPieSlice* slice)
478 483 {
479 484 Q_ASSERT(slice);
480 485 QColor c = slice->brush().color().darker(150);
481 486 slice->setBrush(c);
482 487 slice->setLabelVisible(false);
483 488 }
484 489
485 490 void QPieSeries::updateDerivativeData()
486 491 {
487 492 m_total = 0;
488 493
489 494 // nothing to do?
490 495 if (m_slices.count() == 0)
491 496 return;
492 497
493 498 // calculate total
494 499 foreach (QPieSlice* s, m_slices)
495 500 m_total += s->value();
496 501
497 502 // we must have some values
498 503 Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this?
499 504
500 505 // update slice attributes
501 506 qreal sliceAngle = m_pieStartAngle;
502 507 foreach (QPieSlice* s, m_slices) {
503 508
504 509 bool changed = false;
505 510
506 511 qreal percentage = s->value() / m_total;
507 512 if (s->m_percentage != percentage) {
508 513 s->m_percentage = percentage;
509 514 changed = true;
510 515 }
511 516
512 517 qreal sliceSpan = m_pieAngleSpan * percentage;
513 518 if (s->m_angleSpan != sliceSpan) {
514 519 s->m_angleSpan = sliceSpan;
515 520 changed = true;
516 521 }
517 522
518 523 if (s->m_angle != sliceAngle) {
519 524 s->m_angle = sliceAngle;
520 525 changed = true;
521 526 }
522 527 sliceAngle += sliceSpan;
523 528
524 529 if (changed)
525 530 emit s->changed();
526 531 }
527 532 }
528 533
529 534 #include "moc_qpieseries.cpp"
530 535
531 536 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now