diff options
-rw-r--r-- | src/gui/itemviews/qtreeview.cpp | 38 | ||||
-rw-r--r-- | src/gui/itemviews/qtreeview_p.h | 3 | ||||
-rw-r--r-- | tests/auto/qtreeview/tst_qtreeview.cpp | 101 |
3 files changed, 138 insertions, 4 deletions
diff --git a/src/gui/itemviews/qtreeview.cpp b/src/gui/itemviews/qtreeview.cpp index be64c46..3856293 100644 --- a/src/gui/itemviews/qtreeview.cpp +++ b/src/gui/itemviews/qtreeview.cpp @@ -2434,7 +2434,9 @@ void QTreeView::rowsInserted(const QModelIndex &parent, int start, int end) return; } - if (parent != d->root && !d->isIndexExpanded(parent) && d->model->rowCount(parent) > (end - start) + 1) { + const int parentRowCount = d->model->rowCount(parent); + const int delta = end - start + 1; + if (parent != d->root && !d->isIndexExpanded(parent) && parentRowCount > delta) { QAbstractItemView::rowsInserted(parent, start, end); return; } @@ -2449,11 +2451,29 @@ void QTreeView::rowsInserted(const QModelIndex &parent, int start, int end) ? d->viewItems.count() : d->viewItems.at(parentItem).total) - 1; - const int delta = end - start + 1; + if (parentRowCount == end + 1 && start > 0) { + //need to Update hasMoreSiblings + int previousRow = start - 1; + QModelIndex previousSibilingModelIndex = d->model->index(previousRow, 0, parent); + bool isHidden = d->isRowHidden(previousSibilingModelIndex); + while (isHidden && previousRow > 0) { + previousRow--; + previousSibilingModelIndex = d->model->index(previousRow, 0, parent); + isHidden = d->isRowHidden(previousSibilingModelIndex); + } + if (!isHidden) { + const int previousSibilling = d->viewIndex(previousSibilingModelIndex); + if(previousSibilling != -1) + d->viewItems[previousSibilling].hasMoreSiblings = true; + } + } + QVector<QTreeViewItem> insertedItems(delta); for (int i = 0; i < delta; ++i) { insertedItems[i].index = d->model->index(i + start, 0, parent); insertedItems[i].level = childLevel; + insertedItems[i].hasChildren = d->hasVisibleChildren(insertedItems[i].index); + insertedItems[i].hasMoreSiblings = !((i == delta - 1) && (parentRowCount == end +1)); } if (d->viewItems.isEmpty()) d->defaultItemHeight = indexRowSizeHint(insertedItems[0].index); @@ -2495,13 +2515,17 @@ void QTreeView::rowsInserted(const QModelIndex &parent, int start, int end) d->viewItems.begin() + insertPos + 1); } + if (parentItem != -1) + d->viewItems[parentItem].hasChildren = true; d->updateChildCount(parentItem, delta); + updateGeometries(); viewport()->update(); } else if ((parentItem != -1) && d->viewItems.at(parentItem).expanded) { d->doDelayedItemsLayout(); } else if (parentItem != -1 && (d->model->rowCount(parent) == end - start + 1)) { - // the parent just went from 0 children to having some update to re-paint the decoration + // the parent just went from 0 children to more. update to re-paint the decoration + d->viewItems[parentItem].hasChildren = true; viewport()->update(); } QAbstractItemView::rowsInserted(parent, start, end); @@ -3706,6 +3730,7 @@ void QTreeViewPrivate::rowsRemoved(const QModelIndex &parent, const int delta = end - start + 1; + int previousSibiling = -1; int removedCount = 0; for (int item = firstChildItem; item <= lastChildItem; ) { Q_ASSERT(viewItems.at(item).level == childLevel); @@ -3713,6 +3738,7 @@ void QTreeViewPrivate::rowsRemoved(const QModelIndex &parent, //Q_ASSERT(modelIndex.parent() == parent); const int count = viewItems.at(item).total + 1; if (modelIndex.row() < start) { + previousSibiling = item; // not affected by the removal item += count; } else if (modelIndex.row() <= end) { @@ -3730,7 +3756,13 @@ void QTreeViewPrivate::rowsRemoved(const QModelIndex &parent, } } + if (previousSibiling != -1 && after && model->rowCount(parent) == start) + viewItems[previousSibiling].hasMoreSiblings = false; + + updateChildCount(parentItem, -removedCount); + if (parentItem != -1 && viewItems.at(parentItem).total == 0) + viewItems[parentItem].hasChildren = false; //every children have been removed; if (after) { q->updateGeometries(); viewport->update(); diff --git a/src/gui/itemviews/qtreeview_p.h b/src/gui/itemviews/qtreeview_p.h index 62676d8..aad5837 100644 --- a/src/gui/itemviews/qtreeview_p.h +++ b/src/gui/itemviews/qtreeview_p.h @@ -62,7 +62,8 @@ QT_BEGIN_NAMESPACE struct QTreeViewItem { - QTreeViewItem() : expanded(false), spanning(false), total(0), level(0), height(0) {} + QTreeViewItem() : expanded(false), spanning(false), hasChildren(false), + hasMoreSiblings(false), total(0), level(0), height(0) {} QModelIndex index; // we remove items whenever the indexes are invalidated uint expanded : 1; uint spanning : 1; diff --git a/tests/auto/qtreeview/tst_qtreeview.cpp b/tests/auto/qtreeview/tst_qtreeview.cpp index 75c02e9..90e6c5c 100644 --- a/tests/auto/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/qtreeview/tst_qtreeview.cpp @@ -2892,6 +2892,8 @@ void tst_QTreeView::styleOptionViewItem() QVERIFY(!opt.text.isEmpty()); QCOMPARE(opt.index, index); + //qDebug() << index << opt.text; + if (allCollapsed) QCOMPARE(!(opt.features & QStyleOptionViewItemV2::Alternate), !(index.row() % 2)); QCOMPARE(!(opt.features & QStyleOptionViewItemV2::HasCheckIndicator), !opt.text.contains("Checkable")); @@ -2979,9 +2981,108 @@ void tst_QTreeView::styleOptionViewItem() view.expandAll(); QApplication::processEvents(); QTRY_VERIFY(delegate.count >= 13); + delegate.count = 0; view.collapse(par2->index()); QApplication::processEvents(); QTRY_VERIFY(delegate.count >= 4); + + + //test dynamic models + { + delegate.count = 0; + QStandardItemModel model2; + QStandardItem *item0 = new QStandardItem("OnlyOne Last"); + model2.appendRow(QList<QStandardItem*>() << item0); + view.setModel(&model2); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 1); + QApplication::processEvents(); + + QStandardItem *item00 = new QStandardItem("OnlyOne Last"); + item0->appendRow(QList<QStandardItem*>() << item00); + item0->setText("OnlyOne Last HasChildren"); + QApplication::processEvents(); + delegate.count = 0; + view.expandAll(); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 2); + QApplication::processEvents(); + + QStandardItem *item1 = new QStandardItem("OnlyOne Last"); + delegate.count = 0; + item0->setText("OnlyOne HasChildren"); + model2.appendRow(QList<QStandardItem*>() << item1); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 3); + QApplication::processEvents(); + + QStandardItem *item01 = new QStandardItem("OnlyOne Last"); + delegate.count = 0; + item00->setText("OnlyOne"); + item0->appendRow(QList<QStandardItem*>() << item01); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 4); + QApplication::processEvents(); + + QStandardItem *item000 = new QStandardItem("OnlyOne Last"); + delegate.count = 0; + item00->setText("OnlyOne HasChildren"); + item00->appendRow(QList<QStandardItem*>() << item000); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 5); + QApplication::processEvents(); + + delegate.count = 0; + item0->removeRow(0); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 3); + QApplication::processEvents(); + + item00 = new QStandardItem("OnlyOne"); + item0->insertRow(0, QList<QStandardItem*>() << item00); + QApplication::processEvents(); + delegate.count = 0; + view.expandAll(); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 4); + QApplication::processEvents(); + + delegate.count = 0; + item0->removeRow(1); + item00->setText("OnlyOne Last"); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 3); + QApplication::processEvents(); + + delegate.count = 0; + item0->removeRow(0); + item0->setText("OnlyOne"); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 2); + QApplication::processEvents(); + + //with hidden items + item0->setText("OnlyOne HasChildren"); + item00 = new QStandardItem("OnlyOne"); + item0->appendRow(QList<QStandardItem*>() << item00); + item01 = new QStandardItem("Assert"); + item0->appendRow(QList<QStandardItem*>() << item01); + view.setRowHidden(1, item0->index(), true); + view.expandAll(); + QStandardItem *item02 = new QStandardItem("OnlyOne Last"); + item0->appendRow(QList<QStandardItem*>() << item02); + delegate.count = 0; + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 4); + QApplication::processEvents(); + + item0->removeRow(2); + item00->setText("OnlyOne Last"); + delegate.count = 0; + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 3); + QApplication::processEvents(); + } } class task174627_TreeView : public QTreeView |