diff options
Diffstat (limited to 'framework/src/domain/documenthandler.cpp')
-rw-r--r-- | framework/src/domain/documenthandler.cpp | 323 |
1 files changed, 0 insertions, 323 deletions
diff --git a/framework/src/domain/documenthandler.cpp b/framework/src/domain/documenthandler.cpp deleted file mode 100644 index b4ddc24c..00000000 --- a/framework/src/domain/documenthandler.cpp +++ /dev/null | |||
@@ -1,323 +0,0 @@ | |||
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 | #include "documenthandler.h" | ||
52 | |||
53 | #include <QFile> | ||
54 | #include <QFileInfo> | ||
55 | #include <QFileSelector> | ||
56 | #include <QQmlFile> | ||
57 | #include <QQmlFileSelector> | ||
58 | #include <QQuickTextDocument> | ||
59 | #include <QTextCharFormat> | ||
60 | #include <QTextCodec> | ||
61 | #include <QTextDocument> | ||
62 | #include <QDebug> | ||
63 | |||
64 | DocumentHandler::DocumentHandler(QObject *parent) | ||
65 | : QObject(parent) | ||
66 | , m_document(nullptr) | ||
67 | , m_cursorPosition(-1) | ||
68 | , m_selectionStart(0) | ||
69 | , m_selectionEnd(0) | ||
70 | { | ||
71 | } | ||
72 | |||
73 | QQuickTextDocument *DocumentHandler::document() const | ||
74 | { | ||
75 | return m_document; | ||
76 | } | ||
77 | |||
78 | void DocumentHandler::setDocument(QQuickTextDocument *document) | ||
79 | { | ||
80 | if (document != m_document) { | ||
81 | m_document = document; | ||
82 | connect(m_document->textDocument(), &QTextDocument::contentsChanged, this, [this] () { | ||
83 | emit textChanged(); | ||
84 | }); | ||
85 | connect(m_document->textDocument(), &QTextDocument::contentsChange, this, &DocumentHandler::contentsChange); | ||
86 | emit documentChanged(); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | QString DocumentHandler::plainText() const | ||
91 | { | ||
92 | if (m_document) { | ||
93 | return m_document->textDocument()->toPlainText(); | ||
94 | } | ||
95 | return {}; | ||
96 | } | ||
97 | |||
98 | QString DocumentHandler::htmlText() const | ||
99 | { | ||
100 | if (m_document) { | ||
101 | return m_document->textDocument()->toHtml(); | ||
102 | } | ||
103 | return {}; | ||
104 | } | ||
105 | |||
106 | int DocumentHandler::cursorPosition() const | ||
107 | { | ||
108 | return m_cursorPosition; | ||
109 | } | ||
110 | |||
111 | void DocumentHandler::setCursorPosition(int position) | ||
112 | { | ||
113 | if (position != m_cursorPosition) { | ||
114 | m_cursorPosition = position; | ||
115 | reset(); | ||
116 | emit cursorPositionChanged(); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | int DocumentHandler::selectionStart() const | ||
121 | { | ||
122 | return m_selectionStart; | ||
123 | } | ||
124 | |||
125 | void DocumentHandler::setSelectionStart(int position) | ||
126 | { | ||
127 | if (position != m_selectionStart) { | ||
128 | m_selectionStart = position; | ||
129 | emit selectionStartChanged(); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | int DocumentHandler::selectionEnd() const | ||
134 | { | ||
135 | return m_selectionEnd; | ||
136 | } | ||
137 | |||
138 | void DocumentHandler::setSelectionEnd(int position) | ||
139 | { | ||
140 | if (position != m_selectionEnd) { | ||
141 | m_selectionEnd = position; | ||
142 | emit selectionEndChanged(); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | QTextCharFormat DocumentHandler::charFormat() const | ||
147 | { | ||
148 | if (m_cachedTextFormat) { | ||
149 | return *m_cachedTextFormat; | ||
150 | } | ||
151 | auto cursor = textCursor(); | ||
152 | if (cursor.isNull()) { | ||
153 | return {}; | ||
154 | } | ||
155 | return cursor.charFormat(); | ||
156 | } | ||
157 | |||
158 | QString DocumentHandler::fontFamily() const | ||
159 | { | ||
160 | return charFormat().font().family(); | ||
161 | } | ||
162 | |||
163 | void DocumentHandler::setFontFamily(const QString &family) | ||
164 | { | ||
165 | QTextCharFormat format; | ||
166 | format.setFontFamily(family); | ||
167 | mergeFormatOnWordOrSelection(format); | ||
168 | emit fontFamilyChanged(); | ||
169 | } | ||
170 | |||
171 | QColor DocumentHandler::textColor() const | ||
172 | { | ||
173 | return charFormat().foreground().color(); | ||
174 | } | ||
175 | |||
176 | void DocumentHandler::setTextColor(const QColor &color) | ||
177 | { | ||
178 | QTextCharFormat format; | ||
179 | format.setForeground(QBrush(color)); | ||
180 | mergeFormatOnWordOrSelection(format); | ||
181 | emit textColorChanged(); | ||
182 | } | ||
183 | |||
184 | Qt::Alignment DocumentHandler::alignment() const | ||
185 | { | ||
186 | auto cursor = textCursor(); | ||
187 | if (cursor.isNull()) { | ||
188 | return Qt::AlignLeft; | ||
189 | } | ||
190 | return cursor.blockFormat().alignment(); | ||
191 | } | ||
192 | |||
193 | void DocumentHandler::setAlignment(Qt::Alignment alignment) | ||
194 | { | ||
195 | QTextBlockFormat format; | ||
196 | format.setAlignment(alignment); | ||
197 | QTextCursor cursor = textCursor(); | ||
198 | cursor.mergeBlockFormat(format); | ||
199 | emit alignmentChanged(); | ||
200 | } | ||
201 | |||
202 | bool DocumentHandler::bold() const | ||
203 | { | ||
204 | return charFormat().fontWeight() == QFont::Bold; | ||
205 | } | ||
206 | |||
207 | void DocumentHandler::setBold(bool bold) | ||
208 | { | ||
209 | QTextCharFormat format; | ||
210 | format.setFontWeight(bold ? QFont::Bold : QFont::Normal); | ||
211 | mergeFormatOnWordOrSelection(format); | ||
212 | emit boldChanged(); | ||
213 | } | ||
214 | |||
215 | bool DocumentHandler::italic() const | ||
216 | { | ||
217 | return charFormat().fontItalic(); | ||
218 | } | ||
219 | |||
220 | void DocumentHandler::setItalic(bool italic) | ||
221 | { | ||
222 | QTextCharFormat format; | ||
223 | format.setFontItalic(italic); | ||
224 | mergeFormatOnWordOrSelection(format); | ||
225 | emit italicChanged(); | ||
226 | } | ||
227 | |||
228 | bool DocumentHandler::underline() const | ||
229 | { | ||
230 | return charFormat().fontUnderline(); | ||
231 | } | ||
232 | |||
233 | void DocumentHandler::setUnderline(bool underline) | ||
234 | { | ||
235 | QTextCharFormat format; | ||
236 | format.setFontUnderline(underline); | ||
237 | mergeFormatOnWordOrSelection(format); | ||
238 | emit underlineChanged(); | ||
239 | } | ||
240 | |||
241 | int DocumentHandler::fontSize() const | ||
242 | { | ||
243 | return charFormat().font().pointSize(); | ||
244 | } | ||
245 | |||
246 | void DocumentHandler::setFontSize(int size) | ||
247 | { | ||
248 | if (size <= 0) | ||
249 | return; | ||
250 | |||
251 | if (charFormat().property(QTextFormat::FontPointSize).toInt() == size) | ||
252 | return; | ||
253 | |||
254 | QTextCharFormat format; | ||
255 | format.setFontPointSize(size); | ||
256 | mergeFormatOnWordOrSelection(format); | ||
257 | emit fontSizeChanged(); | ||
258 | } | ||
259 | |||
260 | void DocumentHandler::reset() | ||
261 | { | ||
262 | emit fontFamilyChanged(); | ||
263 | emit alignmentChanged(); | ||
264 | emit boldChanged(); | ||
265 | emit italicChanged(); | ||
266 | emit underlineChanged(); | ||
267 | emit fontSizeChanged(); | ||
268 | emit textColorChanged(); | ||
269 | } | ||
270 | |||
271 | QTextCursor DocumentHandler::textCursor() const | ||
272 | { | ||
273 | if (QTextDocument *doc = textDocument()) { | ||
274 | QTextCursor cursor = QTextCursor(doc); | ||
275 | if (m_selectionStart != m_selectionEnd) { | ||
276 | cursor.setPosition(m_selectionStart); | ||
277 | cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor); | ||
278 | } else { | ||
279 | cursor.setPosition(m_cursorPosition); | ||
280 | } | ||
281 | return cursor; | ||
282 | } | ||
283 | return QTextCursor(); | ||
284 | } | ||
285 | |||
286 | QTextDocument *DocumentHandler::textDocument() const | ||
287 | { | ||
288 | if (!m_document) { | ||
289 | return nullptr; | ||
290 | } | ||
291 | return m_document->textDocument(); | ||
292 | } | ||
293 | |||
294 | void DocumentHandler::contentsChange(int position, int charsRemoved, int charsAdded) | ||
295 | { | ||
296 | if (m_cachedTextFormat) { | ||
297 | if (charsAdded) { | ||
298 | //Apply cached formatting | ||
299 | QTextCursor cursor = textCursor(); | ||
300 | cursor.setPosition(position + charsAdded, QTextCursor::KeepAnchor); | ||
301 | cursor.mergeCharFormat(*m_cachedTextFormat); | ||
302 | //This is somehow necessary, otherwise space can break in the editor. | ||
303 | cursor.setPosition(position + charsAdded, QTextCursor::MoveAnchor); | ||
304 | } | ||
305 | m_cachedTextFormat = {}; | ||
306 | } | ||
307 | } | ||
308 | |||
309 | void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) | ||
310 | { | ||
311 | QTextCursor cursor = textCursor(); | ||
312 | |||
313 | if (cursor.hasSelection()) { | ||
314 | cursor.mergeCharFormat(format); | ||
315 | } else { | ||
316 | if (m_cachedTextFormat) { | ||
317 | m_cachedTextFormat->merge(format); | ||
318 | } else { | ||
319 | //If we have nothing to format right now we cache the result until the next char is inserted. | ||
320 | m_cachedTextFormat = QSharedPointer<QTextCharFormat>::create(format); | ||
321 | } | ||
322 | } | ||
323 | } | ||