From 5ff7b773a0f59e174001da1f0550a7f0c2b6f485 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 27 Oct 2009 11:39:51 +0100 Subject: Fixes QTreeView: stylesheet :has-children pseudo selector doesn't works for the ::item pseudo-class The State_Children was not set on the QStyleOption. Refactorized a little bit the way it was computed. Reviewed-by: Thierry Task-number: 234930 Task-number: QTBUG-3129 --- src/gui/itemviews/qtreeview.cpp | 28 +++++++++++++++------------ src/gui/itemviews/qtreeview_p.h | 3 ++- tests/auto/qtreeview/tst_qtreeview.cpp | 35 +++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/gui/itemviews/qtreeview.cpp b/src/gui/itemviews/qtreeview.cpp index f37d8c7..49c8e34 100644 --- a/src/gui/itemviews/qtreeview.cpp +++ b/src/gui/itemviews/qtreeview.cpp @@ -1422,8 +1422,8 @@ void QTreeView::drawTree(QPainter *painter, const QRegion ®ion) const for (; i < viewItems.count() && y <= area.bottom(); ++i) { const int itemHeight = d->itemHeight(i); option.rect.setRect(0, y, viewportWidth, itemHeight); - option.state = state | (viewItems.at(i).expanded - ? QStyle::State_Open : QStyle::State_None); + option.state = state | (viewItems.at(i).expanded ? QStyle::State_Open : QStyle::State_None) + | (viewItems.at(i).hasChildren ? QStyle::State_Children : QStyle::State_None ); d->current = i; d->spanning = viewItems.at(i).spanning; if (!multipleRects || !drawn.contains(i)) { @@ -1748,8 +1748,7 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect, opt.rect = primitive; const bool expanded = viewItem.expanded; - const bool children = (((expanded && viewItem.total > 0)) // already laid out and has children - || d->hasVisibleChildren(index)); // not laid out yet, so we don't know + const bool children = viewItem.hasChildren; bool moreSiblings = false; if (d->hiddenIndexes.isEmpty()) moreSiblings = (d->model->rowCount(parent) - 1 > index.row()); @@ -3135,17 +3134,22 @@ void QTreeViewPrivate::layout(int i) last = j - hidden + children; } else { last = j - hidden + children; - viewItems[last].index = current; - viewItems[last].level = level; - viewItems[last].height = 0; - viewItems[last].spanning = q->isFirstColumnSpanned(current.row(), parent); - viewItems[last].expanded = false; - viewItems[last].total = 0; + QTreeViewItem *item = &viewItems[last]; + item->index = current; + item->level = level; + item->height = 0; + item->spanning = q->isFirstColumnSpanned(current.row(), parent); + item->expanded = false; + item->total = 0; if (isIndexExpanded(current)) { - viewItems[last].expanded = true; + item->expanded = true; layout(last); - children += viewItems[last].total; + item = &viewItems[last]; + children += item->total; + item->hasChildren = item->total > 0; last = j - hidden + children; + } else { + item->hasChildren = hasVisibleChildren(current); } } } diff --git a/src/gui/itemviews/qtreeview_p.h b/src/gui/itemviews/qtreeview_p.h index def8253..f89c328 100644 --- a/src/gui/itemviews/qtreeview_p.h +++ b/src/gui/itemviews/qtreeview_p.h @@ -66,7 +66,8 @@ struct QTreeViewItem QModelIndex index; // we remove items whenever the indexes are invalidated uint expanded : 1; uint spanning : 1; - uint total : 30; // total number of children visible + uint hasChildren : 1; // if the item has visible children (even if collapsed) + uint total : 29; // total number of children visible uint level : 16; // indentation int height : 16; // row height }; diff --git a/tests/auto/qtreeview/tst_qtreeview.cpp b/tests/auto/qtreeview/tst_qtreeview.cpp index da58725..8d824cb 100644 --- a/tests/auto/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/qtreeview/tst_qtreeview.cpp @@ -2891,7 +2891,8 @@ void tst_QTreeView::styleOptionViewItem() QVERIFY(!opt.text.isEmpty()); QCOMPARE(opt.index, index); - QCOMPARE(!(opt.features & QStyleOptionViewItemV2::Alternate), !(index.row() % 2)); + if (allCollapsed) + QCOMPARE(!(opt.features & QStyleOptionViewItemV2::Alternate), !(index.row() % 2)); QCOMPARE(!(opt.features & QStyleOptionViewItemV2::HasCheckIndicator), !opt.text.contains("Checkable")); if (opt.text.contains("Beginning")) @@ -2911,12 +2912,15 @@ void tst_QTreeView::styleOptionViewItem() else QCOMPARE(opt.checkState, Qt::Unchecked); + QCOMPARE(!(opt.state & QStyle::State_Children) , !opt.text.contains("HasChildren")); + QVERIFY(!opt.text.contains("Assert")); QStyledItemDelegate::paint(painter, option, index); count++; } mutable int count; + bool allCollapsed; }; QTreeView view; @@ -2926,8 +2930,9 @@ void tst_QTreeView::styleOptionViewItem() view.setItemDelegate(&delegate); model.appendRow(QList() << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + QStandardItem *par1 = new QStandardItem("Beginning HasChildren"); model.appendRow(QList() - << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + << par1 << new QStandardItem("Middle HasChildren") << new QStandardItem("Middle HasChildren") << new QStandardItem("End HasChildren") ); model.appendRow(QList() << new QStandardItem("OnlyOne") << new QStandardItem("Assert") << new QStandardItem("Assert") << new QStandardItem("Assert") ); QStandardItem *checkable = new QStandardItem("Checkable"); @@ -2938,12 +2943,37 @@ void tst_QTreeView::styleOptionViewItem() model.appendRow(QList() << new QStandardItem("Beginning") << checkable << checked << new QStandardItem("End") ); + par1->appendRow(QList() + << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + QStandardItem *par2 = new QStandardItem("Beginning HasChildren"); + par1->appendRow(QList() + << par2 << new QStandardItem("Middle HasChildren") << new QStandardItem("Middle HasChildren") << new QStandardItem("End HasChildren") ); + par2->appendRow(QList() + << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + + QStandardItem *par3 = new QStandardItem("Beginning"); + par1->appendRow(QList() + << par3 << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + par3->appendRow(QList() + << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + view.setRowHidden(0, par3->index(), true); + view.setFirstColumnSpanned(2, QModelIndex(), true); view.setAlternatingRowColors(true); delegate.count = 0; + delegate.allCollapsed = true; view.showMaximized(); + QApplication::processEvents(); QTRY_VERIFY(delegate.count >= 13); + delegate.count = 0; + delegate.allCollapsed = false; + view.expandAll(); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 13); + view.collapse(par2->index()); + QApplication::processEvents(); + QTRY_VERIFY(delegate.count >= 4); } class task174627_TreeView : public QTreeView @@ -3508,6 +3538,5 @@ void tst_QTreeView::task245654_changeModelAndExpandAll() } - QTEST_MAIN(tst_QTreeView) #include "tst_qtreeview.moc" -- cgit v0.12