From 764230e0c100818773896ce8a1fcee94532797a3 2013-05-02 10:39:37 From: Miikka Heikkinen Date: 2013-05-02 10:39:37 Subject: [PATCH] Implement index based removing/replacing points in series Task-number: QTRD-1952 Change-Id: I50d374a07506814007d5bd9aeb8921165ea500fa Reviewed-by: Tomi Korpipää --- diff --git a/plugins/declarative/declarativelineseries.h b/plugins/declarative/declarativelineseries.h index a3fd19b..535d692 100644 --- a/plugins/declarative/declarativelineseries.h +++ b/plugins/declarative/declarativelineseries.h @@ -91,7 +91,9 @@ public: // from QDeclarativeParserStatus public: Q_INVOKABLE void append(qreal x, qreal y) { DeclarativeXySeries::append(x, y); } Q_INVOKABLE void replace(qreal oldX, qreal oldY, qreal newX, qreal newY) { DeclarativeXySeries::replace(oldX, oldY, newX, newY); } + Q_REVISION(3) Q_INVOKABLE void replace(int index, qreal newX, qreal newY) { DeclarativeXySeries::replace(index, newX, newY); } Q_INVOKABLE void remove(qreal x, qreal y) { DeclarativeXySeries::remove(x, y); } + Q_REVISION(3) Q_INVOKABLE void remove(int index) { DeclarativeXySeries::remove(index); } Q_INVOKABLE void insert(int index, qreal x, qreal y) { DeclarativeXySeries::insert(index, x, y); } Q_INVOKABLE void clear() { DeclarativeXySeries::clear(); } Q_INVOKABLE QPointF at(int index) { return DeclarativeXySeries::at(index); } diff --git a/plugins/declarative/declarativescatterseries.h b/plugins/declarative/declarativescatterseries.h index 549e5ff..bb45ba0 100644 --- a/plugins/declarative/declarativescatterseries.h +++ b/plugins/declarative/declarativescatterseries.h @@ -85,7 +85,9 @@ public: // from QDeclarativeParserStatus public: Q_INVOKABLE void append(qreal x, qreal y) { DeclarativeXySeries::append(x, y); } Q_INVOKABLE void replace(qreal oldX, qreal oldY, qreal newX, qreal newY) { DeclarativeXySeries::replace(oldX, oldY, newX, newY); } + Q_REVISION(3) Q_INVOKABLE void replace(int index, qreal newX, qreal newY) { DeclarativeXySeries::replace(index, newX, newY); } Q_INVOKABLE void remove(qreal x, qreal y) { DeclarativeXySeries::remove(x, y); } + Q_REVISION(3) Q_INVOKABLE void remove(int index) { DeclarativeXySeries::remove(index); } Q_INVOKABLE void insert(int index, qreal x, qreal y) { DeclarativeXySeries::insert(index, x, y); } Q_INVOKABLE void clear() { DeclarativeXySeries::clear(); } Q_INVOKABLE QPointF at(int index) { return DeclarativeXySeries::at(index); } diff --git a/plugins/declarative/declarativesplineseries.h b/plugins/declarative/declarativesplineseries.h index 6d256e6..b0bb6b4 100644 --- a/plugins/declarative/declarativesplineseries.h +++ b/plugins/declarative/declarativesplineseries.h @@ -91,7 +91,9 @@ public: // from QDeclarativeParserStatus public: Q_INVOKABLE void append(qreal x, qreal y) { DeclarativeXySeries::append(x, y); } Q_INVOKABLE void replace(qreal oldX, qreal oldY, qreal newX, qreal newY) { DeclarativeXySeries::replace(oldX, oldY, newX, newY); } + Q_REVISION(3) Q_INVOKABLE void replace(int index, qreal newX, qreal newY) { DeclarativeXySeries::replace(index, newX, newY); } Q_INVOKABLE void remove(qreal x, qreal y) { DeclarativeXySeries::remove(x, y); } + Q_REVISION(3) Q_INVOKABLE void remove(int index) { DeclarativeXySeries::remove(index); } Q_INVOKABLE void insert(int index, qreal x, qreal y) { DeclarativeXySeries::insert(index, x, y); } Q_INVOKABLE void clear() { DeclarativeXySeries::clear(); } Q_INVOKABLE QPointF at(int index) { return DeclarativeXySeries::at(index); } diff --git a/plugins/declarative/declarativexyseries.cpp b/plugins/declarative/declarativexyseries.cpp index 4320835..6de24ed 100644 --- a/plugins/declarative/declarativexyseries.cpp +++ b/plugins/declarative/declarativexyseries.cpp @@ -71,6 +71,13 @@ void DeclarativeXySeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY series->replace(oldX, oldY, newX, newY); } +void DeclarativeXySeries::replace(int index, qreal newX, qreal newY) +{ + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); + series->replace(index, newX, newY); +} + void DeclarativeXySeries::remove(qreal x, qreal y) { QXYSeries *series = qobject_cast(xySeries()); @@ -78,6 +85,13 @@ void DeclarativeXySeries::remove(qreal x, qreal y) series->remove(x, y); } +void DeclarativeXySeries::remove(int index) +{ + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); + series->remove(index); +} + void DeclarativeXySeries::insert(int index, qreal x, qreal y) { QXYSeries *series = qobject_cast(xySeries()); diff --git a/plugins/declarative/declarativexyseries.h b/plugins/declarative/declarativexyseries.h index ebecb12..6238a5a 100644 --- a/plugins/declarative/declarativexyseries.h +++ b/plugins/declarative/declarativexyseries.h @@ -41,7 +41,9 @@ public: void append(qreal x, qreal y); void replace(qreal oldX, qreal oldY, qreal newX, qreal newY); + void replace(int index, qreal newX, qreal newY); void remove(qreal x, qreal y); + void remove(int index); void insert(int index, qreal x, qreal y); void clear(); QPointF at(int index); diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 1de83d8..5492010 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -292,6 +292,17 @@ void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint) int index = d->m_points.indexOf(oldPoint); if (index == -1) return; + replace(index, newPoint); +} + +void QXYSeries::replace(int index, qreal newX, qreal newY) +{ + replace(index, QPointF(newX, newY)); +} + +void QXYSeries::replace(int index, const QPointF &newPoint) +{ + Q_D(QXYSeries); if (isValidValue(newPoint)) { d->m_points[index] = newPoint; emit pointReplaced(index); @@ -329,6 +340,12 @@ void QXYSeries::remove(const QPointF &point) int index = d->m_points.indexOf(point); if (index == -1) return; + remove(index); +} + +void QXYSeries::remove(int index) +{ + Q_D(QXYSeries); d->m_points.remove(index); emit pointRemoved(index); } diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index 65ab002..72c2de5 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -49,8 +49,11 @@ public: void append(const QList &points); void replace(qreal oldX, qreal oldY, qreal newX, qreal newY); void replace(const QPointF &oldPoint, const QPointF &newPoint); + void replace(int index, qreal newX, qreal newY); + void replace(int index, const QPointF &newPoint); void remove(qreal x, qreal y); void remove(const QPointF &point); + void remove(int index); void insert(int index, const QPointF &point); void clear(); diff --git a/tests/auto/qxyseries/tst_qxyseries.cpp b/tests/auto/qxyseries/tst_qxyseries.cpp index d3c15a1..b8af4b9 100644 --- a/tests/auto/qxyseries/tst_qxyseries.cpp +++ b/tests/auto/qxyseries/tst_qxyseries.cpp @@ -231,6 +231,20 @@ void tst_QXYSeries::remove_raw() m_series->remove(bunchOfPoints.at(i)); QTest::qWait(50); } + QCOMPARE(m_series->points().count(), 0); + + // Removal using index + for (int i = 0; i < 10; i++) + bunchOfPoints.append(QPointF(i, (qreal) rand() / (qreal) RAND_MAX)); + m_series->replace(bunchOfPoints); + m_series->remove(5); + m_series->remove(0); + QCOMPARE(m_series->points().count(), (bunchOfPoints.count() - 2)); + for (int i = bunchOfPoints.count() - 3; i >= 0; i--) { + m_series->remove(i); + QCOMPARE(m_series->points().count(), i); + } + QCOMPARE(m_series->points().count(), 0); } void tst_QXYSeries::remove_chart_data() @@ -366,6 +380,15 @@ void tst_QXYSeries::replace_raw() m_series->replace(QPointF(23,23), otherPoints.at(1)); QCOMPARE(m_series->points().at(1).x(), otherPoints.at(1).x()); QCOMPARE(m_series->points().at(1).y(), otherPoints.at(1).y()); + + // Replace using index + m_series->append(otherPoints); + m_series->replace(0, QPointF(333, 333)); + m_series->replace(3, 444, 444); + m_series->replace(m_series->count() - 1, QPointF(555, 555)); + QCOMPARE(m_series->points().at(0), QPointF(333, 333)); + QCOMPARE(m_series->points().at(3), QPointF(444, 444)); + QCOMPARE(m_series->points().at(m_series->count() - 1), QPointF(555, 555)); } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml index 6cda7e0..bbbc1df 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml @@ -79,12 +79,12 @@ Flow { text: "replace point" onClicked: { var xyPoint = series.at(series.count - 1); - series.replace(xyPoint.x, xyPoint.y, xyPoint.x, xyPoint.y + 0.1); + series.replace(series.count - 1, xyPoint.x, xyPoint.y + 0.1); } } Button { text: "remove point" - onClicked: series.remove(series.at(series.count - 1).x, series.at(series.count - 1).y); + onClicked: series.remove(series.count - 1); } Button { text: "insert point" diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml index 565eb78..ec00753 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml @@ -74,12 +74,12 @@ Flow { text: "replace point" onClicked: { var xyPoint = series.at(series.count - 1); - series.replace(xyPoint.x, xyPoint.y, xyPoint.x, xyPoint.y + 0.1); + series.replace(series.count - 1, xyPoint.x, xyPoint.y + 0.1); } } Button { text: "remove point" - onClicked: series.remove(series.at(series.count - 1).x, series.at(series.count - 1).y); + onClicked: series.remove(series.count - 1); } Button { text: "insert point" diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/LineEditor.qml b/tests/quick2chartproperties/qml/quick2chartproperties/LineEditor.qml index 9669be1..ac1e2fc 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/LineEditor.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/LineEditor.qml @@ -79,12 +79,12 @@ Flow { text: "replace point" onClicked: { var xyPoint = series.at(series.count - 1); - series.replace(xyPoint.x, xyPoint.y, xyPoint.x, xyPoint.y + 0.1); + series.replace(series.count - 1, xyPoint.x, xyPoint.y + 0.1); } } Button { text: "remove point" - onClicked: series.remove(series.at(series.count - 1).x, series.at(series.count - 1).y); + onClicked: series.remove(series.count - 1); } Button { text: "insert point" diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/ScatterEditor.qml b/tests/quick2chartproperties/qml/quick2chartproperties/ScatterEditor.qml index 2e9239c..bd996b0 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/ScatterEditor.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/ScatterEditor.qml @@ -74,12 +74,12 @@ Flow { text: "replace point" onClicked: { var xyPoint = series.at(series.count - 1); - series.replace(xyPoint.x, xyPoint.y, xyPoint.x, xyPoint.y + 0.1); + series.replace(series.count - 1, xyPoint.x, xyPoint.y + 0.1); } } Button { text: "remove point" - onClicked: series.remove(series.at(series.count - 1).x, series.at(series.count - 1).y); + onClicked: series.remove(series.count - 1); } Button { text: "insert point"