aboutsummaryrefslogtreecommitdiff
path: root/ui.qt/source/document.hpp
blob: 5ef1bba79319298c9020b06888f0559913f65391 (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
#pragma once

#include "fwd.hpp"

#include <QAbstractItemModel>
#include <QDateTime>
#include <QObject>
#include <QQuickTextDocument>
#include <QTextCharFormat>

// To be instanciated in QML as the logic backend to some TextArea
class DocumentHandler : public QObject
{
	Q_OBJECT
	QML_ELEMENT

	Q_PROPERTY(QQuickTextDocument* document READ getDoc WRITE setDoc NOTIFY docChanged)
	Q_PROPERTY(QDateTime modifyTime READ getModifyTime NOTIFY modificationChanged)

	Q_PROPERTY(int cursorPos READ getCursorPos WRITE setCursorPos NOTIFY cursorPosChanged)
	Q_PROPERTY(int selectionBegin READ getSelectionBegin WRITE setSelectionBegin NOTIFY selectionBeginChanged)
	Q_PROPERTY(int selectionEnd READ getSelectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)

	Q_PROPERTY(QFont activeFont READ getActiveFont WRITE setActiveFont NOTIFY activeFontChanged)
	Q_PROPERTY(QColor activeTextColor READ getActiveTextColor WRITE setActiveTextColor NOTIFY activeTextColorChanged)

private:
	QQuickTextDocument* mDoc = nullptr;
	QDateTime mModifyTime;

	int mCursorPos;
	int mSelectionBegin;
	int mSelectionEnd;

public:
	explicit DocumentHandler(QObject* parent = nullptr);

	QQuickTextDocument* getDoc() const;
	void setDoc(QQuickTextDocument* newDoc);

	const QDateTime& getModifyTime() const;

	int getCursorPos() const;
	void setCursorPos(int newCursorPos);

	int getSelectionBegin() const;
	void setSelectionBegin(int newSelectionBegin);

	int getSelectionEnd() const;
	void setSelectionEnd(int newSelectionEnd);

	QFont getActiveFont() const;
	void setActiveFont(const QFont& font);

	QColor getActiveTextColor() const;
	void setActiveTextColor(const QColor& color);

signals:
	void docChanged(QQuickTextDocument* oldDoc);
	void modificationChanged(); // Redirected from the currently bound document

	void cursorPosChanged();
	void selectionBeginChanged();
	void selectionEndChanged();

	void activeFontChanged();
	void activeTextColorChanged();

private:
	QTextCursor makeTextCursor() const;
	void mergeFormatOnWordOrSelection(const QTextCharFormat& format);
};