From ef545260cdd803f6d6fe8711276a78ea06e82569 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Fri, 17 Sep 2021 17:33:47 +0200 Subject: [PATCH] Add Show in File System View to project tree context menu That opens the selected file/path in the file system view. Behaves the same as the navigation view shortcut + syncing with the selected file. It would be nice to have that at other places too, but this requires moving the file system view to Core plugin first. Change-Id: Icc6d1041156d97f5d2bda43a3501758f06fe80aa Reviewed-by: Cristian Adam --- src/plugins/coreplugin/fileutils.cpp | 5 +++ src/plugins/coreplugin/fileutils.h | 1 + src/plugins/coreplugin/navigationwidget.cpp | 3 +- .../foldernavigationwidget.cpp | 14 +++++--- .../projectexplorer/foldernavigationwidget.h | 2 ++ .../projectexplorer/projectexplorer.cpp | 33 +++++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/plugins/coreplugin/fileutils.cpp b/src/plugins/coreplugin/fileutils.cpp index 68f2ef45c9e..c77f9836cba 100644 --- a/src/plugins/coreplugin/fileutils.cpp +++ b/src/plugins/coreplugin/fileutils.cpp @@ -147,6 +147,11 @@ QString FileUtils::msgFindInDirectory() return QApplication::translate("Core::Internal", "Find in This Directory..."); } +QString FileUtils::msgFileSystemAction() +{ + return QApplication::translate("Core::Internal", "Show in File System View"); +} + QString FileUtils::msgGraphicalShellAction() { if (HostOsInfo::isWindowsHost()) diff --git a/src/plugins/coreplugin/fileutils.h b/src/plugins/coreplugin/fileutils.h index eadefadc046..56c640981e2 100644 --- a/src/plugins/coreplugin/fileutils.h +++ b/src/plugins/coreplugin/fileutils.h @@ -46,6 +46,7 @@ struct CORE_EXPORT FileUtils static void openTerminal(const Utils::FilePath &path); static void openTerminal(const Utils::FilePath &path, const Utils::Environment &env); static QString msgFindInDirectory(); + static QString msgFileSystemAction(); // Platform-dependent action descriptions static QString msgGraphicalShellAction(); static QString msgTerminalHereAction(); diff --git a/src/plugins/coreplugin/navigationwidget.cpp b/src/plugins/coreplugin/navigationwidget.cpp index 5df66ec27b8..fbc9d5ad6a1 100644 --- a/src/plugins/coreplugin/navigationwidget.cpp +++ b/src/plugins/coreplugin/navigationwidget.cpp @@ -238,8 +238,7 @@ QWidget *NavigationWidget::activateSubWidget(Id factoryId, Side fallbackSide) preferredPosition = info.position; } - navigationWidget->activateSubWidget(factoryId, preferredPosition); - return navigationWidget; + return navigationWidget->activateSubWidget(factoryId, preferredPosition); } void NavigationWidget::setFactories(const QList &factories) diff --git a/src/plugins/projectexplorer/foldernavigationwidget.cpp b/src/plugins/projectexplorer/foldernavigationwidget.cpp index cfaf75a772c..93c7f03265b 100644 --- a/src/plugins/projectexplorer/foldernavigationwidget.cpp +++ b/src/plugins/projectexplorer/foldernavigationwidget.cpp @@ -566,6 +566,15 @@ void FolderNavigationWidget::removeCurrentItem() } } +void FolderNavigationWidget::syncWithFilePath(const Utils::FilePath &filePath) +{ + if (filePath.isEmpty()) + return; + if (m_rootAutoSync) + selectBestRootForFile(filePath); + selectFile(filePath); +} + bool FolderNavigationWidget::autoSynchronization() const { return m_autoSync; @@ -598,10 +607,7 @@ void FolderNavigationWidget::handleCurrentEditorChanged(Core::IEditor *editor) if (!m_autoSync || !editor || editor->document()->filePath().isEmpty() || editor->document()->isTemporary()) return; - const Utils::FilePath filePath = editor->document()->filePath(); - if (m_rootAutoSync) - selectBestRootForFile(filePath); - selectFile(filePath); + syncWithFilePath(editor->document()->filePath()); } void FolderNavigationWidget::selectBestRootForFile(const Utils::FilePath &filePath) diff --git a/src/plugins/projectexplorer/foldernavigationwidget.h b/src/plugins/projectexplorer/foldernavigationwidget.h index a69ddedf478..b39f073b4b8 100644 --- a/src/plugins/projectexplorer/foldernavigationwidget.h +++ b/src/plugins/projectexplorer/foldernavigationwidget.h @@ -115,6 +115,8 @@ public: void editCurrentItem(); void removeCurrentItem(); + void syncWithFilePath(const Utils::FilePath &filePath); + protected: void contextMenuEvent(QContextMenuEvent *ev) override; diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 129caef5d66..b5fdda4be47 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -125,6 +125,7 @@ #include #include #include +#include #include #include #include @@ -236,6 +237,7 @@ const char REMOVEPROJECT[] = "ProjectExplorer.RemoveProject"; const char OPENFILE[] = "ProjectExplorer.OpenFile"; const char SEARCHONFILESYSTEM[] = "ProjectExplorer.SearchOnFileSystem"; const char OPENTERMINALHERE[] = "ProjectExplorer.OpenTerminalHere"; +const char SHOWINFILESYSTEMVIEW[] = "ProjectExplorer.OpenFileSystemView"; const char DUPLICATEFILE[] = "ProjectExplorer.DuplicateFile"; const char DELETEFILE[] = "ProjectExplorer.DeleteFile"; const char DIFFFILE[] = "ProjectExplorer.DiffFile"; @@ -454,6 +456,7 @@ public: void openFile(); void searchOnFileSystem(); void showInGraphicalShell(); + void showInFileSystemPane(); void removeFile(); void duplicateFile(); void deleteFile(); @@ -552,6 +555,7 @@ public: ParameterAction *m_closeProjectFilesActionContextMenu; QAction *m_searchOnFileSystem; QAction *m_showInGraphicalShell; + QAction *m_showFileSystemPane; QAction *m_openTerminalHere; QAction *m_openTerminalHereBuildEnv; QAction *m_openTerminalHereRunEnv; @@ -1045,6 +1049,16 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er mfileContextMenu->addAction(cmd, Constants::G_FILE_OPEN); mfolderContextMenu->addAction(cmd, Constants::G_FOLDER_FILES); + // Show in File System View + dd->m_showFileSystemPane = new QAction(Core::FileUtils::msgFileSystemAction(), this); + cmd = ActionManager::registerAction(dd->m_showFileSystemPane, + Constants::SHOWINFILESYSTEMVIEW, + projectTreeContext); + mfileContextMenu->addAction(cmd, Constants::G_FILE_OPEN); + mfolderContextMenu->addAction(cmd, Constants::G_FOLDER_FILES); + msubProjectContextMenu->addAction(cmd, Constants::G_PROJECT_LAST); + mprojectContextMenu->addAction(cmd, Constants::G_PROJECT_LAST); + // Open Terminal Here menu dd->m_openTerminalHere = new QAction(Core::FileUtils::msgTerminalHereAction(), this); cmd = ActionManager::registerAction(dd->m_openTerminalHere, Constants::OPENTERMINALHERE, @@ -1772,6 +1786,13 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er dd, &ProjectExplorerPluginPrivate::searchOnFileSystem); connect(dd->m_showInGraphicalShell, &QAction::triggered, dd, &ProjectExplorerPluginPrivate::showInGraphicalShell); + // the following can delete the projects view that triggered the action, so make sure we + // are out of the context menu before actually doing it by queuing the action + connect(dd->m_showFileSystemPane, + &QAction::triggered, + dd, + &ProjectExplorerPluginPrivate::showInFileSystemPane, + Qt::QueuedConnection); connect(dd->m_openTerminalHere, &QAction::triggered, dd, []() { dd->openTerminalHere(sysEnv); }); connect(dd->m_openTerminalHereBuildEnv, &QAction::triggered, dd, []() { dd->openTerminalHere(buildEnv); }); @@ -3343,6 +3364,7 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions(Node *currentNode) m_openTerminalHereRunEnv->setVisible(false); m_showInGraphicalShell->setVisible(true); + m_showFileSystemPane->setVisible(true); m_searchOnFileSystem->setVisible(true); ActionContainer *runMenu = ActionManager::actionContainer(Constants::RUNMENUCONTEXTMENU); @@ -3445,6 +3467,7 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions(Node *currentNode) if (supports(HidePathActions)) { m_openTerminalHere->setVisible(false); m_showInGraphicalShell->setVisible(false); + m_showFileSystemPane->setVisible(false); m_searchOnFileSystem->setVisible(false); } @@ -3700,6 +3723,16 @@ void ProjectExplorerPluginPrivate::showInGraphicalShell() Core::FileUtils::showInGraphicalShell(ICore::dialogParent(), currentNode->path()); } +void ProjectExplorerPluginPrivate::showInFileSystemPane() +{ + Node *currentNode = ProjectTree::currentNode(); + QTC_ASSERT(currentNode, return ); + QWidget *widget = NavigationWidget::activateSubWidget(m_folderNavigationWidgetFactory.id(), + Side::Left); + if (auto *navWidget = qobject_cast(widget)) + navWidget->syncWithFilePath(currentNode->filePath()); +} + void ProjectExplorerPluginPrivate::openTerminalHere(const EnvironmentGetter &env) { const Node *currentNode = ProjectTree::currentNode();