Core: Make IEditorFactory::createEditor use a function object

Also, replace or remove unneeded Q_OBJECTs, and make base
setters and adders protected.

Change-Id: I212257ef53984d8852dc8c478537199fc9483486
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2020-02-04 18:16:57 +01:00
parent ccc2a347a7
commit d7ae3b79f8
24 changed files with 104 additions and 175 deletions

View File

@@ -38,17 +38,15 @@ using namespace Android::Internal;
AndroidManifestEditorFactory::AndroidManifestEditorFactory()
{
setId(Constants::ANDROID_MANIFEST_EDITOR_ID);
setDisplayName(tr("Android Manifest editor"));
setDisplayName(AndroidManifestEditorWidget::tr("Android Manifest editor"));
addMimeType(Constants::ANDROID_MANIFEST_MIME_TYPE);
auto actionHandler = new TextEditor::TextEditorActionHandler(
this, id(), Constants::ANDROID_MANIFEST_EDITOR_CONTEXT);
actionHandler->setTextEditorWidgetResolver([](Core::IEditor *editor) {
return static_cast<AndroidManifestEditor *>(editor)->textEditor();
});
}
Core::IEditor *AndroidManifestEditorFactory::createEditor()
{
auto androidManifestEditorWidget = new AndroidManifestEditorWidget();
setEditorCreator([] {
auto androidManifestEditorWidget = new AndroidManifestEditorWidget;
return androidManifestEditorWidget->editor();
});
}

View File

@@ -30,14 +30,10 @@
namespace Android {
namespace Internal {
class AndroidManifestEditorFactory : public Core::IEditorFactory
class AndroidManifestEditorFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
AndroidManifestEditorFactory();
Core::IEditor *createEditor() override;
};
} // namespace Internal

View File

@@ -471,10 +471,8 @@ BinEditorFactory::BinEditorFactory()
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setDisplayName(QCoreApplication::translate("OpenWith::Editors", Constants::C_BINEDITOR_DISPLAY_NAME));
addMimeType(Constants::C_BINEDITOR_MIMETYPE);
}
IEditor *BinEditorFactory::createEditor()
{
setEditorCreator([] {
auto widget = new BinEditorWidget();
auto editor = new BinEditor(widget);
@@ -498,6 +496,7 @@ IEditor *BinEditorFactory::createEditor()
aggregate->add(widget);
return editor;
});
}
///////////////////////////////// BinEditor Services //////////////////////////////////

View File

@@ -44,14 +44,10 @@ class BinEditorPlugin : public ExtensionSystem::IPlugin
void extensionsInitialized() final {}
};
class BinEditorFactory : public Core::IEditorFactory
class BinEditorFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
BinEditorFactory();
Core::IEditor *createEditor() final;
};
class FactoryServiceImpl : public QObject, public FactoryService

View File

@@ -96,6 +96,17 @@ const EditorFactoryList IEditorFactory::preferredEditorFactories(const QString &
return factories;
}
IEditor *IEditorFactory::createEditor() const
{
QTC_ASSERT(m_creator, return nullptr);
return m_creator();
}
void IEditorFactory::setEditorCreator(const std::function<IEditor *()> &creator)
{
m_creator = creator;
}
QHash<Utils::MimeType, Core::IEditorFactory *> Core::Internal::userPreferredEditorFactories()
{
return g_userPreferredEditorFactories;

View File

@@ -52,22 +52,25 @@ public:
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; }
Id id() const { return m_id; }
void setId(Id id) { m_id = id; }
virtual IEditor *createEditor() = 0;
QString displayName() const { return m_displayName; }
QStringList mimeTypes() const { return m_mimeTypes; }
IEditor *createEditor() const;
protected:
void setId(Id id) { m_id = id; }
void setDisplayName(const QString &displayName) { m_displayName = displayName; }
void setMimeTypes(const QStringList &mimeTypes) { m_mimeTypes = mimeTypes; }
void addMimeType(const char *mimeType) { m_mimeTypes.append(QLatin1String(mimeType)); }
void addMimeType(const QString &mimeType) { m_mimeTypes.append(mimeType); }
void setEditorCreator(const std::function<IEditor *()> &creator);
private:
Id m_id;
QString m_displayName;
QStringList m_mimeTypes;
std::function<IEditor *()> m_creator;
};
} // namespace Core

