From dd9d32f835df619a13f803fbffbd7d21a70f3f10 2012-03-28 13:05:17 From: Jani Honkonen Date: 2012-03-28 13:05:17 Subject: [PATCH] Use qFuzzyIsNull to compare (in)equality of real values --- diff --git a/demos/piechartcustomization/main.cpp b/demos/piechartcustomization/main.cpp index e4638fb..8d63692 100644 --- a/demos/piechartcustomization/main.cpp +++ b/demos/piechartcustomization/main.cpp @@ -108,7 +108,7 @@ public Q_SLOTS: void updateWidth(double width) { - if (width != m_pen.widthF()) { + if (!qFuzzyIsNull(width - m_pen.widthF())) { m_pen.setWidthF(width); emit changed(); } diff --git a/src/axis/axisitem.cpp b/src/axis/axisitem.cpp index 7461bb7..1be0394 100644 --- a/src/axis/axisitem.cpp +++ b/src/axis/axisitem.cpp @@ -337,7 +337,7 @@ void Axis::setLayout(QVector &layout) bool Axis::isEmpty() { - return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0; + return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0; } //handlers @@ -390,7 +390,8 @@ void Axis::handleAxisUpdated() void Axis::handleRangeChanged(qreal min, qreal max,int tickCount) { - if (min==max || tickCount<2) return; + if (qFuzzyIsNull(min - max) || tickCount < 2) + return; m_min = min; m_max = max; diff --git a/src/axis/qchartaxis.cpp b/src/axis/qchartaxis.cpp index 1b93f63..fc50a35 100644 --- a/src/axis/qchartaxis.cpp +++ b/src/axis/qchartaxis.cpp @@ -328,13 +328,13 @@ void QChartAxis::setMax(qreal max) void QChartAxis::setRange(qreal min, qreal max) { bool changed = false; - if (m_min != min) { + if (!qFuzzyIsNull(m_min - min)) { m_min = min; changed = true; emit minChanged(min); } - if (m_max != max) { + if (!qFuzzyIsNull(m_max - max)) { m_max = max; changed = true; emit maxChanged(max); diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 2de8ee8..d259cd0 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -139,7 +139,7 @@ QVector BarChartItem::calculateLayout() value->setPos(xPos, yPos-barHeight / 2); value->setPen(barSet->floatingValuePen()); - if (m_series->valueAt(set,category) != 0) { + if (!qFuzzyIsNull(m_series->valueAt(set,category))) { value->setValueString(QString::number(m_series->valueAt(set, category))); } else { value->setValueString(QString("")); diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index e68eaff..90b0a64 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -58,7 +58,7 @@ QVector PercentBarChartItem::calculateLayout() value->setPos(xPos, yPos-barHeight / 2); value->setPen(barSet->floatingValuePen()); - if (m_series->valueAt(set,category) != 0) { + if (!qFuzzyIsNull(m_series->valueAt(set,category))) { int p = m_series->percentageAt(set,category) * 100; QString vString(QString::number(p)); vString.truncate(3); diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index c562340..da54207 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -64,7 +64,7 @@ QVector StackedBarChartItem::calculateLayout() value->setPos(xPos, yPos-barHeight / 2); value->setPen(barSet->floatingValuePen()); - if (m_series->valueAt(set, category) != 0) { + if (!qFuzzyIsNull(m_series->valueAt(set, category))) { value->setValueString(QString::number(m_series->valueAt(set,category))); } else { value->setValueString(QString("")); diff --git a/src/domain.cpp b/src/domain.cpp index c3a1639..6b345b0 100644 --- a/src/domain.cpp +++ b/src/domain.cpp @@ -39,7 +39,7 @@ void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCo tickYChanged=true; } - if(m_minX!=minX || m_maxX!=maxX) { + if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) { if(m_niceNumbers) looseNiceNumbers(minX, maxX, m_tickXCount); m_minX=minX; m_maxX=maxX; @@ -48,7 +48,7 @@ void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCo emit rangeXChanged(minX,maxX, m_tickXCount); } - if(m_minY!=minY || m_maxY!=maxY) { + if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) { if(m_niceNumbers) looseNiceNumbers(minY, maxY, m_tickYCount); m_minY=minY; m_maxY=maxY; @@ -124,7 +124,7 @@ qreal Domain::spanY() const bool Domain::isEmpty() const { - return spanX()==0 || spanY()==0; + return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY()); } void Domain::zoomIn(const QRectF& rect, const QSizeF& size) @@ -235,10 +235,10 @@ qreal Domain::niceNumber(qreal x,bool ceiling) bool operator== (const Domain &domain1, const Domain &domain2) { - return (domain1.m_maxX == domain2.m_maxX && - domain1.m_maxY == domain2.m_maxY && - domain1.m_minX == domain2.m_minX && - domain1.m_minY == domain2.m_minY); + return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) && + qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) && + qFuzzyIsNull(domain1.m_minX - domain2.m_minX) && + qFuzzyIsNull(domain1.m_minY - domain2.m_minY)); } bool operator!= (const Domain &domain1, const Domain &domain2) diff --git a/src/piechart/pieslicedata_p.h b/src/piechart/pieslicedata_p.h index d7d46ec..1f44dab 100644 --- a/src/piechart/pieslicedata_p.h +++ b/src/piechart/pieslicedata_p.h @@ -56,7 +56,7 @@ public: bool operator!=(const PieSliceData &other) { - if (m_value != other.m_value) + if (!qFuzzyIsNull(m_value - other.m_value)) return true; if (m_slicePen != other.m_slicePen || diff --git a/src/scatterseries/qscatterseries.cpp b/src/scatterseries/qscatterseries.cpp index a32c300..085c893 100644 --- a/src/scatterseries/qscatterseries.cpp +++ b/src/scatterseries/qscatterseries.cpp @@ -89,7 +89,7 @@ qreal QScatterSeries::size() const */ void QScatterSeries::setSize(qreal size) { - if (m_size != size) { + if (!qFuzzyIsNull(m_size - size)) { m_size = size; emit updated(); } diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index 8690ed4..f595065 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -143,7 +143,7 @@ void XYChartItem::handleGeometryChanged(const QRectF &rect) bool XYChartItem::isEmpty() { - return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ; + return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY); } void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) diff --git a/test/auto/domain/tst_domain.cpp b/test/auto/domain/tst_domain.cpp index 238b91c..9e96f35 100644 --- a/test/auto/domain/tst_domain.cpp +++ b/test/auto/domain/tst_domain.cpp @@ -99,12 +99,12 @@ void tst_Domain::handleAxisRangeXChanged() domain.handleAxisXChanged(min, max); QList arg0 = spy0.first(); - QVERIFY(arg0.at(0).toReal() == min); - QVERIFY(arg0.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg0.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg0.at(1).toReal() - max)); QList arg1 = spy1.first(); - QVERIFY(arg1.at(0).toReal() == min); - QVERIFY(arg1.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max)); QCOMPARE(spy0.count(), 1); QCOMPARE(spy1.count(), 1); @@ -136,12 +136,12 @@ void tst_Domain::handleAxisRangeYChanged() domain.handleAxisYChanged(min, max,5); QList arg0 = spy0.first(); - QVERIFY(arg0.at(2).toReal() == min); - QVERIFY(arg0.at(3).toReal() == max); + QVERIFY(qFuzzyIsNull(arg0.at(2).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg0.at(3).toReal() - max)); QList arg1 = spy2.first(); - QVERIFY(arg1.at(0).toReal() == min); - QVERIFY(arg1.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max)); QCOMPARE(spy0.count(), 1); QCOMPARE(spy1.count(), 0); @@ -415,12 +415,12 @@ void tst_Domain::setRangeX() domain.setRangeX(min, max); QList arg0 = spy0.first(); - QVERIFY(arg0.at(0).toReal() == min); - QVERIFY(arg0.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg0.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg0.at(1).toReal() - max)); QList arg1 = spy1.first(); - QVERIFY(arg1.at(0).toReal() == min); - QVERIFY(arg1.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max)); QCOMPARE(spy0.count(), 1); QCOMPARE(spy1.count(), 1); @@ -450,12 +450,12 @@ void tst_Domain::setRangeY() domain.setRangeY(min, max); QList arg0 = spy0.first(); - QVERIFY(arg0.at(2).toReal() == min); - QVERIFY(arg0.at(3).toReal() == max); + QVERIFY(qFuzzyIsNull(arg0.at(2).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg0.at(3).toReal() - max)); QList arg1 = spy2.first(); - QVERIFY(arg1.at(0).toReal() == min); - QVERIFY(arg1.at(1).toReal() == max); + QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min)); + QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max)); QCOMPARE(spy0.count(), 1); QCOMPARE(spy1.count(), 0);