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 <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2020-02-18 18:03:22 +01:00
parent 04f933fd67
commit 5451b78367
5 changed files with 43 additions and 3 deletions

View File

@@ -522,6 +522,7 @@ public:
QAction *m_openFileAction; QAction *m_openFileAction;
QAction *m_projectTreeCollapseAllAction; QAction *m_projectTreeCollapseAllAction;
QAction *m_projectTreeExpandAllAction; QAction *m_projectTreeExpandAllAction;
QAction *m_projectTreeExpandNodeAction = nullptr;
Utils::ParameterAction *m_closeProjectFilesActionFileMenu; Utils::ParameterAction *m_closeProjectFilesActionFileMenu;
Utils::ParameterAction *m_closeProjectFilesActionContextMenu; Utils::ParameterAction *m_closeProjectFilesActionContextMenu;
QAction *m_searchOnFileSystem; QAction *m_searchOnFileSystem;
@@ -1416,6 +1417,13 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
// Collapse & Expand. // Collapse & Expand.
const Id treeGroup = Constants::G_PROJECT_TREE; 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); dd->m_projectTreeCollapseAllAction = new QAction(tr("Collapse All"), this);
Command * const collapseCmd = ActionManager::registerAction( Command * const collapseCmd = ActionManager::registerAction(
dd->m_projectTreeCollapseAllAction, Constants::PROJECTTREE_COLLAPSE_ALL, 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, for (Core::ActionContainer * const ac : {mfileContextMenu, msubProjectContextMenu,
mfolderContextMenu, mprojectContextMenu, msessionContextMenu}) { mfolderContextMenu, mprojectContextMenu, msessionContextMenu}) {
ac->addSeparator(treeGroup); ac->addSeparator(treeGroup);
ac->addAction(expandNodeCmd, treeGroup);
ac->addAction(collapseCmd, treeGroup); ac->addAction(collapseCmd, treeGroup);
ac->addAction(expandCmd, treeGroup); ac->addAction(expandCmd, treeGroup);
} }

View File

@@ -154,7 +154,7 @@ void ProjectTree::update()
ProjectTreeWidget *focus = m_focusForContextMenu; ProjectTreeWidget *focus = m_focusForContextMenu;
static QPointer<ProjectTreeWidget> lastFocusedProjectTreeWidget; static QPointer<ProjectTreeWidget> lastFocusedProjectTreeWidget;
if (!focus) { if (!focus) {
focus = Utils::findOrDefault(m_projectTreeWidgets, &ProjectTree::hasFocus); focus = currentWidget();
lastFocusedProjectTreeWidget = focus; lastFocusedProjectTreeWidget = focus;
} }
if (!focus) if (!focus)
@@ -284,15 +284,21 @@ void ProjectTree::sessionAndTreeChanged()
emit treeChanged(); emit treeChanged();
} }
void ProjectTree::expandCurrentNodeRecursively()
{
if (const auto w = currentWidget())
w->expandCurrentNodeRecursively();
}
void ProjectTree::collapseAll() void ProjectTree::collapseAll()
{ {
if (auto w = Utils::findOrDefault(s_instance->m_projectTreeWidgets, &ProjectTree::hasFocus)) if (const auto w = currentWidget())
w->collapseAll(); w->collapseAll();
} }
void ProjectTree::expandAll() void ProjectTree::expandAll()
{ {
if (auto w = Utils::findOrDefault(s_instance->m_projectTreeWidgets, &ProjectTree::hasFocus)) if (const auto w = currentWidget())
w->expandAll(); w->expandAll();
} }
@@ -344,6 +350,11 @@ bool ProjectTree::hasFocus(ProjectTreeWidget *widget)
|| s_instance->m_focusForContextMenu == 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) void ProjectTree::showContextMenu(ProjectTreeWidget *focus, const QPoint &globalPos, Node *node)
{ {
QMenu *contextMenu = nullptr; QMenu *contextMenu = nullptr;

View File

@@ -82,6 +82,8 @@ public:
static Project *projectForNode(const Node *node); static Project *projectForNode(const Node *node);
static Node *nodeForFile(const Utils::FilePath &fileName); static Node *nodeForFile(const Utils::FilePath &fileName);
void expandCurrentNodeRecursively();
void collapseAll(); void collapseAll();
void expandAll(); void expandAll();
@@ -117,6 +119,7 @@ private:
void updateExternalFileWarning(); void updateExternalFileWarning();
static bool hasFocus(Internal::ProjectTreeWidget *widget); static bool hasFocus(Internal::ProjectTreeWidget *widget);
Internal::ProjectTreeWidget *currentWidget() const;
void hideContextMenu(); void hideContextMenu();
private: private:

View File

@@ -432,6 +432,20 @@ void ProjectTreeWidget::setAutoSynchronization(bool sync)
syncFromDocumentManager(); 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() void ProjectTreeWidget::collapseAll()
{ {
m_view->collapseAll(); m_view->collapseAll();

View File

@@ -67,6 +67,7 @@ public:
void toggleAutoSynchronization(); void toggleAutoSynchronization();
void editCurrentItem(); void editCurrentItem();
void expandCurrentNodeRecursively();
void collapseAll(); void collapseAll();
void expandAll(); void expandAll();
@@ -87,6 +88,8 @@ private:
void syncFromDocumentManager(); void syncFromDocumentManager();
void expandNodeRecursively(const QModelIndex &index);
QTreeView *m_view = nullptr; QTreeView *m_view = nullptr;
FlatModel *m_model = nullptr; FlatModel *m_model = nullptr;
QAction *m_filterProjectsAction = nullptr; QAction *m_filterProjectsAction = nullptr;