summaryrefslogtreecommitdiff
path: root/src/plugins/coreplugin/editormanager
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2018-06-25 15:39:22 +0200
committerEike Ziller <eike.ziller@qt.io>2018-07-02 08:24:05 +0000
commitb2d844b22aa7f37d2592ee6e98842c2264deb98f (patch)
tree44a7c330fc84cdea71dad7ae253ff3c234eb62d3 /src/plugins/coreplugin/editormanager
parenta948d5223c6799c0c8f641a1381a29ea69a0bc46 (diff)
downloadqt-creator-b2d844b22aa7f37d2592ee6e98842c2264deb98f.tar.gz
Allow changing the default editor for mime types
Double-clicking or clicking on the selected item shows a combo box with all editor types that can handle that mime type. Modified handlers are shown in italic, and a new button resets all handlers to the default. Change-Id: I4083c31e3867eb2a2a47adc85e4bd20f3d57be9a Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/coreplugin/editormanager')
-rw-r--r--src/plugins/coreplugin/editormanager/editormanager.cpp47
-rw-r--r--src/plugins/coreplugin/editormanager/ieditorfactory.cpp44
-rw-r--r--src/plugins/coreplugin/editormanager/ieditorfactory.h4
-rw-r--r--src/plugins/coreplugin/editormanager/ieditorfactory_p.h42
4 files changed, 125 insertions, 12 deletions
diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index ae70e6a7be..2abb16ccd4 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -44,6 +44,7 @@
#include <coreplugin/diffservice.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/ieditorfactory.h>
+#include <coreplugin/editormanager/ieditorfactory_p.h>
#include <coreplugin/editormanager/iexternaleditor.h>
#include <coreplugin/editortoolbar.h>
#include <coreplugin/fileutils.h>
@@ -121,6 +122,7 @@ static const char autoSuspendMinDocumentCountKey[] = "EditorManager/AutoSuspendM
static const char warnBeforeOpeningBigTextFilesKey[] = "EditorManager/WarnBeforeOpeningBigTextFiles";
static const char bigTextFileSizeLimitKey[] = "EditorManager/BigTextFileSizeLimitInMB";
static const char fileSystemCaseSensitivityKey[] = "Core/FileSystemCaseSensitivity";
+static const char preferredEditorFactoriesKey[] = "EditorManager/PreferredEditorFactories";
static const char scratchBufferKey[] = "_q_emScratchBuffer";
@@ -936,12 +938,11 @@ void EditorManagerPrivate::showPopupOrSelectDocument()
Id EditorManagerPrivate::getOpenWithEditorId(const QString &fileName, bool *isExternalEditor)
{
// Collect editors that can open the file
- const Utils::MimeType mt = Utils::mimeTypeForFile(fileName);
QList<Id> allEditorIds;
QStringList allEditorDisplayNames;
QList<Id> externalEditorIds;
// Built-in
- const EditorFactoryList editors = IEditorFactory::editorFactories(mt);
+ const EditorFactoryList editors = IEditorFactory::preferredEditorFactories(fileName);
const int size = editors.size();
allEditorDisplayNames.reserve(size);
for (int i = 0; i < size; i++) {
@@ -949,6 +950,7 @@ Id EditorManagerPrivate::getOpenWithEditorId(const QString &fileName, bool *isEx
allEditorDisplayNames.push_back(editors.at(i)->displayName());
}
// External editors
+ const Utils::MimeType mt = Utils::mimeTypeForFile(fileName);
const ExternalEditorList exEditors = IExternalEditor::externalEditors(mt);
const int esize = exEditors.size();
for (int i = 0; i < esize; i++) {
@@ -971,6 +973,39 @@ Id EditorManagerPrivate::getOpenWithEditorId(const QString &fileName, bool *isEx
return selectedId;
}
+static QMap<QString, QVariant> toMap(const QHash<Utils::MimeType, IEditorFactory *> &hash)
+{
+ QMap<QString, QVariant> map;
+ auto it = hash.begin();
+ const auto end = hash.end();
+ while (it != end) {
+ map.insert(it.key().name(), it.value()->id().toSetting());
+ ++it;
+ }
+ return map;
+}
+
+static QHash<Utils::MimeType, IEditorFactory *> fromMap(const QMap<QString, QVariant> &map)
+{
+ const EditorFactoryList factories = IEditorFactory::allEditorFactories();
+ QHash<Utils::MimeType, IEditorFactory *> hash;
+ auto it = map.begin();
+ const auto end = map.end();
+ while (it != end) {
+ const Utils::MimeType mimeType = Utils::mimeTypeForName(it.key());
+ if (mimeType.isValid()) {
+ const Id factoryId = Id::fromSetting(it.value());
+ IEditorFactory *factory = Utils::findOrDefault(factories,
+ Utils::equal(&IEditorFactory::id,
+ factoryId));
+ if (factory)
+ hash.insert(mimeType, factory);
+ }
+ ++it;
+ }
+ return hash;
+}
+
void EditorManagerPrivate::saveSettings()
{
ICore::settingsDatabase()->setValue(documentStatesKey, d->m_editorStates);
@@ -992,6 +1027,7 @@ void EditorManagerPrivate::saveSettings()
qsettings->remove(fileSystemCaseSensitivityKey);
else
qsettings->setValue(fileSystemCaseSensitivityKey, sensitivity);
+ qsettings->setValue(preferredEditorFactoriesKey, toMap(userPreferredEditorFactories()));
}
void EditorManagerPrivate::readSettings()
@@ -1023,6 +1059,9 @@ void EditorManagerPrivate::readSettings()
else
HostOsInfo::setOverrideFileNameCaseSensitivity(sensitivity);
}
+ const QHash<Utils::MimeType, IEditorFactory *> preferredEditorFactories = fromMap(
+ qs->value(preferredEditorFactoriesKey).toMap());
+ setUserPreferredEditorFactories(preferredEditorFactories);
SettingsDatabase *settings = ICore::settingsDatabase();
if (settings->contains(documentStatesKey)) {
@@ -1127,7 +1166,7 @@ EditorFactoryList EditorManagerPrivate::findFactories(Id editorId, const QString
EditorFactoryList factories;
if (!editorId.isValid()) {
- factories = IEditorFactory::editorFactories(fileName);
+ factories = IEditorFactory::preferredEditorFactories(fileName);
} else {
// Find by editor id
IEditorFactory *factory = Utils::findOrDefault(IEditorFactory::allEditorFactories(),
@@ -2467,8 +2506,8 @@ void EditorManager::populateOpenWithMenu(QMenu *menu, const QString &fileName)
menu->clear();
+ const EditorFactoryList factories = IEditorFactory::preferredEditorFactories(fileName);
const Utils::MimeType mt = Utils::mimeTypeForFile(fileName);
- const EditorFactoryList factories = IEditorFactory::editorFactories(mt);
const ExternalEditorList extEditors = IExternalEditor::externalEditors(mt);
const bool anyMatches = !factories.empty() || !extEditors.empty();
if (anyMatches) {
diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory.cpp b/src/plugins/coreplugin/editormanager/ieditorfactory.cpp
index 7ffbfe4a9b..fe21fb7a10 100644
--- a/src/plugins/coreplugin/editormanager/ieditorfactory.cpp
+++ b/src/plugins/coreplugin/editormanager/ieditorfactory.cpp
@@ -24,6 +24,7 @@
****************************************************************************/
#include "ieditorfactory.h"
+#include "ieditorfactory_p.h"
#include "editormanager.h"
#include "editormanager_p.h"
@@ -35,6 +36,7 @@
namespace Core {
static QList<IEditorFactory *> g_editorFactories;
+static QHash<Utils::MimeType, IEditorFactory *> g_userPreferredEditorFactories;
IEditorFactory::IEditorFactory(QObject *parent)
: QObject(parent)
@@ -52,7 +54,11 @@ const EditorFactoryList IEditorFactory::allEditorFactories()
return g_editorFactories;
}
-const EditorFactoryList IEditorFactory::editorFactories(const Utils::MimeType &mimeType)
+/*!
+ Returns all available editors for this \a mimeType in the default order
+ (editors ordered by mime type hierarchy).
+*/
+const EditorFactoryList IEditorFactory::defaultEditorFactories(const Utils::MimeType &mimeType)
{
EditorFactoryList rc;
const EditorFactoryList allFactories = IEditorFactory::allEditorFactories();
@@ -60,19 +66,45 @@ const EditorFactoryList IEditorFactory::editorFactories(const Utils::MimeType &m
return rc;
}
-const EditorFactoryList IEditorFactory::editorFactories(const QString &fileName)
+/*!
+ Returns the available editors for \a fileName in order of preference.
+ That is the default order for the file's MIME type but with a user overridden default
+ editor first, and if the file is a too large text file, with the binary editor as the
+ very first.
+*/
+const EditorFactoryList IEditorFactory::preferredEditorFactories(const QString &fileName)
{
const QFileInfo fileInfo(fileName);
- // Find by mime type
- Utils::MimeType mimeType = Utils::mimeTypeForFile(fileInfo);
+ // default factories by mime type
+ const Utils::MimeType mimeType = Utils::mimeTypeForFile(fileInfo);
+ EditorFactoryList factories = defaultEditorFactories(mimeType);
+ const auto factories_moveToFront = [&factories](IEditorFactory *f) {
+ factories.removeAll(f);
+ factories.prepend(f);
+ };
+ // user preferred factory to front
+ IEditorFactory *userPreferred = Internal::userPreferredEditorFactories().value(mimeType);
+ if (userPreferred)
+ factories_moveToFront(userPreferred);
// open text files > 48 MB in binary editor
if (fileInfo.size() > EditorManager::maxTextFileSize()
&& mimeType.inherits("text/plain")) {
- mimeType = Utils::mimeTypeForName("application/octet-stream");
+ const Utils::MimeType binary = Utils::mimeTypeForName("application/octet-stream");
+ const EditorFactoryList binaryEditors = defaultEditorFactories(binary);
+ if (!binaryEditors.isEmpty())
+ factories_moveToFront(binaryEditors.first());
}
+ return factories;
+}
- return IEditorFactory::editorFactories(mimeType);
+QHash<Utils::MimeType, Core::IEditorFactory *> Core::Internal::userPreferredEditorFactories()
+{
+ return g_userPreferredEditorFactories;
}
+void Internal::setUserPreferredEditorFactories(const QHash<Utils::MimeType, IEditorFactory *> &factories)
+{
+ g_userPreferredEditorFactories = factories;
+}
} // Core
diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory.h b/src/plugins/coreplugin/editormanager/ieditorfactory.h
index 779d032873..a759854e72 100644
--- a/src/plugins/coreplugin/editormanager/ieditorfactory.h
+++ b/src/plugins/coreplugin/editormanager/ieditorfactory.h
@@ -49,8 +49,8 @@ public:
~IEditorFactory() override;
static const EditorFactoryList allEditorFactories();
- static const EditorFactoryList editorFactories(const Utils::MimeType &mimeType);
- static const EditorFactoryList editorFactories(const QString &fileName);
+ static const EditorFactoryList defaultEditorFactories(const Utils::MimeType &mimeType);
+ static const EditorFactoryList preferredEditorFactories(const QString &fileName);
QString displayName() const { return m_displayName; }
void setDisplayName(const QString &displayName) { m_displayName = displayName; }
diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory_p.h b/src/plugins/coreplugin/editormanager/ieditorfactory_p.h
new file mode 100644
index 0000000000..74cd0ca029
--- /dev/null
+++ b/src/plugins/coreplugin/editormanager/ieditorfactory_p.h
@@ -0,0 +1,42 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+****************************************************************************/
+
+#pragma once
+
+#include <utils/mimetypes/mimetype.h>
+
+#include <QHash>
+
+namespace Core {
+
+class IEditorFactory;
+
+namespace Internal {
+
+QHash<Utils::MimeType, IEditorFactory *> userPreferredEditorFactories();
+void setUserPreferredEditorFactories(const QHash<Utils::MimeType, IEditorFactory *> &factories);
+
+} // Internal
+} // Core