View File

@@ -45,14 +45,10 @@ FormEditorFactory::FormEditorFactory()
setId(K_DESIGNER_XML_EDITOR_ID);
setDisplayName(QCoreApplication::translate("Designer", C_DESIGNER_XML_DISPLAY_NAME));
addMimeType(FORM_MIMETYPE);
setEditorCreator([] { return FormEditorW::createEditor(); });
FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_UI, "ui");
}
IEditor *FormEditorFactory::createEditor()
{
return FormEditorW::createEditor();
}
} // namespace Internal
} // namespace Designer

View File

@@ -30,14 +30,10 @@
namespace Designer {
namespace Internal {
class FormEditorFactory : public Core::IEditorFactory
class FormEditorFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
FormEditorFactory();
Core::IEditor *createEditor() override;
};
} // namespace Internal

View File

@@ -62,11 +62,8 @@ DiffEditorFactory::DiffEditorFactory(QObject *parent)
rightHandler->setTextEditorWidgetResolver([](Core::IEditor *e) {
return static_cast<DiffEditor *>(e)->rightEditorWidget();
});
}
Core::IEditor *DiffEditorFactory::createEditor()
{
return new DiffEditor(new DiffEditorDocument);
setEditorCreator([] { return new DiffEditor(new DiffEditorDocument); });
}
} // namespace Internal

View File

@@ -25,22 +25,15 @@
#pragma once
#include "diffeditor_global.h"
#include <coreplugin/editormanager/ieditorfactory.h>
namespace DiffEditor {
namespace Internal {
class DiffEditorFactory : public Core::IEditorFactory
{
Q_OBJECT
public:
explicit DiffEditorFactory(QObject *parent);
Core::IEditor *createEditor() override;
};
} // namespace Internal

View File

@@ -38,16 +38,12 @@ ImageViewerFactory::ImageViewerFactory()
{
setId(Constants::IMAGEVIEWER_ID);
setDisplayName(QCoreApplication::translate("OpenWith::Editors", Constants::IMAGEVIEWER_DISPLAY_NAME));
setEditorCreator([] { return new ImageViewer; });
const QList<QByteArray> supportedMimeTypes = QImageReader::supportedMimeTypes();
foreach (const QByteArray &format, supportedMimeTypes)
for (const QByteArray &format : supportedMimeTypes)
addMimeType(format.constData());
}
Core::IEditor *ImageViewerFactory::createEditor()
{
return new ImageViewer();
}
} // namespace Internal
} // namespace ImageViewer

View File

@@ -27,19 +27,14 @@
#pragma once
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
namespace ImageViewer {
namespace Internal {
class ImageViewerFactory : public Core::IEditorFactory
class ImageViewerFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
ImageViewerFactory();
Core::IEditor *createEditor() override;
};
} // namespace Internal

View File

