diff options
author | Bruno Abinader <bruno.abinader@openbossa.org> | 2009-05-25 08:30:57 -0400 |
---|---|---|
committer | Bruno Abinader <bruno.abinader@openbossa.org> | 2009-05-28 08:31:13 -0400 |
commit | 0f5a132721965948a5685f188a16d50c5aa5f6d4 (patch) | |
tree | 05d1af729eec2d8b0ec84c7ee0b66a8a87f12b2e | |
parent | b83fd00b4f1894bce03bdfeac10324db1f495b08 (diff) |
QListFromTreeAdaptor: Only children items from the current item should emit signals.
Fixed bug that children items from other layers of the tree model were
also captured by the adaptor making it emit erroneous signals ahead,
confusing the selection manager.
Signed-off-by: Bruno Abinader <bruno.abinader@openbossa.org>
Reviewed-by: Carlos Eduardo de S. Aguiar <carlos.aguiar@openbossa.org>
-rw-r--r-- | src/qlistfromtreeadaptor.cpp | 52 | ||||
-rw-r--r-- | tests/qlistfromtreeadaptor/tst_qlistfromtreeadaptor.cpp | 94 |
2 files changed, 124 insertions, 22 deletions
diff --git a/src/qlistfromtreeadaptor.cpp b/src/qlistfromtreeadaptor.cpp index 28fffb7..b04dfe0 100644 --- a/src/qlistfromtreeadaptor.cpp +++ b/src/qlistfromtreeadaptor.cpp @@ -21,11 +21,9 @@ ** ****************************************************************************/ -#include "qtreemodelbase.h" #include "qlistfromtreeadaptor.h" #include "qlistfromtreeadaptor_p.h" - -#include <qdebug.h> +#include "qtreemodelbase.h" /*! \internal @@ -59,12 +57,20 @@ void QtListFromTreeAdaptorPrivate::_q_itemsInserted(const QtTreeModelBase::itera Q_Q(QtListFromTreeAdaptor); QtTreeModelBase::iterator_base _it = it; - int index; + QtTreeModelBase::iterator_base root = this->it; + int index = 0; - for (index = 0; _it.hasPreviousSibling(); index++) - _it.previousSibling(); + _it.parent(); - emit q->itemsInserted(index, count); + if ((root.isValid() && root == _it) || !_it.hasParent()) { + _it = it; + + for (; _it.hasPreviousSibling(); index++) { + _it.previousSibling(); + } + + emit q->itemsInserted(index, count); + } } /*! @@ -76,12 +82,20 @@ void QtListFromTreeAdaptorPrivate::_q_itemsRemoved(const QtTreeModelBase::iterat Q_Q(QtListFromTreeAdaptor); QtTreeModelBase::iterator_base _it = it; - int index; + QtTreeModelBase::iterator_base root = this->it; + int index = 0; - for (index = 0; _it.hasPreviousSibling(); index++) - _it.previousSibling(); + _it.parent(); - emit q->itemsRemoved(index, count); + if ((root.isValid() && root == _it) || !_it.hasParent()) { + _it = it; + + for (; _it.hasPreviousSibling(); index++) { + _it.previousSibling(); + } + + emit q->itemsRemoved(index, count); + } } /*! @@ -94,12 +108,20 @@ void QtListFromTreeAdaptorPrivate::_q_itemsChanged(const QtTreeModelBase::iterat Q_Q(QtListFromTreeAdaptor); QtTreeModelBase::iterator_base _it = it; - int index; + QtTreeModelBase::iterator_base root = this->it; + int index = 0; - for (index = 0; _it.hasPreviousSibling(); index++) - _it.previousSibling(); + _it.parent(); - emit q->itemsChanged(index, count, roles); + if ((root.isValid() && root == _it) || !_it.hasParent()) { + _it = it; + + for (; _it.hasPreviousSibling(); index++) { + _it.previousSibling(); + } + + emit q->itemsChanged(index, count, roles); + } } /*! diff --git a/tests/qlistfromtreeadaptor/tst_qlistfromtreeadaptor.cpp b/tests/qlistfromtreeadaptor/tst_qlistfromtreeadaptor.cpp index e874b8b..f2882fe 100644 --- a/tests/qlistfromtreeadaptor/tst_qlistfromtreeadaptor.cpp +++ b/tests/qlistfromtreeadaptor/tst_qlistfromtreeadaptor.cpp @@ -40,8 +40,12 @@ public slots: private slots: void count_data(); void count(); + void parentCount_data(); + void parentCount(); void dataCheck_data(); void dataCheck(); + void rootCheck_data(); + void rootCheck(); protected: QtTreeDefaultModel *source; @@ -91,28 +95,66 @@ void tst_QtListFromTreeAdaptor::count() int x; QSignalSpy sourceItemsInserted(source, - SIGNAL(itemsInserted(const QtTreeModelBase::iterator_base&,int))); + SIGNAL(itemsInserted(const QtTreeModelBase::iterator_base&,int))); QSignalSpy sourceItemsRemoved(source, - SIGNAL(itemsRemoved(const QtTreeModelBase::iterator_base&,int))); + SIGNAL(itemsRemoved(const QtTreeModelBase::iterator_base&,int))); QSignalSpy adaptorItemsInserted(adaptor, SIGNAL(itemsInserted(int,int))); QSignalSpy adaptorItemsRemoved(adaptor, SIGNAL(itemsRemoved(int,int))); - for (x = 0; x < itemCount; x++) + for (x = 0; x < itemCount; x++) { new QtTreeDefaultItem(QString::number(x), source->rootItem()); + } QCOMPARE(source->rootItem()->children().count(), itemCount); QCOMPARE(source->rootItem()->children().count(), adaptor->count()); QCOMPARE(sourceItemsInserted.count(), signalCount); QCOMPARE(adaptorItemsInserted.count(), signalCount); - foreach (QtTreeDefaultItem *item, source->rootItem()->children()) + foreach (QtTreeDefaultItem *item, source->rootItem()->children()) { source->rootItem()->removeChild(item); + } QCOMPARE(sourceItemsRemoved.count(), signalCount); QCOMPARE(adaptorItemsRemoved.count(), signalCount); } +void tst_QtListFromTreeAdaptor::parentCount_data() +{ + QTest::addColumn<int>("parentItemCount"); + QTest::addColumn<int>("childrenItemCount"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("one parent item, one children item") << 1 << 1 << 1; + QTest::newRow("two parent items, two children items") << 2 << 2 << 2; +} + +void tst_QtListFromTreeAdaptor::parentCount() +{ + QFETCH(int, parentItemCount); + QFETCH(int, childrenItemCount); + QFETCH(int, signalCount); + int x, y; + + QSignalSpy sourceItemsInserted(source, + SIGNAL(itemsInserted(const QtTreeModelBase::iterator_base&,int))); + QSignalSpy adaptorItemsInserted(adaptor, SIGNAL(itemsInserted(int,int))); + + for (x = 0; x < parentItemCount; x++) { + QtTreeDefaultItem *item = new QtTreeDefaultItem(QString::number(x), source->rootItem()); + for (y = 0; y < childrenItemCount; y++) { + new QtTreeDefaultItem(QString::number(y), item); + } + } + + QCOMPARE(sourceItemsInserted.count(), (parentItemCount * childrenItemCount) + parentItemCount); + QCOMPARE(adaptorItemsInserted.count(), signalCount); + + foreach (QtTreeDefaultItem *item, source->rootItem()->children()) { + source->rootItem()->removeChild(item); + } +} + void tst_QtListFromTreeAdaptor::dataCheck_data() { QTest::addColumn<int>("itemCount"); @@ -129,9 +171,9 @@ void tst_QtListFromTreeAdaptor::dataCheck() int x; QSignalSpy sourceItemsChanged(source, - SIGNAL(itemsChanged(const QtTreeModelBase::iterator_base&,int,QList<int>))); + SIGNAL(itemsChanged(const QtTreeModelBase::iterator_base&,int,QList<int>))); QSignalSpy adaptorItemsChanged(adaptor, - SIGNAL(itemsChanged(int,int,QList<int>))); + SIGNAL(itemsChanged(int,int,QList<int>))); for (x = 0; x < itemCount; x++) { new QtTreeDefaultItem(QString::number(x), source->rootItem()); @@ -148,8 +190,46 @@ void tst_QtListFromTreeAdaptor::dataCheck() QCOMPARE(sourceItemsChanged.count(), signalCount); QCOMPARE(adaptorItemsChanged.count(), signalCount); - foreach (QtTreeDefaultItem *item, source->rootItem()->children()) + foreach (QtTreeDefaultItem *item, source->rootItem()->children()) { source->rootItem()->removeChild(item); + } +} + +void tst_QtListFromTreeAdaptor::rootCheck_data() +{ + QTest::addColumn<int>("itemCount"); + + QTest::newRow("two items") << 2; +} + +void tst_QtListFromTreeAdaptor::rootCheck() +{ + QFETCH(int, itemCount); + int x; + + delete source; + delete adaptor; + + source = new QtTreeDefaultModel; + + QSignalSpy sourceItemsInserted(source, + SIGNAL(itemsInserted(const QtTreeModelBase::iterator_base&,int))); + + for (x = 0; x < itemCount; x++) { + new QtTreeDefaultItem(QString::number(x), source->rootItem()); + } + + adaptor = new QtListFromTreeAdaptor(source, + source->itemIterator(source->rootItem()->firstChild())); + + QSignalSpy adaptorItemsInserted(adaptor, SIGNAL(itemsInserted(int,int))); + + for (x = 0; x < itemCount; x++) { + new QtTreeDefaultItem(QString::number(x), source->rootItem()->firstChild()); + } + + QCOMPARE(sourceItemsInserted.count(), itemCount * itemCount); + QCOMPARE(adaptorItemsInserted.count(), itemCount); } QTEST_MAIN(tst_QtListFromTreeAdaptor) |