EditorManager: If opening a editor fails, offer other editors factories

Change-Id: Ia8cf298c1a62e01d6df6f9f871a8d3eafde5821d
Task-number: QTCREATORBUG-14064
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
Daniel Teske
2015-04-09 14:10:35 +02:00
parent f5e4b8e8cb
commit 15d8b6ba81
6 changed files with 200 additions and 23 deletions

View File

@@ -0,0 +1,64 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "overridecursor.h"
#include <QApplication>
using namespace Utils;
OverrideCursor::OverrideCursor(const QCursor &cursor)
: m_set(true), m_cursor(cursor)
{
QApplication::setOverrideCursor(cursor);
}
OverrideCursor::~OverrideCursor()
{
if (m_set)
QApplication::restoreOverrideCursor();
}
void OverrideCursor::set()
{
if (!m_set) {
QApplication::setOverrideCursor(m_cursor);
m_set = true;
}
}
void OverrideCursor::reset()
{
if (m_set) {
QApplication::restoreOverrideCursor();
m_set = false;
}
}

View File

@@ -0,0 +1,54 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef OVERRIDECURSOR_H
#define OVERRIDECURSOR_H
#include "utils_global.h"
#include <QCursor>
namespace Utils {
class QTCREATOR_UTILS_EXPORT OverrideCursor
{
public:
OverrideCursor(const QCursor &cursor);
~OverrideCursor();
void set();
void reset();
private:
bool m_set;
QCursor m_cursor;
};
}
#endif // OVERRIDECURSOR_H

View File

@@ -89,7 +89,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/macroexpander.cpp \ $$PWD/macroexpander.cpp \
$$PWD/theme/theme.cpp \ $$PWD/theme/theme.cpp \
$$PWD/progressindicator.cpp \ $$PWD/progressindicator.cpp \
$$PWD/fadingindicator.cpp $$PWD/fadingindicator.cpp \
$$PWD/overridecursor.cpp
win32:SOURCES += $$PWD/consoleprocess_win.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp
@@ -190,7 +191,8 @@ HEADERS += \
$$PWD/theme/theme_p.h \ $$PWD/theme/theme_p.h \
$$PWD/progressindicator.h \ $$PWD/progressindicator.h \
$$PWD/fadingindicator.h \ $$PWD/fadingindicator.h \
$$PWD/executeondestruction.h $$PWD/executeondestruction.h \
$$PWD/overridecursor.h
FORMS += $$PWD/filewizardpage.ui \ FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \ $$PWD/projectintropage.ui \

View File

