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 <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2021-11-02 14:22:17 +01:00
parent 5e77303b57
commit 862c969441
8 changed files with 93 additions and 77 deletions

View File

@@ -49,18 +49,29 @@ namespace Core {
IEditorFactory is then asked to create an editor. IEditorFactory is then asked to create an editor.
Implementations should set the properties of the IEditorFactory subclass in 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 IEditorFactory instances automatically register themselves in \QC in their
constructor. constructor.
\sa Core::EditorType
\sa Core::IEditor \sa Core::IEditor
\sa Core::IDocument \sa Core::IDocument
\sa Core::EditorManager \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. 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. 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. 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 addMimeType()
\sa setMimeTypes() \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 Sets the \a displayName of the editor type. This is for example shown in
the \uicontrol {Open With} menu and the MIME type preferences. 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 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. 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. Sets the MIME types supported by the editor type to \a mimeTypes.
@@ -120,9 +131,31 @@ namespace Core {
\sa mimeTypes() \sa mimeTypes()
*/ */
static QList<EditorType *> g_editorTypes;
static QList<IEditorFactory *> g_editorFactories; static QList<IEditorFactory *> g_editorFactories;
static QHash<Utils::MimeType, IEditorFactory *> g_userPreferredEditorFactories; static QHash<Utils::MimeType, IEditorFactory *> 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. Creates an IEditorFactory.

View File

@@ -39,12 +39,43 @@ namespace Utils { class FilePath; }
namespace Core { namespace Core {
class IExternalEditor;
class IEditor; class IEditor;
class IEditorFactory; class IEditorFactory;
class EditorType;
using EditorFactoryList = QList<IEditorFactory *>; using EditorFactoryList = QList<IEditorFactory *>;
using EditorTypeList = QList<EditorType *>;
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 Q_OBJECT
@@ -56,23 +87,14 @@ public:
static const EditorFactoryList defaultEditorFactories(const Utils::MimeType &mimeType); static const EditorFactoryList defaultEditorFactories(const Utils::MimeType &mimeType);
static const EditorFactoryList preferredEditorFactories(const Utils::FilePath &filePath); 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; IEditor *createEditor() const;
IEditorFactory *asEditorFactory() override { return this; }
protected: 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<IEditor *()> &creator); void setEditorCreator(const std::function<IEditor *()> &creator);
private: private:
Utils::Id m_id;
QString m_displayName;
QStringList m_mimeTypes;
std::function<IEditor *()> m_creator; std::function<IEditor *()> m_creator;
}; };

View File

@@ -66,8 +66,7 @@ static QList<IExternalEditor *> g_externalEditors;
/*! /*!
\internal \internal
*/ */
IExternalEditor::IExternalEditor(QObject *parent) IExternalEditor::IExternalEditor()
: QObject(parent)
{ {
g_externalEditors.append(this); g_externalEditors.append(this);
} }

View File

@@ -25,6 +25,8 @@
#pragma once #pragma once
#include "ieditorfactory.h"
#include <coreplugin/core_global.h> #include <coreplugin/core_global.h>
#include <utils/id.h> #include <utils/id.h>
@@ -40,20 +42,19 @@ class IExternalEditor;
using ExternalEditorList = QList<IExternalEditor *>; using ExternalEditorList = QList<IExternalEditor *>;
class CORE_EXPORT IExternalEditor : public QObject class CORE_EXPORT IExternalEditor : public EditorType
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit IExternalEditor(QObject *parent = nullptr); explicit IExternalEditor();
~IExternalEditor() override; ~IExternalEditor() override;
static const ExternalEditorList allExternalEditors(); static const ExternalEditorList allExternalEditors();
static const ExternalEditorList externalEditors(const Utils::MimeType &mimeType); static const ExternalEditorList externalEditors(const Utils::MimeType &mimeType);
virtual QStringList mimeTypes() const = 0; IExternalEditor *asExternalEditor() override { return this; }
virtual Utils::Id id() const = 0;
virtual QString displayName() const = 0;
virtual bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) = 0; virtual bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) = 0;
}; };

View File

@@ -35,24 +35,11 @@ using namespace Core;
using namespace Core::Internal; using namespace Core::Internal;
using namespace Utils; using namespace Utils;
SystemEditor::SystemEditor(QObject *parent) : SystemEditor::SystemEditor()
IExternalEditor(parent)
{ {
} setId("CorePlugin.OpenWithSystemEditor");
setDisplayName(tr("System Editor"));
QStringList SystemEditor::mimeTypes() const setMimeTypes({"application/octet-stream"});
{
return QStringList("application/octet-stream");
}
Id SystemEditor::id() const
{
return "CorePlugin.OpenWithSystemEditor";
}
QString SystemEditor::displayName() const
{
return tr("System Editor");
} }
bool SystemEditor::startEditor(const FilePath &filePath, QString *errorMessage) bool SystemEditor::startEditor(const FilePath &filePath, QString *errorMessage)

View File

@@ -35,11 +35,7 @@ class SystemEditor : public IExternalEditor
Q_OBJECT Q_OBJECT
public: public:
explicit SystemEditor(QObject *parent = nullptr); explicit SystemEditor();
QStringList mimeTypes() const override;
Utils::Id id() const override;
QString displayName() const override;
bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) override; bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) override;
}; };

View File

@@ -99,12 +99,12 @@ static const char linguistDisplayName[] = QT_TRANSLATE_NOOP("OpenWith::Editors",
ExternalQtEditor::ExternalQtEditor(Utils::Id id, ExternalQtEditor::ExternalQtEditor(Utils::Id id,
const QString &displayName, const QString &displayName,
const QString &mimetype, const QString &mimetype,
const CommandForQtVersion &commandForQtVersion) : const CommandForQtVersion &commandForQtVersion)
m_mimeTypes(mimetype), : m_commandForQtVersion(commandForQtVersion)
m_id(id),
m_displayName(displayName),
m_commandForQtVersion(commandForQtVersion)
{ {
setId(id);
setDisplayName(displayName);
setMimeTypes({mimetype});
} }
ExternalQtEditor *ExternalQtEditor::createLinguistEditor() 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<QtSupport::BaseQtVersion *> qtVersions, static QString findFirstCommand(QVector<QtSupport::BaseQtVersion *> qtVersions,
ExternalQtEditor::CommandForQtVersion command) ExternalQtEditor::CommandForQtVersion command)
{ {

View File

@@ -59,10 +59,6 @@ public:
static ExternalQtEditor *createLinguistEditor(); static ExternalQtEditor *createLinguistEditor();
static ExternalQtEditor *createDesignerEditor(); 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; bool startEditor(const Utils::FilePath &filePath, QString *errorMessage) override;
// Data required to launch the editor // Data required to launch the editor
@@ -89,9 +85,6 @@ protected:
bool startEditorProcess(const LaunchData &data, QString *errorMessage); bool startEditorProcess(const LaunchData &data, QString *errorMessage);
private: private:
const QStringList m_mimeTypes;
const Utils::Id m_id;
const QString m_displayName;
const CommandForQtVersion m_commandForQtVersion; const CommandForQtVersion m_commandForQtVersion;
}; };