1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef DOCUMENTHANDLER_H
52#define DOCUMENTHANDLER_H
53
54#include <QFont>
55#include <QObject>
56#include <QTextCursor>
57#include <QUrl>
58
59QT_BEGIN_NAMESPACE
60class QTextDocument;
61class QQuickTextDocument;
62QT_END_NAMESPACE
63
64class DocumentHandler : public QObject
65{
66 Q_OBJECT
67
68 Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
69 Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
70 Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
71 Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
72
73 Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
74 Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
75 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
76
77 Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
78 Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
79 Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
80
81 Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
82
83 Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged)
84 Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
85 Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged)
86
87 Q_PROPERTY(bool modified READ modified WRITE setModified NOTIFY modifiedChanged)
88
89public:
90 explicit DocumentHandler(QObject *parent = nullptr);
91
92 QQuickTextDocument *document() const;
93 void setDocument(QQuickTextDocument *document);
94
95 int cursorPosition() const;
96 void setCursorPosition(int position);
97
98 int selectionStart() const;
99 void setSelectionStart(int position);
100
101 int selectionEnd() const;
102 void setSelectionEnd(int position);
103
104 QString fontFamily() const;
105 void setFontFamily(const QString &family);
106
107 QColor textColor() const;
108 void setTextColor(const QColor &color);
109
110 Qt::Alignment alignment() const;
111 void setAlignment(Qt::Alignment alignment);
112
113 bool bold() const;
114 void setBold(bool bold);
115
116 bool italic() const;
117 void setItalic(bool italic);
118
119 bool underline() const;
120 void setUnderline(bool underline);
121
122 int fontSize() const;
123 void setFontSize(int size);
124
125 QString fileName() const;
126 QString fileType() const;
127 QUrl fileUrl() const;
128
129 bool modified() const;
130 void setModified(bool m);
131
132public Q_SLOTS:
133 void load(const QUrl &fileUrl);
134 void saveAs(const QUrl &fileUrl);
135
136Q_SIGNALS:
137 void documentChanged();
138 void cursorPositionChanged();
139 void selectionStartChanged();
140 void selectionEndChanged();
141
142 void fontFamilyChanged();
143 void textColorChanged();
144 void alignmentChanged();
145
146 void boldChanged();
147 void italicChanged();
148 void underlineChanged();
149
150 void fontSizeChanged();
151
152 void textChanged();
153 void fileUrlChanged();
154
155 void loaded(const QString &text, int format);
156 void error(const QString &message);
157
158 void modifiedChanged();
159
160private:
161 void reset();
162 QTextCursor textCursor() const;
163 QTextDocument *textDocument() const;
164 void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
165
166 QQuickTextDocument *m_document;
167
168 int m_cursorPosition;
169 int m_selectionStart;
170 int m_selectionEnd;
171
172 QFont m_font;
173 QUrl m_fileUrl;
174};
175
176#endif // DOCUMENTHANDLER_H
177

source code of qtquickcontrols2/examples/quickcontrols2/texteditor/documenthandler.h