summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/documenthandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/documenthandler.cpp')
-rw-r--r--framework/src/domain/documenthandler.cpp372
1 files changed, 372 insertions, 0 deletions
diff --git a/framework/src/domain/documenthandler.cpp b/framework/src/domain/documenthandler.cpp
new file mode 100644
index 00000000..25a83592
--- /dev/null
+++ b/framework/src/domain/documenthandler.cpp
@@ -0,0 +1,372 @@
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
64DocumentHandler::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
73QQuickTextDocument *DocumentHandler::document() const
74{
75 return m_document;
76}
77
78void DocumentHandler::setDocument(QQuickTextDocument *document)
79{
80 if (document == m_document)
81 return;
82
83 m_document = document;
84 emit documentChanged();
85}
86
87int DocumentHandler::cursorPosition() const
88{
89 return m_cursorPosition;
90}
91
92void DocumentHandler::setCursorPosition(int position)
93{
94 if (position == m_cursorPosition)
95 return;
96
97 m_cursorPosition = position;
98 reset();
99 emit cursorPositionChanged();
100}
101
102int DocumentHandler::selectionStart() const
103{
104 return m_selectionStart;
105}
106
107void DocumentHandler::setSelectionStart(int position)
108{
109 if (position == m_selectionStart)
110 return;
111
112 m_selectionStart = position;
113 emit selectionStartChanged();
114}
115
116int DocumentHandler::selectionEnd() const
117{
118 return m_selectionEnd;
119}
120
121void DocumentHandler::setSelectionEnd(int position)
122{
123 if (position == m_selectionEnd)
124 return;
125
126 m_selectionEnd = position;
127 emit selectionEndChanged();
128}
129
130QString DocumentHandler::fontFamily() const
131{
132 QTextCursor cursor = textCursor();
133 if (cursor.isNull())
134 return QString();
135 QTextCharFormat format = cursor.charFormat();
136 return format.font().family();
137}
138
139void DocumentHandler::setFontFamily(const QString &family)
140{
141 QTextCharFormat format;
142 format.setFontFamily(family);
143 mergeFormatOnWordOrSelection(format);
144 emit fontFamilyChanged();
145}
146
147QColor DocumentHandler::textColor() const
148{
149 QTextCursor cursor = textCursor();
150 if (cursor.isNull())
151 return QColor(Qt::black);
152 QTextCharFormat format = cursor.charFormat();
153 return format.foreground().color();
154}
155
156void DocumentHandler::setTextColor(const QColor &color)
157{
158 QTextCharFormat format;
159 format.setForeground(QBrush(color));
160 mergeFormatOnWordOrSelection(format);
161 emit textColorChanged();
162}
163
164Qt::Alignment DocumentHandler::alignment() const
165{
166 QTextCursor cursor = textCursor();
167 if (cursor.isNull())
168 return Qt::AlignLeft;
169 return textCursor().blockFormat().alignment();
170}
171
172void DocumentHandler::setAlignment(Qt::Alignment alignment)
173{
174 QTextBlockFormat format;
175 format.setAlignment(alignment);
176 QTextCursor cursor = textCursor();
177 cursor.mergeBlockFormat(format);
178 emit alignmentChanged();
179}
180
181bool DocumentHandler::bold() const
182{
183 QTextCursor cursor = textCursor();
184 if (cursor.isNull())
185 return false;
186 return textCursor().charFormat().fontWeight() == QFont::Bold;
187}
188
189void DocumentHandler::setBold(bool bold)
190{
191 QTextCharFormat format;
192 format.setFontWeight(bold ? QFont::Bold : QFont::Normal);
193 mergeFormatOnWordOrSelection(format);
194 emit boldChanged();
195}
196
197bool DocumentHandler::italic() const
198{
199 QTextCursor cursor = textCursor();
200 if (cursor.isNull())
201 return false;
202 return textCursor().charFormat().fontItalic();
203}
204
205void DocumentHandler::setItalic(bool italic)
206{
207 QTextCharFormat format;
208 format.setFontItalic(italic);
209 mergeFormatOnWordOrSelection(format);
210 emit italicChanged();
211}
212
213bool DocumentHandler::underline() const
214{
215 QTextCursor cursor = textCursor();
216 if (cursor.isNull())
217 return false;
218 return textCursor().charFormat().fontUnderline();
219}
220
221void DocumentHandler::setUnderline(bool underline)
222{
223 QTextCharFormat format;
224 format.setFontUnderline(underline);
225 mergeFormatOnWordOrSelection(format);
226 emit underlineChanged();
227}
228
229int DocumentHandler::fontSize() const
230{
231 QTextCursor cursor = textCursor();
232 if (cursor.isNull())
233 return 0;
234 QTextCharFormat format = cursor.charFormat();
235 return format.font().pointSize();
236}
237
238void DocumentHandler::setFontSize(int size)
239{
240 if (size <= 0)
241 return;
242
243 QTextCursor cursor = textCursor();
244 if (cursor.isNull())
245 return;
246
247 if (!cursor.hasSelection())
248 cursor.select(QTextCursor::WordUnderCursor);
249
250 if (cursor.charFormat().property(QTextFormat::FontPointSize).toInt() == size)
251 return;
252
253 QTextCharFormat format;
254 format.setFontPointSize(size);
255 mergeFormatOnWordOrSelection(format);
256 emit fontSizeChanged();
257}
258
259QString DocumentHandler::fileName() const
260{
261 const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl);
262 const QString fileName = QFileInfo(filePath).fileName();
263 if (fileName.isEmpty())
264 return QStringLiteral("untitled.txt");
265 return fileName;
266}
267
268QString DocumentHandler::fileType() const
269{
270 return QFileInfo(fileName()).suffix();
271}
272
273QUrl DocumentHandler::fileUrl() const
274{
275 return m_fileUrl;
276}
277
278void DocumentHandler::load(const QUrl &fileUrl)
279{
280 if (fileUrl == m_fileUrl)
281 return;
282
283 QQmlEngine *engine = qmlEngine(this);
284 if (!engine) {
285 qWarning() << "load() called before DocumentHandler has QQmlEngine";
286 return;
287 }
288
289 const QUrl path = QQmlFileSelector::get(engine)->selector()->select(fileUrl);
290 const QString fileName = QQmlFile::urlToLocalFileOrQrc(path);
291 if (QFile::exists(fileName)) {
292 QFile file(fileName);
293 if (file.open(QFile::ReadOnly)) {
294 QByteArray data = file.readAll();
295 QTextCodec *codec = QTextCodec::codecForHtml(data);
296 if (QTextDocument *doc = textDocument())
297 doc->setModified(false);
298
299 emit loaded(codec->toUnicode(data));
300 reset();
301 }
302 }
303
304 m_fileUrl = fileUrl;
305 emit fileUrlChanged();
306}
307
308void DocumentHandler::saveAs(const QUrl &fileUrl)
309{
310 QTextDocument *doc = textDocument();
311 if (!doc)
312 return;
313
314 const QString filePath = fileUrl.toLocalFile();
315 const bool isHtml = QFileInfo(filePath).suffix().contains(QLatin1String("htm"));
316 QFile file(filePath);
317 if (!file.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) {
318 emit error(tr("Cannot save: ") + file.errorString());
319 return;
320 }
321 file.write((isHtml ? doc->toHtml() : doc->toPlainText()).toUtf8());
322 file.close();
323
324 if (fileUrl == m_fileUrl)
325 return;
326
327 m_fileUrl = fileUrl;
328 emit fileUrlChanged();
329}
330
331void DocumentHandler::reset()
332{
333 emit fontFamilyChanged();
334 emit alignmentChanged();
335 emit boldChanged();
336 emit italicChanged();
337 emit underlineChanged();
338 emit fontSizeChanged();
339 emit textColorChanged();
340}
341
342QTextCursor DocumentHandler::textCursor() const
343{
344 QTextDocument *doc = textDocument();
345 if (!doc)
346 return QTextCursor();
347
348 QTextCursor cursor = QTextCursor(doc);
349 if (m_selectionStart != m_selectionEnd) {
350 cursor.setPosition(m_selectionStart);
351 cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor);
352 } else {
353 cursor.setPosition(m_cursorPosition);
354 }
355 return cursor;
356}
357
358QTextDocument *DocumentHandler::textDocument() const
359{
360 if (!m_document)
361 return nullptr;
362
363 return m_document->textDocument();
364}
365
366void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
367{
368 QTextCursor cursor = textCursor();
369 if (!cursor.hasSelection())
370 cursor.select(QTextCursor::WordUnderCursor);
371 cursor.mergeCharFormat(format);
372}