TextEditorActionHandler: Use configuration instead of inheritance

Avoids creating subclasses just for implementing a single method.

Change-Id: I813a0a20eaba8fefa004b74f92d48a9a9c4bfcc1
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2017-05-08 13:52:34 +02:00
parent 2684254fad
commit fd7edcb826
3 changed files with 19 additions and 22 deletions

View File

@@ -35,29 +35,17 @@
using namespace Android; using namespace Android;
using namespace Android::Internal; using namespace Android::Internal;
class AndroidTextEditorActionHandler : public TextEditor::TextEditorActionHandler
{
public:
explicit AndroidTextEditorActionHandler(QObject *parent)
: TextEditorActionHandler(parent,
Constants::ANDROID_MANIFEST_EDITOR_ID,
Constants::ANDROID_MANIFEST_EDITOR_CONTEXT)
{}
private:
TextEditor::TextEditorWidget *resolveTextEditorWidget(Core::IEditor *editor) const
{
AndroidManifestEditor *androidManifestEditor = static_cast<AndroidManifestEditor *>(editor);
return androidManifestEditor->textEditor();
}
};
AndroidManifestEditorFactory::AndroidManifestEditorFactory(QObject *parent) AndroidManifestEditorFactory::AndroidManifestEditorFactory(QObject *parent)
: Core::IEditorFactory(parent) : Core::IEditorFactory(parent)
{ {
setId(Constants::ANDROID_MANIFEST_EDITOR_ID); setId(Constants::ANDROID_MANIFEST_EDITOR_ID);
setDisplayName(tr("Android Manifest editor")); setDisplayName(tr("Android Manifest editor"));
addMimeType(Constants::ANDROID_MANIFEST_MIME_TYPE); addMimeType(Constants::ANDROID_MANIFEST_MIME_TYPE);
new AndroidTextEditorActionHandler(this); 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() Core::IEditor *AndroidManifestEditorFactory::createEditor()

View File

@@ -122,6 +122,7 @@ public:
public: public:
TextEditorActionHandler *q = nullptr; TextEditorActionHandler *q = nullptr;
TextEditorActionHandler::TextEditorWidgetResolver m_findTextWidget;
QAction *m_undoAction = nullptr; QAction *m_undoAction = nullptr;
QAction *m_redoAction = nullptr; QAction *m_redoAction = nullptr;
QAction *m_copyAction = nullptr; QAction *m_copyAction = nullptr;
@@ -189,9 +190,15 @@ public:
Core::Id m_contextId; Core::Id m_contextId;
}; };
static TextEditorWidget *castWidgetToTextEditorWidget(Core::IEditor *editor)
{
return qobject_cast<TextEditorWidget *>(editor->widget());
}
TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate
(TextEditorActionHandler *parent, Core::Id editorId, Core::Id contextId, uint optionalActions) (TextEditorActionHandler *parent, Core::Id editorId, Core::Id contextId, uint optionalActions)
: q(parent) : q(parent)
, m_findTextWidget(castWidgetToTextEditorWidget)
, m_optionalActions(optionalActions) , m_optionalActions(optionalActions)
, m_editorId(editorId) , m_editorId(editorId)
, m_contextId(contextId) , m_contextId(contextId)
@@ -548,7 +555,7 @@ void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor)
m_currentEditorWidget = 0; m_currentEditorWidget = 0;
if (editor && editor->document()->id() == m_editorId) { if (editor && editor->document()->id() == m_editorId) {
TextEditorWidget *editorWidget = q->resolveTextEditorWidget(editor); TextEditorWidget *editorWidget = m_findTextWidget(editor);
QTC_ASSERT(editorWidget, return); // editor has our id, so shouldn't happen QTC_ASSERT(editorWidget, return); // editor has our id, so shouldn't happen
m_currentEditorWidget = editorWidget; m_currentEditorWidget = editorWidget;
connect(editorWidget, &QPlainTextEdit::undoAvailable, connect(editorWidget, &QPlainTextEdit::undoAvailable,
@@ -577,9 +584,9 @@ TextEditorActionHandler::~TextEditorActionHandler()
delete d; delete d;
} }
TextEditorWidget *TextEditorActionHandler::resolveTextEditorWidget(Core::IEditor *editor) const void TextEditorActionHandler::setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver)
{ {
return qobject_cast<TextEditorWidget *>(editor->widget()); d->m_findTextWidget = resolver;
} }
} // namespace TextEditor } // namespace TextEditor

View File

@@ -29,6 +29,8 @@
#include <QObject> #include <QObject>
#include <functional>
namespace Core { namespace Core {
class Id; class Id;
class IEditor; class IEditor;
@@ -54,13 +56,13 @@ public:
FollowSymbolUnderCursor = 8, FollowSymbolUnderCursor = 8,
JumpToFileUnderCursor = 16 JumpToFileUnderCursor = 16
}; };
using TextEditorWidgetResolver = std::function<TextEditorWidget *(Core::IEditor *)>;
explicit TextEditorActionHandler(QObject *parent, Core::Id editorId, Core::Id contextId, explicit TextEditorActionHandler(QObject *parent, Core::Id editorId, Core::Id contextId,
uint optionalActions = None); uint optionalActions = None);
~TextEditorActionHandler(); ~TextEditorActionHandler();
protected: void setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver);
virtual TextEditorWidget *resolveTextEditorWidget(Core::IEditor *editor) const;
private: private:
friend class Internal::TextEditorActionHandlerPrivate; friend class Internal::TextEditorActionHandlerPrivate;