aboutsummaryrefslogtreecommitdiff
path: root/ui.qt/source/document.cpp
blob: b418b0d0834f5624e3732c8baed2e4c09f5c4302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
}