##// END OF EJS Templates
pie: add everything as a property in series...
Jani Honkonen -
r1255:419a2509ab1e
parent child
Show More
@@ -39,9 +39,9 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
39
39
40 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
40 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
41 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
42 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
42 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(updateLayout()));
43 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
45
45
46 // Note: the following does not affect as long as the item does not have anything to paint
46 // Note: the following does not affect as long as the item does not have anything to paint
47 setZValue(ChartPresenter::PieSeriesZValue);
47 setZValue(ChartPresenter::PieSeriesZValue);
@@ -179,6 +179,7 bool QPieSeries::append(QList<QPieSlice*> slices)
179 }
179 }
180
180
181 emit added(slices);
181 emit added(slices);
182 emit countChanged();
182
183
183 return true;
184 return true;
184 }
185 }
@@ -242,6 +243,7 bool QPieSeries::insert(int index, QPieSlice* slice)
242 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
243 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
243
244
244 emit added(QList<QPieSlice*>() << slice);
245 emit added(QList<QPieSlice*>() << slice);
246 emit countChanged();
245
247
246 return true;
248 return true;
247 }
249 }
@@ -263,6 +265,7 bool QPieSeries::remove(QPieSlice* slice)
263 d->updateDerivativeData();
265 d->updateDerivativeData();
264
266
265 emit removed(QList<QPieSlice*>() << slice);
267 emit removed(QList<QPieSlice*>() << slice);
268 emit countChanged();
266
269
267 delete slice;
270 delete slice;
268 slice = 0;
271 slice = 0;
@@ -288,6 +291,7 void QPieSeries::clear()
288 d->updateDerivativeData();
291 d->updateDerivativeData();
289
292
290 emit removed(slices);
293 emit removed(slices);
294 emit countChanged();
291 }
295 }
292
296
293 /*!
297 /*!
@@ -320,15 +324,31 QList<QPieSlice*> QPieSeries::slices() const
320 void QPieSeries::setHorizontalPosition(qreal relativePosition)
324 void QPieSeries::setHorizontalPosition(qreal relativePosition)
321 {
325 {
322 Q_D(QPieSeries);
326 Q_D(QPieSeries);
323 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
327
324 emit d->piePositionChanged();
328 if (relativePosition < 0.0)
329 relativePosition = 0.0;
330 if (relativePosition > 1.0)
331 relativePosition = 1.0;
332
333 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) {
334 d->m_pieRelativeHorPos = relativePosition;
335 emit horizontalPositionChanged();
336 }
325 }
337 }
326
338
327 void QPieSeries::setVerticalPosition(qreal relativePosition)
339 void QPieSeries::setVerticalPosition(qreal relativePosition)
328 {
340 {
329 Q_D(QPieSeries);
341 Q_D(QPieSeries);
330 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
342
331 emit d->piePositionChanged();
343 if (relativePosition < 0.0)
344 relativePosition = 0.0;
345 if (relativePosition > 1.0)
346 relativePosition = 1.0;
347
348 if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) {
349 d->m_pieRelativeVerPos = relativePosition;
350 emit verticalPositionChanged();
351 }
332 }
352 }
333
353
334 qreal QPieSeries::horizontalPosition() const
354 qreal QPieSeries::horizontalPosition() const
@@ -346,8 +366,16 qreal QPieSeries::verticalPosition() const
346 void QPieSeries::setPieSize(qreal relativeSize)
366 void QPieSeries::setPieSize(qreal relativeSize)
347 {
367 {
348 Q_D(QPieSeries);
368 Q_D(QPieSeries);
349 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
369
350 emit d->pieSizeChanged();
370 if (relativeSize < 0.0)
371 relativeSize = 0.0;
372 if (relativeSize > 1.0)
373 relativeSize = 1.0;
374
375 if (!qFuzzyIsNull(d->m_pieRelativeSize - relativeSize)) {
376 d->m_pieRelativeSize = relativeSize;
377 emit pieSizeChanged();
378 }
351 }
379 }
352
380
353 qreal QPieSeries::pieSize() const
381 qreal QPieSeries::pieSize() const
@@ -364,6 +392,7 void QPieSeries::setPieStartAngle(qreal angle)
364 return;
392 return;
365 d->m_pieStartAngle = angle;
393 d->m_pieStartAngle = angle;
366 d->updateDerivativeData();
394 d->updateDerivativeData();
395 emit pieStartAngleChanged();
367 }
396 }
368
397
369 qreal QPieSeries::pieStartAngle() const
398 qreal QPieSeries::pieStartAngle() const
@@ -388,6 +417,7 void QPieSeries::setPieEndAngle(qreal angle)
388 return;
417 return;
389 d->m_pieEndAngle = angle;
418 d->m_pieEndAngle = angle;
390 d->updateDerivativeData();
419 d->updateDerivativeData();
420 emit pieEndAngleChanged();
391 }
421 }
392
422
393 /*!
423 /*!
@@ -482,15 +512,15 QPieSeriesPrivate::~QPieSeriesPrivate()
482
512
483 void QPieSeriesPrivate::updateDerivativeData()
513 void QPieSeriesPrivate::updateDerivativeData()
484 {
514 {
485 m_sum = 0;
486
487 // nothing to do?
488 if (m_slices.count() == 0)
489 return;
490
491 // calculate sum of all slices
515 // calculate sum of all slices
516 qreal sum = 0;
492 foreach (QPieSlice* s, m_slices)
517 foreach (QPieSlice* s, m_slices)
493 m_sum += s->value();
518 sum += s->value();
519
520 if (!qFuzzyIsNull(m_sum - sum)) {
521 m_sum = sum;
522 emit q_func()->sumChanged();
523 }
494
524
495 // nothing to show..
525 // nothing to show..
496 if (qFuzzyIsNull(m_sum))
526 if (qFuzzyIsNull(m_sum))
@@ -546,32 +576,6 void QPieSeriesPrivate::sliceHovered(bool state)
546 emit q->hovered(slice, state);
576 emit q->hovered(slice, state);
547 }
577 }
548
578
549 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
550 {
551 // Remove rounding errors
552 qreal roundedValue = newValue;
553 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
554 roundedValue = 0.0;
555 else if (qFuzzyCompare(newValue, max))
556 roundedValue = max;
557 else if (qFuzzyCompare(newValue, min))
558 roundedValue = min;
559
560 // Check if the position is valid after removing the rounding errors
561 if (roundedValue < min || roundedValue > max) {
562 qWarning("QPieSeries: Illegal value");
563 return false;
564 }
565
566 if (!qFuzzyIsNull(value - roundedValue)) {
567 value = roundedValue;
568 return true;
569 }
570
571 // The change was so small it is considered a rounding error
572 return false;
573 }
574
575 void QPieSeriesPrivate::scaleDomain(Domain& domain)
579 void QPieSeriesPrivate::scaleDomain(Domain& domain)
576 {
580 {
577 Q_UNUSED(domain);
581 Q_UNUSED(domain);
@@ -30,13 +30,13 class QPieSlice;
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition NOTIFY horizontalPositionChanged)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition NOTIFY verticalPositionChanged)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize NOTIFY pieSizeChanged)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle NOTIFY pieStartAngleChanged)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle NOTIFY pieEndAngleChanged)
38 Q_PROPERTY(int count READ count)
38 Q_PROPERTY(int count READ count NOTIFY countChanged)
39 Q_PROPERTY(qreal sum READ sum)
39 Q_PROPERTY(qreal sum READ sum NOTIFY sumChanged)
40
40
41 public:
41 public:
42 explicit QPieSeries(QObject *parent = 0);
42 explicit QPieSeries(QObject *parent = 0);
@@ -48,18 +48,22 public:
48 bool append(QList<QPieSlice*> slices);
48 bool append(QList<QPieSlice*> slices);
49 QPieSeries& operator << (QPieSlice* slice);
49 QPieSeries& operator << (QPieSlice* slice);
50 QPieSlice* append(QString label, qreal value);
50 QPieSlice* append(QString label, qreal value);
51
51 bool insert(int index, QPieSlice* slice);
52 bool insert(int index, QPieSlice* slice);
53
52 bool remove(QPieSlice* slice);
54 bool remove(QPieSlice* slice);
53 void clear();
55 void clear();
54
56
55 QList<QPieSlice*> slices() const;
57 QList<QPieSlice*> slices() const;
56 int count() const;
58 int count() const;
59
57 bool isEmpty() const;
60 bool isEmpty() const;
58
61
59 qreal sum() const;
62 qreal sum() const;
60
63
61 void setHorizontalPosition(qreal relativePosition);
64 void setHorizontalPosition(qreal relativePosition);
62 qreal horizontalPosition() const;
65 qreal horizontalPosition() const;
66
63 void setVerticalPosition(qreal relativePosition);
67 void setVerticalPosition(qreal relativePosition);
64 qreal verticalPosition() const;
68 qreal verticalPosition() const;
65
69
@@ -68,6 +72,7 public:
68
72
69 void setPieStartAngle(qreal startAngle);
73 void setPieStartAngle(qreal startAngle);
70 qreal pieStartAngle() const;
74 qreal pieStartAngle() const;
75
71 void setPieEndAngle(qreal endAngle);
76 void setPieEndAngle(qreal endAngle);
72 qreal pieEndAngle() const;
77 qreal pieEndAngle() const;
73
78
@@ -78,6 +83,13 Q_SIGNALS:
78 void removed(QList<QPieSlice*> slices);
83 void removed(QList<QPieSlice*> slices);
79 void clicked(QPieSlice* slice);
84 void clicked(QPieSlice* slice);
80 void hovered(QPieSlice* slice, bool state);
85 void hovered(QPieSlice* slice, bool state);
86 void countChanged();
87 void sumChanged();
88 void pieSizeChanged();
89 void pieStartAngleChanged();
90 void pieEndAngleChanged();
91 void horizontalPositionChanged();
92 void verticalPositionChanged();
81
93
82 private:
94 private:
83 Q_DECLARE_PRIVATE(QPieSeries)
95 Q_DECLARE_PRIVATE(QPieSeries)
@@ -43,15 +43,10 public:
43
43
44 static QPieSeriesPrivate* seriesData(QPieSeries &series);
44 static QPieSeriesPrivate* seriesData(QPieSeries &series);
45
45
46 Q_SIGNALS:
47 void piePositionChanged();
48 void pieSizeChanged();
49
50 public Q_SLOTS:
46 public Q_SLOTS:
51 void sliceChanged();
47 void sliceChanged();
52 void sliceClicked();
48 void sliceClicked();
53 void sliceHovered(bool state);
49 void sliceHovered(bool state);
54 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
55
50
56 public:
51 public:
57 QList<QPieSlice*> m_slices;
52 QList<QPieSlice*> m_slices;
@@ -42,7 +42,7 public slots:
42 void cleanup();
42 void cleanup();
43
43
44 private slots:
44 private slots:
45 void construction();
45 void properties();
46 void append();
46 void append();
47 void insert();
47 void insert();
48 void remove();
48 void remove();
@@ -76,10 +76,18 void tst_qpieseries::cleanup()
76
76
77 }
77 }
78
78
79 void tst_qpieseries::construction()
79 void tst_qpieseries::properties()
80 {
80 {
81 // verify default values
82 QPieSeries s;
81 QPieSeries s;
82
83 QSignalSpy countSpy(&s, SIGNAL(countChanged()));
84 QSignalSpy sumSpy(&s, SIGNAL(sumChanged()));
85 QSignalSpy sizeSpy(&s, SIGNAL(pieSizeChanged()));
86 QSignalSpy startAngleSpy(&s, SIGNAL(pieStartAngleChanged()));
87 QSignalSpy endAngleSpy(&s, SIGNAL(pieEndAngleChanged()));
88 QSignalSpy horPosSpy(&s, SIGNAL(horizontalPositionChanged()));
89 QSignalSpy verPosSpy(&s, SIGNAL(verticalPositionChanged()));
90
83 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
91 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
84 QVERIFY(s.count() == 0);
92 QVERIFY(s.count() == 0);
85 QVERIFY(s.isEmpty());
93 QVERIFY(s.isEmpty());
@@ -89,6 +97,53 void tst_qpieseries::construction()
89 QCOMPARE(s.pieSize(), 0.7);
97 QCOMPARE(s.pieSize(), 0.7);
90 QCOMPARE(s.pieStartAngle(), 0.0);
98 QCOMPARE(s.pieStartAngle(), 0.0);
91 QCOMPARE(s.pieEndAngle(), 360.0);
99 QCOMPARE(s.pieEndAngle(), 360.0);
100
101 s.append("s1", 1);
102 s.append("s2", 1);
103 s.append("s3", 1);
104 s.insert(1, new QPieSlice("s4", 1));
105 s.remove(s.slices().first());
106 QCOMPARE(s.count(), 3);
107 QCOMPARE(s.sum(), 3.0);
108 s.clear();
109 QCOMPARE(s.count(), 0);
110 QCOMPARE(s.sum(), 0.0);
111 QCOMPARE(countSpy.count(), 6);
112 QCOMPARE(sumSpy.count(), 6);
113
114 s.setPieSize(-1.0);
115 QCOMPARE(s.pieSize(), 0.0);
116 s.setPieSize(0.0);
117 s.setPieSize(0.9);
118 s.setPieSize(2.0);
119 QCOMPARE(s.pieSize(), 1.0);
120 QCOMPARE(sizeSpy.count(), 3);
121
122 s.setPieStartAngle(0);
123 s.setPieStartAngle(-180);
124 s.setPieStartAngle(180);
125 QCOMPARE(startAngleSpy.count(), 2);
126
127 s.setPieEndAngle(360);
128 s.setPieEndAngle(-180);
129 s.setPieEndAngle(180);
130 QCOMPARE(endAngleSpy.count(), 2);
131
132 s.setHorizontalPosition(0.5);
133 s.setHorizontalPosition(-1.0);
134 QCOMPARE(s.horizontalPosition(), 0.0);
135 s.setHorizontalPosition(1.0);
136 s.setHorizontalPosition(2.0);
137 QCOMPARE(s.horizontalPosition(), 1.0);
138 QCOMPARE(horPosSpy.count(), 2);
139
140 s.setVerticalPosition(0.5);
141 s.setVerticalPosition(-1.0);
142 QCOMPARE(s.verticalPosition(), 0.0);
143 s.setVerticalPosition(1.0);
144 s.setVerticalPosition(2.0);
145 QCOMPARE(s.verticalPosition(), 1.0);
146 QCOMPARE(verPosSpy.count(), 2);
92 }
147 }
93
148
94 void tst_qpieseries::append()
149 void tst_qpieseries::append()
General Comments 0
You need to be logged in to leave comments. Login now