diff --git a/src/widgets/pageview.cpp b/src/widgets/pageview.cpp --- a/src/widgets/pageview.cpp +++ b/src/widgets/pageview.cpp @@ -352,19 +352,17 @@ if (currentIndexes.isEmpty()) return; - QString text; - if (currentIndexes.size() > 1) { - bool hasDescendants = false; - foreach (const QModelIndex ¤tIndex, currentIndexes) { - if (!currentIndex.isValid()) - continue; - - if (currentIndex.model()->rowCount(currentIndex) > 0) { - hasDescendants = true; - break; - } + auto indexHasChildren = [](QModelIndex index) { + if (const QAbstractProxyModel *proxy = qobject_cast(index.model())) { + index = proxy->mapToSource(index); } + return index.model()->rowCount(index) > 0; + }; + QString text; + if (currentIndexes.size() > 1) { + const bool hasDescendants = std::any_of(currentIndexes.constBegin(), currentIndexes.constEnd(), [&](const QModelIndex ¤tIndex) { + return currentIndex.isValid() && indexHasChildren(currentIndex); }); if (hasDescendants) text = i18n("Do you really want to delete the selected items and their children?"); else @@ -375,7 +373,7 @@ if (!currentIndex.isValid()) return; - if (currentIndex.model()->rowCount(currentIndex) > 0) + if (indexHasChildren(currentIndex)) text = i18n("Do you really want to delete the selected task and all its children?"); } diff --git a/tests/units/widgets/pageviewtest.cpp b/tests/units/widgets/pageviewtest.cpp --- a/tests/units/widgets/pageviewtest.cpp +++ b/tests/units/widgets/pageviewtest.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -57,8 +58,16 @@ Q_OBJECT Q_PROPERTY(QAbstractItemModel* centralListModel READ centralListModel) public: + void setProxyModel(QAbstractProxyModel *proxy) + { + proxyModel = proxy; + proxyModel->setSourceModel(&itemModel); + } + QAbstractItemModel *centralListModel() { + if (proxyModel) + return proxyModel; return &itemModel; } @@ -114,6 +123,7 @@ QList removedIndices; QList promotedIndices; QStandardItemModel itemModel; + QAbstractProxyModel *proxyModel = nullptr; }; class RunningTaskModelStub : public Presentation::RunningTaskModelInterface @@ -616,6 +626,45 @@ QCOMPARE(stubPageModel.removedIndices.first(), index); } + void shouldDisplayNotificationWhenHittingTheDeleteKeyOnAnItemWithHiddenChildren() + { + // GIVEN + PageModelStub stubPageModel; + Q_ASSERT(stubPageModel.property("centralListModel").canConvert()); + stubPageModel.addStubItems(QStringList() << QStringLiteral("A") << QStringLiteral("B")); + QStandardItem *parentIndex = stubPageModel.itemModel.item(1, 0); + stubPageModel.addStubItem(QStringLiteral("C"), parentIndex); + + QSortFilterProxyModel proxyModel; + stubPageModel.setProxyModel(&proxyModel); + proxyModel.setFilterFixedString("B"); + + QPersistentModelIndex index = stubPageModel.centralListModel()->index(0, 0); + QCOMPARE(index.data().toString(), QLatin1String("B")); + Widgets::PageView page; + page.setModel(&stubPageModel); + auto msgbox = MessageBoxStub::Ptr::create(); + page.setMessageBoxInterface(msgbox); + + QTreeView *centralView = page.findChild(QStringLiteral("centralView")); + centralView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); + QVERIFY(centralView->selectionModel()->currentIndex().isValid()); + centralView->setFocus(); + + // Needed for shortcuts to work + page.show(); + QVERIFY(QTest::qWaitForWindowShown(&page)); + QTest::qWait(100); + + // WHEN + QTest::keyPress(centralView, Qt::Key_Delete); + + // THEN + QVERIFY(msgbox->called()); + QCOMPARE(stubPageModel.removedIndices.size(), 1); + QCOMPARE(stubPageModel.removedIndices.first(), index); + } + void shouldDeleteItemsWhenHittingTheDeleteKey() { // GIVEN