From fd7edcb8263059d828873e5ed675fa2e43dfdf01 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 8 May 2017 13:52:34 +0200 Subject: [PATCH] TextEditorActionHandler: Use configuration instead of inheritance Avoids creating subclasses just for implementing a single method. Change-Id: I813a0a20eaba8fefa004b74f92d48a9a9c4bfcc1 Reviewed-by: David Schulz --- .../android/androidmanifesteditorfactory.cpp | 22 +++++-------------- .../texteditor/texteditoractionhandler.cpp | 13 ++++++++--- .../texteditor/texteditoractionhandler.h | 6 +++-- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/plugins/android/androidmanifesteditorfactory.cpp b/src/plugins/android/androidmanifesteditorfactory.cpp index 2dba95f0f5e..dfb18265067 100644 --- a/src/plugins/android/androidmanifesteditorfactory.cpp +++ b/src/plugins/android/androidmanifesteditorfactory.cpp @@ -35,29 +35,17 @@ using namespace Android; 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(editor); - return androidManifestEditor->textEditor(); - } -}; - AndroidManifestEditorFactory::AndroidManifestEditorFactory(QObject *parent) : Core::IEditorFactory(parent) { setId(Constants::ANDROID_MANIFEST_EDITOR_ID); setDisplayName(tr("Android Manifest editor")); 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(editor)->textEditor(); + }); } Core::IEditor *AndroidManifestEditorFactory::createEditor() diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index 3c4eab56871..cb2515acec8 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -122,6 +122,7 @@ public: public: TextEditorActionHandler *q = nullptr; + TextEditorActionHandler::TextEditorWidgetResolver m_findTextWidget; QAction *m_undoAction = nullptr; QAction *m_redoAction = nullptr; QAction *m_copyAction = nullptr; @@ -189,9 +190,15 @@ public: Core::Id m_contextId; }; +static TextEditorWidget *castWidgetToTextEditorWidget(Core::IEditor *editor) +{ + return qobject_cast(editor->widget()); +} + TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate (TextEditorActionHandler *parent, Core::Id editorId, Core::Id contextId, uint optionalActions) : q(parent) + , m_findTextWidget(castWidgetToTextEditorWidget) , m_optionalActions(optionalActions) , m_editorId(editorId) , m_contextId(contextId) @@ -548,7 +555,7 @@ void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor) m_currentEditorWidget = 0; 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 m_currentEditorWidget = editorWidget; connect(editorWidget, &QPlainTextEdit::undoAvailable, @@ -577,9 +584,9 @@ TextEditorActionHandler::~TextEditorActionHandler() delete d; } -TextEditorWidget *TextEditorActionHandler::resolveTextEditorWidget(Core::IEditor *editor) const +void TextEditorActionHandler::setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver) { - return qobject_cast(editor->widget()); + d->m_findTextWidget = resolver; } } // namespace TextEditor diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h index 0b99c3b9b7e..77d06c86b00 100644 --- a/src/plugins/texteditor/texteditoractionhandler.h +++ b/src/plugins/texteditor/texteditoractionhandler.h @@ -29,6 +29,8 @@ #include +#include + namespace Core { class Id; class IEditor; @@ -54,13 +56,13 @@ public: FollowSymbolUnderCursor = 8, JumpToFileUnderCursor = 16 }; + using TextEditorWidgetResolver = std::function; explicit TextEditorActionHandler(QObject *parent, Core::Id editorId, Core::Id contextId, uint optionalActions = None); ~TextEditorActionHandler(); -protected: - virtual TextEditorWidget *resolveTextEditorWidget(Core::IEditor *editor) const; + void setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver); private: friend class Internal::TextEditorActionHandlerPrivate;