@@ -34,37 +34,24 @@
namespace ModelEditor {
namespace Internal {
class ModelEditorFactory::ModelEditorFactoryPrivate
{
public:
UiController *uiController = nullptr;
ActionHandler *actionHandler = nullptr;
};
ModelEditorFactory::ModelEditorFactory(UiController *uiController)
: d(new ModelEditorFactoryPrivate())
{
setId(Constants::MODEL_EDITOR_ID);
setDisplayName(QCoreApplication::translate("OpenWith::Editors", Constants::MODEL_EDITOR_DISPLAY_NAME));
addMimeType(Constants::MIME_TYPE_MODEL);
d->uiController = uiController;
d->actionHandler = new ActionHandler(Core::Context(Constants::MODEL_EDITOR_ID), this);
m_uiController = uiController;
m_actionHandler = new ActionHandler(Core::Context(Constants::MODEL_EDITOR_ID), this);
setEditorCreator([this] { return new ModelEditor(m_uiController, m_actionHandler); });
}
ModelEditorFactory::~ModelEditorFactory()
{
delete d->actionHandler;
delete d;
}
Core::IEditor *ModelEditorFactory::createEditor()
{
return new ModelEditor(d->uiController, d->actionHandler);
delete m_actionHandler;
}
void ModelEditorFactory::extensionsInitialized()
{
d->actionHandler->createActions();
m_actionHandler->createActions();
}
} // namespace Internal

View File

@@ -30,24 +30,21 @@
namespace ModelEditor {
namespace Internal {
class ActionHandler;
class ModelEditor;
class UiController;
class ModelEditorFactory :
public Core::IEditorFactory
class ModelEditorFactory : public Core::IEditorFactory
{
Q_OBJECT
class ModelEditorFactoryPrivate;
public:
explicit ModelEditorFactory(UiController *uiController);
~ModelEditorFactory();
Core::IEditor *createEditor() override;
void extensionsInitialized();
private:
ModelEditorFactoryPrivate *d;
UiController *m_uiController = nullptr;
ActionHandler *m_actionHandler = nullptr;
};
} // namespace Internal

View File

@@ -71,11 +71,6 @@ NimEditorFactory::NimEditorFactory()
setCodeFoldingSupported(true);
}
Core::IEditor *NimEditorFactory::createEditor()
{
return TextEditorFactory::createEditor();
}
void NimEditorFactory::decorateEditor(TextEditorWidget *editor)
{
editor->textDocument()->setSyntaxHighlighter(new NimHighlighter());

View File

@@ -36,7 +36,6 @@ class NimEditorFactory : public TextEditor::TextEditorFactory
public:
NimEditorFactory();
Core::IEditor *createEditor() override;
static void decorateEditor(TextEditor::TextEditorWidget *editor);
};

View File

@@ -40,8 +40,7 @@ using namespace ResourceEditor::Internal;
using namespace ResourceEditor::Constants;
ResourceEditorFactory::ResourceEditorFactory(ResourceEditorPlugin *plugin) :
Core::IEditorFactory(plugin),
m_plugin(plugin)
Core::IEditorFactory(plugin)
{
setId(RESOURCEEDITOR_ID);
setMimeTypes(QStringList(QLatin1String(C_RESOURCE_MIMETYPE)));
@@ -49,10 +48,8 @@ ResourceEditorFactory::ResourceEditorFactory(ResourceEditorPlugin *plugin) :
Core::FileIconProvider::registerIconOverlayForSuffix(
ProjectExplorer::Constants::FILEOVERLAY_QRC, "qrc");
}
Core::IEditor *ResourceEditorFactory::createEditor()
{
Core::Context context(C_RESOURCEEDITOR);
return new ResourceEditorW(context, m_plugin);
setEditorCreator([plugin] {
return new ResourceEditorW(Core::Context(C_RESOURCEEDITOR), plugin);
});
}

View File

@@ -26,26 +26,16 @@
#pragma once
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/icontext.h>
#include <QStringList>
namespace ResourceEditor {
namespace Internal {
class ResourceEditorPlugin;
class ResourceEditorFactory : public Core::IEditorFactory
class ResourceEditorFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
explicit ResourceEditorFactory(ResourceEditorPlugin *plugin);
Core::IEditor *createEditor() override;
private:
ResourceEditorPlugin *m_plugin;
};
} // namespace Internal

View File

@@ -46,10 +46,8 @@ ScxmlEditorFactory::ScxmlEditorFactory(QObject *parent)
addMimeType(ProjectExplorer::Constants::SCXML_MIMETYPE);
Core::FileIconProvider::registerIconOverlayForSuffix(":/projectexplorer/images/fileoverlay_scxml.png", "scxml");
}
Core::IEditor *ScxmlEditorFactory::createEditor()
{
setEditorCreator([this] {
if (!m_editorData) {
m_editorData = new ScxmlEditorData(this);
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
@@ -57,4 +55,5 @@ Core::IEditor *ScxmlEditorFactory::createEditor()
QGuiApplication::restoreOverrideCursor();
}
return m_editorData->createEditor();
});
}

View File

