From 753c26d320e894069157bd401f7779ad07073d7c Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 27 Jun 2022 00:09:44 +0000 Subject: (From git) Initial Qt GUI setup git-svn-id: file:///home/arch/svn/epistmool/trunk@2 71f44415-077c-4ad7-a976-72ddbf76608f --- ui.qt/source/document.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ui.qt/source/document.cpp (limited to 'ui.qt/source/document.cpp') 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 +#include + +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 DocumentModel::roleNames() const +{ + QHash roles; + roles[Qt::DisplayRole] = "display", + roles[ModifyTimeRole] = "modifyTime"; + return roles; +} -- cgit v1.2.3-70-g09d2