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