ProjectTree: Add per-node "Expand" and "Collapse" actions

Change-Id: Ic722598f7bb2665ac7a09c24b7eefa9dc6787808
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2019-12-19 15:45:24 +01:00
parent 5ef320d9fc
commit a5785e678e
5 changed files with 58 additions and 3 deletions

View File

@@ -511,6 +511,8 @@ public:
QAction *m_openFileAction; QAction *m_openFileAction;
QAction *m_projectTreeCollapseAllAction; QAction *m_projectTreeCollapseAllAction;
QAction *m_projectTreeExpandAllAction; QAction *m_projectTreeExpandAllAction;
QAction *m_projectTreeCollapseNodeAction = nullptr;
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;
@@ -1350,6 +1352,26 @@ 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_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); 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,

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,27 @@ void ProjectTree::sessionAndTreeChanged()
emit treeChanged(); 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() 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 +356,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,9 @@ 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 collapseCurrentNode();
void expandCurrentNode();
void collapseAll(); void collapseAll();
void expandAll(); void expandAll();
@@ -117,6 +120,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

@@ -430,6 +430,16 @@ void ProjectTreeWidget::setAutoSynchronization(bool sync)
syncFromDocumentManager(); syncFromDocumentManager();
} }
void ProjectTreeWidget::collapseCurrentNode()
{
m_view->collapse(m_view->currentIndex());
}
void ProjectTreeWidget::expandCurrentNode()
{
m_view->expand(m_view->currentIndex());
}
void ProjectTreeWidget::collapseAll() void ProjectTreeWidget::collapseAll()
{ {
m_view->collapseAll(); m_view->collapseAll();

View File

@@ -67,6 +67,8 @@ public:
void toggleAutoSynchronization(); void toggleAutoSynchronization();
void editCurrentItem(); void editCurrentItem();
void collapseCurrentNode();
void expandCurrentNode();
void collapseAll(); void collapseAll();
void expandAll(); void expandAll();