@@ -123,6 +123,8 @@ QtcLibrary {
"outputformat.h", "outputformat.h",
"outputformatter.cpp", "outputformatter.cpp",
"outputformatter.h", "outputformatter.h",
"overridecursor.cpp",
"overridecursor.h",
"parameteraction.cpp", "parameteraction.cpp",
"parameteraction.h", "parameteraction.h",
"pathchooser.cpp", "pathchooser.cpp",

View File

@@ -72,6 +72,7 @@
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
#include <utils/mimetypes/mimetype.h> #include <utils/mimetypes/mimetype.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/overridecursor.h>
#include <QClipboard> #include <QClipboard>
#include <QDateTime> #include <QDateTime>
@@ -91,6 +92,8 @@
#include <QPushButton> #include <QPushButton>
#include <QSplitter> #include <QSplitter>
#include <algorithm>
using namespace Utils; using namespace Utils;
enum { debugEditorManager=0 }; enum { debugEditorManager=0 };
@@ -579,22 +582,64 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
realFn = fn; realFn = fn;
} }
IEditor *editor = createEditor(editorId, fn); EditorManager::EditorFactoryList factories = EditorManagerPrivate::findFactories(Id(), fn);
// If we could not open the file in the requested editor, fall if (editorId.isValid())
// back to the default editor: std::stable_partition(factories.begin(), factories.end(), Utils::equal(&IEditorFactory::id, editorId));
if (!editor)
editor = createEditor(Id(), fn); IEditor *editor = 0;
QTC_ASSERT(editor, return 0); auto overrideCursor = Utils::OverrideCursor(QCursor(Qt::WaitCursor));
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
IEditorFactory *factory = factories.takeFirst();
while (factory) {
editor = createEditor(factory, fn);
if (!editor) {
factory = factories.takeFirst();
continue;
}
QString errorString; QString errorString;
if (!editor->open(&errorString, fn, realFn)) { if (editor->open(&errorString, fn, realFn))
QApplication::restoreOverrideCursor(); break;
overrideCursor.reset();
delete editor;
if (errorString.isEmpty()) if (errorString.isEmpty())
errorString = tr("Could not open \"%1\": Unknown error.").arg(realFn); errorString = tr("Could not open \"%1\": Unknown error.").arg(realFn);
QMessageBox::critical(ICore::mainWindow(), EditorManager::tr("File Error"), errorString);
delete editor; QMessageBox msgbox(QMessageBox::Critical, EditorManager::tr("File Error"), errorString, QMessageBox::Open | QMessageBox::Cancel, ICore::mainWindow());
return 0;
IEditorFactory *selectedFactory = 0;
if (!factories.isEmpty()) {
QPushButton *button = qobject_cast<QPushButton *>(msgbox.button(QMessageBox::Open));
QTC_ASSERT(button, return 0);
QMenu *menu = new QMenu(button);
foreach (IEditorFactory *factory, factories) {
QAction *action = menu->addAction(factory->displayName());
connect(action, &QAction::triggered, [&selectedFactory, factory, &msgbox]() {
selectedFactory = factory;
msgbox.done(QMessageBox::Open);
});
} }
button->setMenu(menu);
} else {
msgbox.setStandardButtons(QMessageBox::Ok);
}
int ret = msgbox.exec();
if (ret == QMessageBox::Cancel || ret == QMessageBox::Ok)
return 0;
overrideCursor.set();
factories.removeOne(selectedFactory);
factory = selectedFactory;
}
if (!editor)
return 0;
if (realFn != fn) if (realFn != fn)
editor->document()->setRestoredFrom(realFn); editor->document()->setRestoredFrom(realFn);
addEditor(editor); addEditor(editor);
@@ -609,7 +654,6 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
if (flags & EditorManager::CanContainLineAndColumnNumber) if (flags & EditorManager::CanContainLineAndColumnNumber)
editor->gotoLine(lineNumber, columnNumber); editor->gotoLine(lineNumber, columnNumber);
QApplication::restoreOverrideCursor();
return result; return result;
} }
@@ -905,7 +949,7 @@ int EditorManagerPrivate::autoSaveInterval()
return d->m_autoSaveInterval; return d->m_autoSaveInterval;
} }
IEditor *EditorManagerPrivate::createEditor(Id editorId, const QString &fileName) EditorManager::EditorFactoryList EditorManagerPrivate::findFactories(Id editorId, const QString &fileName)
{ {
if (debugEditorManager) if (debugEditorManager)
qDebug() << Q_FUNC_INFO << editorId.name() << fileName; qDebug() << Q_FUNC_INFO << editorId.name() << fileName;
@@ -926,7 +970,7 @@ IEditor *EditorManagerPrivate::createEditor(Id editorId, const QString &fileName
&& mimeType.name().startsWith(QLatin1String("text"))) { && mimeType.name().startsWith(QLatin1String("text"))) {
mimeType = mdb.mimeTypeForName(QLatin1String("application/octet-stream")); mimeType = mdb.mimeTypeForName(QLatin1String("application/octet-stream"));
} }
factories = EditorManager::editorFactories(mimeType, true); factories = EditorManager::editorFactories(mimeType, false);
} else { } else {
// Find by editor id // Find by editor id
if (IEditorFactory *factory = findById<IEditorFactory>(editorId)) if (IEditorFactory *factory = findById<IEditorFactory>(editorId))
@@ -935,10 +979,17 @@ IEditor *EditorManagerPrivate::createEditor(Id editorId, const QString &fileName
if (factories.empty()) { if (factories.empty()) {
qWarning("%s: unable to find an editor factory for the file '%s', editor Id '%s'.", qWarning("%s: unable to find an editor factory for the file '%s', editor Id '%s'.",
Q_FUNC_INFO, fileName.toUtf8().constData(), editorId.name().constData()); Q_FUNC_INFO, fileName.toUtf8().constData(), editorId.name().constData());
return 0;
} }
IEditor *editor = factories.front()->createEditor(); return factories;
}
IEditor *EditorManagerPrivate::createEditor(IEditorFactory *factory, const QString &fileName)
{
if (!factory)
return 0;
IEditor *editor = factory->createEditor();
if (editor) { if (editor) {
QTC_CHECK(editor->document()->id().isValid()); // sanity check that the editor has an id set QTC_CHECK(editor->document()->id().isValid()); // sanity check that the editor has an id set
connect(editor->document(), SIGNAL(changed()), d, SLOT(handleDocumentStateChange())); connect(editor->document(), SIGNAL(changed()), d, SLOT(handleDocumentStateChange()));
@@ -2420,10 +2471,13 @@ IEditor *EditorManager::openEditorWithContents(Id editorId,
} }
} }
edt = EditorManagerPrivate::createEditor(editorId, title); EditorFactoryList factories = EditorManagerPrivate::findFactories(editorId, title);
if (!edt) if (factories.isEmpty())
return 0; return 0;
edt = EditorManagerPrivate::createEditor(factories.first(), title);
if (!edt)
return 0;
if (!edt->document()->setContents(contents)) { if (!edt->document()->setContents(contents)) {
delete edt; delete edt;
edt = 0; edt = 0;

View File

@@ -174,7 +174,8 @@ private:
static OpenEditorsWindow *windowPopup(); static OpenEditorsWindow *windowPopup();
static void showPopupOrSelectDocument(); static void showPopupOrSelectDocument();
static IEditor *createEditor(Id editorId = Id(), const QString &fileName = QString()); static EditorManager::EditorFactoryList findFactories(Id editorId, const QString &fileName);
static IEditor *createEditor(IEditorFactory *factory, const QString &fileName);
static void addEditor(IEditor *editor); static void addEditor(IEditor *editor);
static void removeEditor(IEditor *editor); static void removeEditor(IEditor *editor);
static IEditor *placeEditor(EditorView *view, IEditor *editor); static IEditor *placeEditor(EditorView *view, IEditor *editor);