Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquicktextdocument.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
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 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qquicktextdocument.h" | - | ||||||||||||
41 | #include "qquicktextdocument_p.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #include "qquicktextedit_p.h" | - | ||||||||||||
44 | #include "qquicktextedit_p_p.h" | - | ||||||||||||
45 | #include "qquicktext_p_p.h" | - | ||||||||||||
46 | - | |||||||||||||
47 | #include <QtQml/qqmlinfo.h> | - | ||||||||||||
48 | #include <QtQml/qqmlcontext.h> | - | ||||||||||||
49 | #include <QtQuick/private/qquickpixmapcache_p.h> | - | ||||||||||||
50 | - | |||||||||||||
51 | QT_BEGIN_NAMESPACE | - | ||||||||||||
52 | - | |||||||||||||
53 | /*! | - | ||||||||||||
54 | \class QQuickTextDocument | - | ||||||||||||
55 | \since 5.1 | - | ||||||||||||
56 | \brief The QQuickTextDocument class provides access to the QTextDocument of QQuickTextEdit. | - | ||||||||||||
57 | \inmodule QtQuick | - | ||||||||||||
58 | - | |||||||||||||
59 | This class provides access to the QTextDocument of QQuickTextEdit elements. | - | ||||||||||||
60 | This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt. | - | ||||||||||||
61 | You are not allowed to modify the document, but it can be used to output content, for example with \l{QTextDocumentWriter}), | - | ||||||||||||
62 | or provide additional formatting, for example with \l{QSyntaxHighlighter}. | - | ||||||||||||
63 | - | |||||||||||||
64 | The class has to be used from C++ directly, using the property of the \l TextEdit. | - | ||||||||||||
65 | - | |||||||||||||
66 | Warning: The QTextDocument provided is used internally by \l {Qt Quick} elements to provide text manipulation primitives. | - | ||||||||||||
67 | You are not allowed to perform any modification of the internal state of the QTextDocument. If you do, the element | - | ||||||||||||
68 | in question may stop functioning or crash. | - | ||||||||||||
69 | */ | - | ||||||||||||
70 | - | |||||||||||||
71 | class QQuickTextDocumentPrivate : public QObjectPrivate | - | ||||||||||||
72 | { | - | ||||||||||||
73 | public: | - | ||||||||||||
74 | QPointer<QTextDocument> document; | - | ||||||||||||
75 | }; | - | ||||||||||||
76 | - | |||||||||||||
77 | /*! | - | ||||||||||||
78 | Constructs a QQuickTextDocument object with | - | ||||||||||||
79 | \a parent as the parent object. | - | ||||||||||||
80 | */ | - | ||||||||||||
81 | QQuickTextDocument::QQuickTextDocument(QQuickItem *parent) | - | ||||||||||||
82 | : QObject(*(new QQuickTextDocumentPrivate), parent) | - | ||||||||||||
83 | { | - | ||||||||||||
84 | Q_D(QQuickTextDocument); | - | ||||||||||||
85 | Q_ASSERT(parent); | - | ||||||||||||
86 | Q_ASSERT(qobject_cast<QQuickTextEdit*>(parent)); | - | ||||||||||||
87 | d->document = QPointer<QTextDocument>(qobject_cast<QQuickTextEdit*>(parent)->d_func()->document); | - | ||||||||||||
88 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
89 | - | |||||||||||||
90 | /*! | - | ||||||||||||
91 | Returns a pointer to the QTextDocument object. | - | ||||||||||||
92 | */ | - | ||||||||||||
93 | QTextDocument* QQuickTextDocument::textDocument() const | - | ||||||||||||
94 | { | - | ||||||||||||
95 | Q_D(const QQuickTextDocument); | - | ||||||||||||
96 | return d->document.data(); executed 4 times by 1 test: return d->document.data(); Executed by:
| 4 | ||||||||||||
97 | } | - | ||||||||||||
98 | - | |||||||||||||
99 | QQuickTextDocumentWithImageResources::QQuickTextDocumentWithImageResources(QQuickItem *parent) | - | ||||||||||||
100 | : QTextDocument(parent), outstanding(0) | - | ||||||||||||
101 | { | - | ||||||||||||
102 | setUndoRedoEnabled(false); | - | ||||||||||||
103 | documentLayout()->registerHandler(QTextFormat::ImageObject, this); | - | ||||||||||||
104 | connect(this, SIGNAL(baseUrlChanged(QUrl)), this, SLOT(reset())); | - | ||||||||||||
105 | } executed 1228 times by 7 tests: end of block Executed by:
| 1228 | ||||||||||||
106 | - | |||||||||||||
107 | QQuickTextDocumentWithImageResources::~QQuickTextDocumentWithImageResources() | - | ||||||||||||
108 | { | - | ||||||||||||
109 | if (!m_resources.isEmpty())
| 10-462 | ||||||||||||
110 | qDeleteAll(m_resources); executed 10 times by 2 tests: qDeleteAll(m_resources); Executed by:
| 10 | ||||||||||||
111 | } executed 472 times by 7 tests: end of block Executed by:
| 472 | ||||||||||||
112 | - | |||||||||||||
113 | QVariant QQuickTextDocumentWithImageResources::loadResource(int type, const QUrl &name) | - | ||||||||||||
114 | { | - | ||||||||||||
115 | QVariant resource = QTextDocument::loadResource(type, name); | - | ||||||||||||
116 | if (resource.isNull() && type == QTextDocument::ImageResource) {
| 0-62 | ||||||||||||
117 | QQmlContext *context = qmlContext(parent()); | - | ||||||||||||
118 | QUrl url = baseUrl().resolved(name); | - | ||||||||||||
119 | QQuickPixmap *p = loadPixmap(context, url); | - | ||||||||||||
120 | resource = p->image(); | - | ||||||||||||
121 | } executed 62 times by 2 tests: end of block Executed by:
| 62 | ||||||||||||
122 | - | |||||||||||||
123 | return resource; executed 70 times by 2 tests: return resource; Executed by:
| 70 | ||||||||||||
124 | } | - | ||||||||||||
125 | - | |||||||||||||
126 | void QQuickTextDocumentWithImageResources::requestFinished() | - | ||||||||||||
127 | { | - | ||||||||||||
128 | outstanding--; | - | ||||||||||||
129 | if (outstanding == 0) {
| 0-6 | ||||||||||||
130 | markContentsDirty(0, characterCount()); | - | ||||||||||||
131 | emit imagesLoaded(); | - | ||||||||||||
132 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
133 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
134 | - | |||||||||||||
135 | QSizeF QQuickTextDocumentWithImageResources::intrinsicSize( | - | ||||||||||||
136 | QTextDocument *, int, const QTextFormat &format) | - | ||||||||||||
137 | { | - | ||||||||||||
138 | if (format.isImageFormat()) {
| 0-108 | ||||||||||||
139 | QTextImageFormat imageFormat = format.toImageFormat(); | - | ||||||||||||
140 | - | |||||||||||||
141 | const int width = qRound(imageFormat.width()); | - | ||||||||||||
142 | const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0;
| 0-108 | ||||||||||||
143 | const int height = qRound(imageFormat.height()); | - | ||||||||||||
144 | const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0;
| 0-108 | ||||||||||||
145 | - | |||||||||||||
146 | QSizeF size(width, height); | - | ||||||||||||
147 | if (!hasWidth || !hasHeight) {
| 0-108 | ||||||||||||
148 | QVariant res = resource(QTextDocument::ImageResource, QUrl(imageFormat.name())); | - | ||||||||||||
149 | QImage image = res.value<QImage>(); | - | ||||||||||||
150 | if (image.isNull()) {
| 48-60 | ||||||||||||
151 | if (!hasWidth)
| 0-48 | ||||||||||||
152 | size.setWidth(16); executed 48 times by 2 tests: size.setWidth(16); Executed by:
| 48 | ||||||||||||
153 | if (!hasHeight)
| 0-48 | ||||||||||||
154 | size.setHeight(16); executed 48 times by 2 tests: size.setHeight(16); Executed by:
| 48 | ||||||||||||
155 | return size; executed 48 times by 2 tests: return size; Executed by:
| 48 | ||||||||||||
156 | } | - | ||||||||||||
157 | QSize imgSize = image.size(); | - | ||||||||||||
158 | - | |||||||||||||
159 | if (!hasWidth) {
| 0-60 | ||||||||||||
160 | if (!hasHeight)
| 0-60 | ||||||||||||
161 | size.setWidth(imgSize.width()); executed 60 times by 2 tests: size.setWidth(imgSize.width()); Executed by:
| 60 | ||||||||||||
162 | else | - | ||||||||||||
163 | size.setWidth(qRound(height * (imgSize.width() / (qreal) imgSize.height()))); never executed: size.setWidth(qRound(height * (imgSize.width() / (qreal) imgSize.height()))); | 0 | ||||||||||||
164 | } | - | ||||||||||||
165 | if (!hasHeight) {
| 0-60 | ||||||||||||
166 | if (!hasWidth)
| 0-60 | ||||||||||||
167 | size.setHeight(imgSize.height()); executed 60 times by 2 tests: size.setHeight(imgSize.height()); Executed by:
| 60 | ||||||||||||
168 | else | - | ||||||||||||
169 | size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width()))); never executed: size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width()))); | 0 | ||||||||||||
170 | } | - | ||||||||||||
171 | } executed 60 times by 2 tests: end of block Executed by:
| 60 | ||||||||||||
172 | return size; executed 60 times by 2 tests: return size; Executed by:
| 60 | ||||||||||||
173 | } | - | ||||||||||||
174 | return QSizeF(); never executed: return QSizeF(); | 0 | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | void QQuickTextDocumentWithImageResources::drawObject( | - | ||||||||||||
178 | QPainter *, const QRectF &, QTextDocument *, int, const QTextFormat &) | - | ||||||||||||
179 | { | - | ||||||||||||
180 | } | - | ||||||||||||
181 | - | |||||||||||||
182 | QImage QQuickTextDocumentWithImageResources::image(const QTextImageFormat &format) const | - | ||||||||||||
183 | { | - | ||||||||||||
184 | QVariant res = resource(QTextDocument::ImageResource, QUrl(format.name())); | - | ||||||||||||
185 | return res.value<QImage>(); executed 8 times by 2 tests: return res.value<QImage>(); Executed by:
| 8 | ||||||||||||
186 | } | - | ||||||||||||
187 | - | |||||||||||||
188 | void QQuickTextDocumentWithImageResources::reset() | - | ||||||||||||
189 | { | - | ||||||||||||
190 | clearResources(); | - | ||||||||||||
191 | markContentsDirty(0, characterCount()); | - | ||||||||||||
192 | } executed 346 times by 7 tests: end of block Executed by:
| 346 | ||||||||||||
193 | - | |||||||||||||
194 | QQuickPixmap *QQuickTextDocumentWithImageResources::loadPixmap( | - | ||||||||||||
195 | QQmlContext *context, const QUrl &url) | - | ||||||||||||
196 | { | - | ||||||||||||
197 | - | |||||||||||||
198 | QHash<QUrl, QQuickPixmap *>::Iterator iter = m_resources.find(url); | - | ||||||||||||
199 | - | |||||||||||||
200 | if (iter == m_resources.end()) {
| 10-52 | ||||||||||||
201 | QQuickPixmap *p = new QQuickPixmap(context->engine(), url); | - | ||||||||||||
202 | iter = m_resources.insert(url, p); | - | ||||||||||||
203 | - | |||||||||||||
204 | if (p->isLoading()) {
| 4-6 | ||||||||||||
205 | p->connectFinished(this, SLOT(requestFinished())); | - | ||||||||||||
206 | outstanding++; | - | ||||||||||||
207 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
208 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||
209 | - | |||||||||||||
210 | QQuickPixmap *p = *iter; | - | ||||||||||||
211 | if (p->isError()) {
| 30-32 | ||||||||||||
212 | if (!errors.contains(url)) {
| 6-26 | ||||||||||||
213 | errors.insert(url); | - | ||||||||||||
214 | qmlWarning(parent()) << p->error(); | - | ||||||||||||
215 | } executed 6 times by 2 tests: end of block Executed by:
| 6 | ||||||||||||
216 | } executed 32 times by 2 tests: end of block Executed by:
| 32 | ||||||||||||
217 | return p; executed 62 times by 2 tests: return p; Executed by:
| 62 | ||||||||||||
218 | } | - | ||||||||||||
219 | - | |||||||||||||
220 | void QQuickTextDocumentWithImageResources::clearResources() | - | ||||||||||||
221 | { | - | ||||||||||||
222 | for (QQuickPixmap *pixmap : qAsConst(m_resources)) | - | ||||||||||||
223 | pixmap->clear(this); never executed: pixmap->clear(this); | 0 | ||||||||||||
224 | qDeleteAll(m_resources); | - | ||||||||||||
225 | m_resources.clear(); | - | ||||||||||||
226 | outstanding = 0; | - | ||||||||||||
227 | } executed 1594 times by 7 tests: end of block Executed by:
| 1594 | ||||||||||||
228 | - | |||||||||||||
229 | void QQuickTextDocumentWithImageResources::setText(const QString &text) | - | ||||||||||||
230 | { | - | ||||||||||||
231 | clearResources(); | - | ||||||||||||
232 | - | |||||||||||||
233 | #if QT_CONFIG(texthtmlparser) | - | ||||||||||||
234 | setHtml(text); | - | ||||||||||||
235 | #else | - | ||||||||||||
236 | setPlainText(text); | - | ||||||||||||
237 | #endif | - | ||||||||||||
238 | } executed 196 times by 2 tests: end of block Executed by:
| 196 | ||||||||||||
239 | - | |||||||||||||
240 | QSet<QUrl> QQuickTextDocumentWithImageResources::errors; | - | ||||||||||||
241 | - | |||||||||||||
242 | QT_END_NAMESPACE | - | ||||||||||||
243 | - | |||||||||||||
244 | #include "moc_qquicktextdocument.cpp" | - | ||||||||||||
245 | #include "moc_qquicktextdocument_p.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |