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