TextEditor: De-QObject-ify TextEditorActionHandler

Change-Id: I8c4919b89ce35d421edd4a21a650d5e0df4a7ead
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2020-02-05 17:51:41 +01:00
parent e12ba2d683
commit 9c934ed44c
7 changed files with 65 additions and 54 deletions

View File

@@ -29,22 +29,20 @@
#include "androidmanifesteditor.h" #include "androidmanifesteditor.h"
#include <coreplugin/id.h> #include <coreplugin/id.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
using namespace Android; using namespace Android;
using namespace Android::Internal; using namespace Android::Internal;
AndroidManifestEditorFactory::AndroidManifestEditorFactory() AndroidManifestEditorFactory::AndroidManifestEditorFactory()
: m_actionHandler(Constants::ANDROID_MANIFEST_EDITOR_ID,
Constants::ANDROID_MANIFEST_EDITOR_CONTEXT,
TextEditor::TextEditorActionHandler::None,
[](Core::IEditor *editor) { return static_cast<AndroidManifestEditor *>(editor)->textEditor(); })
{ {
setId(Constants::ANDROID_MANIFEST_EDITOR_ID); setId(Constants::ANDROID_MANIFEST_EDITOR_ID);
setDisplayName(AndroidManifestEditorWidget::tr("Android Manifest editor")); setDisplayName(AndroidManifestEditorWidget::tr("Android Manifest editor"));
addMimeType(Constants::ANDROID_MANIFEST_MIME_TYPE); 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();
});
setEditorCreator([] { setEditorCreator([] {
auto androidManifestEditorWidget = new AndroidManifestEditorWidget; auto androidManifestEditorWidget = new AndroidManifestEditorWidget;
return androidManifestEditorWidget->editor(); return androidManifestEditorWidget->editor();

View File

@@ -27,6 +27,8 @@
#include <coreplugin/editormanager/ieditorfactory.h> #include <coreplugin/editormanager/ieditorfactory.h>
#include <texteditor/texteditoractionhandler.h>
namespace Android { namespace Android {
namespace Internal { namespace Internal {
@@ -34,6 +36,9 @@ class AndroidManifestEditorFactory final : public Core::IEditorFactory
{ {
public: public:
AndroidManifestEditorFactory(); AndroidManifestEditorFactory();
private:
TextEditor::TextEditorActionHandler m_actionHandler;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -33,35 +33,41 @@
#include <QCoreApplication> #include <QCoreApplication>
using namespace Core;
using namespace TextEditor;
namespace DiffEditor { namespace DiffEditor {
namespace Internal { namespace Internal {
DiffEditorFactory::DiffEditorFactory() DiffEditorFactory::DiffEditorFactory() :
descriptionHandler {
Constants::DIFF_EDITOR_ID,
Constants::C_DIFF_EDITOR_DESCRIPTION,
TextEditorActionHandler::None,
[](IEditor *e) { return static_cast<DiffEditor *>(e)->descriptionWidget(); }
},
unifiedHandler {
Constants::DIFF_EDITOR_ID,
Constants::UNIFIED_VIEW_ID,
TextEditorActionHandler::None,
[](IEditor *e) { return static_cast<DiffEditor *>(e)->unifiedEditorWidget(); }
},
leftHandler {
Constants::DIFF_EDITOR_ID,
Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(1),
TextEditorActionHandler::None,
[](IEditor *e) { return static_cast<DiffEditor *>(e)->leftEditorWidget(); }
},
rightHandler {
Constants::DIFF_EDITOR_ID,
Core::Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(2),
TextEditorActionHandler::None,
[](Core::IEditor *e) { return static_cast<DiffEditor *>(e)->rightEditorWidget(); }
}
{ {
setId(Constants::DIFF_EDITOR_ID); setId(Constants::DIFF_EDITOR_ID);
setDisplayName(QCoreApplication::translate("DiffEditorFactory", Constants::DIFF_EDITOR_DISPLAY_NAME)); setDisplayName(QCoreApplication::translate("DiffEditorFactory", Constants::DIFF_EDITOR_DISPLAY_NAME));
addMimeType(Constants::DIFF_EDITOR_MIMETYPE); addMimeType(Constants::DIFF_EDITOR_MIMETYPE);
auto descriptionHandler = new TextEditor::TextEditorActionHandler(
this, id(), Constants::C_DIFF_EDITOR_DESCRIPTION);
descriptionHandler->setTextEditorWidgetResolver([](Core::IEditor *e) {
return static_cast<DiffEditor *>(e)->descriptionWidget();
});
auto unifiedHandler = new TextEditor::TextEditorActionHandler(
this, id(), Constants::UNIFIED_VIEW_ID);
unifiedHandler->setTextEditorWidgetResolver([](Core::IEditor *e) {
return static_cast<DiffEditor *>(e)->unifiedEditorWidget();
});
auto leftHandler = new TextEditor::TextEditorActionHandler(
this, id(), Core::Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(1));
leftHandler->setTextEditorWidgetResolver([](Core::IEditor *e) {
return static_cast<DiffEditor *>(e)->leftEditorWidget();
});
auto rightHandler = new TextEditor::TextEditorActionHandler(
this, id(), Core::Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(2));
rightHandler->setTextEditorWidgetResolver([](Core::IEditor *e) {
return static_cast<DiffEditor *>(e)->rightEditorWidget();
});
setEditorCreator([] { return new DiffEditor(new DiffEditorDocument); }); setEditorCreator([] { return new DiffEditor(new DiffEditorDocument); });
} }

View File

@@ -27,6 +27,8 @@
#include <coreplugin/editormanager/ieditorfactory.h> #include <coreplugin/editormanager/ieditorfactory.h>
#include <texteditor/texteditoractionhandler.h>
namespace DiffEditor { namespace DiffEditor {
namespace Internal { namespace Internal {
@@ -34,6 +36,12 @@ class DiffEditorFactory : public Core::IEditorFactory
{ {
public: public:
DiffEditorFactory(); DiffEditorFactory();
private:
TextEditor::TextEditorActionHandler descriptionHandler;
TextEditor::TextEditorActionHandler unifiedHandler;
TextEditor::TextEditorActionHandler leftHandler;
TextEditor::TextEditorActionHandler rightHandler;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -8598,8 +8598,7 @@ void TextEditorFactory::setAutoCompleterCreator(const AutoCompleterCreator &crea
void TextEditorFactory::setEditorActionHandlers(uint optionalActions) void TextEditorFactory::setEditorActionHandlers(uint optionalActions)
{ {
d->m_textEditorActionHandler.reset( d->m_textEditorActionHandler.reset(new TextEditorActionHandler(id(), id(), optionalActions));
new TextEditorActionHandler(nullptr, id(), id(), optionalActions));
} }
void TextEditorFactory::addHoverHandler(BaseHoverHandler *handler) void TextEditorFactory::addHoverHandler(BaseHoverHandler *handler)

View File

@@ -190,15 +190,9 @@ 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
(Core::Id editorId, Core::Id contextId, uint optionalActions) (Core::Id editorId, Core::Id contextId, uint optionalActions)
: m_findTextWidget(castWidgetToTextEditorWidget) : m_optionalActions(optionalActions)
, m_optionalActions(optionalActions)
, m_editorId(editorId) , m_editorId(editorId)
, m_contextId(contextId) , m_contextId(contextId)
{ {
@@ -228,7 +222,7 @@ void TextEditorActionHandlerPrivate::createActions()
QString locatorString = TextEditorPlugin::lineNumberFilter()->shortcutString(); QString locatorString = TextEditorPlugin::lineNumberFilter()->shortcutString();
locatorString += QLatin1Char(' '); locatorString += QLatin1Char(' ');
const int selectionStart = locatorString.size(); const int selectionStart = locatorString.size();
locatorString += TextEditorActionHandler::tr("<line>:<column>"); locatorString += tr("<line>:<column>");
Core::LocatorManager::show(locatorString, selectionStart, locatorString.size() - selectionStart); Core::LocatorManager::show(locatorString, selectionStart, locatorString.size() - selectionStart);
}); });
m_printAction = registerAction(PRINT, m_printAction = registerAction(PRINT,
@@ -589,11 +583,16 @@ void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor)
} // namespace Internal } // namespace Internal
TextEditorActionHandler::TextEditorActionHandler(QObject *parent, Core::Id editorId, TextEditorActionHandler::TextEditorActionHandler(Core::Id editorId,
Core::Id contextId, uint optionalActions) Core::Id contextId,
: QObject(parent), d(new Internal::TextEditorActionHandlerPrivate(editorId, contextId, uint optionalActions,
optionalActions)) const TextEditorWidgetResolver &resolver)
: d(new Internal::TextEditorActionHandlerPrivate(editorId, contextId, optionalActions))
{ {
if (resolver)
d->m_findTextWidget = resolver;
else
d->m_findTextWidget = [](Core::IEditor *editor) { return qobject_cast<TextEditorWidget *>(editor->widget()); };
} }
TextEditorActionHandler::~TextEditorActionHandler() TextEditorActionHandler::~TextEditorActionHandler()
@@ -601,9 +600,4 @@ TextEditorActionHandler::~TextEditorActionHandler()
delete d; delete d;
} }
void TextEditorActionHandler::setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver)
{
d->m_findTextWidget = resolver;
}
} // namespace TextEditor } // namespace TextEditor

View File

@@ -43,9 +43,10 @@ namespace Internal { class TextEditorActionHandlerPrivate; }
// Redirects slots from global actions to the respective editor. // Redirects slots from global actions to the respective editor.
class TEXTEDITOR_EXPORT TextEditorActionHandler : public QObject class TEXTEDITOR_EXPORT TextEditorActionHandler final
{ {
Q_OBJECT TextEditorActionHandler(const TextEditorActionHandler &) = delete;
TextEditorActionHandler &operator=(const TextEditorActionHandler &) = delete;
public: public:
enum OptionalActionsMask { enum OptionalActionsMask {
@@ -58,14 +59,14 @@ public:
}; };
using TextEditorWidgetResolver = std::function<TextEditorWidget *(Core::IEditor *)>; using TextEditorWidgetResolver = std::function<TextEditorWidget *(Core::IEditor *)>;
explicit TextEditorActionHandler(QObject *parent, Core::Id editorId, Core::Id contextId, TextEditorActionHandler(Core::Id editorId,
uint optionalActions = None); Core::Id contextId,
~TextEditorActionHandler() override; uint optionalActions = None,
const TextEditorWidgetResolver &resolver = {});
void setTextEditorWidgetResolver(const TextEditorWidgetResolver &resolver); ~TextEditorActionHandler();
private: private:
friend class Internal::TextEditorActionHandlerPrivate;
Internal::TextEditorActionHandlerPrivate *d; Internal::TextEditorActionHandlerPrivate *d;
}; };