From 32b55bc3549f9210654b02c7f5e8cd6cbc8e803b 2012-08-22 09:38:21 From: sauimone Date: 2012-08-22 09:38:21 Subject: [PATCH] Implemented QBarSeries::take(), updated tests --- diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 1f8e087..58c2cff 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -252,7 +252,8 @@ bool QAbstractBarSeries::append(QBarSet *set) } /*! - Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. + Removes barset from series. Releases ownership of \a set. Deletes the set, if remove + was successful. Returns true, if set was removed. */ bool QAbstractBarSeries::remove(QBarSet *set) @@ -265,6 +266,29 @@ bool QAbstractBarSeries::remove(QBarSet *set) set->setParent(0); emit barsetsRemoved(sets); emit countChanged(); + delete set; + set = 0; + } + return success; +} + +/*! + Takes a single \a set from the series. Does not delete the barset object. + + NOTE: The series remains as the barset's parent object. You must set the + parent object to take full ownership. + + Returns true if take was successfull. +*/ +bool QAbstractBarSeries::take(QBarSet *set) +{ + Q_D(QAbstractBarSeries); + bool success = d->remove(set); + if (success) { + QList sets; + sets.append(set); + emit barsetsRemoved(sets); + emit countChanged(); } return success; } @@ -305,7 +329,7 @@ bool QAbstractBarSeries::insert(int index, QBarSet *set) } /*! - Removes all of the bar sets from the series + Removes all barsets from the series. Deletes removed sets. */ void QAbstractBarSeries::clear() { @@ -315,6 +339,9 @@ void QAbstractBarSeries::clear() if (success) { emit barsetsRemoved(sets); emit countChanged(); + foreach (QBarSet* set, sets) { + delete set; + } } } diff --git a/src/barchart/qabstractbarseries.h b/src/barchart/qabstractbarseries.h index 764b075..6cc678c 100644 --- a/src/barchart/qabstractbarseries.h +++ b/src/barchart/qabstractbarseries.h @@ -49,6 +49,7 @@ public: bool append(QBarSet *set); bool remove(QBarSet *set); + bool take(QBarSet *set); bool append(QList sets); bool insert(int index, QBarSet *set); int count() const; diff --git a/tests/auto/qbarseries/tst_qbarseries.cpp b/tests/auto/qbarseries/tst_qbarseries.cpp index 33830f3..fcad567 100644 --- a/tests/auto/qbarseries/tst_qbarseries.cpp +++ b/tests/auto/qbarseries/tst_qbarseries.cpp @@ -28,6 +28,7 @@ QTCOMMERCIALCHART_USE_NAMESPACE Q_DECLARE_METATYPE(QBarSet*) +Q_DECLARE_METATYPE(QList) class tst_QBarSeries : public QObject { @@ -48,6 +49,8 @@ private slots: void append(); void remove_data(); void remove(); + void take_data(); + void take(); void appendList_data(); void appendList(); void count_data(); @@ -74,6 +77,7 @@ private: void tst_QBarSeries::initTestCase() { qRegisterMetaType("QBarSet*"); + qRegisterMetaType>("QList"); } void tst_QBarSeries::cleanupTestCase() @@ -95,7 +99,6 @@ void tst_QBarSeries::cleanup() { foreach(QBarSet* s, m_testSets) { m_barseries_with_sets->remove(s); - delete s; } m_testSets.clear(); @@ -206,6 +209,35 @@ void tst_QBarSeries::remove() QVERIFY(m_barseries_with_sets->count() == 0); } +void tst_QBarSeries::take_data() +{ + +} + +void tst_QBarSeries::take() +{ + int count = m_testSets.count(); + QVERIFY(m_barseries_with_sets->count() == count); + + QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged())); + QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList))); + + for (int i=0; itake(set); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QVERIFY(success); + TRY_COMPARE(countSpy.count(),1); + TRY_COMPARE(removedSpy.count(),1); + + QList removedSpyArg = removedSpy.takeFirst(); + QList removedSets = qvariant_cast> (removedSpyArg.at(0)); + QCOMPARE(removedSets.at(0), m_testSets.at(i)); + countSpy.takeFirst(); + } +} + + void tst_QBarSeries::appendList_data() {