From 5451b783676fec17edb45a94a3c43604b4fec30c Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 18 Feb 2020 18:03:22 +0100 Subject: [PATCH] Project Tree: Add functionality to expand a single node recursively From time to time, I find myself wanting to fully expand a specific sub-tree. Change-Id: Ie49b76a7a7a6fae3684bbdfb5735638bbc41b301 Reviewed-by: hjk --- src/plugins/projectexplorer/projectexplorer.cpp | 9 +++++++++ src/plugins/projectexplorer/projecttree.cpp | 17 ++++++++++++++--- src/plugins/projectexplorer/projecttree.h | 3 +++ .../projectexplorer/projecttreewidget.cpp | 14 ++++++++++++++ src/plugins/projectexplorer/projecttreewidget.h | 3 +++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 959f80f93e1..2afe540f1dd 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -522,6 +522,7 @@ public: QAction *m_openFileAction; QAction *m_projectTreeCollapseAllAction; QAction *m_projectTreeExpandAllAction; + QAction *m_projectTreeExpandNodeAction = nullptr; Utils::ParameterAction *m_closeProjectFilesActionFileMenu; Utils::ParameterAction *m_closeProjectFilesActionContextMenu; QAction *m_searchOnFileSystem; @@ -1416,6 +1417,13 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er // Collapse & Expand. const Id treeGroup = Constants::G_PROJECT_TREE; + + dd->m_projectTreeExpandNodeAction = new QAction(tr("Expand"), this); + connect(dd->m_projectTreeExpandNodeAction, &QAction::triggered, + ProjectTree::instance(), &ProjectTree::expandCurrentNodeRecursively); + Command * const expandNodeCmd = ActionManager::registerAction( + dd->m_projectTreeExpandNodeAction, "ProjectExplorer.ExpandNode", + projectTreeContext); dd->m_projectTreeCollapseAllAction = new QAction(tr("Collapse All"), this); Command * const collapseCmd = ActionManager::registerAction( dd->m_projectTreeCollapseAllAction, Constants::PROJECTTREE_COLLAPSE_ALL, @@ -1427,6 +1435,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er for (Core::ActionContainer * const ac : {mfileContextMenu, msubProjectContextMenu, mfolderContextMenu, mprojectContextMenu, msessionContextMenu}) { ac->addSeparator(treeGroup); + ac->addAction(expandNodeCmd, treeGroup); ac->addAction(collapseCmd, treeGroup); ac->addAction(expandCmd, treeGroup); } diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index 1ff829a061e..fed383008d6 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,21 @@ void ProjectTree::sessionAndTreeChanged() emit treeChanged(); } +void ProjectTree::expandCurrentNodeRecursively() +{ + if (const auto w = currentWidget()) + w->expandCurrentNodeRecursively(); +} + 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 +350,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..bedcb45862d 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -82,6 +82,8 @@ public: static Project *projectForNode(const Node *node); static Node *nodeForFile(const Utils::FilePath &fileName); + void expandCurrentNodeRecursively(); + void collapseAll(); void expandAll(); @@ -117,6 +119,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 ddf1bdf496b..cdd24622cd5 100644 --- a/src/plugins/projectexplorer/projecttreewidget.cpp +++ b/src/plugins/projectexplorer/projecttreewidget.cpp @@ -432,6 +432,20 @@ void ProjectTreeWidget::setAutoSynchronization(bool sync) syncFromDocumentManager(); } +void ProjectTreeWidget::expandNodeRecursively(const QModelIndex &index) +{ + const int rc = index.model()->rowCount(index); + for (int i = 0; i < rc; ++i) + expandNodeRecursively(index.model()->index(i, index.column(), index)); + if (rc > 0) + m_view->expand(index); +} + +void ProjectTreeWidget::expandCurrentNodeRecursively() +{ + expandNodeRecursively(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..aa612fdf7e6 100644 --- a/src/plugins/projectexplorer/projecttreewidget.h +++ b/src/plugins/projectexplorer/projecttreewidget.h @@ -67,6 +67,7 @@ public: void toggleAutoSynchronization(); void editCurrentItem(); + void expandCurrentNodeRecursively(); void collapseAll(); void expandAll(); @@ -87,6 +88,8 @@ private: void syncFromDocumentManager(); + void expandNodeRecursively(const QModelIndex &index); + QTreeView *m_view = nullptr; FlatModel *m_model = nullptr; QAction *m_filterProjectsAction = nullptr;