From f5ae7379352559384a2bd839b7e46a2e4a1aa051 2012-07-17 09:43:22 From: Marek Rosa Date: 2012-07-17 09:43:22 Subject: [PATCH] Fixes and tests to Interval axis --- diff --git a/src/axis/intervalsaxis/chartintervalsaxisx.cpp b/src/axis/intervalsaxis/chartintervalsaxisx.cpp index 0ed8578..2325061 100644 --- a/src/axis/intervalsaxis/chartintervalsaxisx.cpp +++ b/src/axis/intervalsaxis/chartintervalsaxisx.cpp @@ -45,6 +45,10 @@ QVector ChartIntervalAxisX::calculateLayout() const QIntervalsAxis *axis = qobject_cast(m_chartAxis); int tickCount = axis->intervalsLabels().count() + 1; QVector points; + + if (tickCount < 2) + return points; + points.resize(tickCount); qreal scale = m_rect.width() / axis->max(); diff --git a/src/axis/intervalsaxis/chartintervalsaxisy.cpp b/src/axis/intervalsaxis/chartintervalsaxisy.cpp index 7658490..2c983e4 100644 --- a/src/axis/intervalsaxis/chartintervalsaxisy.cpp +++ b/src/axis/intervalsaxis/chartintervalsaxisy.cpp @@ -45,6 +45,10 @@ QVector ChartIntervalAxisY::calculateLayout() const QIntervalsAxis *axis = qobject_cast(m_chartAxis); int tickCount = axis->intervalsLabels().count() + 1; QVector points; + + if (tickCount < 2) + return points; + points.resize(tickCount); qreal scale = m_rect.height() / axis->max(); diff --git a/src/axis/intervalsaxis/qintervalsaxis.cpp b/src/axis/intervalsaxis/qintervalsaxis.cpp index f2260e0..6b5bb7f 100644 --- a/src/axis/intervalsaxis/qintervalsaxis.cpp +++ b/src/axis/intervalsaxis/qintervalsaxis.cpp @@ -76,31 +76,31 @@ QIntervalsAxis::QIntervalsAxis(QIntervalsAxisPrivate &d,QObject *parent):QValues void QIntervalsAxis::append(const QString& intervalLabel, qreal interval) { Q_D(QIntervalsAxis); + if (!d->m_intervals.contains(intervalLabel)) { if(d->m_intervals.isEmpty()){ Range range(d->m_categoryMinimum,interval); d->m_intervalsMap.insert(intervalLabel, range); d->m_intervals.append(intervalLabel); - }else{ + }else if (interval > intervalMax(d->m_intervals.last())){ Range range = d->m_intervalsMap.value(d->m_intervals.last()); d->m_intervalsMap.insert(intervalLabel, Range(range.second,interval)); d->m_intervals.append(intervalLabel); - } -// setRange(d->m_min,interval); + } } } void QIntervalsAxis::setFisrtIntervalMinimum(qreal min) { - Q_D(QIntervalsAxis); - if(d->m_intervals.isEmpty()){ - d->m_categoryMinimum = min; - }else{ - Range range = d->m_intervalsMap.value(d->m_intervals.first()); - d->m_intervalsMap.insert(d->m_intervals.first(), Range(min, range.second)); - setRange(min, d->m_min); - } + Q_D(QIntervalsAxis); + if(d->m_intervals.isEmpty()){ + d->m_categoryMinimum = min; + }else{ + Range range = d->m_intervalsMap.value(d->m_intervals.first()); + d->m_intervalsMap.insert(d->m_intervals.first(), Range(min, range.second)); +// setRange(min, d->m_min); + } } qreal QIntervalsAxis::intervalMin(const QString& intervalLabel) const @@ -120,8 +120,29 @@ qreal QIntervalsAxis::intervalMax(const QString& intervalLabel) const */ void QIntervalsAxis::remove(const QString &intervalLabel) { - Q_UNUSED(intervalLabel); - //TODO + Q_D(QIntervalsAxis); + int labelIndex = d->m_intervals.indexOf(intervalLabel); + + // check if such label exists + if (labelIndex != -1) { + d->m_intervals.removeAt(labelIndex); + d->m_intervalsMap.remove(intervalLabel); + + // the range of the interval that follows (if exists) needs to be updated + if (labelIndex < d->m_intervals.count()) { + QString label = d->m_intervals.at(labelIndex); + Range range = d->m_intervalsMap.value(label); + + // set the range + if (labelIndex == 0) { + range.first = d->m_categoryMinimum; + d->m_intervalsMap.insert(label, range); + } else { + range.first = d->m_intervalsMap.value(d->m_intervals.at(labelIndex - 1)).second; + d->m_intervalsMap.insert(label, range); + } + } + } } QStringList QIntervalsAxis::intervalsLabels() @@ -144,7 +165,7 @@ int QIntervalsAxis::count() const */ QAbstractAxis::AxisType QIntervalsAxis::type() const { - return AxisTypeCategories; + return QAbstractAxis::AxisTypeIntervals; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -171,7 +192,6 @@ void QIntervalsAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int coun Q_UNUSED(count); m_min = min; m_max = max; -// m_ticksCount = count; } ChartAxis* QIntervalsAxisPrivate::createGraphics(ChartPresenter* presenter) diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index 7ed52c8..82a3d38 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -50,7 +50,8 @@ public: enum AxisType { AxisTypeNoAxis = 0x0, AxisTypeValues = 0x1, - AxisTypeCategories = 0x2 + AxisTypeCategories = 0x2, + AxisTypeIntervals = 0x3 }; Q_DECLARE_FLAGS(AxisTypes, AxisType) diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 48479d7..7fcceaa 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -20,7 +20,8 @@ SUBDIRS += \ qhorizontalbarseries \ qhorizontalstackedbarseries \ qhorizontalpercentbarseries \ - qvaluesaxis + qvaluesaxis \ + qintervalsaxis test_private:{ SUBDIRS += domain chartdataset diff --git a/tests/auto/qabstractaxis/tst_qabstractaxis.cpp b/tests/auto/qabstractaxis/tst_qabstractaxis.cpp index eab0856..776d721 100644 --- a/tests/auto/qabstractaxis/tst_qabstractaxis.cpp +++ b/tests/auto/qabstractaxis/tst_qabstractaxis.cpp @@ -31,7 +31,7 @@ void tst_QAbstractAxis::cleanupTestCase() { } -void tst_QAbstractAxis::init(QAbstractAxis* axis,QAbstractSeries* series) +void tst_QAbstractAxis::init(QAbstractAxis* axis, QAbstractSeries* series) { m_axis = axis; m_series = series; diff --git a/tests/auto/qintervalsaxis/qintervalsaxis.pro b/tests/auto/qintervalsaxis/qintervalsaxis.pro new file mode 100644 index 0000000..20a4976 --- /dev/null +++ b/tests/auto/qintervalsaxis/qintervalsaxis.pro @@ -0,0 +1,5 @@ +!include( ../auto.pri ) { + error( "Couldn't find the auto.pri file!" ) +} +HEADERS += ../qabstractaxis/tst_qabstractaxis.h +SOURCES += tst_qintervalsaxis.cpp ../qabstractaxis/tst_qabstractaxis.cpp diff --git a/tests/auto/qintervalsaxis/tst_qintervalsaxis.cpp b/tests/auto/qintervalsaxis/tst_qintervalsaxis.cpp new file mode 100644 index 0000000..5b70985 --- /dev/null +++ b/tests/auto/qintervalsaxis/tst_qintervalsaxis.cpp @@ -0,0 +1,299 @@ +/**************************************************************************** + ** + ** Copyright (C) 2012 Digia Plc + ** All rights reserved. + ** For any questions to Digia, please use contact form at http://qt.digia.com + ** + ** This file is part of the Qt Commercial Charts Add-on. + ** + ** $QT_BEGIN_LICENSE$ + ** Licensees holding valid Qt Commercial licenses may use this file in + ** accordance with the Qt Commercial License Agreement provided with the + ** Software or, alternatively, in accordance with the terms contained in + ** a written agreement between you and Digia. + ** + ** If you have questions regarding the use of this file, please use + ** contact form at http://qt.digia.com + ** $QT_END_LICENSE$ + ** + ****************************************************************************/ + +#include "../qabstractaxis/tst_qabstractaxis.h" +#include "qintervalsaxis.h" +#include + +class tst_QIntervalsAxis: public tst_QAbstractAxis +{ +Q_OBJECT + +public slots: + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + +private slots: + void qintervalsaxis_data(); + void qintervalsaxis(); + + void max_raw_data(); + void max_raw(); + void max_data(); + void max(); + void max_animation_data(); + void max_animation(); + void min_raw_data(); + void min_raw(); + void min_data(); + void min(); + void min_animation_data(); + void min_animation(); + void range_raw_data(); + void range_raw(); + void range_data(); + void range(); + void range_animation_data(); + void range_animation(); + + void interval_data(); + void interval(); + +private: + QIntervalsAxis* m_intervalsaxis; + QLineSeries* m_series; +}; + +void tst_QIntervalsAxis::initTestCase() +{ +} + +void tst_QIntervalsAxis::cleanupTestCase() +{ +} + +void tst_QIntervalsAxis::init() +{ + m_intervalsaxis = new QIntervalsAxis(); + m_series = new QLineSeries(); + *m_series << QPointF(-100, -100) << QPointF(0, 0) << QPointF(100, 100); + tst_QAbstractAxis::init(m_intervalsaxis, m_series); + m_chart->addSeries(m_series); + m_chart->createDefaultAxes(); +} + +void tst_QIntervalsAxis::cleanup() +{ + delete m_series; + delete m_intervalsaxis; + m_series = 0; + m_intervalsaxis = 0; + tst_QAbstractAxis::cleanup(); +} + +void tst_QIntervalsAxis::qintervalsaxis_data() +{ +} + +void tst_QIntervalsAxis::qintervalsaxis() +{ + qabstractaxis(); + + QVERIFY(qFuzzyIsNull(m_intervalsaxis->max())); + QVERIFY(qFuzzyIsNull(m_intervalsaxis->min())); + QCOMPARE(m_intervalsaxis->type(), QAbstractAxis::AxisTypeIntervals); + + m_chart->setAxisX(m_intervalsaxis, m_series); + m_view->show(); + QTest::qWaitForWindowShown(m_view); + + QVERIFY(!qFuzzyIsNull(m_intervalsaxis->max())); + QVERIFY(!qFuzzyIsNull(m_intervalsaxis->min())); +} + +void tst_QIntervalsAxis::max_raw_data() +{ + QTest::addColumn("max"); + QTest::newRow("1.0") << 1.0; + QTest::newRow("50.0") << 50.0; + QTest::newRow("101.0") << 101.0; +} + +void tst_QIntervalsAxis::max_raw() +{ + QFETCH(qreal, max); + + QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal))); + QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal))); + QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal))); + + m_intervalsaxis->setMax(max); + QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Not equal"); + + QCOMPARE(spy0.count(), 1); + QCOMPARE(spy1.count(), 0); + QCOMPARE(spy2.count(), 1); +} + +void tst_QIntervalsAxis::max_data() +{ + max_raw_data(); +} + +void tst_QIntervalsAxis::max() +{ + m_chart->setAxisX(m_intervalsaxis, m_series); + m_view->show(); + QTest::qWaitForWindowShown(m_view); + max_raw(); +} + +void tst_QIntervalsAxis::max_animation_data() +{ + max_data(); +} + +void tst_QIntervalsAxis::max_animation() +{ + m_chart->setAnimationOptions(QChart::GridAxisAnimations); + max(); +} + +void tst_QIntervalsAxis::min_raw_data() +{ + QTest::addColumn("min"); + QTest::newRow("-1.0") << -1.0; + QTest::newRow("-50.0") << -50.0; + QTest::newRow("-101.0") << -101.0; +} + +void tst_QIntervalsAxis::min_raw() +{ + QFETCH(qreal, min); + + QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal))); + QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal))); + QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal))); + + m_intervalsaxis->setMin(min); + QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Not equal"); + + QCOMPARE(spy0.count(), 0); + QCOMPARE(spy1.count(), 1); + QCOMPARE(spy2.count(), 1); +} + +void tst_QIntervalsAxis::min_data() +{ + min_raw_data(); +} + +void tst_QIntervalsAxis::min() +{ + m_chart->setAxisX(m_intervalsaxis, m_series); + m_view->show(); + QTest::qWaitForWindowShown(m_view); + min_raw(); +} + +void tst_QIntervalsAxis::min_animation_data() +{ + min_data(); +} + +void tst_QIntervalsAxis::min_animation() +{ + m_chart->setAnimationOptions(QChart::GridAxisAnimations); + min(); +} + +void tst_QIntervalsAxis::range_raw_data() +{ + QTest::addColumn("min"); + QTest::addColumn("max"); + QTest::newRow("1.0 - 101.0") << -1.0 << 101.0; + QTest::newRow("25.0 - 75.0") << 25.0 << 75.0; + QTest::newRow("101.0") << 40.0 << 60.0; +} + +void tst_QIntervalsAxis::range_raw() +{ + QFETCH(qreal, min); + QFETCH(qreal, max); + + QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal))); + QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal))); + QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal))); + + m_intervalsaxis->setRange(min, max); + QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Min not equal"); + QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Max not equal"); + + QCOMPARE(spy0.count(), 1); + QCOMPARE(spy1.count(), 1); + QCOMPARE(spy2.count(), 1); +} + +void tst_QIntervalsAxis::range_data() +{ + range_raw_data(); +} + +void tst_QIntervalsAxis::range() +{ + m_chart->setAxisX(m_intervalsaxis, m_series); + m_view->show(); + QTest::qWaitForWindowShown(m_view); + range_raw(); +} + +void tst_QIntervalsAxis::range_animation_data() +{ + range_data(); +} + +void tst_QIntervalsAxis::range_animation() +{ + m_chart->setAnimationOptions(QChart::GridAxisAnimations); + range(); +} + +void tst_QIntervalsAxis::interval_data() +{ + // +} + +void tst_QIntervalsAxis::interval() +{ + // append one correct interval + m_intervalsaxis->append("first", (qreal)45); + QCOMPARE(m_intervalsaxis->intervalMin("first"), (qreal)0); + QCOMPARE(m_intervalsaxis->intervalMax("first"), (qreal)45); + + // append one more correct interval + m_intervalsaxis->append("second", (qreal)75); + QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)45); + QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75); + + // append one incorrect interval + m_intervalsaxis->append("third", (qreal)15); + QCOMPARE(m_intervalsaxis->count(), 2); + QCOMPARE(m_intervalsaxis->intervalMax(m_intervalsaxis->intervalsLabels().last()), (qreal)75); +// QCOMPARE(intervalMax("first"), (qreal)75); + + // append one more correct interval + m_intervalsaxis->append("third", (qreal)100); + QCOMPARE(m_intervalsaxis->count(), 3); + QCOMPARE(m_intervalsaxis->intervalMin("third"), (qreal)75); + QCOMPARE(m_intervalsaxis->intervalMax("third"), (qreal)100); + + // remove one interval + m_intervalsaxis->remove("first"); + QCOMPARE(m_intervalsaxis->count(), 2); + QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)0); // second interval should extend to firstInterval minimum + QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75); + +} + +QTEST_MAIN(tst_QIntervalsAxis) +#include "tst_qintervalsaxis.moc" +