Commit 3998a0cb authored by Eike Ziller's avatar Eike Ziller
Browse files

Move "Open with" menu logic to FileManager.



Better place than ProjectExplorer since it will then be available
elsewhere too.

Change-Id: I0508f121918105a562d2499ac6b31d19d733abad
Reviewed-by: default avatarDaniel Teske <daniel.teske@nokia.com>
parent cb717979
......@@ -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 <QtCore/QTimer>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QDateTime>
#include <QtGui/QAction>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtGui/QPushButton>
/*!
......@@ -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<IEditorFactory*> EditorFactoryList;
typedef QList<IExternalEditor*> 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<IEditorFactory *>(data)) {
IEditorFactory *factory = qVariantValue<IEditorFactory *>(data);
// close any open editors that have this file open, but have a different type.
QList<IEditor *> 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<IExternalEditor *>(data)) {
IExternalEditor *externalEditor = qVariantValue<IExternalEditor *>(data);
em->openExternalEditor(fileName, externalEditor->id());
}
}
// -------------- FileChangeBlocker
FileChangeBlocker::FileChangeBlocker(const QString &fileName)
......
......@@ -41,7 +41,9 @@
#include <QtCore/QVariant>
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. */
......
......@@ -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()
......
......@@ -101,8 +101,6 @@
#include <coreplugin/id.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/editormanager/iexternaleditor.h>
#include <coreplugin/findplaceholder.h>
#include <coreplugin/basefilewizard.h>
#include <coreplugin/vcsmanager.h>
......@@ -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<Core::IEditorFactory*> EditorFactoryList;
typedef QList<Core::IExternalEditor*> 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<Core::IEditorFactory *>(data)) {
Core::IEditorFactory *factory = qVariantValue<Core::IEditorFactory *>(data);
// close any open editors that have this file open, but have a different type.
QList<Core::IEditor *> 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<Core::IExternalEditor *>(data)) {
Core::IExternalEditor *externalEditor = qVariantValue<Core::IExternalEditor *>(data);
em->openExternalEditor(fileName, externalEditor->id());
}
Core::FileManager::executeOpenWithMenuAction(action, currentNode()->path());
}
void ProjectExplorerPlugin::updateSessionMenu()
......
......@@ -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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment