Skip to content

Commit

Permalink
Add collapse recursive feature to project trees (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
arBmind committed Oct 14, 2024
1 parent 3a7c7c7 commit 041e240
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/plugins/coreplugin/foldernavigationwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
}

menu.addSeparator();
QAction *const collapseNodeAction = menu.addAction(Tr::tr("Collapse"));
QAction *const collapseAllAction = menu.addAction(Tr::tr("Collapse All"));

QAction *action = menu.exec(ev->globalPos());
Expand All @@ -722,6 +723,14 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
if (!errorMessage.isEmpty())
QMessageBox::critical(ICore::dialogParent(), Tr::tr("Error"), errorMessage);
}
} else if (action == collapseNodeAction) {
auto collapseRecursive = [this](auto&& recurse, QModelIndex index) -> void {
auto const rc = m_fileSystemModel->rowCount(index);
for (auto i = 0; i < rc; ++i)
recurse(recurse, index.model()->index(i, index.column(), index));
if (rc > 0) m_listView->collapse(index);
};
collapseRecursive(collapseRecursive, m_listView->currentIndex());
} else if (action == collapseAllAction) {
m_listView->collapseAll();
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/projectexplorer/projectexplorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,11 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
Command * const expandNodeCmd = ActionManager::registerAction(
dd->m_projectTreeExpandNodeAction, "ProjectExplorer.ExpandNode",
projectTreeContext);
auto* projectTreeCollapseNodeAction = new QAction(Tr::tr("Collapse"), this);
QObject::connect(projectTreeCollapseNodeAction, &QAction::triggered, ProjectTree::instance(),
&ProjectTree::collapseCurrentNodeRecursively);
auto* const collapseNodeCmd = ActionManager::registerAction(projectTreeCollapseNodeAction,
"ProjectExplorer.CollapseNode", projectTreeContext);
dd->m_projectTreeCollapseAllAction = new QAction(Tr::tr("Collapse All"), this);
Command * const collapseCmd = ActionManager::registerAction(
dd->m_projectTreeCollapseAllAction, Constants::PROJECTTREE_COLLAPSE_ALL,
Expand All @@ -1639,6 +1644,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
mfolderContextMenu, mprojectContextMenu, msessionContextMenu}) {
ac->addSeparator(treeGroup);
ac->addAction(expandNodeCmd, treeGroup);
ac->addAction(collapseNodeCmd, treeGroup);
ac->addAction(collapseCmd, treeGroup);
ac->addAction(expandCmd, treeGroup);
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/projectexplorer/projecttree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ void ProjectTree::expandCurrentNodeRecursively()
w->expandCurrentNodeRecursively();
}

void ProjectTree::collapseCurrentNodeRecursively()
{
if (const auto w = currentWidget())
w->collapseCurrentNodeRecursively();
}

void ProjectTree::collapseAll()
{
if (const auto w = currentWidget())
Expand Down
1 change: 1 addition & 0 deletions src/plugins/projectexplorer/projecttree.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class PROJECTEXPLORER_EXPORT ProjectTree : public QObject
static const QList<Node *> siblingsWithSameBaseName(const Node *fileNode);

void expandCurrentNodeRecursively();
void collapseCurrentNodeRecursively();

void collapseAll();
void expandAll();
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/projectexplorer/projecttreewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,25 @@ void ProjectTreeWidget::expandNodeRecursively(const QModelIndex &index)
m_view->expand(index);
}

void ProjectTreeWidget::collapseNodeRecursively(const QModelIndex &index)
{
const int rc = index.model()->rowCount(index);
for (int i = 0; i < rc; ++i)
collapseNodeRecursively(index.model()->index(i, index.column(), index));
if (rc > 0)
m_view->collapse(index);
}

void ProjectTreeWidget::expandCurrentNodeRecursively()
{
expandNodeRecursively(m_view->currentIndex());
}

void ProjectTreeWidget::collapseCurrentNodeRecursively()
{
collapseNodeRecursively(m_view->currentIndex());
}

void ProjectTreeWidget::collapseAll()
{
m_view->collapseAll();
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/projectexplorer/projecttreewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProjectTreeWidget final : public QWidget
void toggleAutoSynchronization();
void editCurrentItem();
void expandCurrentNodeRecursively();
void collapseCurrentNodeRecursively();
void collapseAll();
void expandAll();

Expand All @@ -68,6 +69,7 @@ class ProjectTreeWidget final : public QWidget
void syncFromDocumentManager();

void expandNodeRecursively(const QModelIndex &index);
void collapseNodeRecursively(const QModelIndex &index);

QTreeView *m_view = nullptr;
FlatModel *m_model = nullptr;
Expand Down

0 comments on commit 041e240

Please sign in to comment.