Move Open With... handling to editor manager.

Change-Id: I27faa327ae33244927e21aa74875d9601aaf9e50
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
Eike Ziller
2015-05-20 13:07:13 +02:00
parent 2e4a86a87c
commit 697479572f
9 changed files with 95 additions and 93 deletions

View File

@@ -1375,88 +1375,6 @@ void DocumentManager::notifyFilesChangedInternally(const QStringList &files)
emit m_instance->filesChangedInternally(files);
}
static void openEditorWith(const QString &fileName, Core::Id editorId)
{
// close any open editors that have this file open
// remember the views to open new editors in there
QList<EditorView *> views;
QList<IEditor *> editorsOpenForFile
= DocumentModel::editorsForFilePath(fileName);
foreach (IEditor *openEditor, editorsOpenForFile) {
EditorView *view = EditorManagerPrivate::viewForEditor(openEditor);
if (view && view->currentEditor() == openEditor) // visible
views.append(view);
}
if (!EditorManager::closeEditors(editorsOpenForFile)) // don't open if cancel was pressed
return;
if (views.isEmpty()) {
EditorManager::openEditor(fileName, editorId);
} else {
if (EditorView *currentView = EditorManagerPrivate::currentEditorView()) {
if (views.removeOne(currentView))
views.prepend(currentView); // open editor in current view first
}
EditorManager::OpenEditorFlags flags;
foreach (EditorView *view, views) {
IEditor *editor = EditorManagerPrivate::openEditor(view, fileName, editorId, flags);
// Do not change the current editor after opening the first one. That
// * prevents multiple updates of focus etc which are not necessary
// * lets us control which editor is made current by putting the current editor view
// to the front (if that was in the list in the first place)
flags |= EditorManager::DoNotChangeCurrentEditor;
// do not try to open more editors if this one failed, or editor type does not
// support duplication anyhow
if (!editor || !editor->duplicateSupported())
break;
}
}
}
void DocumentManager::populateOpenWithMenu(QMenu *menu, const QString &fileName)
{
typedef QList<IEditorFactory*> EditorFactoryList;
typedef QList<IExternalEditor*> ExternalEditorList;
menu->clear();
bool anyMatches = false;
Utils::MimeDatabase mdb;
const Utils::MimeType mt = mdb.mimeTypeForFile(fileName);
if (mt.isValid()) {
const EditorFactoryList factories = EditorManager::editorFactories(mt, false);
const ExternalEditorList externalEditors = EditorManager::externalEditors(mt, false);
anyMatches = !factories.empty() || !externalEditors.empty();
if (anyMatches) {
// Add all suitable editors
foreach (IEditorFactory *editorFactory, factories) {
Core::Id editorId = editorFactory->id();
// Add action to open with this very editor factory
QString const actionTitle = editorFactory->displayName();
QAction *action = menu->addAction(actionTitle);
// Below we need QueuedConnection because otherwise, if a qrc file
// is inside of a qrc file itself, and the qrc editor opens the Open with menu,
// crashes happen, because the editor instance is deleted by openEditorWith
// while the menu is still being processed.
connect(action, &QAction::triggered, EditorManager::instance(),
[fileName, editorId]() {
openEditorWith(fileName, editorId);
}, Qt::QueuedConnection);
}
// Add all suitable external editors
foreach (IExternalEditor *externalEditor, externalEditors) {
QAction *action = menu->addAction(externalEditor->displayName());
Core::Id editorId = externalEditor->id();
connect(action, &QAction::triggered, [fileName, editorId]() {
EditorManager::openExternalEditor(fileName, editorId);
});
}
}
}
menu->setEnabled(anyMatches);
}
bool DocumentManager::eventFilter(QObject *obj, QEvent *e)
{
if (obj == qApp && e->type() == QEvent::ApplicationActivate) {