BinEditor: Re-organize plugin setup

Adapt to recent development.

Change-Id: I9c121e5b793469e5dae319ea50d74c59f6ee68b9
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-07-11 13:36:04 +02:00
parent a4c177491a
commit 04109bf05d

View File

@@ -2340,91 +2340,76 @@ public:
} }
}; };
///////////////////////////////// BinEditorPluginPrivate ////////////////////////////////// static FactoryServiceImpl &binEditorService()
{
static FactoryServiceImpl theFactoryService;
return theFactoryService;
}
///////////////////////////////// BinEditorFactory //////////////////////////////////
class BinEditorFactory final : public QObject, public IEditorFactory class BinEditorFactory final : public QObject, public IEditorFactory
{ {
public: public:
BinEditorFactory(); BinEditorFactory()
}; {
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setDisplayName(::Core::Tr::tr("Binary Editor"));
addMimeType(Utils::Constants::OCTET_STREAM_MIMETYPE);
class BinEditorPluginPrivate : public QObject m_undoAction = new QAction(Tr::tr("&Undo"), this);
{ m_redoAction = new QAction(Tr::tr("&Redo"), this);
public: m_copyAction = new QAction(this);
BinEditorPluginPrivate(); m_selectAllAction = new QAction(this);
~BinEditorPluginPrivate() override;
Context context;
context.add(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
context.add(Constants::C_BINEDITOR);
ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context);
ActionManager::registerAction(m_redoAction, Core::Constants::REDO, context);
ActionManager::registerAction(m_copyAction, Core::Constants::COPY, context);
ActionManager::registerAction(m_selectAllAction, Core::Constants::SELECTALL, context);
setEditorCreator([this] {
auto widget = new BinEditorWidget();
auto editor = new BinEditorImpl(widget);
connect(m_undoAction, &QAction::triggered, widget, &BinEditorWidget::undo);
connect(m_redoAction, &QAction::triggered, widget, &BinEditorWidget::redo);
connect(m_copyAction, &QAction::triggered, widget, &BinEditorWidget::copy);
connect(m_selectAllAction, &QAction::triggered, widget, &BinEditorWidget::selectAll);
auto updateActions = [this, widget] {
m_selectAllAction->setEnabled(true);
m_undoAction->setEnabled(widget->isUndoAvailable());
m_redoAction->setEnabled(widget->isRedoAvailable());
};
connect(widget, &BinEditorWidget::undoAvailable, widget, updateActions);
connect(widget, &BinEditorWidget::redoAvailable, widget, updateActions);
auto aggregate = new Aggregation::Aggregate;
auto binEditorFind = new BinEditorFind(widget);
aggregate->add(binEditorFind);
aggregate->add(widget);
return editor;
});
}
QAction *m_undoAction = nullptr; QAction *m_undoAction = nullptr;
QAction *m_redoAction = nullptr; QAction *m_redoAction = nullptr;
QAction *m_copyAction = nullptr; QAction *m_copyAction = nullptr;
QAction *m_selectAllAction = nullptr; QAction *m_selectAllAction = nullptr;
FactoryServiceImpl m_factoryService;
BinEditorFactory m_editorFactory;
}; };
BinEditorPluginPrivate::BinEditorPluginPrivate() static BinEditorFactory &binEditorFactory()
{ {
ExtensionSystem::PluginManager::addObject(&m_factoryService); static BinEditorFactory theBinEditorFactory;
ExtensionSystem::PluginManager::addObject(&m_editorFactory); return theBinEditorFactory;
m_undoAction = new QAction(Tr::tr("&Undo"), this);
m_redoAction = new QAction(Tr::tr("&Redo"), this);
m_copyAction = new QAction(this);
m_selectAllAction = new QAction(this);
Context context;
context.add(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
context.add(Constants::C_BINEDITOR);
ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context);
ActionManager::registerAction(m_redoAction, Core::Constants::REDO, context);
ActionManager::registerAction(m_copyAction, Core::Constants::COPY, context);
ActionManager::registerAction(m_selectAllAction, Core::Constants::SELECTALL, context);
} }
BinEditorPluginPrivate::~BinEditorPluginPrivate()
{
ExtensionSystem::PluginManager::removeObject(&m_editorFactory);
ExtensionSystem::PluginManager::removeObject(&m_factoryService);
}
static BinEditorPluginPrivate *dd = nullptr;
///////////////////////////////// BinEditorFactory //////////////////////////////////
BinEditorFactory::BinEditorFactory()
{
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setDisplayName(::Core::Tr::tr("Binary Editor"));
addMimeType(Utils::Constants::OCTET_STREAM_MIMETYPE);
setEditorCreator([] {
auto widget = new BinEditorWidget();
auto editor = new BinEditorImpl(widget);
connect(dd->m_undoAction, &QAction::triggered, widget, &BinEditorWidget::undo);
connect(dd->m_redoAction, &QAction::triggered, widget, &BinEditorWidget::redo);
connect(dd->m_copyAction, &QAction::triggered, widget, &BinEditorWidget::copy);
connect(dd->m_selectAllAction, &QAction::triggered, widget, &BinEditorWidget::selectAll);
auto updateActions = [widget] {
dd->m_selectAllAction->setEnabled(true);
dd->m_undoAction->setEnabled(widget->isUndoAvailable());
dd->m_redoAction->setEnabled(widget->isRedoAvailable());
};
connect(widget, &BinEditorWidget::undoAvailable, widget, updateActions);
connect(widget, &BinEditorWidget::redoAvailable, widget, updateActions);
auto aggregate = new Aggregation::Aggregate;
auto binEditorFind = new BinEditorFind(widget);
aggregate->add(binEditorFind);
aggregate->add(widget);
return editor;
});
}
///////////////////////////////// BinEditorPlugin ////////////////////////////////// ///////////////////////////////// BinEditorPlugin //////////////////////////////////
@@ -2433,15 +2418,16 @@ class BinEditorPlugin final : public ExtensionSystem::IPlugin
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "BinEditor.json") Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "BinEditor.json")
~BinEditorPlugin() override ~BinEditorPlugin() final
{ {
delete dd; ExtensionSystem::PluginManager::removeObject(&binEditorService());
dd = nullptr; ExtensionSystem::PluginManager::removeObject(&binEditorFactory());
} }
void initialize() final void initialize() final
{ {
dd = new BinEditorPluginPrivate; ExtensionSystem::PluginManager::addObject(&binEditorService());
ExtensionSystem::PluginManager::addObject(&binEditorFactory());
} }
}; };