diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index a6173a6f26d..09c529b7dd0 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -511,6 +511,8 @@ public: QAction *m_openFileAction; QAction *m_projectTreeCollapseAllAction; QAction *m_projectTreeExpandAllAction; + QAction *m_projectTreeCollapseNodeAction = nullptr; + QAction *m_projectTreeExpandNodeAction = nullptr; Utils::ParameterAction *m_closeProjectFilesActionFileMenu; Utils::ParameterAction *m_closeProjectFilesActionContextMenu; QAction *m_searchOnFileSystem; @@ -1350,6 +1352,26 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er // Collapse & Expand. const Id treeGroup = Constants::G_PROJECT_TREE; + + dd->m_projectTreeCollapseNodeAction = new QAction(tr("Collapse"), this); + connect(dd->m_projectTreeCollapseNodeAction, &QAction::triggered, + ProjectTree::instance(), &ProjectTree::collapseCurrentNode); + Command * const collapseNodeCmd = ActionManager::registerAction( + dd->m_projectTreeCollapseNodeAction, "ProjectExplorer.CollapseNode", + projectTreeContext); + dd->m_projectTreeExpandNodeAction = new QAction(tr("Expand"), this); + connect(dd->m_projectTreeExpandNodeAction, &QAction::triggered, + ProjectTree::instance(), &ProjectTree::expandCurrentNode); + Command * const expandNodeCmd = ActionManager::registerAction( + dd->m_projectTreeExpandNodeAction, "ProjectExplorer.ExpandNode", + projectTreeContext); + for (Core::ActionContainer * const ac : {msubProjectContextMenu, mfolderContextMenu, + mprojectContextMenu}) { + ac->addSeparator(treeGroup); + ac->addAction(collapseNodeCmd, treeGroup); + ac->addAction(expandNodeCmd, treeGroup); + } + dd->m_projectTreeCollapseAllAction = new QAction(tr("Collapse All"), this); Command * const collapseCmd = ActionManager::registerAction( dd->m_projectTreeCollapseAllAction, Constants::PROJECTTREE_COLLAPSE_ALL, diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index b42da7ad66c..d22541872c3 100644 --- a/src/plugins/projectexplorer/projecttree.cpp +++ b/src/plugins/projectexplorer/projecttree.cpp @@ -154,7 +154,7 @@ void ProjectTree::update() ProjectTreeWidget *focus = m_focusForContextMenu; static QPointer lastFocusedProjectTreeWidget; if (!focus) { - focus = Utils::findOrDefault(m_projectTreeWidgets, &ProjectTree::hasFocus); + focus = currentWidget(); lastFocusedProjectTreeWidget = focus; } if (!focus) @@ -284,15 +284,27 @@ void ProjectTree::sessionAndTreeChanged() emit treeChanged(); } +void ProjectTree::collapseCurrentNode() +{ + if (const auto w = currentWidget()) + w->collapseCurrentNode(); +} + +void ProjectTree::expandCurrentNode() +{ + if (const auto w = currentWidget()) + w->expandCurrentNode(); +} + void ProjectTree::collapseAll() { - if (auto w = Utils::findOrDefault(s_instance->m_projectTreeWidgets, &ProjectTree::hasFocus)) + if (const auto w = currentWidget()) w->collapseAll(); } void ProjectTree::expandAll() { - if (auto w = Utils::findOrDefault(s_instance->m_projectTreeWidgets, &ProjectTree::hasFocus)) + if (const auto w = currentWidget()) w->expandAll(); } @@ -344,6 +356,11 @@ bool ProjectTree::hasFocus(ProjectTreeWidget *widget) || s_instance->m_focusForContextMenu == widget); } +ProjectTreeWidget *ProjectTree::currentWidget() const +{ + return findOrDefault(m_projectTreeWidgets, &ProjectTree::hasFocus); +} + void ProjectTree::showContextMenu(ProjectTreeWidget *focus, const QPoint &globalPos, Node *node) { QMenu *contextMenu = nullptr; diff --git a/src/plugins/projectexplorer/projecttree.h b/src/plugins/projectexplorer/projecttree.h index bf1335e5be5..6b25b4a12eb 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -82,6 +82,9 @@ public: static Project *projectForNode(const Node *node); static Node *nodeForFile(const Utils::FilePath &fileName); + void collapseCurrentNode(); + void expandCurrentNode(); + void collapseAll(); void expandAll(); @@ -117,6 +120,7 @@ private: void updateExternalFileWarning(); static bool hasFocus(Internal::ProjectTreeWidget *widget); + Internal::ProjectTreeWidget *currentWidget() const; void hideContextMenu(); private: diff --git a/src/plugins/projectexplorer/projecttreewidget.cpp b/src/plugins/projectexplorer/projecttreewidget.cpp index b3ae5756e7a..66613eb5910 100644 --- a/src/plugins/projectexplorer/projecttreewidget.cpp +++ b/src/plugins/projectexplorer/projecttreewidget.cpp @@ -430,6 +430,16 @@ void ProjectTreeWidget::setAutoSynchronization(bool sync) syncFromDocumentManager(); } +void ProjectTreeWidget::collapseCurrentNode() +{ + m_view->collapse(m_view->currentIndex()); +} + +void ProjectTreeWidget::expandCurrentNode() +{ + m_view->expand(m_view->currentIndex()); +} + void ProjectTreeWidget::collapseAll() { m_view->collapseAll(); diff --git a/src/plugins/projectexplorer/projecttreewidget.h b/src/plugins/projectexplorer/projecttreewidget.h index e53396c4b9a..935d041f7fd 100644 --- a/src/plugins/projectexplorer/projecttreewidget.h +++ b/src/plugins/projectexplorer/projecttreewidget.h @@ -67,6 +67,8 @@ public: void toggleAutoSynchronization(); void editCurrentItem(); + void collapseCurrentNode(); + void expandCurrentNode(); void collapseAll(); void expandAll();