diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index 49fa8e8..b9c6e81 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -39,9 +39,9 @@ PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) connect(series, SIGNAL(added(QList)), this, SLOT(handleSlicesAdded(QList))); connect(series, SIGNAL(removed(QList)), this, SLOT(handleSlicesRemoved(QList))); - QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series); - connect(d, SIGNAL(piePositionChanged()), this, SLOT(updateLayout())); - connect(d, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout())); + connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout())); + connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout())); + connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout())); // Note: the following does not affect as long as the item does not have anything to paint setZValue(ChartPresenter::PieSeriesZValue); diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 79ab89b..e262d37 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -179,6 +179,7 @@ bool QPieSeries::append(QList slices) } emit added(slices); + emit countChanged(); return true; } @@ -242,6 +243,7 @@ bool QPieSeries::insert(int index, QPieSlice* slice) connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); emit added(QList() << slice); + emit countChanged(); return true; } @@ -263,6 +265,7 @@ bool QPieSeries::remove(QPieSlice* slice) d->updateDerivativeData(); emit removed(QList() << slice); + emit countChanged(); delete slice; slice = 0; @@ -288,6 +291,7 @@ void QPieSeries::clear() d->updateDerivativeData(); emit removed(slices); + emit countChanged(); } /*! @@ -320,15 +324,31 @@ QList QPieSeries::slices() const void QPieSeries::setHorizontalPosition(qreal relativePosition) { Q_D(QPieSeries); - if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0)) - emit d->piePositionChanged(); + + if (relativePosition < 0.0) + relativePosition = 0.0; + if (relativePosition > 1.0) + relativePosition = 1.0; + + if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) { + d->m_pieRelativeHorPos = relativePosition; + emit horizontalPositionChanged(); + } } void QPieSeries::setVerticalPosition(qreal relativePosition) { Q_D(QPieSeries); - if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0)) - emit d->piePositionChanged(); + + if (relativePosition < 0.0) + relativePosition = 0.0; + if (relativePosition > 1.0) + relativePosition = 1.0; + + if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) { + d->m_pieRelativeVerPos = relativePosition; + emit verticalPositionChanged(); + } } qreal QPieSeries::horizontalPosition() const @@ -346,8 +366,16 @@ qreal QPieSeries::verticalPosition() const void QPieSeries::setPieSize(qreal relativeSize) { Q_D(QPieSeries); - if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0)) - emit d->pieSizeChanged(); + + if (relativeSize < 0.0) + relativeSize = 0.0; + if (relativeSize > 1.0) + relativeSize = 1.0; + + if (!qFuzzyIsNull(d->m_pieRelativeSize - relativeSize)) { + d->m_pieRelativeSize = relativeSize; + emit pieSizeChanged(); + } } qreal QPieSeries::pieSize() const @@ -364,6 +392,7 @@ void QPieSeries::setPieStartAngle(qreal angle) return; d->m_pieStartAngle = angle; d->updateDerivativeData(); + emit pieStartAngleChanged(); } qreal QPieSeries::pieStartAngle() const @@ -388,6 +417,7 @@ void QPieSeries::setPieEndAngle(qreal angle) return; d->m_pieEndAngle = angle; d->updateDerivativeData(); + emit pieEndAngleChanged(); } /*! @@ -482,15 +512,15 @@ QPieSeriesPrivate::~QPieSeriesPrivate() void QPieSeriesPrivate::updateDerivativeData() { - m_sum = 0; - - // nothing to do? - if (m_slices.count() == 0) - return; - // calculate sum of all slices + qreal sum = 0; foreach (QPieSlice* s, m_slices) - m_sum += s->value(); + sum += s->value(); + + if (!qFuzzyIsNull(m_sum - sum)) { + m_sum = sum; + emit q_func()->sumChanged(); + } // nothing to show.. if (qFuzzyIsNull(m_sum)) @@ -546,32 +576,6 @@ void QPieSeriesPrivate::sliceHovered(bool state) emit q->hovered(slice, state); } -bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min) -{ - // Remove rounding errors - qreal roundedValue = newValue; - if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue)) - roundedValue = 0.0; - else if (qFuzzyCompare(newValue, max)) - roundedValue = max; - else if (qFuzzyCompare(newValue, min)) - roundedValue = min; - - // Check if the position is valid after removing the rounding errors - if (roundedValue < min || roundedValue > max) { - qWarning("QPieSeries: Illegal value"); - return false; - } - - if (!qFuzzyIsNull(value - roundedValue)) { - value = roundedValue; - return true; - } - - // The change was so small it is considered a rounding error - return false; -} - void QPieSeriesPrivate::scaleDomain(Domain& domain) { Q_UNUSED(domain); diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index 153b916..21ea607 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -30,13 +30,13 @@ class QPieSlice; class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries { Q_OBJECT - Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition) - Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition) - Q_PROPERTY(qreal size READ pieSize WRITE setPieSize) - Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle) - Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle) - Q_PROPERTY(int count READ count) - Q_PROPERTY(qreal sum READ sum) + Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition NOTIFY horizontalPositionChanged) + Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition NOTIFY verticalPositionChanged) + Q_PROPERTY(qreal size READ pieSize WRITE setPieSize NOTIFY pieSizeChanged) + Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle NOTIFY pieStartAngleChanged) + Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle NOTIFY pieEndAngleChanged) + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_PROPERTY(qreal sum READ sum NOTIFY sumChanged) public: explicit QPieSeries(QObject *parent = 0); @@ -48,18 +48,22 @@ public: bool append(QList slices); QPieSeries& operator << (QPieSlice* slice); QPieSlice* append(QString label, qreal value); + bool insert(int index, QPieSlice* slice); + bool remove(QPieSlice* slice); void clear(); QList slices() const; int count() const; + bool isEmpty() const; qreal sum() const; void setHorizontalPosition(qreal relativePosition); qreal horizontalPosition() const; + void setVerticalPosition(qreal relativePosition); qreal verticalPosition() const; @@ -68,6 +72,7 @@ public: void setPieStartAngle(qreal startAngle); qreal pieStartAngle() const; + void setPieEndAngle(qreal endAngle); qreal pieEndAngle() const; @@ -78,6 +83,13 @@ Q_SIGNALS: void removed(QList slices); void clicked(QPieSlice* slice); void hovered(QPieSlice* slice, bool state); + void countChanged(); + void sumChanged(); + void pieSizeChanged(); + void pieStartAngleChanged(); + void pieEndAngleChanged(); + void horizontalPositionChanged(); + void verticalPositionChanged(); private: Q_DECLARE_PRIVATE(QPieSeries) diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index 4dec0a0..2a37731 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -43,15 +43,10 @@ public: static QPieSeriesPrivate* seriesData(QPieSeries &series); -Q_SIGNALS: - void piePositionChanged(); - void pieSizeChanged(); - public Q_SLOTS: void sliceChanged(); void sliceClicked(); void sliceHovered(bool state); - bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0); public: QList m_slices; diff --git a/tests/auto/qpieseries/tst_qpieseries.cpp b/tests/auto/qpieseries/tst_qpieseries.cpp index e304938..d63eb8a 100644 --- a/tests/auto/qpieseries/tst_qpieseries.cpp +++ b/tests/auto/qpieseries/tst_qpieseries.cpp @@ -42,7 +42,7 @@ public slots: void cleanup(); private slots: - void construction(); + void properties(); void append(); void insert(); void remove(); @@ -76,10 +76,18 @@ void tst_qpieseries::cleanup() } -void tst_qpieseries::construction() +void tst_qpieseries::properties() { - // verify default values QPieSeries s; + + QSignalSpy countSpy(&s, SIGNAL(countChanged())); + QSignalSpy sumSpy(&s, SIGNAL(sumChanged())); + QSignalSpy sizeSpy(&s, SIGNAL(pieSizeChanged())); + QSignalSpy startAngleSpy(&s, SIGNAL(pieStartAngleChanged())); + QSignalSpy endAngleSpy(&s, SIGNAL(pieEndAngleChanged())); + QSignalSpy horPosSpy(&s, SIGNAL(horizontalPositionChanged())); + QSignalSpy verPosSpy(&s, SIGNAL(verticalPositionChanged())); + QVERIFY(s.type() == QAbstractSeries::SeriesTypePie); QVERIFY(s.count() == 0); QVERIFY(s.isEmpty()); @@ -89,6 +97,53 @@ void tst_qpieseries::construction() QCOMPARE(s.pieSize(), 0.7); QCOMPARE(s.pieStartAngle(), 0.0); QCOMPARE(s.pieEndAngle(), 360.0); + + s.append("s1", 1); + s.append("s2", 1); + s.append("s3", 1); + s.insert(1, new QPieSlice("s4", 1)); + s.remove(s.slices().first()); + QCOMPARE(s.count(), 3); + QCOMPARE(s.sum(), 3.0); + s.clear(); + QCOMPARE(s.count(), 0); + QCOMPARE(s.sum(), 0.0); + QCOMPARE(countSpy.count(), 6); + QCOMPARE(sumSpy.count(), 6); + + s.setPieSize(-1.0); + QCOMPARE(s.pieSize(), 0.0); + s.setPieSize(0.0); + s.setPieSize(0.9); + s.setPieSize(2.0); + QCOMPARE(s.pieSize(), 1.0); + QCOMPARE(sizeSpy.count(), 3); + + s.setPieStartAngle(0); + s.setPieStartAngle(-180); + s.setPieStartAngle(180); + QCOMPARE(startAngleSpy.count(), 2); + + s.setPieEndAngle(360); + s.setPieEndAngle(-180); + s.setPieEndAngle(180); + QCOMPARE(endAngleSpy.count(), 2); + + s.setHorizontalPosition(0.5); + s.setHorizontalPosition(-1.0); + QCOMPARE(s.horizontalPosition(), 0.0); + s.setHorizontalPosition(1.0); + s.setHorizontalPosition(2.0); + QCOMPARE(s.horizontalPosition(), 1.0); + QCOMPARE(horPosSpy.count(), 2); + + s.setVerticalPosition(0.5); + s.setVerticalPosition(-1.0); + QCOMPARE(s.verticalPosition(), 0.0); + s.setVerticalPosition(1.0); + s.setVerticalPosition(2.0); + QCOMPARE(s.verticalPosition(), 1.0); + QCOMPARE(verPosSpy.count(), 2); } void tst_qpieseries::append()