diff options
author | hjk <hjk@qt.io> | 2024-01-16 17:43:48 +0100 |
---|---|---|
committer | hjk <hjk@qt.io> | 2024-01-16 17:28:31 +0000 |
commit | fc36218d390e6706c4b14cd80f63bf65ec9d5f0f (patch) | |
tree | aec7fe9db6091304e058debd3df63d16e43e735b /src/plugins/resourceeditor | |
parent | a845fad7bd85d63684a8000559a561867ab36b48 (diff) |
ResourceEditor: Move editor action setup closer to editor
Not yet business as usual, but moving closer and cutting some
dependencies.
Change-Id: I6331b256cbbde0abcc67251b1d32521f234fca2b
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'src/plugins/resourceeditor')
-rw-r--r-- | src/plugins/resourceeditor/resourceeditorplugin.cpp | 47 | ||||
-rw-r--r-- | src/plugins/resourceeditor/resourceeditorplugin.h | 3 | ||||
-rw-r--r-- | src/plugins/resourceeditor/resourceeditorw.cpp | 57 | ||||
-rw-r--r-- | src/plugins/resourceeditor/resourceeditorw.h | 9 |
4 files changed, 49 insertions, 67 deletions
diff --git a/src/plugins/resourceeditor/resourceeditorplugin.cpp b/src/plugins/resourceeditor/resourceeditorplugin.cpp index 44fbf4c0bb..03556ba0d6 100644 --- a/src/plugins/resourceeditor/resourceeditorplugin.cpp +++ b/src/plugins/resourceeditor/resourceeditorplugin.cpp @@ -111,10 +111,6 @@ public: ResourceEditorW * currentEditor() const; - QAction *m_redoAction = nullptr; - QAction *m_undoAction = nullptr; - QAction *m_refreshAction = nullptr; - // project tree's folder context menu QAction *m_addPrefix = nullptr; QAction *m_removePrefix = nullptr; @@ -134,18 +130,6 @@ public: ResourceEditorPluginPrivate::ResourceEditorPluginPrivate() { - // Register undo and redo - const Core::Context context(Constants::C_RESOURCEEDITOR); - m_undoAction = new QAction(Tr::tr("&Undo"), this); - m_redoAction = new QAction(Tr::tr("&Redo"), this); - m_refreshAction = new QAction(Tr::tr("Recheck Existence of Referenced Files"), this); - Core::ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context); - Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, context); - Core::ActionManager::registerAction(m_refreshAction, Constants::REFRESH, context); - connect(m_undoAction, &QAction::triggered, this, &ResourceEditorPluginPrivate::onUndo); - connect(m_redoAction, &QAction::triggered, this, &ResourceEditorPluginPrivate::onRedo); - connect(m_refreshAction, &QAction::triggered, this, &ResourceEditorPluginPrivate::onRefresh); - Core::Context projectTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE); Core::ActionContainer *folderContextMenu = Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FOLDERCONTEXT); @@ -251,21 +235,6 @@ void ResourceEditorPlugin::extensionsInitialized() }); } -void ResourceEditorPluginPrivate::onUndo() -{ - currentEditor()->onUndo(); -} - -void ResourceEditorPluginPrivate::onRedo() -{ - currentEditor()->onRedo(); -} - -void ResourceEditorPluginPrivate::onRefresh() -{ - currentEditor()->onRefresh(); -} - void ResourceEditorPluginPrivate::addPrefixContextMenu() { auto topLevel = dynamic_cast<ResourceTopLevelNode *>(ProjectTree::currentNode()); @@ -407,13 +376,6 @@ void ResourceEditorPluginPrivate::updateContextActions(Node *node) } } -ResourceEditorW * ResourceEditorPluginPrivate::currentEditor() const -{ - auto const focusEditor = qobject_cast<ResourceEditorW *>(Core::EditorManager::currentEditor()); - QTC_ASSERT(focusEditor, return nullptr); - return focusEditor; -} - // ResourceEditorPlugin ResourceEditorPlugin::~ResourceEditorPlugin() @@ -428,13 +390,4 @@ void ResourceEditorPlugin::initialize() setupResourceEditor(this); } -void ResourceEditorPlugin::onUndoStackChanged(ResourceEditorW const *editor, - bool canUndo, bool canRedo) -{ - if (editor == d->currentEditor()) { - d->m_undoAction->setEnabled(canUndo); - d->m_redoAction->setEnabled(canRedo); - } -} - } // ResourceEditor::Internal diff --git a/src/plugins/resourceeditor/resourceeditorplugin.h b/src/plugins/resourceeditor/resourceeditorplugin.h index dd7390a76b..d8146ad965 100644 --- a/src/plugins/resourceeditor/resourceeditorplugin.h +++ b/src/plugins/resourceeditor/resourceeditorplugin.h @@ -16,9 +16,6 @@ class ResourceEditorPlugin final : public ExtensionSystem::IPlugin ~ResourceEditorPlugin() final; -public: - void onUndoStackChanged(ResourceEditorW const *editor, bool canUndo, bool canRedo); - private: void initialize() final; void extensionsInitialized() override; diff --git a/src/plugins/resourceeditor/resourceeditorw.cpp b/src/plugins/resourceeditor/resourceeditorw.cpp index 77b2ace5d4..1c52e956d2 100644 --- a/src/plugins/resourceeditor/resourceeditorw.cpp +++ b/src/plugins/resourceeditor/resourceeditorw.cpp @@ -23,6 +23,7 @@ #include <utils/reloadpromptutils.h> #include <utils/stringutils.h> +#include <QAction> #include <QDebug> #include <QMenu> #include <QToolBar> @@ -33,6 +34,17 @@ namespace ResourceEditor::Internal { enum { debugResourceEditorW = 0 }; +static ResourceEditorW *currentEditor() +{ + auto const focusEditor = qobject_cast<ResourceEditorW *>(Core::EditorManager::currentEditor()); + QTC_ASSERT(focusEditor, return nullptr); + return focusEditor; +} + +QAction *m_redoAction = nullptr; +QAction *m_undoAction = nullptr; +QAction *m_refreshAction = nullptr; + ResourceEditorDocument::ResourceEditorDocument(QObject *parent) : IDocument(parent), m_model(new RelativeResourceModel(this)) @@ -48,11 +60,8 @@ ResourceEditorDocument::ResourceEditorDocument(QObject *parent) : qDebug() << "ResourceEditorFile::ResourceEditorFile()"; } -ResourceEditorW::ResourceEditorW(const Core::Context &context, - ResourceEditorPlugin *plugin, - QWidget *parent) +ResourceEditorW::ResourceEditorW(const Core::Context &context, QWidget *parent) : m_resourceDocument(new ResourceEditorDocument(this)), - m_plugin(plugin), m_contextMenu(new QMenu), m_toolBar(new QToolBar) { @@ -265,7 +274,10 @@ void ResourceEditorDocument::dirtyChanged(bool dirty) void ResourceEditorW::onUndoStackChanged(bool canUndo, bool canRedo) { - m_plugin->onUndoStackChanged(this, canUndo, canRedo); + if (currentEditor() == this) { + m_undoAction->setEnabled(canUndo); + m_redoAction->setEnabled(canRedo); + } } void ResourceEditorW::showContextMenu(const QPoint &globalPoint, const QString &fileName) @@ -319,7 +331,7 @@ void ResourceEditorW::onRedo() class ResourceEditorFactory final : public Core::IEditorFactory { public: - explicit ResourceEditorFactory(ResourceEditorPlugin *plugin) + ResourceEditorFactory() { setId(Constants::RESOURCEEDITOR_ID); setMimeTypes(QStringList(Utils::Constants::RESOURCE_MIMETYPE)); @@ -328,15 +340,40 @@ public: FileIconProvider::registerIconOverlayForSuffix( ProjectExplorer::Constants::FILEOVERLAY_QRC, "qrc"); - setEditorCreator([plugin] { - return new ResourceEditorW(Core::Context(Constants::C_RESOURCEEDITOR), plugin); + setEditorCreator([] { + return new ResourceEditorW(Core::Context(Constants::C_RESOURCEEDITOR)); }); } }; -void setupResourceEditor(ResourceEditorPlugin *plugin) +void setupResourceEditor(QObject *guard) { - static ResourceEditorFactory theResourceEditorFactory(plugin); + static ResourceEditorFactory theResourceEditorFactory; + + // Register undo and redo + const Core::Context context(Constants::C_RESOURCEEDITOR); + + m_undoAction = new QAction(Tr::tr("&Undo"), guard); + m_redoAction = new QAction(Tr::tr("&Redo"), guard); + m_refreshAction = new QAction(Tr::tr("Recheck Existence of Referenced Files"), guard); + Core::ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context); + Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, context); + Core::ActionManager::registerAction(m_refreshAction, Constants::REFRESH, context); + + QObject::connect(m_undoAction, &QAction::triggered, guard, [] { + if (ResourceEditorW *editor = currentEditor()) + editor->onUndo(); + }); + + QObject::connect(m_redoAction, &QAction::triggered, guard, [] { + if (ResourceEditorW *editor = currentEditor()) + editor->onRedo(); + }); + + QObject::connect(m_refreshAction, &QAction::triggered, guard, [] { + if (ResourceEditorW *editor = currentEditor()) + editor->onRefresh(); + }); } } // ResourceEditor::Internal diff --git a/src/plugins/resourceeditor/resourceeditorw.h b/src/plugins/resourceeditor/resourceeditorw.h index 910a361cba..24702885a5 100644 --- a/src/plugins/resourceeditor/resourceeditorw.h +++ b/src/plugins/resourceeditor/resourceeditorw.h @@ -14,7 +14,6 @@ QT_END_NAMESPACE namespace ResourceEditor::Internal { class RelativeResourceModel; -class ResourceEditorPlugin; class ResourceEditorW; class QrcEditor; @@ -62,9 +61,7 @@ class ResourceEditorW : public Core::IEditor Q_OBJECT public: - ResourceEditorW(const Core::Context &context, - ResourceEditorPlugin *plugin, - QWidget *parent = nullptr); + ResourceEditorW(const Core::Context &context, QWidget *parent = nullptr); ~ResourceEditorW() override; // IEditor @@ -87,7 +84,6 @@ private: QString m_displayName; QrcEditor *m_resourceEditor; ResourceEditorDocument *m_resourceDocument; - ResourceEditorPlugin *m_plugin; QMenu *m_contextMenu; QMenu *m_openWithMenu; QString m_currentFileName; @@ -104,7 +100,6 @@ public: friend class ResourceEditorDocument; }; -class ResourceEditorPlugin; -void setupResourceEditor(ResourceEditorPlugin *plugin); +void setupResourceEditor(QObject *guard); } // ResourceEditor::Internal |