diff options
Diffstat (limited to 'ui.qt/source/document.cpp')
-rw-r--r-- | ui.qt/source/document.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/ui.qt/source/document.cpp b/ui.qt/source/document.cpp new file mode 100644 index 0000000..b418b0d --- /dev/null +++ b/ui.qt/source/document.cpp @@ -0,0 +1,78 @@ +#include "document.hpp" + +#include <QMetaType> +#include <QVariant> + +DocumentBlock::DocumentBlock(QObject* parent) + : QObject{ parent } +{ +} + +DocumentModel* DocumentBlock::getModel() const +{ + return mModel; +} + +void DocumentBlock::setModel(DocumentModel* newModel) +{ + mModel = newModel; +} + +QQuickTextDocument* DocumentBlock::getDoc() const +{ + return mDoc; +} + +void DocumentBlock::setDoc(QQuickTextDocument* newDoc) +{ + if (mDoc != newDoc) { + if (mDoc) { + disconnect(mDoc->textDocument(), nullptr, this, nullptr); + } + mDoc = newDoc; + if (newDoc) { + connect(newDoc->textDocument(), &QTextDocument::modificationChanged, this, [&]() { + // TODO add a timer to wait for 1 second before updating? + mModifyTime = QDateTime::currentDateTime(); + emit modificationChanged(); + }); + } + emit docChanged(); + } +} + +const QDateTime& DocumentBlock::getModifyTime() const +{ + return mModifyTime; +} + +void DocumentModel::appendBlock(DocumentBlock* block) +{ + mBlocks.push_back(block); +} + +int DocumentModel::rowCount(const QModelIndex& parent) const +{ + return mBlocks.size(); +} + +QVariant DocumentModel::data(const QModelIndex& index, int role) const +{ + if (index.row() < 0 || index.row() >= mBlocks.size()) { + return QVariant(); + } + + switch (role) { + case Qt::DisplayRole: return QVariant::fromValue(mBlocks[index.row()]); + case ModifyTimeRole: return mBlocks[index.row()]->getModifyTime(); + default: return QVariant(); + } +} + +QHash<int, QByteArray> DocumentModel::roleNames() const +{ + QHash<int, QByteArray> roles; + roles[Qt::DisplayRole] = "display", + roles[ModifyTimeRole] = "modifyTime"; + return roles; +} |