BinEditor: Simplify editor setup

Make BinEditorImpl also the EditorService implementation.

Change-Id: I6a741f678c93f96e80947c041f9d55c9f8190148
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2024-07-30 16:46:10 +02:00
parent 363a7ab127
commit 86b130f7c0

View File

@@ -2190,13 +2190,13 @@ bool BinEditorDocument::saveImpl(QString *errorString, const FilePath &filePath,
return false; return false;
} }
class BinEditorImpl final : public IEditor class BinEditorImpl final : public IEditor, public EditorService
{ {
public: public:
BinEditorImpl(BinEditorWidget *widget, const std::shared_ptr<BinEditorDocument> &doc) BinEditorImpl(const std::shared_ptr<BinEditorDocument> &doc)
: m_document(doc), m_widget(widget) : m_document(doc), m_widget(new BinEditorWidget(doc))
{ {
setWidget(widget); setWidget(m_widget);
setDuplicateSupported(true); setDuplicateSupported(true);
auto codecChooser = new CodecChooser(CodecChooser::Filter::SingleByte); auto codecChooser = new CodecChooser(CodecChooser::Filter::SingleByte);
codecChooser->prependNone(); codecChooser->prependNone();
@@ -2206,7 +2206,7 @@ public:
l->setContentsMargins(0, 0, 5, 0); l->setContentsMargins(0, 0, 5, 0);
l->addStretch(1); l->addStretch(1);
l->addWidget(codecChooser); l->addWidget(codecChooser);
l->addWidget(widget->addressEdit()); l->addWidget(m_widget->addressEdit());
w->setLayout(l); w->setLayout(l);
m_toolBar = new QToolBar; m_toolBar = new QToolBar;
@@ -2214,7 +2214,7 @@ public:
m_toolBar->addWidget(w); m_toolBar->addWidget(w);
connect(codecChooser, &CodecChooser::codecChanged, connect(codecChooser, &CodecChooser::codecChanged,
widget, &BinEditorWidget::setCodec); m_widget, &BinEditorWidget::setCodec);
const QVariant setting = ICore::settings()->value(Constants::C_ENCODING_SETTING); const QVariant setting = ICore::settings()->value(Constants::C_ENCODING_SETTING);
if (!setting.isNull()) if (!setting.isNull())
codecChooser->setAssignedCodec(QTextCodec::codecForName(setting.toByteArray())); codecChooser->setAssignedCodec(QTextCodec::codecForName(setting.toByteArray()));
@@ -2228,42 +2228,29 @@ public:
IEditor *duplicate() final IEditor *duplicate() final
{ {
auto widget = new BinEditorWidget(m_document); auto editor = new BinEditorImpl(m_document);
widget->setCursorPosition(m_widget->cursorPosition()); editor->m_widget->setCursorPosition(m_widget->cursorPosition());
auto editor = new BinEditorImpl(widget, m_document);
emit editorDuplicated(editor); emit editorDuplicated(editor);
return editor; return editor;
} }
private: // Service interface
std::shared_ptr<BinEditorDocument> m_document;
BinEditorWidget *m_widget = nullptr;
QToolBar *m_toolBar;
};
///////////////////////////////// BinEditor Services //////////////////////////////////
class BinEditorService final : public EditorService
{
public:
~BinEditorService() = default;
QWidget *widget() { return m_widget; } QWidget *widget() { return m_widget; }
Core::IEditor *editor() { return m_editor; } Core::IEditor *editor() { return this; }
// "Slots" // "Slots"
void setSizes(quint64 address, qint64 range, int blockSize) final { m_document->setSizes(address, range, blockSize); } void setSizes(quint64 address, qint64 range, int blockSize) { m_document->setSizes(address, range, blockSize); }
void setReadOnly(bool on) final { m_widget->setReadOnly(on); } void setReadOnly(bool on) { m_widget->setReadOnly(on); }
void setFinished() final { m_widget->setReadOnly(true); m_document->setFinished(); } void setFinished() { m_widget->setReadOnly(true); m_document->setFinished(); }
void setNewWindowRequestAllowed(bool on) final { m_widget->setNewWindowRequestAllowed(on); } void setNewWindowRequestAllowed(bool on) { m_widget->setNewWindowRequestAllowed(on); }
void setCursorPosition(qint64 pos, MoveMode moveMode = MoveAnchor) final { m_widget->setCursorPosition(pos, moveMode); } void setCursorPosition(qint64 pos, MoveMode moveMode = MoveAnchor) { m_widget->setCursorPosition(pos, moveMode); }
void updateContents() final { m_document->updateContents(); } void updateContents() { m_document->updateContents(); }
void addData(quint64 address, const QByteArray &data) final { m_document->addData(address, data); } void addData(quint64 address, const QByteArray &data) { m_document->addData(address, data); }
void clearMarkup() final { m_widget->clearMarkup(); } void clearMarkup() { m_widget->clearMarkup(); }
void addMarkup(quint64 address, quint64 len, const QColor &color, const QString &toolTip) final void addMarkup(quint64 address, quint64 len, const QColor &color, const QString &toolTip)
{ m_widget->addMarkup(address, len, color, toolTip); } { m_widget->addMarkup(address, len, color, toolTip); }
void commitMarkup() final { m_widget->commitMarkup(); } void commitMarkup() { m_widget->commitMarkup(); }
// "Signals" // "Signals"
void setFetchDataHandler(const std::function<void(quint64)> &cb) final { m_document->m_fetchDataHandler = cb; } void setFetchDataHandler(const std::function<void(quint64)> &cb) final { m_document->m_fetchDataHandler = cb; }
@@ -2273,9 +2260,10 @@ public:
void setWatchPointRequestHandler(const std::function<void(quint64, uint)> &cb) final { m_document->m_watchPointRequestHandler = cb; } void setWatchPointRequestHandler(const std::function<void(quint64, uint)> &cb) final { m_document->m_watchPointRequestHandler = cb; }
void setAboutToBeDestroyedHandler(const std::function<void()> & cb) final { m_document->m_aboutToBeDestroyedHandler = cb; } void setAboutToBeDestroyedHandler(const std::function<void()> & cb) final { m_document->m_aboutToBeDestroyedHandler = cb; }
IEditor *m_editor = nullptr; private:
BinEditorDocument *m_document = nullptr; std::shared_ptr<BinEditorDocument> m_document;
BinEditorWidget *m_widget = nullptr; BinEditorWidget *m_widget = nullptr;
QToolBar *m_toolBar;
}; };
class BinEditorFactoryService final : public QObject, public FactoryService class BinEditorFactoryService final : public QObject, public FactoryService
@@ -2287,15 +2275,10 @@ public:
EditorService *createEditorService(const QString &title, bool wantsEditor) final EditorService *createEditorService(const QString &title, bool wantsEditor) final
{ {
auto document = std::make_shared<BinEditorDocument>(); auto document = std::make_shared<BinEditorDocument>();
auto widget = new BinEditorWidget(document); auto service = new BinEditorImpl(document);
widget->setWindowTitle(title); service->widget()->setWindowTitle(title);
auto service = new BinEditorService;
service->m_widget = widget;
service->m_document = document.get();
service->m_editor = new BinEditorImpl(widget, document);
if (wantsEditor) if (wantsEditor)
EditorManager::activateEditor(service->m_editor); EditorManager::activateEditor(service);
return service; return service;
} }
}; };
@@ -2333,8 +2316,8 @@ public:
setEditorCreator([this] { setEditorCreator([this] {
auto doc = std::make_shared<BinEditorDocument>(); auto doc = std::make_shared<BinEditorDocument>();
auto widget = new BinEditorWidget(doc); auto editor = new BinEditorImpl(doc);
auto editor = new BinEditorImpl(widget, doc); BinEditorWidget *widget = dynamic_cast<BinEditorWidget *>(editor->widget());
connect(m_undoAction, &QAction::triggered, doc.get(), &BinEditorDocument::undo); connect(m_undoAction, &QAction::triggered, doc.get(), &BinEditorDocument::undo);
connect(m_redoAction, &QAction::triggered, doc.get(), &BinEditorDocument::redo); connect(m_redoAction, &QAction::triggered, doc.get(), &BinEditorDocument::redo);