summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Bohlender <michael.bohlender@kdemail.net>2017-09-12 09:44:39 +0200
committerMichael Bohlender <michael.bohlender@kdemail.net>2017-09-12 09:44:39 +0200
commita4f6d04436ff02f0c8a5b6e6f2427e823a18f0d4 (patch)
tree27506c078c4297fd031f94b3cce4db83035b35f3
parentaa559f644da3b6be8ffa28d0f53b9f989c2076d0 (diff)
downloadkube-a4f6d04436ff02f0c8a5b6e6f2427e823a18f0d4.tar.gz
kube-a4f6d04436ff02f0c8a5b6e6f2427e823a18f0d4.zip
cpoy over example texteditor code and expose it through the framewoks plugin
-rw-r--r--framework/src/CMakeLists.txt1
-rw-r--r--framework/src/domain/documenthandler.cpp372
-rw-r--r--framework/src/domain/documenthandler.h170
-rw-r--r--framework/src/frameworkplugin.cpp2
4 files changed, 545 insertions, 0 deletions
diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt
index 43651e0a..5e9dd269 100644
--- a/framework/src/CMakeLists.txt
+++ b/framework/src/CMakeLists.txt
@@ -31,6 +31,7 @@ set(SRCS
31 domain/contactcontroller.cpp 31 domain/contactcontroller.cpp
32 domain/controller.cpp 32 domain/controller.cpp
33 domain/peoplemodel.cpp 33 domain/peoplemodel.cpp
34 domain/documenthandler.cpp
34 domain/mime/htmlutils.cpp 35 domain/mime/htmlutils.cpp
35 domain/mime/messageparser.cpp 36 domain/mime/messageparser.cpp
36 domain/mime/attachmentmodel.cpp 37 domain/mime/attachmentmodel.cpp
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}
diff --git a/framework/src/domain/documenthandler.h b/framework/src/domain/documenthandler.h
new file mode 100644
index 00000000..a6125bc3
--- /dev/null
+++ b/framework/src/domain/documenthandler.h
@@ -0,0 +1,170 @@
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
87public:
88 explicit DocumentHandler(QObject *parent = nullptr);
89
90 QQuickTextDocument *document() const;
91 void setDocument(QQuickTextDocument *document);
92
93 int cursorPosition() const;
94 void setCursorPosition(int position);
95
96 int selectionStart() const;
97 void setSelectionStart(int position);
98
99 int selectionEnd() const;
100 void setSelectionEnd(int position);
101
102 QString fontFamily() const;
103 void setFontFamily(const QString &family);
104
105 QColor textColor() const;
106 void setTextColor(const QColor &color);
107
108 Qt::Alignment alignment() const;
109 void setAlignment(Qt::Alignment alignment);
110
111 bool bold() const;
112 void setBold(bool bold);
113
114 bool italic() const;
115 void setItalic(bool italic);
116
117 bool underline() const;
118 void setUnderline(bool underline);
119
120 int fontSize() const;
121 void setFontSize(int size);
122
123 QString fileName() const;
124 QString fileType() const;
125 QUrl fileUrl() const;
126
127public Q_SLOTS:
128 void load(const QUrl &fileUrl);
129 void saveAs(const QUrl &fileUrl);
130
131Q_SIGNALS:
132 void documentChanged();
133 void cursorPositionChanged();
134 void selectionStartChanged();
135 void selectionEndChanged();
136
137 void fontFamilyChanged();
138 void textColorChanged();
139 void alignmentChanged();
140
141 void boldChanged();
142 void italicChanged();
143 void underlineChanged();
144
145 void fontSizeChanged();
146
147 void textChanged();
148 void fileUrlChanged();
149
150 void loaded(const QString &text);
151 void error(const QString &message);
152
153private:
154 void reset();
155 QTextCursor textCursor() const;
156 QTextDocument *textDocument() const;
157 void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
158
159 QQuickTextDocument *m_document;
160
161 int m_cursorPosition;
162 int m_selectionStart;
163 int m_selectionEnd;
164
165 QFont m_font;
166 int m_fontSize;
167 QUrl m_fileUrl;
168};
169
170#endif // DOCUMENTHANDLER_H
diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp
index 8fd457fc..b8cad45d 100644
--- a/framework/src/frameworkplugin.cpp
+++ b/framework/src/frameworkplugin.cpp
@@ -29,6 +29,7 @@
29#include "domain/mouseproxy.h" 29#include "domain/mouseproxy.h"
30#include "domain/contactcontroller.h" 30#include "domain/contactcontroller.h"
31#include "domain/peoplemodel.h" 31#include "domain/peoplemodel.h"
32#include "domain/documenthandler.h"
32#include "accounts/accountsmodel.h" 33#include "accounts/accountsmodel.h"
33#include "accounts/accountfactory.h" 34#include "accounts/accountfactory.h"
34#include "settings/settings.h" 35#include "settings/settings.h"
@@ -65,6 +66,7 @@ void FrameworkPlugin::registerTypes (const char *uri)
65 qmlRegisterType<MouseProxy>(uri, 1, 0, "MouseProxy"); 66 qmlRegisterType<MouseProxy>(uri, 1, 0, "MouseProxy");
66 qmlRegisterType<ContactController>(uri, 1, 0,"ContactController"); 67 qmlRegisterType<ContactController>(uri, 1, 0,"ContactController");
67 qmlRegisterType<PeopleModel>(uri, 1, 0,"PeopleModel"); 68 qmlRegisterType<PeopleModel>(uri, 1, 0,"PeopleModel");
69 qmlRegisterType<DocumentHandler>(uri, 1, 0, "DocumentHandler");
68 70
69 qmlRegisterType<AccountFactory>(uri, 1, 0, "AccountFactory"); 71 qmlRegisterType<AccountFactory>(uri, 1, 0, "AccountFactory");
70 qmlRegisterType<AccountsModel>(uri, 1, 0, "AccountsModel"); 72 qmlRegisterType<AccountsModel>(uri, 1, 0, "AccountsModel");