diff --git a/src/plugins/coreplugin/filemanager.cpp b/src/plugins/coreplugin/filemanager.cpp index 4d53a503bee..b96fbecc76e 100644 --- a/src/plugins/coreplugin/filemanager.cpp +++ b/src/plugins/coreplugin/filemanager.cpp @@ -33,8 +33,10 @@ #include "filemanager.h" #include "editormanager.h" -#include "ieditor.h" #include "icore.h" +#include "ieditor.h" +#include "ieditorfactory.h" +#include "iexternaleditor.h" #include "ifile.h" #include "iversioncontrol.h" #include "mimedatabase.h" @@ -53,9 +55,11 @@ #include #include #include +#include #include -#include #include +#include +#include #include /*! @@ -95,6 +99,9 @@ static const char directoryGroupC[] = "Directories"; static const char projectDirectoryKeyC[] = "Projects"; static const char useProjectDirectoryKeyC[] = "UseProjectsDirectory"; +Q_DECLARE_METATYPE(Core::IEditorFactory*) +Q_DECLARE_METATYPE(Core::IExternalEditor*) + namespace Core { namespace Internal { @@ -1274,6 +1281,65 @@ void FileManager::notifyFilesChangedInternally(const QStringList &files) emit filesChangedInternally(files); } +void FileManager::populateOpenWithMenu(QMenu *menu, const QString &fileName) +{ + typedef QList EditorFactoryList; + typedef QList ExternalEditorList; + + menu->clear(); + + bool anyMatches = false; + + ICore *core = ICore::instance(); + if (const MimeType mt = core->mimeDatabase()->findByFile(QFileInfo(fileName))) { + const EditorFactoryList factories = core->editorManager()->editorFactories(mt, false); + const ExternalEditorList externalEditors = core->editorManager()->externalEditors(mt, false); + anyMatches = !factories.empty() || !externalEditors.empty(); + if (anyMatches) { + // Add all suitable editors + foreach (IEditorFactory *editorFactory, factories) { + // Add action to open with this very editor factory + QString const actionTitle = editorFactory->displayName(); + QAction * const action = menu->addAction(actionTitle); + action->setData(qVariantFromValue(editorFactory)); + } + // Add all suitable external editors + foreach (IExternalEditor *externalEditor, externalEditors) { + QAction * const action = menu->addAction(externalEditor->displayName()); + action->setData(qVariantFromValue(externalEditor)); + } + } + } + menu->setEnabled(anyMatches); +} + +void FileManager::executeOpenWithMenuAction(QAction *action, const QString &fileName) +{ + EditorManager *em = EditorManager::instance(); + const QVariant data = action->data(); + if (qVariantCanConvert(data)) { + IEditorFactory *factory = qVariantValue(data); + + // close any open editors that have this file open, but have a different type. + QList editorsOpenForFile = em->editorsForFileName(fileName); + if (!editorsOpenForFile.isEmpty()) { + foreach (IEditor *openEditor, editorsOpenForFile) { + if (factory->id() == openEditor->id()) + editorsOpenForFile.removeAll(openEditor); + } + if (!em->closeEditors(editorsOpenForFile)) // don't open if cancel was pressed + return; + } + + em->openEditor(fileName, factory->id(), EditorManager::ModeSwitch); + return; + } + if (qVariantCanConvert(data)) { + IExternalEditor *externalEditor = qVariantValue(data); + em->openExternalEditor(fileName, externalEditor->id()); + } +} + // -------------- FileChangeBlocker FileChangeBlocker::FileChangeBlocker(const QString &fileName) diff --git a/src/plugins/coreplugin/filemanager.h b/src/plugins/coreplugin/filemanager.h index b877b34ed8b..e5357a98f7c 100644 --- a/src/plugins/coreplugin/filemanager.h +++ b/src/plugins/coreplugin/filemanager.h @@ -41,7 +41,9 @@ #include QT_BEGIN_NAMESPACE +class QAction; class QMainWindow; +class QMenu; QT_END_NAMESPACE namespace Core { @@ -135,6 +137,9 @@ public: QString projectsDirectory() const; void setProjectsDirectory(const QString &); + static void populateOpenWithMenu(QMenu *menu, const QString &fileName); + static void executeOpenWithMenuAction(QAction *action, const QString &fileName); + public slots: /* Used to notify e.g. the code model to update the given files. Does *not* lead to any editors to reload or any other editor manager actions. */ diff --git a/src/plugins/projectexplorer/foldernavigationwidget.cpp b/src/plugins/projectexplorer/foldernavigationwidget.cpp index 5605836b295..d7787f950e8 100644 --- a/src/plugins/projectexplorer/foldernavigationwidget.cpp +++ b/src/plugins/projectexplorer/foldernavigationwidget.cpp @@ -323,8 +323,8 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev) // open with... if (!m_fileSystemModel->isDir(current)) { QMenu *openWith = menu.addMenu(tr("Open with")); - ProjectExplorerPlugin::populateOpenWithMenu(openWith, - m_fileSystemModel->filePath(current)); + Core::FileManager::populateOpenWithMenu(openWith, + m_fileSystemModel->filePath(current)); } // Open file dialog to choose a path starting from current @@ -361,8 +361,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev) findOnFileSystem(info.absolutePath()); return; } - ProjectExplorerPlugin::openEditorFromAction(action, - m_fileSystemModel->filePath(current)); + Core::FileManager::executeOpenWithMenuAction(action, m_fileSystemModel->filePath(current)); } QString FolderNavigationWidget::msgFindOnFileSystem() diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index ef448b1216b..4db45fc0199 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -101,8 +101,6 @@ #include #include #include -#include -#include #include #include #include @@ -149,9 +147,6 @@ current project, open projects, etc. */ -Q_DECLARE_METATYPE(Core::IEditorFactory*) -Q_DECLARE_METATYPE(Core::IExternalEditor*) - namespace { bool debug = false; } @@ -2769,41 +2764,9 @@ void ProjectExplorerPlugin::setStartupProject() setStartupProject(d->m_currentProject); } -void ProjectExplorerPlugin::populateOpenWithMenu(QMenu *menu, const QString &fileName) -{ - typedef QList EditorFactoryList; - typedef QList ExternalEditorList; - - menu->clear(); - - bool anyMatches = false; - - Core::ICore *core = Core::ICore::instance(); - if (const Core::MimeType mt = core->mimeDatabase()->findByFile(QFileInfo(fileName))) { - const EditorFactoryList factories = core->editorManager()->editorFactories(mt, false); - const ExternalEditorList externalEditors = core->editorManager()->externalEditors(mt, false); - anyMatches = !factories.empty() || !externalEditors.empty(); - if (anyMatches) { - // Add all suitable editors - foreach (Core::IEditorFactory *editorFactory, factories) { - // Add action to open with this very editor factory - QString const actionTitle = editorFactory->displayName(); - QAction * const action = menu->addAction(actionTitle); - action->setData(qVariantFromValue(editorFactory)); - } - // Add all suitable external editors - foreach (Core::IExternalEditor *externalEditor, externalEditors) { - QAction * const action = menu->addAction(externalEditor->displayName()); - action->setData(qVariantFromValue(externalEditor)); - } - } - } - menu->setEnabled(anyMatches); -} - void ProjectExplorerPlugin::populateOpenWithMenu() { - populateOpenWithMenu(d->m_openWithMenu, currentNode()->path()); + Core::FileManager::populateOpenWithMenu(d->m_openWithMenu, currentNode()->path()); } void ProjectExplorerPlugin::openWithMenuTriggered(QAction *action) @@ -2811,34 +2774,7 @@ void ProjectExplorerPlugin::openWithMenuTriggered(QAction *action) if (!action) qWarning() << "ProjectExplorerPlugin::openWithMenuTriggered no action, can't happen."; else - openEditorFromAction(action, currentNode()->path()); -} - -void ProjectExplorerPlugin::openEditorFromAction(QAction *action, const QString &fileName) -{ - Core::EditorManager *em = Core::EditorManager::instance(); - const QVariant data = action->data(); - if (qVariantCanConvert(data)) { - Core::IEditorFactory *factory = qVariantValue(data); - - // close any open editors that have this file open, but have a different type. - QList editorsOpenForFile = em->editorsForFileName(fileName); - if (!editorsOpenForFile.isEmpty()) { - foreach (Core::IEditor *openEditor, editorsOpenForFile) { - if (factory->id() == openEditor->id()) - editorsOpenForFile.removeAll(openEditor); - } - if (!em->closeEditors(editorsOpenForFile)) // don't open if cancel was pressed - return; - } - - em->openEditor(fileName, factory->id(), Core::EditorManager::ModeSwitch); - return; - } - if (qVariantCanConvert(data)) { - Core::IExternalEditor *externalEditor = qVariantValue(data); - em->openExternalEditor(fileName, externalEditor->id()); - } + Core::FileManager::executeOpenWithMenuAction(action, currentNode()->path()); } void ProjectExplorerPlugin::updateSessionMenu() diff --git a/src/plugins/projectexplorer/projectexplorer.h b/src/plugins/projectexplorer/projectexplorer.h index 8f7bfffee60..d59c0356fcc 100644 --- a/src/plugins/projectexplorer/projectexplorer.h +++ b/src/plugins/projectexplorer/projectexplorer.h @@ -96,8 +96,6 @@ public: bool saveModifiedFiles(); void showContextMenu(QWidget *view, const QPoint &globalPos, Node *node); - static void populateOpenWithMenu(QMenu *menu, const QString &fileName); - static void openEditorFromAction(QAction *action, const QString &fileName); //PluginInterface bool initialize(const QStringList &arguments, QString *errorMessage);