##// END OF EJS Templates
Fixes qpieseries warnings
Michal Klocek -
r352:92cf1cc39711
parent child
Show More
@@ -1,538 +1,537
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 119
120 120 To help with the most common user intercation scenarions there some convenience functions. Specifically
121 121 exploding and higlighting:
122 122 \snippet ../example/piechart/main.cpp 2
123 123
124 124 */
125 125
126 126 /*!
127 127 Constructs a series object which is a child of \a parent.
128 128 */
129 129 QPieSeries::QPieSeries(QObject *parent) :
130 130 QChartSeries(parent),
131 131 m_sizeFactor(1.0),
132 132 m_position(PiePositionMaximized),
133 133 m_pieStartAngle(0),
134 134 m_pieAngleSpan(360)
135 135 {
136 136
137 137 }
138 138
139 139 /*!
140 140 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
141 141 */
142 142 QPieSeries::~QPieSeries()
143 143 {
144 144
145 145 }
146 146
147 147 /*!
148 148 Returns QChartSeries::SeriesTypePie.
149 149 */
150 150 QChartSeries::QChartSeriesType QPieSeries::type() const
151 151 {
152 152 return QChartSeries::SeriesTypePie;
153 153 }
154 154
155 155 /*!
156 Sets an array of values to the series.
157 TO BE REMOVED
156 \internal \a data
158 157 */
159 158 bool QPieSeries::setData(QList<qreal> data)
160 159 {
161 160 // TODO: remove this function
162 161 QList<QPieSlice*> slices;
163 162 foreach (qreal value, data)
164 163 slices << new QPieSlice(value, QString::number(value));
165 164 set(slices);
166 165 return true;
167 166 }
168 167
169 168 /*!
170 169 Sets an array of \a slices to the series.
171 170 Slice ownership is passed to the series.
172 171 */
173 172 void QPieSeries::set(QList<QPieSlice*> slices)
174 173 {
175 174 clear();
176 175 add(slices);
177 176 }
178 177
179 178 /*!
180 179 Adds an array of \a slices to the series.
181 180 Slice ownership is passed to the series.
182 181 */
183 182 void QPieSeries::add(QList<QPieSlice*> slices)
184 183 {
185 184 ChangeSet changeSet;
186 185 foreach (QPieSlice* s, slices) {
187 186 s->setParent(this);
188 187 m_slices << s;
189 188 changeSet.appendAdded(s);
190 189 }
191 190
192 191 updateDerivativeData();
193 192
194 193 foreach (QPieSlice* s, slices) {
195 194 connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
196 195 connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
197 196 connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
198 197 connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
199 198 }
200 199
201 200 emit changed(changeSet);
202 201 }
203 202
204 203 /*!
205 204 Adds a single \a slice to the series.
206 205 Slice ownership is passed to the series.
207 206 */
208 207 void QPieSeries::add(QPieSlice* slice)
209 208 {
210 209 add(QList<QPieSlice*>() << slice);
211 210 }
212 211
213 212
214 213 /*!
215 214 Adds a single slice to the series with give \a value and \a name.
216 215 Slice ownership is passed to the series.
217 216 */
218 217 QPieSlice* QPieSeries::add(qreal value, QString name)
219 218 {
220 219 QPieSlice* slice = new QPieSlice(value, name);
221 220 add(slice);
222 221 return slice;
223 222 }
224 223
225 224 /*!
226 225 Removes a single \a slice from the series and deletes the slice.
227 226
228 227 Do not reference this pointer after this call.
229 228 */
230 229 void QPieSeries::remove(QPieSlice* slice)
231 230 {
232 231 if (!m_slices.removeOne(slice)) {
233 232 Q_ASSERT(0); // TODO: how should this be reported?
234 233 return;
235 234 }
236 235
237 236 ChangeSet changeSet;
238 237 changeSet.appendRemoved(slice);
239 238 emit changed(changeSet);
240 239
241 240 delete slice;
242 241 slice = NULL;
243 242
244 243 updateDerivativeData();
245 244 }
246 245
247 246 /*!
248 247 Clears all slices from the series.
249 248 */
250 249 void QPieSeries::clear()
251 250 {
252 251 if (m_slices.count() == 0)
253 252 return;
254 253
255 254 ChangeSet changeSet;
256 255 foreach (QPieSlice* s, m_slices) {
257 256 changeSet.appendRemoved(s);
258 257 m_slices.removeOne(s);
259 258 delete s;
260 259 }
261 260 emit changed(changeSet);
262 261 updateDerivativeData();
263 262 }
264 263
265 264 /*!
266 265 Counts the number of the slices in this series.
267 266 */
268 267 int QPieSeries::count() const
269 268 {
270 269 return m_slices.count();
271 270 }
272 271
273 272 /*!
274 273 Returns a list of slices that belong to this series.
275 274 */
276 275 QList<QPieSlice*> QPieSeries::slices() const
277 276 {
278 277 return m_slices;
279 278 }
280 279
281 280 /*!
282 281 Sets the size \a factor of the pie. 1.0 is the default value.
283 282 Note that the pie will not grow beyond its absolute maximum size.
284 283 In practice its use is to make the pie appear smaller.
285 284 \sa sizeFactor()
286 285 */
287 286 void QPieSeries::setSizeFactor(qreal factor)
288 287 {
289 288 if (factor < 0.0)
290 289 return;
291 290
292 291 if (m_sizeFactor != factor) {
293 292 m_sizeFactor = factor;
294 293 emit sizeFactorChanged();
295 294 }
296 295 }
297 296
298 297 /*!
299 298 Gets the size factor of the pie.
300 299 \sa setSizeFactor()
301 300 */
302 301 qreal QPieSeries::sizeFactor() const
303 302 {
304 303 return m_sizeFactor;
305 304 }
306 305
307 306 /*!
308 307 Sets the \a position of the pie within its bounding rectangle.
309 308 \sa PiePosition, position()
310 309 */
311 310 void QPieSeries::setPosition(PiePosition position)
312 311 {
313 312 if (m_position != position) {
314 313 m_position = position;
315 314 emit positionChanged();
316 315 }
317 316 }
318 317
319 318 /*!
320 319 Gets the position of the pie within its bounding rectangle.
321 320 \sa PiePosition, setPosition()
322 321 */
323 322 QPieSeries::PiePosition QPieSeries::position() const
324 323 {
325 324 return m_position;
326 325 }
327 326
328 327
329 328 /*!
330 329 Sets the \a startAngle and \a angleSpan of this series.
331 330
332 331 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
333 332 */
334 333 void QPieSeries::setSpan(qreal startAngle, qreal angleSpan)
335 334 {
336 335 if (startAngle >= 0 && startAngle < 360 &&
337 336 angleSpan > 0 && angleSpan <= 360) {
338 337 m_pieStartAngle = startAngle;
339 338 m_pieAngleSpan = angleSpan;
340 339 updateDerivativeData();
341 340 }
342 341 }
343 342
344 343 /*!
345 344 Sets the all the slice labels \a visible or invisible.
346 345
347 346 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
348 347 */
349 348 void QPieSeries::setLabelsVisible(bool visible)
350 349 {
351 350 foreach (QPieSlice* s, m_slices)
352 351 s->setLabelVisible(visible);
353 352 }
354 353
355 354 /*!
356 355 Convenience method for exploding a slice when user clicks the pie. Set \a enable to true to
357 356 explode slices by clicking.
358 357
359 358 \sa QPieSlice::isExploded(), QPieSlice::setExploded(), QPieSlice::setExplodeDistance()
360 359 */
361 360 void QPieSeries::enableClickExplodes(bool enable)
362 361 {
363 362 if (enable)
364 363 connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*)));
365 364 else
366 365 disconnect(this, SLOT(toggleExploded(QPieSlice*)));
367 366 }
368 367
369 368 /*!
370 369 Convenience method for highlighting a slice when user hovers over the slice.
371 370 It changes the slice color to be lighter and shows the label of the slice.
372 371 Set \a enable to true to highlight a slice when user hovers on top of it.
373 372
374 373 \sa QPieSlice::isExploded(), QPieSlice::setExploded()
375 374 */
376 375
377 376 void QPieSeries::enableHoverHighlight(bool enable)
378 377 {
379 378 if (enable) {
380 379 connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*)));
381 380 connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*)));
382 381 } else {
383 382 disconnect(this, SLOT(hoverEnter(QPieSlice*)));
384 383 disconnect(this, SLOT(hoverLeave(QPieSlice*)));
385 384 }
386 385 }
387 386
388 387 /*!
389 388 \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet)
390 389
391 390 This signal emitted when something has changed in the series.
392 391 The \a changeSet contains the details of which slices have been added, changed or removed.
393 392
394 393 \sa QPieSeries::ChangeSet, QPieSlice::changed()
395 394 */
396 395
397 396 /*!
398 397 \fn void QPieSeries::clicked(QPieSlice* slice)
399 398
400 399 This signal is emitted when a \a slice has been clicked.
401 400
402 401 \sa QPieSlice::clicked()
403 402 */
404 403
405 404 /*!
406 405 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
407 406
408 407 This signal is emitted when user has hovered over a \a slice.
409 408
410 409 \sa QPieSlice::hoverEnter()
411 410 */
412 411
413 412 /*!
414 413 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
415 414
416 415 This signal is emitted when user has hovered away from a \a slice.
417 416
418 417 \sa QPieSlice::hoverLeave()
419 418 */
420 419
421 420 /*!
422 421 \fn void QPieSeries::sizeFactorChanged()
423 422
424 423 This signal is emitted when size factor has been changed.
425 424
426 425 \sa sizeFactor(), setSizeFactor()
427 426 */
428 427
429 428 /*!
430 429 \fn void QPieSeries::positionChanged()
431 430
432 431 This signal is emitted when position of the pie has been changed.
433 432
434 433 \sa position(), setPosition()
435 434 */
436 435
437 436 void QPieSeries::sliceChanged()
438 437 {
439 438 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
440 439 Q_ASSERT(m_slices.contains(slice));
441 440
442 441 ChangeSet changeSet;
443 442 changeSet.appendChanged(slice);
444 443 emit changed(changeSet);
445 444
446 445 updateDerivativeData();
447 446 }
448 447
449 448 void QPieSeries::sliceClicked()
450 449 {
451 450 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
452 451 Q_ASSERT(m_slices.contains(slice));
453 452 emit clicked(slice);
454 453 }
455 454
456 455 void QPieSeries::sliceHoverEnter()
457 456 {
458 457 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
459 458 Q_ASSERT(m_slices.contains(slice));
460 459 emit hoverEnter(slice);
461 460 }
462 461
463 462 void QPieSeries::sliceHoverLeave()
464 463 {
465 464 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
466 465 Q_ASSERT(m_slices.contains(slice));
467 466 emit hoverLeave(slice);
468 467 }
469 468
470 469 void QPieSeries::toggleExploded(QPieSlice* slice)
471 470 {
472 471 Q_ASSERT(slice);
473 472 slice->setExploded(!slice->isExploded());
474 473 }
475 474
476 475 void QPieSeries::highlightOn(QPieSlice* slice)
477 476 {
478 477 Q_ASSERT(slice);
479 478 QColor c = slice->brush().color().lighter();
480 479 slice->setBrush(c);
481 480 slice->setLabelVisible(true);
482 481 }
483 482
484 483 void QPieSeries::highlightOff(QPieSlice* slice)
485 484 {
486 485 Q_ASSERT(slice);
487 486 QColor c = slice->brush().color().darker(150);
488 487 slice->setBrush(c);
489 488 slice->setLabelVisible(false);
490 489 }
491 490
492 491 void QPieSeries::updateDerivativeData()
493 492 {
494 493 m_total = 0;
495 494
496 495 // nothing to do?
497 496 if (m_slices.count() == 0)
498 497 return;
499 498
500 499 // calculate total
501 500 foreach (QPieSlice* s, m_slices)
502 501 m_total += s->value();
503 502
504 503 // we must have some values
505 504 Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this?
506 505
507 506 // update slice attributes
508 507 qreal sliceAngle = m_pieStartAngle;
509 508 foreach (QPieSlice* s, m_slices) {
510 509
511 510 bool changed = false;
512 511
513 512 qreal percentage = s->value() / m_total;
514 513 if (s->m_percentage != percentage) {
515 514 s->m_percentage = percentage;
516 515 changed = true;
517 516 }
518 517
519 518 qreal sliceSpan = m_pieAngleSpan * percentage;
520 519 if (s->m_angleSpan != sliceSpan) {
521 520 s->m_angleSpan = sliceSpan;
522 521 changed = true;
523 522 }
524 523
525 524 if (s->m_angle != sliceAngle) {
526 525 s->m_angle = sliceAngle;
527 526 changed = true;
528 527 }
529 528 sliceAngle += sliceSpan;
530 529
531 530 if (changed)
532 531 emit s->changed();
533 532 }
534 533 }
535 534
536 535 #include "moc_qpieseries.cpp"
537 536
538 537 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now