From b8e8466add8b472d096b36c8a6b86a824ef028c4 2012-06-20 13:08:36 From: sauimone Date: 2012-06-20 13:08:36 Subject: [PATCH] barset: removed boolean return value from remove(index,count) function --- diff --git a/plugins/declarative/declarativebarseries.h b/plugins/declarative/declarativebarseries.h index eff8639..873aa1d 100644 --- a/plugins/declarative/declarativebarseries.h +++ b/plugins/declarative/declarativebarseries.h @@ -47,7 +47,7 @@ public: public: // From QBarSet Q_INVOKABLE void append(qreal value) { QBarSet::append(value); } Q_INVOKABLE void append(qreal x, qreal y) { QBarSet::append(QPointF(x, y)); } - Q_INVOKABLE bool remove(const int index, const int count = 1) { return QBarSet::remove(index, count); } + Q_INVOKABLE void remove(const int index, const int count = 1) { QBarSet::remove(index, count); } Q_INVOKABLE void replace(int index, qreal value) { QBarSet::replace(index, value); } Q_INVOKABLE void replace(int index, qreal x, qreal y) { QBarSet::replace(index, QPointF(x, y)); } Q_INVOKABLE QPointF at(int index) { return QBarSet::at(index); } diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 77e9bc3..e187a9e 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -338,16 +338,15 @@ void QBarSet::insert(const int index, const QPointF value) /*! Removes \a count number of values from the set starting at \a index. - Returns true if remove operation was succesfull. \sa insert() */ -bool QBarSet::remove(const int index, const int count) +void QBarSet::remove(const int index, const int count) { - bool success = d_ptr->remove(index,count); - if (success) { - emit valuesRemoved(index,count); + int removedCount = d_ptr->remove(index,count); + if (removedCount > 0) { + emit valuesRemoved(index,removedCount); } - return success; + return; } /*! @@ -610,19 +609,25 @@ void QBarSetPrivate::insert(const int index, const QPointF value) emit restructuredBars(); } -bool QBarSetPrivate::remove(const int index, const int count) +int QBarSetPrivate::remove(const int index, const int count) { - if (index < 0 || (index + count) > m_values.count()) { - // cant remove more values than there are - return false; + int removeCount = count; + + if ((index <0) || (m_values.count() == 0)) { + // Invalid index or not values in list, remove nothing. + return 0; + } else if ((index + count) > m_values.count()) { + // Trying to remove more items than list has. Limit amount to be removed. + removeCount = m_values.count() - index; } - int c = count; - while (c > 0) { + + int c = 0; + while (c < removeCount) { m_values.removeAt(index); - c--; + c++; } emit restructuredBars(); - return true; + return removeCount; } void QBarSetPrivate::replace(const int index, const qreal value) diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 6f49844..9d056a3 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -58,7 +58,7 @@ public: void insert(const int index, const qreal value); void insert(const int index, const QPointF value); - bool remove(const int index, const int count = 1); + void remove(const int index, const int count = 1); void replace(const int index, const qreal value); void replace(const int index, const QPointF value); QPointF at(const int index) const; diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index 9762853..1888dba 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -52,7 +52,7 @@ public: void insert(const int index, const qreal value); void insert(const int index, const QPointF value); - bool remove(const int index, const int count); + int remove(const int index, const int count); void replace(const int index, const qreal value); void replace(const int index, const QPointF value); diff --git a/tests/auto/qbarset/tst_qbarset.cpp b/tests/auto/qbarset/tst_qbarset.cpp index a0cb320..365f422 100644 --- a/tests/auto/qbarset/tst_qbarset.cpp +++ b/tests/auto/qbarset/tst_qbarset.cpp @@ -233,6 +233,15 @@ void tst_QBarSet::remove() QCOMPARE(m_barset->at(2).y(), 4.0); QCOMPARE(m_barset->count(), 3); QCOMPARE(m_barset->sum(), 7.0); + QCOMPARE(valueSpy.count(), 1); + + QList valueSpyArg = valueSpy.takeFirst(); + // Verify index of removed signal + QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(0).toInt() == 2); + // Verify count of removed signal + QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(1).toInt() == 1); // Remove first m_barset->remove(0); // 2.0 4.0 @@ -241,6 +250,16 @@ void tst_QBarSet::remove() QCOMPARE(m_barset->count(), 2); QCOMPARE(m_barset->sum(), 6.0); + QCOMPARE(valueSpy.count(), 1); + valueSpyArg = valueSpy.takeFirst(); + // Verify index of removed signal + QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(0).toInt() == 0); + // Verify count of removed signal + QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(1).toInt() == 1); + + // Illegal indexes m_barset->remove(4); QCOMPARE(m_barset->count(), 2); @@ -249,7 +268,23 @@ void tst_QBarSet::remove() QCOMPARE(m_barset->count(), 2); QCOMPARE(m_barset->sum(), 6.0); - QCOMPARE(valueSpy.count(), 2); + // nothing removed, no signals should be emitted + QCOMPARE(valueSpy.count(), 0); + + // Remove more items than list has + m_barset->remove(0,312); + QCOMPARE(m_barset->count(), 0); + QVERIFY(qFuzzyIsNull(m_barset->sum())); + + QCOMPARE(valueSpy.count(), 1); + valueSpyArg = valueSpy.takeFirst(); + + // Verify index of removed signal + QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(0).toInt() == 0); + // Verify count of removed signal (expect 2 values removed, because list had only 2 items) + QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); + QVERIFY(valueSpyArg.at(1).toInt() == 2); } void tst_QBarSet::replace_data()