@@ -32,15 +32,11 @@ namespace Internal {
class ScxmlEditorData;
class ScxmlEditorFactory : public Core::IEditorFactory
class ScxmlEditorFactory final : public Core::IEditorFactory
{
Q_OBJECT
public:
explicit ScxmlEditorFactory(QObject *parent);
Core::IEditor *createEditor() override;
private:
ScxmlEditorData* m_editorData = nullptr;
};

View File

@@ -8506,8 +8506,7 @@ class TextEditorFactoryPrivate
public:
TextEditorFactoryPrivate(TextEditorFactory *parent) :
q(parent),
m_widgetCreator([]() { return new TextEditorWidget; }),
m_editorCreator([]() { return new BaseTextEditor; })
m_widgetCreator([]() { return new TextEditorWidget; })
{}
BaseTextEditor *duplicateTextEditor(BaseTextEditor *other)
@@ -8540,7 +8539,9 @@ public:
TextEditorFactory::TextEditorFactory(QObject *parent)
: IEditorFactory(parent), d(new TextEditorFactoryPrivate(this))
{}
{
setEditorCreator([]() { return new BaseTextEditor; });
}
TextEditorFactory::~TextEditorFactory()
{
@@ -8562,6 +8563,21 @@ void TextEditorFactory::setEditorWidgetCreator(const EditorWidgetCreator &creato
void TextEditorFactory::setEditorCreator(const EditorCreator &creator)
{
d->m_editorCreator = creator;
IEditorFactory::setEditorCreator([this] {
static DocumentContentCompletionProvider basicSnippetProvider;
TextDocumentPtr doc(d->m_documentCreator());
if (d->m_indenterCreator)
doc->setIndenter(d->m_indenterCreator(doc->document()));
if (d->m_syntaxHighlighterCreator)
doc->setSyntaxHighlighter(d->m_syntaxHighlighterCreator());
doc->setCompletionAssistProvider(d->m_completionAssistProvider ? d->m_completionAssistProvider
: &basicSnippetProvider);
return d->createEditorHelper(doc);
});
}
void TextEditorFactory::setIndenterCreator(const IndenterCreator &creator)
@@ -8629,23 +8645,6 @@ void TextEditorFactory::setParenthesesMatchingEnabled(bool on)
d->m_paranthesesMatchinEnabled = on;
}
IEditor *TextEditorFactory::createEditor()
{
static DocumentContentCompletionProvider basicSnippetProvider;
TextDocumentPtr doc(d->m_documentCreator());
if (d->m_indenterCreator)
doc->setIndenter(d->m_indenterCreator(doc->document()));
if (d->m_syntaxHighlighterCreator)
doc->setSyntaxHighlighter(d->m_syntaxHighlighterCreator());
doc->setCompletionAssistProvider(d->m_completionAssistProvider ? d->m_completionAssistProvider
: &basicSnippetProvider);
return d->createEditorHelper(doc);
}
BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentPtr &document)
{
TextEditorWidget *widget = m_widgetCreator();

View File

@@ -666,8 +666,6 @@ public:
void setParenthesesMatchingEnabled(bool on);
void setCodeFoldingSupported(bool on);
Core::IEditor *createEditor() override;
private:
friend class BaseTextEditor;
friend class PlainTextEditorFactory;

View File

@@ -44,12 +44,18 @@ VcsSubmitEditorFactory::VcsSubmitEditorFactory
(const VcsBaseSubmitEditorParameters *parameters,
const EditorCreator &editorCreator,
VcsBasePluginPrivate *plugin)
: IEditorFactory(plugin), m_editorCreator(editorCreator)
: IEditorFactory(plugin)
{
setId(parameters->id);
setDisplayName(QLatin1String(parameters->displayName));
addMimeType(parameters->mimeType);
setEditorCreator([this, editorCreator] {
VcsBaseSubmitEditor *editor = editorCreator();
editor->registerActions(m_undoAction, m_redoAction, m_submitAction, m_diffAction);
return editor;
});
Context context(parameters->id);
m_undoAction = new QAction(tr("&Undo"), this);
ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context);
@@ -68,11 +74,4 @@ VcsSubmitEditorFactory::VcsSubmitEditorFactory
ActionManager::registerAction(m_diffAction, DIFF_SELECTED, context);
}
Core::IEditor *VcsSubmitEditorFactory::createEditor()
{
VcsBaseSubmitEditor *editor = m_editorCreator();
editor->registerActions(m_undoAction, m_redoAction, m_submitAction, m_diffAction);
return editor;
}
} // namespace VcsBase

View File

@@ -51,10 +51,7 @@ public:
const EditorCreator &editorCreator,
VcsBasePluginPrivate *plugin);
Core::IEditor *createEditor() override;
private:
EditorCreator m_editorCreator;
QAction *m_submitAction = nullptr;
QAction *m_diffAction = nullptr;
QAction *m_undoAction = nullptr;