##// END OF EJS Templates
remove unnecessary stuff from pie series
Jani Honkonen -
r620:51f99c2be375
parent child
Show More
@@ -1,649 +1,565
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
8 /*!
9 \class QPieSeries::ChangeSet
10 \brief Defines the changes in the series.
11
12 Contains the changes that have occurred in the series. Lists of added, changed and removed slices.
13
14 \sa QPieSeries::changed()
15 */
16
17 /*!
18 \internal
19 */
20 void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice)
21 {
22 if (!m_added.contains(slice))
23 m_added << slice;
24 }
25
26 /*!
27 \internal
28 */
29 void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices)
30 {
31 foreach (QPieSlice* s, slices)
32 appendAdded(s);
33 }
34
35 /*!
36 \internal
37 */
38 void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice)
39 {
40 if (!m_changed.contains(slice))
41 m_changed << slice;
42 }
43
44 /*!
45 \internal
46 */
47 void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice)
48 {
49 if (!m_removed.contains(slice))
50 m_removed << slice;
51 }
52
53 /*!
54 Returns a list of slices that have been added to the series.
55 \sa QPieSeries::changed()
56 */
57 QList<QPieSlice*> QPieSeries::ChangeSet::added() const
58 {
59 return m_added;
60 }
61
62 /*!
63 Returns a list of slices that have been changed in the series.
64 \sa QPieSeries::changed()
65 */
66 QList<QPieSlice*> QPieSeries::ChangeSet::changed() const
67 {
68 return m_changed;
69 }
70
71 /*!
72 Returns a list of slices that have been removed from the series.
73 \sa QPieSeries::changed()
74 */
75 QList<QPieSlice*> QPieSeries::ChangeSet::removed() const
76 {
77 return m_removed;
78 }
79
80
81 /*!
82 Returns true if there are no added/changed or removed slices in the change set.
83 */
84 bool QPieSeries::ChangeSet::isEmpty() const
85 {
86 if (m_added.count() || m_changed.count() || m_removed.count())
87 return false;
88 return true;
89 }
90
91 /*!
7 /*!
92 \class QPieSeries
8 \class QPieSeries
93 \brief Pie series API for QtCommercial Charts
9 \brief Pie series API for QtCommercial Charts
94
10
95 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
11 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
96 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
12 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
97 The actual slice size is determined by that relative value.
13 The actual slice size is determined by that relative value.
98
14
99 By default the pie is defined as a full pie but it can be a partial pie.
15 By default the pie is defined as a full pie but it can be a partial pie.
100 This can be done by setting a starting angle and angle span to the series.
16 This can be done by setting a starting angle and angle span to the series.
101 */
17 */
102
18
103 /*!
19 /*!
104 Constructs a series object which is a child of \a parent.
20 Constructs a series object which is a child of \a parent.
105 */
21 */
106 QPieSeries::QPieSeries(QObject *parent) :
22 QPieSeries::QPieSeries(QObject *parent) :
107 QSeries(parent),
23 QSeries(parent),
108 m_pieRelativeHorPos(0.5),
24 m_pieRelativeHorPos(0.5),
109 m_pieRelativeVerPos(0.5),
25 m_pieRelativeVerPos(0.5),
110 m_pieRelativeSize(0.7),
26 m_pieRelativeSize(0.7),
111 m_pieStartAngle(0),
27 m_pieStartAngle(0),
112 m_pieEndAngle(360),
28 m_pieEndAngle(360),
113 m_total(0)
29 m_total(0)
114 {
30 {
115
31
116 }
32 }
117
33
118 /*!
34 /*!
119 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
35 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
120 */
36 */
121 QPieSeries::~QPieSeries()
37 QPieSeries::~QPieSeries()
122 {
38 {
123
39
124 }
40 }
125
41
126 /*!
42 /*!
127 Returns QChartSeries::SeriesTypePie.
43 Returns QChartSeries::SeriesTypePie.
128 */
44 */
129 QSeries::QSeriesType QPieSeries::type() const
45 QSeries::QSeriesType QPieSeries::type() const
130 {
46 {
131 return QSeries::SeriesTypePie;
47 return QSeries::SeriesTypePie;
132 }
48 }
133
49
134 /*!
50 /*!
135 Sets an array of \a slices to the series replacing the existing slices.
51 Sets an array of \a slices to the series replacing the existing slices.
136 Slice ownership is passed to the series.
52 Slice ownership is passed to the series.
137 */
53 */
138 void QPieSeries::replace(QList<QPieSlice*> slices)
54 void QPieSeries::replace(QList<QPieSlice*> slices)
139 {
55 {
140 clear();
56 clear();
141 add(slices);
57 add(slices);
142 }
58 }
143
59
144 /*!
60 /*!
145 Adds an array of \a slices to the series.
61 Adds an array of \a slices to the series.
146 Slice ownership is passed to the series.
62 Slice ownership is passed to the series.
147 */
63 */
148 void QPieSeries::add(QList<QPieSlice*> slices)
64 void QPieSeries::add(QList<QPieSlice*> slices)
149 {
65 {
150 foreach (QPieSlice* s, slices) {
66 foreach (QPieSlice* s, slices) {
151 s->setParent(this);
67 s->setParent(this);
152 m_slices << s;
68 m_slices << s;
153 }
69 }
154
70
155 updateDerivativeData();
71 updateDerivativeData();
156
72
157 foreach (QPieSlice* s, slices) {
73 foreach (QPieSlice* s, slices) {
158 connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
74 connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
159 connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
75 connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
160 connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
76 connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
161 connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
77 connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
162 }
78 }
163
79
164 emit changed();
80 emit changed();
165 }
81 }
166
82
167 /*!
83 /*!
168 Adds a single \a slice to the series.
84 Adds a single \a slice to the series.
169 Slice ownership is passed to the series.
85 Slice ownership is passed to the series.
170 */
86 */
171 void QPieSeries::add(QPieSlice* slice)
87 void QPieSeries::add(QPieSlice* slice)
172 {
88 {
173 add(QList<QPieSlice*>() << slice);
89 add(QList<QPieSlice*>() << slice);
174 }
90 }
175
91
176 /*!
92 /*!
177 Adds a single \a slice to the series and returns a reference to the series.
93 Adds a single \a slice to the series and returns a reference to the series.
178 Slice ownership is passed to the series.
94 Slice ownership is passed to the series.
179 */
95 */
180 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
96 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
181 {
97 {
182 add(slice);
98 add(slice);
183 return *this;
99 return *this;
184 }
100 }
185
101
186
102
187 /*!
103 /*!
188 Adds a single slice to the series with give \a value and \a name.
104 Adds a single slice to the series with give \a value and \a name.
189 Slice ownership is passed to the series.
105 Slice ownership is passed to the series.
190 */
106 */
191 QPieSlice* QPieSeries::add(qreal value, QString name)
107 QPieSlice* QPieSeries::add(qreal value, QString name)
192 {
108 {
193 QPieSlice* slice = new QPieSlice(value, name);
109 QPieSlice* slice = new QPieSlice(value, name);
194 add(slice);
110 add(slice);
195 return slice;
111 return slice;
196 }
112 }
197
113
198 void QPieSeries::insert(int i, QPieSlice* slice)
114 void QPieSeries::insert(int i, QPieSlice* slice)
199 {
115 {
200 Q_ASSERT(i <= m_slices.count());
116 Q_ASSERT(i <= m_slices.count());
201 slice->setParent(this);
117 slice->setParent(this);
202 m_slices.insert(i, slice);
118 m_slices.insert(i, slice);
203
119
204 updateDerivativeData();
120 updateDerivativeData();
205
121
206 connect(slice, SIGNAL(changed()), this, SLOT(sliceChanged()));
122 connect(slice, SIGNAL(changed()), this, SLOT(sliceChanged()));
207 connect(slice, SIGNAL(clicked()), this, SLOT(sliceClicked()));
123 connect(slice, SIGNAL(clicked()), this, SLOT(sliceClicked()));
208 connect(slice, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
124 connect(slice, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
209 connect(slice, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
125 connect(slice, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
210
126
211 emit changed();
127 emit changed();
212 }
128 }
213
129
214 /*!
130 /*!
215 Removes a single \a slice from the series and deletes the slice.
131 Removes a single \a slice from the series and deletes the slice.
216
132
217 Do not reference this pointer after this call.
133 Do not reference this pointer after this call.
218 */
134 */
219 void QPieSeries::remove(QPieSlice* slice)
135 void QPieSeries::remove(QPieSlice* slice)
220 {
136 {
221 if (!m_slices.removeOne(slice)) {
137 if (!m_slices.removeOne(slice)) {
222 Q_ASSERT(0); // TODO: how should this be reported?
138 Q_ASSERT(0); // TODO: how should this be reported?
223 return;
139 return;
224 }
140 }
225 emit changed();
141 emit changed();
226
142
227 updateDerivativeData();
143 updateDerivativeData();
228
144
229 delete slice;
145 delete slice;
230 slice = NULL;
146 slice = NULL;
231 }
147 }
232
148
233 /*!
149 /*!
234 Clears all slices from the series.
150 Clears all slices from the series.
235 */
151 */
236 void QPieSeries::clear()
152 void QPieSeries::clear()
237 {
153 {
238 if (m_slices.count() == 0)
154 if (m_slices.count() == 0)
239 return;
155 return;
240
156
241 foreach (QPieSlice* s, m_slices) {
157 foreach (QPieSlice* s, m_slices) {
242 m_slices.removeOne(s);
158 m_slices.removeOne(s);
243 delete s;
159 delete s;
244 }
160 }
245
161
246 emit changed();
162 emit changed();
247
163
248 updateDerivativeData();
164 updateDerivativeData();
249 }
165 }
250
166
251 /*!
167 /*!
252 Counts the number of the slices in this series.
168 Counts the number of the slices in this series.
253 */
169 */
254 int QPieSeries::count() const
170 int QPieSeries::count() const
255 {
171 {
256 return m_slices.count();
172 return m_slices.count();
257 }
173 }
258
174
259 /*!
175 /*!
260 Returns a list of slices that belong to this series.
176 Returns a list of slices that belong to this series.
261 */
177 */
262 QList<QPieSlice*> QPieSeries::slices() const
178 QList<QPieSlice*> QPieSeries::slices() const
263 {
179 {
264 return m_slices;
180 return m_slices;
265 }
181 }
266
182
267 /*!
183 /*!
268 Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition.
184 Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition.
269
185
270 The factors are relative to the chart rectangle where:
186 The factors are relative to the chart rectangle where:
271
187
272 \a relativeHorizontalPosition 0.0 means the absolute left.
188 \a relativeHorizontalPosition 0.0 means the absolute left.
273 \a relativeHorizontalPosition 1.0 means the absolute right.
189 \a relativeHorizontalPosition 1.0 means the absolute right.
274 \a relativeVerticalPosition 0.0 means the absolute top.
190 \a relativeVerticalPosition 0.0 means the absolute top.
275 \a relativeVerticalPosition 1.0 means the absolute bottom.
191 \a relativeVerticalPosition 1.0 means the absolute bottom.
276
192
277 By default both values are 0.5 which puts the pie in the middle of the chart rectangle.
193 By default both values are 0.5 which puts the pie in the middle of the chart rectangle.
278
194
279 \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize()
195 \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize()
280 */
196 */
281 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition)
197 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition)
282 {
198 {
283 if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 ||
199 if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 ||
284 relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0)
200 relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0)
285 return;
201 return;
286
202
287 if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) {
203 if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) {
288 m_pieRelativeHorPos = relativeHorizontalPosition;
204 m_pieRelativeHorPos = relativeHorizontalPosition;
289 m_pieRelativeVerPos = relativeVerticalPosition;
205 m_pieRelativeVerPos = relativeVerticalPosition;
290 emit changed();
206 emit changed();
291 }
207 }
292 }
208 }
293
209
294 /*!
210 /*!
295 Gets the horizontal position of the pie.
211 Gets the horizontal position of the pie.
296
212
297 The returned value is relative to the chart rectangle where:
213 The returned value is relative to the chart rectangle where:
298
214
299 0.0 means the absolute left.
215 0.0 means the absolute left.
300 1.0 means the absolute right.
216 1.0 means the absolute right.
301
217
302 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
218 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
303
219
304 \sa setPiePosition(), pieVerticalPosition(), setPieSize()
220 \sa setPiePosition(), pieVerticalPosition(), setPieSize()
305 */
221 */
306 qreal QPieSeries::pieHorizontalPosition() const
222 qreal QPieSeries::pieHorizontalPosition() const
307 {
223 {
308 return m_pieRelativeHorPos;
224 return m_pieRelativeHorPos;
309 }
225 }
310
226
311 /*!
227 /*!
312 Gets the vertical position position of the pie.
228 Gets the vertical position position of the pie.
313
229
314 The returned value is relative to the chart rectangle where:
230 The returned value is relative to the chart rectangle where:
315
231
316 0.0 means the absolute top.
232 0.0 means the absolute top.
317 1.0 means the absolute bottom.
233 1.0 means the absolute bottom.
318
234
319 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
235 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
320
236
321 \sa setPiePosition(), pieHorizontalPosition(), setPieSize()
237 \sa setPiePosition(), pieHorizontalPosition(), setPieSize()
322 */
238 */
323 qreal QPieSeries::pieVerticalPosition() const
239 qreal QPieSeries::pieVerticalPosition() const
324 {
240 {
325 return m_pieRelativeVerPos;
241 return m_pieRelativeVerPos;
326 }
242 }
327
243
328 /*!
244 /*!
329 Sets the relative size of the pie.
245 Sets the relative size of the pie.
330
246
331 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
247 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
332
248
333 Default value is 0.7.
249 Default value is 0.7.
334
250
335 \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
251 \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
336 */
252 */
337 void QPieSeries::setPieSize(qreal relativeSize)
253 void QPieSeries::setPieSize(qreal relativeSize)
338 {
254 {
339 if (relativeSize < 0.0 || relativeSize > 1.0)
255 if (relativeSize < 0.0 || relativeSize > 1.0)
340 return;
256 return;
341
257
342 if (m_pieRelativeSize != relativeSize) {
258 if (m_pieRelativeSize != relativeSize) {
343 m_pieRelativeSize = relativeSize;
259 m_pieRelativeSize = relativeSize;
344 emit changed();
260 emit changed();
345 }
261 }
346 }
262 }
347
263
348 /*!
264 /*!
349 Gets the relative size of the pie.
265 Gets the relative size of the pie.
350
266
351 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
267 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
352
268
353 Default value is 0.7.
269 Default value is 0.7.
354
270
355 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
271 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
356 */
272 */
357 qreal QPieSeries::pieSize() const
273 qreal QPieSeries::pieSize() const
358 {
274 {
359 return m_pieRelativeSize;
275 return m_pieRelativeSize;
360 }
276 }
361
277
362
278
363 /*!
279 /*!
364 Sets the end angle of the pie.
280 Sets the end angle of the pie.
365
281
366 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
282 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
367
283
368 \a angle must be less than pie end angle. Default value is 0.
284 \a angle must be less than pie end angle. Default value is 0.
369
285
370 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
286 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
371 */
287 */
372 void QPieSeries::setPieStartAngle(qreal angle)
288 void QPieSeries::setPieStartAngle(qreal angle)
373 {
289 {
374 if (angle >= 0 && angle <= 360 && angle != m_pieStartAngle && angle <= m_pieEndAngle) {
290 if (angle >= 0 && angle <= 360 && angle != m_pieStartAngle && angle <= m_pieEndAngle) {
375 m_pieStartAngle = angle;
291 m_pieStartAngle = angle;
376 updateDerivativeData();
292 updateDerivativeData();
377 }
293 }
378 }
294 }
379
295
380 /*!
296 /*!
381 Gets the start angle of the pie.
297 Gets the start angle of the pie.
382
298
383 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
299 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
384
300
385 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
301 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
386 */
302 */
387 qreal QPieSeries::pieStartAngle() const
303 qreal QPieSeries::pieStartAngle() const
388 {
304 {
389 return m_pieStartAngle;
305 return m_pieStartAngle;
390 }
306 }
391
307
392 /*!
308 /*!
393 Sets the end angle of the pie.
309 Sets the end angle of the pie.
394
310
395 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
311 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
396
312
397 \a angle must be greater than start angle.
313 \a angle must be greater than start angle.
398
314
399 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
315 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
400 */
316 */
401 void QPieSeries::setPieEndAngle(qreal angle)
317 void QPieSeries::setPieEndAngle(qreal angle)
402 {
318 {
403 if (angle >= 0 && angle <= 360 && angle != m_pieEndAngle && angle >= m_pieStartAngle) {
319 if (angle >= 0 && angle <= 360 && angle != m_pieEndAngle && angle >= m_pieStartAngle) {
404 m_pieEndAngle = angle;
320 m_pieEndAngle = angle;
405 updateDerivativeData();
321 updateDerivativeData();
406 }
322 }
407 }
323 }
408
324
409 /*!
325 /*!
410 Returns the end angle of the pie.
326 Returns the end angle of the pie.
411
327
412 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.
413
329
414 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
330 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
415 */
331 */
416 qreal QPieSeries::pieEndAngle() const
332 qreal QPieSeries::pieEndAngle() const
417 {
333 {
418 return m_pieEndAngle;
334 return m_pieEndAngle;
419 }
335 }
420
336
421 /*!
337 /*!
422 Sets the all the slice labels \a visible or invisible.
338 Sets the all the slice labels \a visible or invisible.
423
339
424 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
340 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
425 */
341 */
426 void QPieSeries::setLabelsVisible(bool visible)
342 void QPieSeries::setLabelsVisible(bool visible)
427 {
343 {
428 foreach (QPieSlice* s, m_slices)
344 foreach (QPieSlice* s, m_slices)
429 s->setLabelVisible(visible);
345 s->setLabelVisible(visible);
430 }
346 }
431
347
432 /*!
348 /*!
433 Returns the sum of all slice values in this series.
349 Returns the sum of all slice values in this series.
434
350
435 \sa QPieSlice::value(), QPieSlice::setValue()
351 \sa QPieSlice::value(), QPieSlice::setValue()
436 */
352 */
437 qreal QPieSeries::total() const
353 qreal QPieSeries::total() const
438 {
354 {
439 return m_total;
355 return m_total;
440 }
356 }
441
357
442 /*!
358 /*!
443 \fn void QPieSeries::changed()
359 \fn void QPieSeries::changed()
444
360
445 This signal emitted when something has changed in the series.
361 This signal emitted when something has changed in the series.
446
362
447 \sa QPieSeries::ChangeSet, QPieSlice::changed()
363 \sa QPieSeries::ChangeSet, QPieSlice::changed()
448 */
364 */
449
365
450 /*!
366 /*!
451 \fn void QPieSeries::clicked(QPieSlice* slice)
367 \fn void QPieSeries::clicked(QPieSlice* slice)
452
368
453 This signal is emitted when a \a slice has been clicked.
369 This signal is emitted when a \a slice has been clicked.
454
370
455 \sa QPieSlice::clicked()
371 \sa QPieSlice::clicked()
456 */
372 */
457
373
458 /*!
374 /*!
459 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
375 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
460
376
461 This signal is emitted when user has hovered over a \a slice.
377 This signal is emitted when user has hovered over a \a slice.
462
378
463 \sa QPieSlice::hoverEnter()
379 \sa QPieSlice::hoverEnter()
464 */
380 */
465
381
466 /*!
382 /*!
467 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
383 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
468
384
469 This signal is emitted when user has hovered away from a \a slice.
385 This signal is emitted when user has hovered away from a \a slice.
470
386
471 \sa QPieSlice::hoverLeave()
387 \sa QPieSlice::hoverLeave()
472 */
388 */
473
389
474 void QPieSeries::sliceChanged()
390 void QPieSeries::sliceChanged()
475 {
391 {
476 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
392 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
477 Q_ASSERT(m_slices.contains(slice));
393 Q_ASSERT(m_slices.contains(slice));
478 updateDerivativeData();
394 updateDerivativeData();
479 }
395 }
480
396
481 void QPieSeries::sliceClicked()
397 void QPieSeries::sliceClicked()
482 {
398 {
483 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
399 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
484 Q_ASSERT(m_slices.contains(slice));
400 Q_ASSERT(m_slices.contains(slice));
485 emit clicked(slice);
401 emit clicked(slice);
486 }
402 }
487
403
488 void QPieSeries::sliceHoverEnter()
404 void QPieSeries::sliceHoverEnter()
489 {
405 {
490 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
406 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
491 Q_ASSERT(m_slices.contains(slice));
407 Q_ASSERT(m_slices.contains(slice));
492 emit hoverEnter(slice);
408 emit hoverEnter(slice);
493 }
409 }
494
410
495 void QPieSeries::sliceHoverLeave()
411 void QPieSeries::sliceHoverLeave()
496 {
412 {
497 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
413 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
498 Q_ASSERT(m_slices.contains(slice));
414 Q_ASSERT(m_slices.contains(slice));
499 emit hoverLeave(slice);
415 emit hoverLeave(slice);
500 }
416 }
501
417
502 void QPieSeries::updateDerivativeData()
418 void QPieSeries::updateDerivativeData()
503 {
419 {
504 m_total = 0;
420 m_total = 0;
505
421
506 // nothing to do?
422 // nothing to do?
507 if (m_slices.count() == 0)
423 if (m_slices.count() == 0)
508 return;
424 return;
509
425
510 // calculate total
426 // calculate total
511 foreach (QPieSlice* s, m_slices)
427 foreach (QPieSlice* s, m_slices)
512 m_total += s->value();
428 m_total += s->value();
513
429
514 // TODO: emit totalChanged?
430 // TODO: emit totalChanged?
515
431
516 // we must have some values
432 // we must have some values
517 if (m_total == 0) {
433 if (m_total == 0) {
518 qDebug() << "QPieSeries::updateDerivativeData() total == 0";
434 qDebug() << "QPieSeries::updateDerivativeData() total == 0";
519 Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this?
435 Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this?
520 }
436 }
521
437
522 // update slice attributes
438 // update slice attributes
523 qreal sliceAngle = m_pieStartAngle;
439 qreal sliceAngle = m_pieStartAngle;
524 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
440 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
525 QVector<QPieSlice*> changed;
441 QVector<QPieSlice*> changed;
526 foreach (QPieSlice* s, m_slices) {
442 foreach (QPieSlice* s, m_slices) {
527
443
528 bool isChanged = false;
444 bool isChanged = false;
529
445
530 qreal percentage = s->value() / m_total;
446 qreal percentage = s->value() / m_total;
531 if (s->m_percentage != percentage) {
447 if (s->m_percentage != percentage) {
532 s->m_percentage = percentage;
448 s->m_percentage = percentage;
533 isChanged = true;
449 isChanged = true;
534 }
450 }
535
451
536 qreal sliceSpan = pieSpan * percentage;
452 qreal sliceSpan = pieSpan * percentage;
537 if (s->m_angleSpan != sliceSpan) {
453 if (s->m_angleSpan != sliceSpan) {
538 s->m_angleSpan = sliceSpan;
454 s->m_angleSpan = sliceSpan;
539 isChanged = true;
455 isChanged = true;
540 }
456 }
541
457
542 if (s->m_startAngle != sliceAngle) {
458 if (s->m_startAngle != sliceAngle) {
543 s->m_startAngle = sliceAngle;
459 s->m_startAngle = sliceAngle;
544 isChanged = true;
460 isChanged = true;
545 }
461 }
546 sliceAngle += sliceSpan;
462 sliceAngle += sliceSpan;
547
463
548 if (isChanged)
464 if (isChanged)
549 changed << s;
465 changed << s;
550 }
466 }
551
467
552 foreach (QPieSlice* s, changed)
468 foreach (QPieSlice* s, changed)
553 emit s->changed();
469 emit s->changed();
554 }
470 }
555
471
556 bool QPieSeries::setModel(QAbstractItemModel* model)
472 bool QPieSeries::setModel(QAbstractItemModel* model)
557 {
473 {
558 // disconnect signals from old model
474 // disconnect signals from old model
559 if(m_model)
475 if(m_model)
560 {
476 {
561 disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0);
477 disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0);
562 disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0);
478 disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0);
563 disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0);
479 disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0);
564 }
480 }
565
481
566 // set new model if not NULL and connect necessary signals from it
482 // set new model if not NULL and connect necessary signals from it
567 if(model)
483 if(model)
568 {
484 {
569 m_model = model;
485 m_model = model;
570 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
486 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
571 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
487 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
572 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
488 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
573 }
489 }
574
490
575 return true;
491 return true;
576 }
492 }
577
493
578 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
494 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
579 {
495 {
580 m_mapValues = modelValuesLine;
496 m_mapValues = modelValuesLine;
581 m_mapLabels = modelLabelsLine;
497 m_mapLabels = modelLabelsLine;
582 m_mapOrientation = orientation;
498 m_mapOrientation = orientation;
583
499
584 if (m_model == NULL)
500 if (m_model == NULL)
585 return;
501 return;
586
502
587 if (m_mapOrientation == Qt::Vertical)
503 if (m_mapOrientation == Qt::Vertical)
588 for (int i = 0; i < m_model->rowCount(); i++)
504 for (int i = 0; i < m_model->rowCount(); i++)
589 add(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
505 add(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
590 else
506 else
591 for (int i = 0; i < m_model->columnCount(); i++)
507 for (int i = 0; i < m_model->columnCount(); i++)
592 add(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
508 add(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
593
509
594
510
595 }
511 }
596
512
597 void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
513 void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
598 {
514 {
599 Q_UNUSED(bottomRight)
515 Q_UNUSED(bottomRight)
600
516
601 if (m_mapOrientation == Qt::Vertical)
517 if (m_mapOrientation == Qt::Vertical)
602 {
518 {
603 // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
519 // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
604 if (topLeft.column() == m_mapValues)
520 if (topLeft.column() == m_mapValues)
605 slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
521 slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
606 else if (topLeft.column() == m_mapLabels)
522 else if (topLeft.column() == m_mapLabels)
607 slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
523 slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
608 }
524 }
609 else
525 else
610 {
526 {
611 // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
527 // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
612 if (topLeft.column() == m_mapValues)
528 if (topLeft.column() == m_mapValues)
613 slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
529 slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
614 else if (topLeft.column() == m_mapLabels)
530 else if (topLeft.column() == m_mapLabels)
615 slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
531 slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
616 }
532 }
617 }
533 }
618
534
619 void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end)
535 void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end)
620 {
536 {
621 Q_UNUSED(parent)
537 Q_UNUSED(parent)
622 Q_UNUSED(end)
538 Q_UNUSED(end)
623
539
624 QPieSlice* newSlice = new QPieSlice;
540 QPieSlice* newSlice = new QPieSlice;
625 newSlice->setLabelVisible(true);
541 newSlice->setLabelVisible(true);
626 if (m_mapOrientation == Qt::Vertical)
542 if (m_mapOrientation == Qt::Vertical)
627 {
543 {
628 newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
544 newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
629 newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
545 newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
630 }
546 }
631 else
547 else
632 {
548 {
633 newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
549 newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
634 newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
550 newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
635 }
551 }
636
552
637 insert(start, newSlice);
553 insert(start, newSlice);
638 }
554 }
639
555
640 void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end)
556 void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end)
641 {
557 {
642 Q_UNUSED(parent)
558 Q_UNUSED(parent)
643 Q_UNUSED(end)
559 Q_UNUSED(end)
644 remove(slices().at(start));
560 remove(slices().at(start));
645 }
561 }
646
562
647 #include "moc_qpieseries.cpp"
563 #include "moc_qpieseries.cpp"
648
564
649 QTCOMMERCIALCHART_END_NAMESPACE
565 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,145 +1,113
1 #ifndef PIESERIES_H
1 #ifndef PIESERIES_H
2 #define PIESERIES_H
2 #define PIESERIES_H
3
3
4 #include "qseries.h"
4 #include "qseries.h"
5 #include <QObject>
5 #include <QObject>
6 #include <QRectF>
7 #include <QColor>
8 #include <QPen>
9 #include <QBrush>
10 #include <QSignalMapper>
11
6
12 class QGraphicsObject;
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 class PieChartItem;
15 class PieSlice;
16 class QPieSlice;
8 class QPieSlice;
17
9
18 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
10 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
19 {
11 {
20 Q_OBJECT
12 Q_OBJECT
21
13
22 public:
14 public:
23
24 class ChangeSet
25 {
26 public:
27
28 // TODO: these should not really be exposed to the public API
29 void appendAdded(QPieSlice* slice);
30 void appendAdded(QList<QPieSlice*> slices);
31 void appendChanged(QPieSlice* slice);
32 void appendRemoved(QPieSlice* slice);
33
34 QList<QPieSlice*> added() const;
35 QList<QPieSlice*> changed() const;
36 QList<QPieSlice*> removed() const;
37
38 bool isEmpty() const;
39
40 private:
41 QList<QPieSlice*> m_added;
42 QList<QPieSlice*> m_changed;
43 QList<QPieSlice*> m_removed;
44 };
45
46 public:
47 QPieSeries(QObject *parent = 0);
15 QPieSeries(QObject *parent = 0);
48 virtual ~QPieSeries();
16 virtual ~QPieSeries();
49
17
50 public: // from QChartSeries
18 public: // from QChartSeries
51 QSeriesType type() const;
19 QSeriesType type() const;
52
20
53 public:
21 public:
54
22
55 // slice setters
23 // slice setters
56 void add(QPieSlice* slice);
24 void add(QPieSlice* slice);
57 void add(QList<QPieSlice*> slices);
25 void add(QList<QPieSlice*> slices);
58 void insert(int i, QPieSlice* slice);
26 void insert(int i, QPieSlice* slice);
59 void replace(QList<QPieSlice*> slices);
27 void replace(QList<QPieSlice*> slices);
60 void remove(QPieSlice* slice);
28 void remove(QPieSlice* slice);
61 void clear();
29 void clear();
62
30
63 // sluce getters
31 // sluce getters
64 QList<QPieSlice*> slices() const;
32 QList<QPieSlice*> slices() const;
65
33
66 // calculated data
34 // calculated data
67 int count() const;
35 int count() const;
68 qreal total() const;
36 qreal total() const;
69
37
70 // pie customization
38 // pie customization
71 void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition);
39 void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition);
72 qreal pieHorizontalPosition() const;
40 qreal pieHorizontalPosition() const;
73 qreal pieVerticalPosition() const;
41 qreal pieVerticalPosition() const;
74 void setPieSize(qreal relativeSize);
42 void setPieSize(qreal relativeSize);
75 qreal pieSize() const;
43 qreal pieSize() const;
76 void setPieStartAngle(qreal startAngle);
44 void setPieStartAngle(qreal startAngle);
77 qreal pieStartAngle() const;
45 qreal pieStartAngle() const;
78 void setPieEndAngle(qreal endAngle);
46 void setPieEndAngle(qreal endAngle);
79 qreal pieEndAngle() const;
47 qreal pieEndAngle() const;
80
48
81 // convenience function
49 // convenience function
82 QPieSeries& operator << (QPieSlice* slice);
50 QPieSeries& operator << (QPieSlice* slice);
83 QPieSlice* add(qreal value, QString name);
51 QPieSlice* add(qreal value, QString name);
84 void setLabelsVisible(bool visible = true);
52 void setLabelsVisible(bool visible = true);
85
53
86 // data from model
54 // data from model
87 bool setModel(QAbstractItemModel* model);
55 bool setModel(QAbstractItemModel* model);
88 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
56 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
89
57
90 // TODO: find slices?
58 // TODO: find slices?
91 // QList<QPieSlice*> findByValue(qreal value);
59 // QList<QPieSlice*> findByValue(qreal value);
92 // ...
60 // ...
93
61
94 // TODO: sorting slices?
62 // TODO: sorting slices?
95 // void sort(QPieSeries::SortByValue|label|??)
63 // void sort(QPieSeries::SortByValue|label|??)
96
64
97 // TODO: general graphics customization
65 // TODO: general graphics customization
98 // setDrawStyle(2d|3d)
66 // setDrawStyle(2d|3d)
99 // setDropShadows
67 // setDropShadows
100
68
101 Q_SIGNALS:
69 Q_SIGNALS:
102 void clicked(QPieSlice* slice);
70 void clicked(QPieSlice* slice);
103 void hoverEnter(QPieSlice* slice);
71 void hoverEnter(QPieSlice* slice);
104 void hoverLeave(QPieSlice* slice);
72 void hoverLeave(QPieSlice* slice);
105 void changed(); // TODO: hide this in PIMPL
73 void changed(); // TODO: hide this in PIMPL
106
74
107 private Q_SLOTS: // TODO: should be private and not visible in the interface at all
75 private Q_SLOTS: // TODO: should be private and not visible in the interface at all
108 void sliceChanged();
76 void sliceChanged();
109 void sliceClicked();
77 void sliceClicked();
110 void sliceHoverEnter();
78 void sliceHoverEnter();
111 void sliceHoverLeave();
79 void sliceHoverLeave();
112
80
113 // slots for updating pie when data in model changes
81 // slots for updating pie when data in model changes
114 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
82 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
115 void modelDataAdded(QModelIndex parent, int start, int end);
83 void modelDataAdded(QModelIndex parent, int start, int end);
116 void modelDataRemoved(QModelIndex parent, int start, int end);
84 void modelDataRemoved(QModelIndex parent, int start, int end);
117
85
118 private:
86 private:
119 void updateDerivativeData();
87 void updateDerivativeData();
120
88
121 private:
89 private:
122 Q_DISABLE_COPY(QPieSeries)
90 Q_DISABLE_COPY(QPieSeries)
123
91
124 // TODO: use PIML
92 // TODO: use PIML
125 friend class PieChartItem;
93 friend class PieChartItem;
126 friend class PieSlice;
94 friend class PieSlice;
127 friend class QPieSlice;
95 friend class QPieSlice;
128
96
129 QList<QPieSlice*> m_slices;
97 QList<QPieSlice*> m_slices;
130 qreal m_pieRelativeHorPos;
98 qreal m_pieRelativeHorPos;
131 qreal m_pieRelativeVerPos;
99 qreal m_pieRelativeVerPos;
132 qreal m_pieRelativeSize;
100 qreal m_pieRelativeSize;
133 qreal m_pieStartAngle;
101 qreal m_pieStartAngle;
134 qreal m_pieEndAngle;
102 qreal m_pieEndAngle;
135 qreal m_total;
103 qreal m_total;
136
104
137 // model map
105 // model map
138 int m_mapValues;
106 int m_mapValues;
139 int m_mapLabels;
107 int m_mapLabels;
140 Qt::Orientation m_mapOrientation;
108 Qt::Orientation m_mapOrientation;
141 };
109 };
142
110
143 QTCOMMERCIALCHART_END_NAMESPACE
111 QTCOMMERCIALCHART_END_NAMESPACE
144
112
145 #endif // PIESERIES_H
113 #endif // PIESERIES_H
General Comments 0
You need to be logged in to leave comments. Login now