diff --git a/src/widgets/availablepagesview.h b/src/widgets/availablepagesview.h --- a/src/widgets/availablepagesview.h +++ b/src/widgets/availablepagesview.h @@ -89,6 +89,7 @@ QAction *m_addProjectAction; QAction *m_addContextAction; QAction *m_addTagAction; + QAction *m_removeAction; QHash m_actions; QObject *m_model; diff --git a/src/widgets/availablepagesview.cpp b/src/widgets/availablepagesview.cpp --- a/src/widgets/availablepagesview.cpp +++ b/src/widgets/availablepagesview.cpp @@ -51,6 +51,7 @@ m_addProjectAction(new QAction(this)), m_addContextAction(new QAction(this)), m_addTagAction(new QAction(this)), + m_removeAction(new QAction(this)), m_model(Q_NULLPTR), m_sources(Q_NULLPTR), m_pagesView(new QTreeView(this)) @@ -81,12 +82,11 @@ connect(m_addTagAction, SIGNAL(triggered()), this, SLOT(onAddTagTriggered())); actionBar->addAction(m_addTagAction); - auto removeAction = new QAction(this); - removeAction->setObjectName("removeAction"); - removeAction->setText(tr("Remove page")); - removeAction->setIcon(QIcon::fromTheme("list-remove")); - connect(removeAction, SIGNAL(triggered()), this, SLOT(onRemoveTriggered())); - actionBar->addAction(removeAction); + m_removeAction->setObjectName("removeAction"); + m_removeAction->setText(tr("Remove page")); + m_removeAction->setIcon(QIcon::fromTheme("list-remove")); + connect(m_removeAction, SIGNAL(triggered()), this, SLOT(onRemoveTriggered())); + actionBar->addAction(m_removeAction); auto actionBarLayout = new QHBoxLayout; actionBarLayout->setAlignment(Qt::AlignRight); @@ -128,7 +128,7 @@ m_actions.insert("pages_project_add", m_addProjectAction); m_actions.insert("pages_context_add", m_addContextAction); m_actions.insert("pages_tag_add", m_addTagAction); - m_actions.insert("pages_remove", removeAction); + m_actions.insert("pages_remove", m_removeAction); m_actions.insert("pages_go_previous", goPreviousAction); m_actions.insert("pages_go_next", goNextAction); m_actions.insert("pages_go_to", goToAction); @@ -223,6 +223,11 @@ Q_RETURN_ARG(QObject*, page), Q_ARG(QModelIndex, current)); emit currentPageChanged(page); + + const auto object = current.data(QueryTreeModelBase::ObjectRole).value(); + m_removeAction->setEnabled(object.objectCast() + || object.objectCast() + || object.objectCast()); } void AvailablePagesView::onAddProjectTriggered() diff --git a/tests/units/widgets/availablepagesviewtest.cpp b/tests/units/widgets/availablepagesviewtest.cpp --- a/tests/units/widgets/availablepagesviewtest.cpp +++ b/tests/units/widgets/availablepagesviewtest.cpp @@ -375,31 +375,36 @@ void shouldRemoveAPage_data() { QTest::addColumn("object"); + QTest::addColumn("actionEnabled"); auto project1 = Domain::Project::Ptr::create(); project1->setName("Project 1"); - QTest::newRow("project") << QObjectPtr(project1); + QTest::newRow("project") << QObjectPtr(project1) << true; auto context1 = Domain::Context::Ptr::create(); context1->setName("Context 1"); - QTest::newRow("context") << QObjectPtr(context1); + QTest::newRow("context") << QObjectPtr(context1) << true; auto tag1 = Domain::Tag::Ptr::create(); tag1->setName("Tag 1"); - QTest::newRow("tag") << QObjectPtr(tag1); + QTest::newRow("tag") << QObjectPtr(tag1) << true; + + QTest::newRow("non removable") << QObjectPtr::create() << false; } void shouldRemoveAPage() { QFETCH(QObjectPtr, object); + QFETCH(bool, actionEnabled); // GIVEN QStringList list; list << "A" << "B" << "C"; QStandardItemModel model; for (int row = 0; row < list.count(); ++row) { model.setItem(row, new QStandardItem(list.at(row))); } + QVERIFY(model.setData(model.index(0, 0), QVariant::fromValue(object), Presentation::QueryTreeModelBase::ObjectRole)); AvailablePagesModelStub stubPagesModel; stubPagesModel.setProperty("pageListModel", QVariant::fromValue(static_cast(&model))); @@ -414,16 +419,18 @@ auto removeAction = available.findChild("removeAction"); - QVERIFY(model.setData(model.index(0, 0), QVariant::fromValue(object), Presentation::QueryTreeModelBase::ObjectRole)); - auto msgbox = MessageBoxStub::Ptr::create(); available.setMessageBoxInterface(msgbox); // WHEN - removeAction->trigger(); + if (actionEnabled) + removeAction->trigger(); // THEN - QCOMPARE(stubPagesModel.projectRemoved, list.first()); + QCOMPARE(removeAction->isEnabled(), actionEnabled); + if (actionEnabled) { + QCOMPARE(stubPagesModel.projectRemoved, list.first()); + } } void shouldGoToPreviousSelectablePage()