#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; }