BinEditor: Split out BinEditorDocument class definition

Still preparing data holder re-organization.

Change-Id: I29473fed8320b3812288b72d0d5fc943aa67a2cf
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-07-11 15:25:10 +02:00
parent 5cd6f6086a
commit 0469dd5602

View File

@@ -75,6 +75,50 @@ using namespace Core;
namespace BinEditor::Internal { namespace BinEditor::Internal {
class BinEditorWidget;
class BinEditorDocument : public IDocument
{
Q_OBJECT
public:
BinEditorDocument(BinEditorWidget *parent);
QByteArray contents() const final;
bool setContents(const QByteArray &contents) final;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const final
{
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
}
OpenResult open(QString *errorString, const FilePath &filePath,
const FilePath &realFilePath) final
{
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
return openImpl(errorString, filePath);
}
OpenResult openImpl(QString *errorString, const FilePath &filePath, quint64 offset = 0);
void provideData(quint64 address);
void provideNewRange(quint64 offset)
{
if (filePath().exists())
openImpl(nullptr, filePath(), offset);
}
bool isModified() const final;
bool isSaveAsAllowed() const final { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final;
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) final;
private:
BinEditorWidget *m_widget;
};
class BinEditorWidget final : public QAbstractScrollArea, public EditorService class BinEditorWidget final : public QAbstractScrollArea, public EditorService
{ {
Q_OBJECT Q_OBJECT
@@ -2065,12 +2109,10 @@ private:
}; };
class BinEditorDocument : public IDocument // BinEditorDocument
{
Q_OBJECT BinEditorDocument::BinEditorDocument(BinEditorWidget *parent)
public: : IDocument(parent)
BinEditorDocument(BinEditorWidget *parent) :
IDocument(parent)
{ {
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID); setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setMimeType(Utils::Constants::OCTET_STREAM_MIMETYPE); setMimeType(Utils::Constants::OCTET_STREAM_MIMETYPE);
@@ -2081,12 +2123,12 @@ public:
es->setDataChangedHandler([this](quint64, const QByteArray &) { contentsChanged(); }); es->setDataChangedHandler([this](quint64, const QByteArray &) { contentsChanged(); });
} }
QByteArray contents() const override QByteArray BinEditorDocument::contents() const
{ {
return m_widget->contents(); return m_widget->contents();
} }
bool setContents(const QByteArray &contents) override bool BinEditorDocument::setContents(const QByteArray &contents)
{ {
m_widget->clear(); m_widget->clear();
if (!contents.isEmpty()) { if (!contents.isEmpty()) {
@@ -2096,19 +2138,7 @@ public:
return true; return true;
} }
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override IDocument::OpenResult BinEditorDocument::openImpl(QString *errorString, const FilePath &filePath, quint64 offset)
{
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
}
OpenResult open(QString *errorString, const FilePath &filePath,
const FilePath &realFilePath) override
{
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
return openImpl(errorString, filePath);
}
OpenResult openImpl(QString *errorString, const FilePath &filePath, quint64 offset = 0)
{ {
const qint64 size = filePath.fileSize(); const qint64 size = filePath.fileSize();
if (size < 0) { if (size < 0) {
@@ -2148,7 +2178,7 @@ public:
return OpenResult::Success; return OpenResult::Success;
} }
void provideData(quint64 address) void BinEditorDocument::provideData(quint64 address)
{ {
const FilePath fn = filePath(); const FilePath fn = filePath();
if (fn.isEmpty()) if (fn.isEmpty())
@@ -2164,22 +2194,13 @@ public:
// fn.toUserOutput(), file.errorString())); // fn.toUserOutput(), file.errorString()));
} }
void provideNewRange(quint64 offset) bool BinEditorDocument::isModified() const
{
if (filePath().exists())
openImpl(nullptr, filePath(), offset);
}
public:
bool isModified() const override
{ {
return isTemporary()/*e.g. memory view*/ ? false return isTemporary()/*e.g. memory view*/ ? false
: m_widget->isModified(); : m_widget->isModified();
} }
bool isSaveAsAllowed() const override { return true; } bool BinEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type)
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override
{ {
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
@@ -2193,8 +2214,7 @@ public:
return success; return success;
} }
protected: bool BinEditorDocument::saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override
{ {
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
if (m_widget->save(errorString, this->filePath(), filePath)) { if (m_widget->save(errorString, this->filePath(), filePath)) {
@@ -2204,10 +2224,6 @@ protected:
return false; return false;
} }
private:
BinEditorWidget *m_widget;
};
class BinEditorImpl: public IEditor class BinEditorImpl: public IEditor
{ {
Q_OBJECT Q_OBJECT