##// 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 40 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 41 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
42 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(updateLayout()));
44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
42 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
43 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
45 45
46 46 // Note: the following does not affect as long as the item does not have anything to paint
47 47 setZValue(ChartPresenter::PieSeriesZValue);
@@ -179,6 +179,7 bool QPieSeries::append(QList<QPieSlice*> slices)
179 179 }
180 180
181 181 emit added(slices);
182 emit countChanged();
182 183
183 184 return true;
184 185 }
@@ -242,6 +243,7 bool QPieSeries::insert(int index, QPieSlice* slice)
242 243 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
243 244
244 245 emit added(QList<QPieSlice*>() << slice);
246 emit countChanged();
245 247
246 248 return true;
247 249 }
@@ -263,6 +265,7 bool QPieSeries::remove(QPieSlice* slice)
263 265 d->updateDerivativeData();
264 266
265 267 emit removed(QList<QPieSlice*>() << slice);
268 emit countChanged();
266 269
267 270 delete slice;
268 271 slice = 0;
@@ -288,6 +291,7 void QPieSeries::clear()
288 291 d->updateDerivativeData();
289 292
290 293 emit removed(slices);
294 emit countChanged();
291 295 }
292 296
293 297 /*!
@@ -320,15 +324,31 QList<QPieSlice*> QPieSeries::slices() const
320 324 void QPieSeries::setHorizontalPosition(qreal relativePosition)
321 325 {
322 326 Q_D(QPieSeries);
323 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
324 emit d->piePositionChanged();
327
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 339 void QPieSeries::setVerticalPosition(qreal relativePosition)
328 340 {
329 341 Q_D(QPieSeries);
330 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
331 emit d->piePositionChanged();
342
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 354 qreal QPieSeries::horizontalPosition() const
@@ -346,8 +366,16 qreal QPieSeries::verticalPosition() const
346 366 void QPieSeries::setPieSize(qreal relativeSize)
347 367 {
348 368 Q_D(QPieSeries);
349 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
350 emit d->pieSizeChanged();
369
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 381 qreal QPieSeries::pieSize() const
@@ -364,6 +392,7 void QPieSeries::setPieStartAngle(qreal angle)
364 392 return;
365 393 d->m_pieStartAngle = angle;
366 394 d->updateDerivativeData();
395 emit pieStartAngleChanged();
367 396 }
368 397
369 398 qreal QPieSeries::pieStartAngle() const
@@ -388,6 +417,7 void QPieSeries::setPieEndAngle(qreal angle)
388 417 return;
389 418 d->m_pieEndAngle = angle;
390 419 d->updateDerivativeData();
420 emit pieEndAngleChanged();
391 421 }
392 422
393 423 /*!
@@ -482,15 +512,15 QPieSeriesPrivate::~QPieSeriesPrivate()
482 512
483 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 515 // calculate sum of all slices
516 qreal sum = 0;
492 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 525 // nothing to show..
496 526 if (qFuzzyIsNull(m_sum))
@@ -546,32 +576,6 void QPieSeriesPrivate::sliceHovered(bool state)
546 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 579 void QPieSeriesPrivate::scaleDomain(Domain& domain)
576 580 {
577 581 Q_UNUSED(domain);
@@ -30,13 +30,13 class QPieSlice;
30 30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 31 {
32 32 Q_OBJECT
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38 Q_PROPERTY(int count READ count)
39 Q_PROPERTY(qreal sum READ sum)
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition NOTIFY horizontalPositionChanged)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition NOTIFY verticalPositionChanged)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize NOTIFY pieSizeChanged)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle NOTIFY pieStartAngleChanged)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle NOTIFY pieEndAngleChanged)
38 Q_PROPERTY(int count READ count NOTIFY countChanged)
39 Q_PROPERTY(qreal sum READ sum NOTIFY sumChanged)
40 40
41 41 public:
42 42 explicit QPieSeries(QObject *parent = 0);
@@ -48,18 +48,22 public:
48 48 bool append(QList<QPieSlice*> slices);
49 49 QPieSeries& operator << (QPieSlice* slice);
50 50 QPieSlice* append(QString label, qreal value);
51
51 52 bool insert(int index, QPieSlice* slice);
53
52 54 bool remove(QPieSlice* slice);
53 55 void clear();
54 56
55 57 QList<QPieSlice*> slices() const;
56 58 int count() const;
59
57 60 bool isEmpty() const;
58 61
59 62 qreal sum() const;
60 63
61 64 void setHorizontalPosition(qreal relativePosition);
62 65 qreal horizontalPosition() const;
66
63 67 void setVerticalPosition(qreal relativePosition);
64 68 qreal verticalPosition() const;
65 69
@@ -68,6 +72,7 public:
68 72
69 73 void setPieStartAngle(qreal startAngle);
70 74 qreal pieStartAngle() const;
75
71 76 void setPieEndAngle(qreal endAngle);
72 77 qreal pieEndAngle() const;
73 78
@@ -78,6 +83,13 Q_SIGNALS:
78 83 void removed(QList<QPieSlice*> slices);
79 84 void clicked(QPieSlice* slice);
80 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 94 private:
83 95 Q_DECLARE_PRIVATE(QPieSeries)
@@ -43,15 +43,10 public:
43 43
44 44 static QPieSeriesPrivate* seriesData(QPieSeries &series);
45 45
46 Q_SIGNALS:
47 void piePositionChanged();
48 void pieSizeChanged();
49
50 46 public Q_SLOTS:
51 47 void sliceChanged();
52 48 void sliceClicked();
53 49 void sliceHovered(bool state);
54 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
55 50
56 51 public:
57 52 QList<QPieSlice*> m_slices;
@@ -42,7 +42,7 public slots:
42 42 void cleanup();
43 43
44 44 private slots:
45 void construction();
45 void properties();
46 46 void append();
47 47 void insert();
48 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 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 91 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
84 92 QVERIFY(s.count() == 0);
85 93 QVERIFY(s.isEmpty());
@@ -89,6 +97,53 void tst_qpieseries::construction()
89 97 QCOMPARE(s.pieSize(), 0.7);
90 98 QCOMPARE(s.pieStartAngle(), 0.0);
91 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 149 void tst_qpieseries::append()
General Comments 0
You need to be logged in to leave comments. Login now