From 862c969441f54f53aea56cc97368d4e7099f33fd Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 2 Nov 2021 14:22:17 +0100 Subject: [PATCH] Create common base for IEditorFactory and IExternalEditor As preparation for both being selectable as default editor type for a mime type. Change-Id: Ie34ad25caa7fe0cc0b740c96743db9bab378bf24 Reviewed-by: David Schulz --- .../editormanager/ieditorfactory.cpp | 51 +++++++++++++++---- .../coreplugin/editormanager/ieditorfactory.h | 46 ++++++++++++----- .../editormanager/iexternaleditor.cpp | 3 +- .../editormanager/iexternaleditor.h | 11 ++-- .../coreplugin/editormanager/systemeditor.cpp | 21 ++------ .../coreplugin/editormanager/systemeditor.h | 6 +-- .../qmakeprojectmanager/externaleditors.cpp | 25 ++------- .../qmakeprojectmanager/externaleditors.h | 7 --- 8 files changed, 93 insertions(+), 77 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory.cpp b/src/plugins/coreplugin/editormanager/ieditorfactory.cpp index 59d0b988c08..046bf330a84 100644 --- a/src/plugins/coreplugin/editormanager/ieditorfactory.cpp +++ b/src/plugins/coreplugin/editormanager/ieditorfactory.cpp @@ -49,18 +49,29 @@ namespace Core { IEditorFactory is then asked to create an editor. Implementations should set the properties of the IEditorFactory subclass in - their constructor. + their constructor with EditorType::setId(), EditorType::setDisplayName(), + EditorType::setMimeTypes(), and setEditorCreator() IEditorFactory instances automatically register themselves in \QC in their constructor. + \sa Core::EditorType \sa Core::IEditor \sa Core::IDocument \sa Core::EditorManager */ /*! - \fn void Core::IEditorFactory::addMimeType(const QString &mimeType) + \class Core::EditorType + \inheaderfile coreplugin/editormanager/ieditorfactory.h + \inmodule QtCreator + + \brief The EditorType class is the base class for Core::IEditorFactory and + Core::IExternalEditor. +*/ + +/*! + \fn void Core::EditorType::addMimeType(const QString &mimeType) Adds \a mimeType to the list of MIME types supported by this editor type. @@ -69,7 +80,7 @@ namespace Core { */ /*! - \fn QString Core::IEditorFactory::displayName() const + \fn QString Core::EditorType::displayName() const Returns a user-visible description of the editor type. @@ -77,7 +88,7 @@ namespace Core { */ /*! - \fn Utils::Id Core::IEditorFactory::id() const + \fn Utils::Id Core::EditorType::id() const Returns the ID of the editors' document type. @@ -85,16 +96,16 @@ namespace Core { */ /*! - \fn QString Core::IEditorFactory::mimeTypes() const + \fn QString Core::EditorType::mimeTypes() const - Returns the list of supported MIME types of this editor factory. + Returns the list of supported MIME types of this editor type. \sa addMimeType() \sa setMimeTypes() */ /*! - \fn void Core::IEditorFactory::setDisplayName(const QString &displayName) + \fn void Core::EditorType::setDisplayName(const QString &displayName) Sets the \a displayName of the editor type. This is for example shown in the \uicontrol {Open With} menu and the MIME type preferences. @@ -103,7 +114,7 @@ namespace Core { */ /*! - \fn void Core::IEditorFactory::setId(Utils::Id id) + \fn void Core::EditorType::setId(Utils::Id id) Sets the \a id of the editors' document type. This must be the same as the IDocument::id() of the documents returned by created editors. @@ -112,7 +123,7 @@ namespace Core { */ /*! - \fn void Core::IEditorFactory::setMimeTypes(const QStringList &mimeTypes) + \fn void Core::EditorType::setMimeTypes(const QStringList &mimeTypes) Sets the MIME types supported by the editor type to \a mimeTypes. @@ -120,9 +131,31 @@ namespace Core { \sa mimeTypes() */ +static QList g_editorTypes; static QList g_editorFactories; static QHash g_userPreferredEditorFactories; +/*! + \internal +*/ +EditorType::EditorType() +{ + g_editorTypes.append(this); +} + +/*! + \internal +*/ +EditorType::~EditorType() +{ + g_editorTypes.removeOne(this); +} + +const EditorTypeList EditorType::allEditorTypes() +{ + return g_editorTypes; +} + /*! Creates an IEditorFactory. diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory.h b/src/plugins/coreplugin/editormanager/ieditorfactory.h index a876e4c430b..d26083f0b7c 100644 --- a/src/plugins/coreplugin/editormanager/ieditorfactory.h +++ b/src/plugins/coreplugin/editormanager/ieditorfactory.h @@ -39,12 +39,43 @@ namespace Utils { class FilePath; } namespace Core { +class IExternalEditor; class IEditor; class IEditorFactory; +class EditorType; using EditorFactoryList = QList; +using EditorTypeList = QList; -class CORE_EXPORT IEditorFactory : public QObject +class CORE_EXPORT EditorType : public QObject +{ + Q_OBJECT +public: + ~EditorType() override; + + static const EditorTypeList allEditorTypes(); + + Utils::Id id() const { return m_id; } + QString displayName() const { return m_displayName; } + QStringList mimeTypes() const { return m_mimeTypes; } + + virtual IEditorFactory *asEditorFactory() { return nullptr; }; + virtual IExternalEditor *asExternalEditor() { return nullptr; }; + +protected: + EditorType(); + void setId(Utils::Id id) { m_id = id; } + void setDisplayName(const QString &displayName) { m_displayName = displayName; } + void setMimeTypes(const QStringList &mimeTypes) { m_mimeTypes = mimeTypes; } + void addMimeType(const QString &mimeType) { m_mimeTypes.append(mimeType); } + +private: + Utils::Id m_id; + QString m_displayName; + QStringList m_mimeTypes; +}; + +class CORE_EXPORT IEditorFactory : public EditorType { Q_OBJECT @@ -56,23 +87,14 @@ public: static const EditorFactoryList defaultEditorFactories(const Utils::MimeType &mimeType); static const EditorFactoryList preferredEditorFactories(const Utils::FilePath &filePath); - Utils::Id id() const { return m_id; } - QString displayName() const { return m_displayName; } - QStringList mimeTypes() const { return m_mimeTypes; } - IEditor *createEditor() const; + IEditorFactory *asEditorFactory() override { return this; } + protected: - void setId(Utils::Id id) { m_id = id; } - void setDisplayName(const QString &displayName) { m_displayName = displayName; } - void setMimeTypes(const QStringList &mimeTypes) { m_mimeTypes = mimeTypes; } - void addMimeType(const QString &mimeType) { m_mimeTypes.append(mimeType); } void setEditorCreator(const std::function &creator); private: - Utils::Id m_id; - QString m_displayName; - QStringList m_mimeTypes; std::function m_creator; }; diff --git a/src/plugins/coreplugin/editormanager/iexternaleditor.cpp b/src/plugins/coreplugin/editormanager/iexternaleditor.cpp index 0680da44a42..274be781cc5 100644 --- a/src/plugins/coreplugin/editormanager/iexternaleditor.cpp +++ b/src/plugins/coreplugin/editormanager/iexternaleditor.cpp @@ -66,8 +66,7 @@ static QList g_externalEditors; /*! \internal */ -IExternalEditor::IExternalEditor(QObject *parent) - : QObject(parent) +IExternalEditor::IExternalEditor() { g_externalEditors.append(this); } diff --git a/src/plugins/coreplugin/editormanager/iexternaleditor.h b/src/plugins/coreplugin/editormanager/iexternaleditor.h index b105f376405..d37fc9fd12d 100644 --- a/src/plugins/coreplugin/editormanager/iexternaleditor.h +++ b/src/plugins/coreplugin/editormanager/iexternaleditor.h @@ -25,6 +25,8 @@ #pragma once +#include "ieditorfactory.h" + #include #include @@ -40,20 +42,19 @@ class IExternalEditor; using ExternalEditorList = QList; -class CORE_EXPORT IExternalEditor : public QObject +class CORE_EXPORT IExternalEditor : public EditorType { Q_OBJECT public: - explicit IExternalEditor(QObject *parent = nullptr); + explicit IExternalEditor(); ~IExternalEditor() override; static const ExternalEditorList allExternalEditors(); static const ExternalEditorList externalEditors(const Utils::MimeType &mimeType); - virtual QStringList mimeTypes() const = 0; - virtual Utils::Id id() const = 0; - virtual QString displayName() const = 0; + IExternalEditor *asExternalEditor() override { return this; } + virtual bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) = 0; }; diff --git a/src/plugins/coreplugin/editormanager/systemeditor.cpp b/src/plugins/coreplugin/editormanager/systemeditor.cpp index 30840881006..c2791791ccb 100644 --- a/src/plugins/coreplugin/editormanager/systemeditor.cpp +++ b/src/plugins/coreplugin/editormanager/systemeditor.cpp @@ -35,24 +35,11 @@ using namespace Core; using namespace Core::Internal; using namespace Utils; -SystemEditor::SystemEditor(QObject *parent) : - IExternalEditor(parent) +SystemEditor::SystemEditor() { -} - -QStringList SystemEditor::mimeTypes() const -{ - return QStringList("application/octet-stream"); -} - -Id SystemEditor::id() const -{ - return "CorePlugin.OpenWithSystemEditor"; -} - -QString SystemEditor::displayName() const -{ - return tr("System Editor"); + setId("CorePlugin.OpenWithSystemEditor"); + setDisplayName(tr("System Editor")); + setMimeTypes({"application/octet-stream"}); } bool SystemEditor::startEditor(const FilePath &filePath, QString *errorMessage) diff --git a/src/plugins/coreplugin/editormanager/systemeditor.h b/src/plugins/coreplugin/editormanager/systemeditor.h index 870bc5c3158..1c8bdab4a19 100644 --- a/src/plugins/coreplugin/editormanager/systemeditor.h +++ b/src/plugins/coreplugin/editormanager/systemeditor.h @@ -35,11 +35,7 @@ class SystemEditor : public IExternalEditor Q_OBJECT public: - explicit SystemEditor(QObject *parent = nullptr); - - QStringList mimeTypes() const override; - Utils::Id id() const override; - QString displayName() const override; + explicit SystemEditor(); bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) override; }; diff --git a/src/plugins/qmakeprojectmanager/externaleditors.cpp b/src/plugins/qmakeprojectmanager/externaleditors.cpp index ad6192844de..d7e2b5cacac 100644 --- a/src/plugins/qmakeprojectmanager/externaleditors.cpp +++ b/src/plugins/qmakeprojectmanager/externaleditors.cpp @@ -99,12 +99,12 @@ static const char linguistDisplayName[] = QT_TRANSLATE_NOOP("OpenWith::Editors", ExternalQtEditor::ExternalQtEditor(Utils::Id id, const QString &displayName, const QString &mimetype, - const CommandForQtVersion &commandForQtVersion) : - m_mimeTypes(mimetype), - m_id(id), - m_displayName(displayName), - m_commandForQtVersion(commandForQtVersion) + const CommandForQtVersion &commandForQtVersion) + : m_commandForQtVersion(commandForQtVersion) { + setId(id); + setDisplayName(displayName); + setMimeTypes({mimetype}); } ExternalQtEditor *ExternalQtEditor::createLinguistEditor() @@ -127,21 +127,6 @@ ExternalQtEditor *ExternalQtEditor::createDesignerEditor() } } -QStringList ExternalQtEditor::mimeTypes() const -{ - return m_mimeTypes; -} - -Utils::Id ExternalQtEditor::id() const -{ - return m_id; -} - -QString ExternalQtEditor::displayName() const -{ - return m_displayName; -} - static QString findFirstCommand(QVector qtVersions, ExternalQtEditor::CommandForQtVersion command) { diff --git a/src/plugins/qmakeprojectmanager/externaleditors.h b/src/plugins/qmakeprojectmanager/externaleditors.h index d4b4c5becdc..9760d35ba85 100644 --- a/src/plugins/qmakeprojectmanager/externaleditors.h +++ b/src/plugins/qmakeprojectmanager/externaleditors.h @@ -59,10 +59,6 @@ public: static ExternalQtEditor *createLinguistEditor(); static ExternalQtEditor *createDesignerEditor(); - QStringList mimeTypes() const override; - Utils::Id id() const override; - QString displayName() const override; - bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) override; // Data required to launch the editor @@ -89,9 +85,6 @@ protected: bool startEditorProcess(const LaunchData &data, QString *errorMessage); private: - const QStringList m_mimeTypes; - const Utils::Id m_id; - const QString m_displayName; const CommandForQtVersion m_commandForQtVersion; };