io/fileview: add adapter support and JsonAdapter

This commit is contained in:
outfoxxed 2025-05-13 17:07:51 -07:00
parent cb69c2d016
commit fee4942771
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
6 changed files with 487 additions and 2 deletions

View file

@ -289,6 +289,12 @@ void FileViewWriter::write(
}
}
FileView::~FileView() {
if (this->mAdapter) {
this->mAdapter->setFileView(nullptr);
}
}
void FileView::loadAsync(bool doStringConversion) {
// Writes update via operationFinished, making a read both invalid and outdated.
if (!this->liveOperation || this->pathInFlight != this->targetPath) {
@ -636,4 +642,55 @@ void FileView::setBlockAllReads(bool blockAllReads) {
}
}
FileViewAdapter* FileView::adapter() const { return this->mAdapter; }
void FileView::setAdapter(FileViewAdapter* adapter) {
if (adapter == this->mAdapter) return;
if (this->mAdapter) {
this->mAdapter->setFileView(nullptr);
QObject::disconnect(this->mAdapter, nullptr, this, nullptr);
}
this->mAdapter = adapter;
if (adapter) {
this->mAdapter->setFileView(this);
QObject::connect(adapter, &FileViewAdapter::adapterUpdated, this, &FileView::adapterUpdated);
QObject::connect(adapter, &QObject::destroyed, this, &FileView::onAdapterDestroyed);
}
emit this->adapterChanged();
}
void FileView::writeAdapter() {
if (!this->mAdapter) {
qmlWarning(this) << "Cannot call writeAdapter without an adapter.";
return;
}
this->setData(this->mAdapter->serializeAdapter());
}
void FileView::onAdapterDestroyed() { this->mAdapter = nullptr; }
void FileViewAdapter::setFileView(FileView* fileView) {
if (fileView == this->mFileView) return;
if (this->mFileView) {
QObject::disconnect(this->mFileView, nullptr, this, nullptr);
}
this->mFileView = fileView;
if (fileView) {
QObject::connect(fileView, &FileView::dataChanged, this, &FileViewAdapter::onDataChanged);
this->setFileView(fileView);
} else {
this->setFileView(nullptr);
}
}
void FileViewAdapter::onDataChanged() { this->deserializeAdapter(this->mFileView->data()); }
} // namespace qs::io