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