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