aboutsummaryrefslogtreecommitdiff
path: root/ui.qt/src/Document.cpp
blob: 272ac4c68e00da585bcaae10f326b952b127415b (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Document.hpp"

#include <QBrush>
#include <QColor>
#include <QTextCursor>
#include <QVariant>

DocumentHandler::DocumentHandler(QObject* parent)
	: QObject{ parent }
{
}

QQuickTextDocument* DocumentHandler::getDoc() const
{
	return mDoc;
}

void DocumentHandler::setDoc(QQuickTextDocument* newDoc)
{
	if (mDoc != newDoc) {
		auto oldDoc = mDoc;
		mDoc = newDoc;

		if (oldDoc) {
			disconnect(oldDoc->textDocument(), nullptr, this, nullptr);
		}
		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(oldDoc);
	}
}

const QDateTime& DocumentHandler::getModifyTime() const
{
	return mModifyTime;
}

int DocumentHandler::getCursorPos() const
{
	return mCursorPos;
}

void DocumentHandler::setCursorPos(int newCursorPos)
{
	if (mCursorPos == newCursorPos) {
		return;
	}
	mCursorPos = newCursorPos;
	emit cursorPosChanged();
}

int DocumentHandler::getSelectionBegin() const
{
	return mSelectionBegin;
}

void DocumentHandler::setSelectionBegin(int newSelectionBegin)
{
	if (mSelectionBegin == newSelectionBegin) {
		return;
	}
	mSelectionBegin = newSelectionBegin;
	emit selectionBeginChanged();
}

int DocumentHandler::getSelectionEnd() const
{
	return mSelectionEnd;
}

void DocumentHandler::setSelectionEnd(int newSelectionEnd)
{
	if (mSelectionEnd == newSelectionEnd) {
		return;
	}
	mSelectionEnd = newSelectionEnd;
	emit selectionEndChanged();
}

QFont DocumentHandler::getActiveFont() const
{
	auto cursor = makeTextCursor();
	if (cursor.isNull()) {
		return mDoc->textDocument()->defaultFont();
	}
	auto format = cursor.charFormat();
	return format.font();
}

void DocumentHandler::setActiveFont(const QFont& font)
{
	auto cursor = makeTextCursor();
	if (!cursor.isNull() && cursor.charFormat().font() == font) {
		return;
	}

	QTextCharFormat format;
	format.setFont(font);
	mergeFormatOnWordOrSelection(format);

	emit activeFontChanged();
}

QColor DocumentHandler::getActiveTextColor() const
{
	auto cursor = makeTextCursor();
	if (cursor.isNull()) {
		return QColor(Qt::black);
	}
	QTextCharFormat format = cursor.charFormat();
	return format.foreground().color();
}

void DocumentHandler::setActiveTextColor(const QColor& color)
{
	QTextCharFormat format;
	format.setForeground(QBrush(color));
	mergeFormatOnWordOrSelection(format);
	emit activeTextColorChanged();
}

QTextCursor DocumentHandler::makeTextCursor() const
{
	auto doc = mDoc->textDocument();
	if (!doc) {
		return QTextCursor();
	}

	QTextCursor cursor(doc);
	if (mSelectionBegin != mSelectionEnd) {
		cursor.setPosition(mSelectionBegin);
		cursor.setPosition(mSelectionEnd, QTextCursor::KeepAnchor);
	} else {
		cursor.setPosition(mCursorPos);
	}
	return cursor;
}

void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat& format)
{
	auto cursor = makeTextCursor();
	if (!cursor.hasSelection()) {
		cursor.select(QTextCursor::WordUnderCursor);
	}
	cursor.mergeCharFormat(format);
}