summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-10-09 09:59:21 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-10-09 09:59:21 +0200
commit8e0bc9119487840025a3ce8cdfcb842f7dff4eaf (patch)
tree2b460feaa45c84cbeee6c1d0017ab904b07805b3
parent9f2626385bf9c12a240f5fd17b90304c231fba5e (diff)
downloadkube-8e0bc9119487840025a3ce8cdfcb842f7dff4eaf.tar.gz
kube-8e0bc9119487840025a3ce8cdfcb842f7dff4eaf.zip
Cleanup
-rw-r--r--framework/src/domain/documenthandler.cpp323
-rw-r--r--framework/src/domain/documenthandler.h161
2 files changed, 0 insertions, 484 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
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 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
90QString DocumentHandler::plainText() const
91{
92 if (m_document) {
93 return m_document->textDocument()->toPlainText();
94 }
95 return {};
96}
97
98QString DocumentHandler::htmlText() const
99{
100 if (m_document) {
101 return m_document->textDocument()->toHtml();
102 }
103 return {};
104}
105
106int DocumentHandler::cursorPosition() const
107{
108 return m_cursorPosition;
109}
110
111void DocumentHandler::setCursorPosition(int position)
112{
113 if (position != m_cursorPosition) {
114 m_cursorPosition = position;
115 reset();
116 emit cursorPositionChanged();
117 }
118}
119
120int DocumentHandler::selectionStart() const
121{
122 return m_selectionStart;
123}
124
125void DocumentHandler::setSelectionStart(int position)
126{
127 if (position != m_selectionStart) {
128 m_selectionStart = position;
129 emit selectionStartChanged();
130 }
131}
132
133int DocumentHandler::selectionEnd() const
134{
135 return m_selectionEnd;
136}
137
138void DocumentHandler::setSelectionEnd(int position)
139{
140 if (position != m_selectionEnd) {
141 m_selectionEnd = position;
142 emit selectionEndChanged();
143 }
144}
145
146QTextCharFormat 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
158QString DocumentHandler::fontFamily() const
159{
160 return charFormat().font().family();
161}
162
163void DocumentHandler::setFontFamily(const QString &family)
164{
165 QTextCharFormat format;
166 format.setFontFamily(family);
167 mergeFormatOnWordOrSelection(format);
168 emit fontFamilyChanged();
169}
170
171QColor DocumentHandler::textColor() const
172{
173 return charFormat().foreground().color();
174}
175
176void DocumentHandler::setTextColor(const QColor &color)
177{
178 QTextCharFormat format;
179 format.setForeground(QBrush(color));
180 mergeFormatOnWordOrSelection(format);
181 emit textColorChanged();
182}
183
184Qt::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
193void 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
202bool DocumentHandler::bold() const
203{
204 return charFormat().fontWeight() == QFont::Bold;
205}
206
207void DocumentHandler::setBold(bool bold)
208{
209 QTextCharFormat format;
210 format.setFontWeight(bold ? QFont::Bold : QFont::Normal);
211 mergeFormatOnWordOrSelection(format);
212 emit boldChanged();
213}
214
215bool DocumentHandler::italic() const
216{
217 return charFormat().fontItalic();
218}
219
220void DocumentHandler::setItalic(bool italic)
221{
222 QTextCharFormat format;
223 format.setFontItalic(italic);
224 mergeFormatOnWordOrSelection(format);
225 emit italicChanged();
226}
227
228bool DocumentHandler::underline() const
229{
230 return charFormat().fontUnderline();
231}
232
233void DocumentHandler::setUnderline(bool underline)
234{
235 QTextCharFormat format;
236 format.setFontUnderline(underline);
237 mergeFormatOnWordOrSelection(format);
238 emit underlineChanged();
239}
240
241int DocumentHandler::fontSize() const
242{
243 return charFormat().font().pointSize();
244}
245
246void 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
260void 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
271QTextCursor 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
286QTextDocument *DocumentHandler::textDocument() const
287{
288 if (!m_document) {
289 return nullptr;
290 }
291 return m_document->textDocument();
292}
293
294void 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
309void 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}
diff --git a/framework/src/domain/documenthandler.h b/framework/src/domain/documenthandler.h
deleted file mode 100644
index 8be3ba16..00000000
--- a/framework/src/domain/documenthandler.h
+++ /dev/null
@@ -1,161 +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#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 plainText READ plainText NOTIFY textChanged)
84 Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged)
85
86public:
87 explicit DocumentHandler(QObject *parent = nullptr);
88
89 QQuickTextDocument *document() const;
90 void setDocument(QQuickTextDocument *document);
91
92 QString plainText() const;
93 QString htmlText() const;
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
125Q_SIGNALS:
126 void documentChanged();
127 void cursorPositionChanged();
128 void selectionStartChanged();
129 void selectionEndChanged();
130
131 void fontFamilyChanged();
132 void textColorChanged();
133 void alignmentChanged();
134
135 void boldChanged();
136 void italicChanged();
137 void underlineChanged();
138
139 void fontSizeChanged();
140
141 void textChanged();
142
143private Q_SLOTS:
144 void contentsChange(int position, int charsRemoved, int charsAdded);
145
146private:
147 void reset();
148 QTextCharFormat charFormat() const;
149 QTextCursor textCursor() const;
150 QTextDocument *textDocument() const;
151 void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
152
153 QQuickTextDocument *m_document;
154
155 int m_cursorPosition;
156 int m_selectionStart;
157 int m_selectionEnd;
158 QSharedPointer<QTextCharFormat> m_cachedTextFormat;
159};
160
161#endif // DOCUMENTHANDLER_H