diff options
author | Andy Shaw <andy.shaw@qt.io> | 2017-08-23 09:52:48 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2017-08-23 15:09:33 +0000 |
commit | 5261307d061a2b925faffb6ea21aa3dbca1e50a9 (patch) | |
tree | aa6f1903b9936c51091958ce373c1b5811ae39d8 | |
parent | 1f47b1a7ae58702dccc57a9ccbaa905441f4fecb (diff) |
Account for very small numbers when setting the range
When using qFuzzyCompare it would not allow for very small numbers so
we remove that need and do a straight compare instead. This also
includes a test to check that zooming and then resetting gets us back
to the original range.
Task-number: QTBUG-56097
Change-Id: I722be55f602c936e03fe3cc57c30ba15c0e0bed0
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
-rw-r--r-- | src/charts/axis/valueaxis/qvalueaxis.cpp | 16 | ||||
-rw-r--r-- | tests/auto/qchart/tst_qchart.cpp | 112 | ||||
-rw-r--r-- | tests/auto/qvalueaxis/tst_qvalueaxis.cpp | 1 |
3 files changed, 115 insertions, 14 deletions
diff --git a/src/charts/axis/valueaxis/qvalueaxis.cpp b/src/charts/axis/valueaxis/qvalueaxis.cpp index e8f9698f..42dd8ed9 100644 --- a/src/charts/axis/valueaxis/qvalueaxis.cpp +++ b/src/charts/axis/valueaxis/qvalueaxis.cpp @@ -408,25 +408,13 @@ void QValueAxisPrivate::setRange(qreal min, qreal max) return; } - bool changeMin = false; - if (m_min == 0 || min == 0) - changeMin = !qFuzzyCompare(1 + m_min, 1 + min); - else - changeMin = !qFuzzyCompare(m_min, min); - - bool changeMax = false; - if (m_max == 0 || max == 0) - changeMax = !qFuzzyCompare(1 + m_max, 1 + max); - else - changeMax = !qFuzzyCompare(m_max, max); - - if (changeMin) { + if (m_min != min) { m_min = min; changed = true; emit q->minChanged(min); } - if (changeMax) { + if (m_max != max) { m_max = max; changed = true; emit q->maxChanged(max); diff --git a/tests/auto/qchart/tst_qchart.cpp b/tests/auto/qchart/tst_qchart.cpp index 75a395a2..8ca2b0e0 100644 --- a/tests/auto/qchart/tst_qchart.cpp +++ b/tests/auto/qchart/tst_qchart.cpp @@ -40,6 +40,7 @@ #include <QtCharts/QStackedBarSeries> #include <QtCharts/QValueAxis> #include <QtCharts/QBarCategoryAxis> +#include <QtCharts/QDateTimeAxis> #include "tst_definitions.h" QT_CHARTS_USE_NAMESPACE @@ -122,6 +123,8 @@ private slots: void createDefaultAxesForLineSeries(); void axisPolarOrientation(); void backgroundRoundness(); + void zoomInAndOut_data(); + void zoomInAndOut(); private: void createTestData(); @@ -1088,6 +1091,115 @@ void tst_QChart::backgroundRoundness() QVERIFY(m_chart->backgroundRoundness() == 100.0); } +void tst_QChart::zoomInAndOut_data() +{ + const qreal hourInMSecs = 60.0 * 60.0 * 1000.0; + + QTest::addColumn<QString>("axisXType"); + QTest::addColumn<QString>("axisYType"); + QTest::addColumn<qreal>("minX"); + QTest::addColumn<qreal>("maxX"); + QTest::addColumn<qreal>("minY"); + QTest::addColumn<qreal>("maxY"); + + QTest::newRow("value-value-normal") << "QValueAxis" << "QValueAxis" << 0.0 << 100.0 << 0.0 << 100.0; + QTest::newRow("value-value-small") << "QValueAxis" << "QValueAxis" << -1e-12 << 1e-13 << -1e-12 << 1e-13; + QTest::newRow("value-value-mixed") << "QValueAxis" << "QValueAxis" << 0.0 << 100.0 << -1e-12 << 1e-13; + + QTest::newRow("datetime-datetime-normal") << "QDateTimeAxis" << "QDateTimeAxis" + << 0.0 << hourInMSecs << 0.0 << hourInMSecs; + QTest::newRow("datetime-datetime-small") << "QDateTimeAxis" << "QDateTimeAxis" + << 0.0 << 60.0 << 0.0 << 60.0; + QTest::newRow("datetime-datetime-mixed") << "QDateTimeAxis" << "QDateTimeAxis" + << 0.0 << hourInMSecs << 0.0 << 60.0; +} + +#define CHECK_AXIS_RANGES_MATCH \ + if (valueAxisX) { \ + QVERIFY(valueAxisX->min() == minX); \ + QVERIFY(valueAxisX->max() == maxX); \ + } else if (dateTimeAxisX) { \ + QVERIFY(dateTimeAxisX->min().toMSecsSinceEpoch() == minX); \ + QVERIFY(dateTimeAxisX->max().toMSecsSinceEpoch() == maxX); \ + } \ + if (valueAxisY) { \ + QVERIFY(valueAxisY->min() == minY); \ + QVERIFY(valueAxisY->max() == maxY); \ + } else if (dateTimeAxisY) { \ + QVERIFY(dateTimeAxisY->min().toMSecsSinceEpoch() == minY); \ + QVERIFY(dateTimeAxisY->max().toMSecsSinceEpoch() == maxY); \ + } + +void tst_QChart::zoomInAndOut() +{ + QFETCH(QString, axisXType); + QFETCH(QString, axisYType); + QFETCH(qreal, minX); + QFETCH(qreal, maxX); + QFETCH(qreal, minY); + QFETCH(qreal, maxY); + + createTestData(); + QAbstractAxis *axisX = 0; + QAbstractAxis *axisY = 0; + QValueAxis *valueAxisX = 0; + QValueAxis *valueAxisY = 0; + QDateTimeAxis *dateTimeAxisX = 0; + QDateTimeAxis *dateTimeAxisY = 0; + + if (axisXType == "QValueAxis") { + axisX = valueAxisX = new QValueAxis(m_chart); + valueAxisX->setRange(minX, maxX); + } else if (axisXType == "QDateTimeAxis") { + axisX = dateTimeAxisX = new QDateTimeAxis(m_chart); + dateTimeAxisX->setRange(QDateTime::fromMSecsSinceEpoch(minX), QDateTime::fromMSecsSinceEpoch(maxX)); + } + if (axisXType == "QValueAxis") { + axisY = valueAxisY = new QValueAxis(m_chart); + valueAxisY->setRange(minY, maxY); + } else if (axisXType == "QDateTimeAxis") { + axisY = dateTimeAxisY = new QDateTimeAxis(m_chart); + dateTimeAxisY->setRange(QDateTime::fromMSecsSinceEpoch(minY), QDateTime::fromMSecsSinceEpoch(maxY)); + } + + m_chart->setAxisX(axisX, m_chart->series().first()); + m_chart->setAxisY(axisY, m_chart->series().first()); + CHECK_AXIS_RANGES_MATCH + + m_chart->zoomIn(); + if (valueAxisX) { + QVERIFY(valueAxisX->min() != minX); + QVERIFY(valueAxisX->max() != maxX); + } else if (dateTimeAxisX) { + QVERIFY(dateTimeAxisX->min().toMSecsSinceEpoch() != minX); + QVERIFY(dateTimeAxisX->max().toMSecsSinceEpoch() != maxX); + } + if (valueAxisY) { + QVERIFY(valueAxisY->min() != minY); + QVERIFY(valueAxisY->max() != maxY); + } else if (dateTimeAxisY) { + QVERIFY(dateTimeAxisY->min().toMSecsSinceEpoch() != minY); + QVERIFY(dateTimeAxisY->max().toMSecsSinceEpoch() != maxY); + } + + m_chart->zoomReset(); + CHECK_AXIS_RANGES_MATCH + + m_chart->zoomIn(); + m_chart->zoomIn(); + m_chart->zoomReset(); + CHECK_AXIS_RANGES_MATCH + + m_chart->zoomOut(); + m_chart->zoomReset(); + CHECK_AXIS_RANGES_MATCH + + m_chart->zoomOut(); + m_chart->zoomOut(); + m_chart->zoomReset(); + CHECK_AXIS_RANGES_MATCH +} + QTEST_MAIN(tst_QChart) #include "tst_qchart.moc" diff --git a/tests/auto/qvalueaxis/tst_qvalueaxis.cpp b/tests/auto/qvalueaxis/tst_qvalueaxis.cpp index d5d69a5b..4de89cb1 100644 --- a/tests/auto/qvalueaxis/tst_qvalueaxis.cpp +++ b/tests/auto/qvalueaxis/tst_qvalueaxis.cpp @@ -285,6 +285,7 @@ void tst_QValueAxis::range_raw_data() QTest::newRow("1.0 - 101.0") << (qreal)-1.0 << (qreal)101.0; QTest::newRow("25.0 - 75.0") << (qreal)25.0 << (qreal)75.0; QTest::newRow("101.0") << (qreal)40.0 << (qreal)60.0; + QTest::newRow("smallNumbers") << (qreal)-1e-12 << (qreal)1e-13; } void tst_QValueAxis::range_raw() |