##// END OF EJS Templates
barset: removed boolean return value from remove(index,count) function
sauimone -
r1514:b8e8466add8b
parent child
Show More
@@ -47,7 +47,7 public:
47 public: // From QBarSet
47 public: // From QBarSet
48 Q_INVOKABLE void append(qreal value) { QBarSet::append(value); }
48 Q_INVOKABLE void append(qreal value) { QBarSet::append(value); }
49 Q_INVOKABLE void append(qreal x, qreal y) { QBarSet::append(QPointF(x, y)); }
49 Q_INVOKABLE void append(qreal x, qreal y) { QBarSet::append(QPointF(x, y)); }
50 Q_INVOKABLE bool remove(const int index, const int count = 1) { return QBarSet::remove(index, count); }
50 Q_INVOKABLE void remove(const int index, const int count = 1) { QBarSet::remove(index, count); }
51 Q_INVOKABLE void replace(int index, qreal value) { QBarSet::replace(index, value); }
51 Q_INVOKABLE void replace(int index, qreal value) { QBarSet::replace(index, value); }
52 Q_INVOKABLE void replace(int index, qreal x, qreal y) { QBarSet::replace(index, QPointF(x, y)); }
52 Q_INVOKABLE void replace(int index, qreal x, qreal y) { QBarSet::replace(index, QPointF(x, y)); }
53 Q_INVOKABLE QPointF at(int index) { return QBarSet::at(index); }
53 Q_INVOKABLE QPointF at(int index) { return QBarSet::at(index); }
@@ -338,16 +338,15 void QBarSet::insert(const int index, const QPointF value)
338
338
339 /*!
339 /*!
340 Removes \a count number of values from the set starting at \a index.
340 Removes \a count number of values from the set starting at \a index.
341 Returns true if remove operation was succesfull.
342 \sa insert()
341 \sa insert()
343 */
342 */
344 bool QBarSet::remove(const int index, const int count)
343 void QBarSet::remove(const int index, const int count)
345 {
344 {
346 bool success = d_ptr->remove(index,count);
345 int removedCount = d_ptr->remove(index,count);
347 if (success) {
346 if (removedCount > 0) {
348 emit valuesRemoved(index,count);
347 emit valuesRemoved(index,removedCount);
349 }
348 }
350 return success;
349 return;
351 }
350 }
352
351
353 /*!
352 /*!
@@ -610,19 +609,25 void QBarSetPrivate::insert(const int index, const QPointF value)
610 emit restructuredBars();
609 emit restructuredBars();
611 }
610 }
612
611
613 bool QBarSetPrivate::remove(const int index, const int count)
612 int QBarSetPrivate::remove(const int index, const int count)
614 {
613 {
615 if (index < 0 || (index + count) > m_values.count()) {
614 int removeCount = count;
616 // cant remove more values than there are
615
617 return false;
616 if ((index <0) || (m_values.count() == 0)) {
617 // Invalid index or not values in list, remove nothing.
618 return 0;
619 } else if ((index + count) > m_values.count()) {
620 // Trying to remove more items than list has. Limit amount to be removed.
621 removeCount = m_values.count() - index;
618 }
622 }
619 int c = count;
623
620 while (c > 0) {
624 int c = 0;
625 while (c < removeCount) {
621 m_values.removeAt(index);
626 m_values.removeAt(index);
622 c--;
627 c++;
623 }
628 }
624 emit restructuredBars();
629 emit restructuredBars();
625 return true;
630 return removeCount;
626 }
631 }
627
632
628 void QBarSetPrivate::replace(const int index, const qreal value)
633 void QBarSetPrivate::replace(const int index, const qreal value)
@@ -58,7 +58,7 public:
58
58
59 void insert(const int index, const qreal value);
59 void insert(const int index, const qreal value);
60 void insert(const int index, const QPointF value);
60 void insert(const int index, const QPointF value);
61 bool remove(const int index, const int count = 1);
61 void remove(const int index, const int count = 1);
62 void replace(const int index, const qreal value);
62 void replace(const int index, const qreal value);
63 void replace(const int index, const QPointF value);
63 void replace(const int index, const QPointF value);
64 QPointF at(const int index) const;
64 QPointF at(const int index) const;
@@ -52,7 +52,7 public:
52
52
53 void insert(const int index, const qreal value);
53 void insert(const int index, const qreal value);
54 void insert(const int index, const QPointF value);
54 void insert(const int index, const QPointF value);
55 bool remove(const int index, const int count);
55 int remove(const int index, const int count);
56
56
57 void replace(const int index, const qreal value);
57 void replace(const int index, const qreal value);
58 void replace(const int index, const QPointF value);
58 void replace(const int index, const QPointF value);
@@ -233,6 +233,15 void tst_QBarSet::remove()
233 QCOMPARE(m_barset->at(2).y(), 4.0);
233 QCOMPARE(m_barset->at(2).y(), 4.0);
234 QCOMPARE(m_barset->count(), 3);
234 QCOMPARE(m_barset->count(), 3);
235 QCOMPARE(m_barset->sum(), 7.0);
235 QCOMPARE(m_barset->sum(), 7.0);
236 QCOMPARE(valueSpy.count(), 1);
237
238 QList<QVariant> valueSpyArg = valueSpy.takeFirst();
239 // Verify index of removed signal
240 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
241 QVERIFY(valueSpyArg.at(0).toInt() == 2);
242 // Verify count of removed signal
243 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
244 QVERIFY(valueSpyArg.at(1).toInt() == 1);
236
245
237 // Remove first
246 // Remove first
238 m_barset->remove(0); // 2.0 4.0
247 m_barset->remove(0); // 2.0 4.0
@@ -241,6 +250,16 void tst_QBarSet::remove()
241 QCOMPARE(m_barset->count(), 2);
250 QCOMPARE(m_barset->count(), 2);
242 QCOMPARE(m_barset->sum(), 6.0);
251 QCOMPARE(m_barset->sum(), 6.0);
243
252
253 QCOMPARE(valueSpy.count(), 1);
254 valueSpyArg = valueSpy.takeFirst();
255 // Verify index of removed signal
256 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
257 QVERIFY(valueSpyArg.at(0).toInt() == 0);
258 // Verify count of removed signal
259 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
260 QVERIFY(valueSpyArg.at(1).toInt() == 1);
261
262
244 // Illegal indexes
263 // Illegal indexes
245 m_barset->remove(4);
264 m_barset->remove(4);
246 QCOMPARE(m_barset->count(), 2);
265 QCOMPARE(m_barset->count(), 2);
@@ -249,7 +268,23 void tst_QBarSet::remove()
249 QCOMPARE(m_barset->count(), 2);
268 QCOMPARE(m_barset->count(), 2);
250 QCOMPARE(m_barset->sum(), 6.0);
269 QCOMPARE(m_barset->sum(), 6.0);
251
270
252 QCOMPARE(valueSpy.count(), 2);
271 // nothing removed, no signals should be emitted
272 QCOMPARE(valueSpy.count(), 0);
273
274 // Remove more items than list has
275 m_barset->remove(0,312);
276 QCOMPARE(m_barset->count(), 0);
277 QVERIFY(qFuzzyIsNull(m_barset->sum()));
278
279 QCOMPARE(valueSpy.count(), 1);
280 valueSpyArg = valueSpy.takeFirst();
281
282 // Verify index of removed signal
283 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
284 QVERIFY(valueSpyArg.at(0).toInt() == 0);
285 // Verify count of removed signal (expect 2 values removed, because list had only 2 items)
286 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
287 QVERIFY(valueSpyArg.at(1).toInt() == 2);
253 }
288 }
254
289
255 void tst_QBarSet::replace_data()
290 void tst_QBarSet::replace_data()
General Comments 0
You need to be logged in to leave comments. Login now