From eecdecc834b2d6ac3e2da1b4feb32f8ce665dc21 2012-03-28 12:14:23 From: Jani Honkonen Date: 2012-03-28 12:14:23 Subject: [PATCH] Fix issues with comparing equality of floating values in pie --- diff --git a/src/piechart/pieslicedata_p.h b/src/piechart/pieslicedata_p.h index f1585e2..d7d46ec 100644 --- a/src/piechart/pieslicedata_p.h +++ b/src/piechart/pieslicedata_p.h @@ -64,21 +64,21 @@ public: return true; if (m_isExploded != other.m_isExploded || - m_explodeDistanceFactor != other.m_explodeDistanceFactor) + !qFuzzyIsNull(m_explodeDistanceFactor - other.m_explodeDistanceFactor)) return true; if (m_isLabelVisible != other.m_isLabelVisible || m_labelText != other.m_labelText || m_labelFont != other.m_labelFont || - m_labelArmLengthFactor != other.m_labelArmLengthFactor || + !qFuzzyIsNull(m_labelArmLengthFactor - other.m_labelArmLengthFactor) || m_labelPen != other.m_labelPen) return true; - if (m_percentage != other.m_percentage || + if (!qFuzzyIsNull(m_percentage - other.m_percentage) || m_center != other.m_center || - m_radius != other.m_radius || - m_startAngle != other.m_startAngle || - m_angleSpan != other.m_angleSpan) + !qFuzzyIsNull(m_radius - other.m_radius) || + !qFuzzyIsNull(m_startAngle - other.m_startAngle) || + !qFuzzyIsNull(m_angleSpan - other.m_angleSpan)) return true; return false; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 78fe8ff..3c89c37 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -36,7 +36,7 @@ void QPieSeriesPrivate::updateDerivativeData() m_total += s->value(); // nothing to show.. - if (m_total == 0) + if (qFuzzyIsNull(m_total)) return; // update slice attributes @@ -45,28 +45,16 @@ void QPieSeriesPrivate::updateDerivativeData() QVector changed; foreach (QPieSlice* s, m_slices) { - bool isChanged = false; + PieSliceData data = s->data_ptr()->m_data; + data.m_percentage = s->value() / m_total; + data.m_angleSpan = pieSpan * data.m_percentage; + data.m_startAngle = sliceAngle; + sliceAngle += data.m_angleSpan; - qreal percentage = s->value() / m_total; - if (s->data_ptr()->m_data.m_percentage != percentage) { - s->data_ptr()->m_data.m_percentage = percentage; - isChanged = true; - } - - qreal sliceSpan = pieSpan * percentage; - if (s->data_ptr()->m_data.m_angleSpan != sliceSpan) { - s->data_ptr()->m_data.m_angleSpan = sliceSpan; - isChanged = true; - } - - if (s->data_ptr()->m_data.m_startAngle != sliceAngle) { - s->data_ptr()->m_data.m_startAngle = sliceAngle; - isChanged = true; - } - sliceAngle += sliceSpan; - - if (isChanged) + if (s->data_ptr()->m_data != data) { + s->data_ptr()->m_data = data; changed << s; + } } // emit signals @@ -385,7 +373,8 @@ void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relative relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) return; - if (d->m_pieRelativeHorPos != relativeHorizontalPosition || d->m_pieRelativeVerPos != relativeVerticalPosition) { + if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativeHorizontalPosition) || + !qFuzzyIsNull(d->m_pieRelativeVerPos - relativeVerticalPosition)) { d->m_pieRelativeHorPos = relativeHorizontalPosition; d->m_pieRelativeVerPos = relativeVerticalPosition; emit piePositionChanged(); @@ -443,7 +432,7 @@ void QPieSeries::setPieSize(qreal relativeSize) if (relativeSize < 0.0 || relativeSize > 1.0) return; - if (d->m_pieRelativeSize != relativeSize) { + if (!qFuzzyIsNull(d->m_pieRelativeSize- relativeSize)) { d->m_pieRelativeSize = relativeSize; emit pieSizeChanged(); } @@ -477,7 +466,11 @@ qreal QPieSeries::pieSize() const void QPieSeries::setPieStartAngle(qreal angle) { Q_D(QPieSeries); - if (angle >= 0 && angle <= 360 && angle != d->m_pieStartAngle && angle <= d->m_pieEndAngle) { + + if (angle < 0 || angle > 360 || angle > d->m_pieEndAngle) + return; + + if (!qFuzzyIsNull(angle - d->m_pieStartAngle)) { d->m_pieStartAngle = angle; d->updateDerivativeData(); } @@ -508,7 +501,11 @@ qreal QPieSeries::pieStartAngle() const void QPieSeries::setPieEndAngle(qreal angle) { Q_D(QPieSeries); - if (angle >= 0 && angle <= 360 && angle != d->m_pieEndAngle && angle >= d->m_pieStartAngle) { + + if (angle < 0 || angle > 360 || angle < d->m_pieStartAngle) + return; + + if (!qFuzzyIsNull(angle - d->m_pieEndAngle)) { d->m_pieEndAngle = angle; d->updateDerivativeData(); } diff --git a/src/piechart/qpieslice.cpp b/src/piechart/qpieslice.cpp index 1bba2ff..28066ea 100644 --- a/src/piechart/qpieslice.cpp +++ b/src/piechart/qpieslice.cpp @@ -257,7 +257,7 @@ qreal QPieSlice::labelArmLengthFactor() const void QPieSlice::setValue(qreal value) { Q_D(QPieSlice); - if (d->m_data.m_value != value) { + if (!qFuzzyIsNull(d->m_data.m_value - value)) { d->m_data.m_value = value; QPieSeries *series = qobject_cast(parent()); @@ -321,7 +321,7 @@ void QPieSlice::setExploded(bool exploded) void QPieSlice::setExplodeDistanceFactor(qreal factor) { Q_D(QPieSlice); - if (d->m_data.m_explodeDistanceFactor != factor) { + if (!qFuzzyIsNull(d->m_data.m_explodeDistanceFactor - factor)) { d->m_data.m_explodeDistanceFactor = factor; emit changed(); } @@ -401,7 +401,7 @@ void QPieSlice::setLabelFont(const QFont &font) void QPieSlice::setLabelArmLengthFactor(qreal factor) { Q_D(QPieSlice); - if (d->m_data.m_labelArmLengthFactor != factor) { + if (!qFuzzyIsNull(d->m_data.m_labelArmLengthFactor - factor)) { d->m_data.m_labelArmLengthFactor = factor; emit changed(); }