Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtGui 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 Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | | - |
43 | #include "qtextimagehandler_p.h" | - |
44 | | - |
45 | #include <qcoreapplication.h> | - |
46 | #include <qtextformat.h> | - |
47 | #include <qpainter.h> | - |
48 | #include <qdebug.h> | - |
49 | #include <private/qtextengine_p.h> | - |
50 | #include <qpalette.h> | - |
51 | #include <qthread.h> | - |
52 | | - |
53 | QT_BEGIN_NAMESPACE | - |
54 | | - |
55 | static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format) | - |
56 | { | - |
57 | QPixmap pm; executed (the execution status of this line is deduced): QPixmap pm; | - |
58 | | - |
59 | QString name = format.name(); executed (the execution status of this line is deduced): QString name = format.name(); | - |
60 | if (name.startsWith(QLatin1String(":/"))) // auto-detect resources evaluated: name.startsWith(QLatin1String(":/")) yes Evaluation Count:3 | yes Evaluation Count:10 |
| 3-10 |
61 | name.prepend(QLatin1String("qrc")); executed: name.prepend(QLatin1String("qrc")); Execution Count:3 | 3 |
62 | QUrl url = QUrl(name); executed (the execution status of this line is deduced): QUrl url = QUrl(name); | - |
63 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); executed (the execution status of this line is deduced): const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
64 | if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) { partially evaluated: data.type() == QVariant::Pixmap no Evaluation Count:0 | yes Evaluation Count:13 |
evaluated: data.type() == QVariant::Image yes Evaluation Count:6 | yes Evaluation Count:7 |
| 0-13 |
65 | pm = qvariant_cast<QPixmap>(data); executed (the execution status of this line is deduced): pm = qvariant_cast<QPixmap>(data); | - |
66 | } else if (data.type() == QVariant::ByteArray) { executed: } Execution Count:6 partially evaluated: data.type() == QVariant::ByteArray no Evaluation Count:0 | yes Evaluation Count:7 |
| 0-7 |
67 | pm.loadFromData(data.toByteArray()); never executed (the execution status of this line is deduced): pm.loadFromData(data.toByteArray()); | - |
68 | } | 0 |
69 | | - |
70 | if (pm.isNull()) { evaluated: pm.isNull() yes Evaluation Count:7 | yes Evaluation Count:6 |
| 6-7 |
71 | QString context; executed (the execution status of this line is deduced): QString context; | - |
72 | #if 0 | - |
73 | // ### Qt5 | - |
74 | QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); | - |
75 | if (browser) | - |
76 | context = browser->source().toString(); | - |
77 | #endif | - |
78 | QImage img; executed (the execution status of this line is deduced): QImage img; | - |
79 | if (img.isNull()) { // try direct loading partially evaluated: img.isNull() yes Evaluation Count:7 | no Evaluation Count:0 |
| 0-7 |
80 | name = format.name(); // remove qrc:/ prefix again executed (the execution status of this line is deduced): name = format.name(); | - |
81 | if (name.isEmpty() || !img.load(name)) evaluated: name.isEmpty() yes Evaluation Count:1 | yes Evaluation Count:6 |
partially evaluated: !img.load(name) yes Evaluation Count:6 | no Evaluation Count:0 |
| 0-6 |
82 | return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); executed: return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); Execution Count:7 | 7 |
83 | } | 0 |
84 | pm = QPixmap::fromImage(img); never executed (the execution status of this line is deduced): pm = QPixmap::fromImage(img); | - |
85 | doc->addResource(QTextDocument::ImageResource, url, pm); never executed (the execution status of this line is deduced): doc->addResource(QTextDocument::ImageResource, url, pm); | - |
86 | } | 0 |
87 | | - |
88 | return pm; executed: return pm; Execution Count:6 | 6 |
89 | } | - |
90 | | - |
91 | static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format) | - |
92 | { | - |
93 | QPixmap pm; executed (the execution status of this line is deduced): QPixmap pm; | - |
94 | | - |
95 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); executed (the execution status of this line is deduced): const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
96 | const int width = qRound(format.width()); executed (the execution status of this line is deduced): const int width = qRound(format.width()); | - |
97 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); executed (the execution status of this line is deduced): const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
98 | const int height = qRound(format.height()); executed (the execution status of this line is deduced): const int height = qRound(format.height()); | - |
99 | | - |
100 | QSize size(width, height); executed (the execution status of this line is deduced): QSize size(width, height); | - |
101 | if (!hasWidth || !hasHeight) { evaluated: !hasWidth yes Evaluation Count:11 | yes Evaluation Count:5 |
partially evaluated: !hasHeight no Evaluation Count:0 | yes Evaluation Count:5 |
| 0-11 |
102 | pm = getPixmap(doc, format); executed (the execution status of this line is deduced): pm = getPixmap(doc, format); | - |
103 | if (!hasWidth) { partially evaluated: !hasWidth yes Evaluation Count:11 | no Evaluation Count:0 |
| 0-11 |
104 | if (!hasHeight) partially evaluated: !hasHeight yes Evaluation Count:11 | no Evaluation Count:0 |
| 0-11 |
105 | size.setWidth(pm.width()); executed: size.setWidth(pm.width()); Execution Count:11 | 11 |
106 | else | - |
107 | size.setWidth(qRound(height * (pm.width() / (qreal) pm.height()))); never executed: size.setWidth(qRound(height * (pm.width() / (qreal) pm.height()))); | 0 |
108 | } | - |
109 | if (!hasHeight) { partially evaluated: !hasHeight yes Evaluation Count:11 | no Evaluation Count:0 |
| 0-11 |
110 | if (!hasWidth) partially evaluated: !hasWidth yes Evaluation Count:11 | no Evaluation Count:0 |
| 0-11 |
111 | size.setHeight(pm.height()); executed: size.setHeight(pm.height()); Execution Count:11 | 11 |
112 | else | - |
113 | size.setHeight(qRound(width * (pm.height() / (qreal) pm.width()))); never executed: size.setHeight(qRound(width * (pm.height() / (qreal) pm.width()))); | 0 |
114 | } | - |
115 | } executed: } Execution Count:11 | 11 |
116 | | - |
117 | qreal scale = 1.0; executed (the execution status of this line is deduced): qreal scale = 1.0; | - |
118 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); executed (the execution status of this line is deduced): QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
119 | if (pdev) { evaluated: pdev yes Evaluation Count:6 | yes Evaluation Count:10 |
| 6-10 |
120 | if (pm.isNull()) partially evaluated: pm.isNull() no Evaluation Count:0 | yes Evaluation Count:6 |
| 0-6 |
121 | pm = getPixmap(doc, format); never executed: pm = getPixmap(doc, format); | 0 |
122 | if (!pm.isNull()) partially evaluated: !pm.isNull() yes Evaluation Count:6 | no Evaluation Count:0 |
| 0-6 |
123 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); Execution Count:6 | 6 |
124 | } executed: } Execution Count:6 | 6 |
125 | size *= scale; executed (the execution status of this line is deduced): size *= scale; | - |
126 | | - |
127 | return size; executed: return size; Execution Count:16 | 16 |
128 | } | - |
129 | | - |
130 | static QImage getImage(QTextDocument *doc, const QTextImageFormat &format) | - |
131 | { | - |
132 | QImage image; never executed (the execution status of this line is deduced): QImage image; | - |
133 | | - |
134 | QString name = format.name(); never executed (the execution status of this line is deduced): QString name = format.name(); | - |
135 | if (name.startsWith(QLatin1String(":/"))) // auto-detect resources never evaluated: name.startsWith(QLatin1String(":/")) | 0 |
136 | name.prepend(QLatin1String("qrc")); never executed: name.prepend(QLatin1String("qrc")); | 0 |
137 | QUrl url = QUrl(name); never executed (the execution status of this line is deduced): QUrl url = QUrl(name); | - |
138 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); never executed (the execution status of this line is deduced): const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
139 | if (data.type() == QVariant::Image) { never evaluated: data.type() == QVariant::Image | 0 |
140 | image = qvariant_cast<QImage>(data); never executed (the execution status of this line is deduced): image = qvariant_cast<QImage>(data); | - |
141 | } else if (data.type() == QVariant::ByteArray) { never executed: } never evaluated: data.type() == QVariant::ByteArray | 0 |
142 | image.loadFromData(data.toByteArray()); never executed (the execution status of this line is deduced): image.loadFromData(data.toByteArray()); | - |
143 | } | 0 |
144 | | - |
145 | if (image.isNull()) { never evaluated: image.isNull() | 0 |
146 | QString context; never executed (the execution status of this line is deduced): QString context; | - |
147 | | - |
148 | #if 0 | - |
149 | // ### Qt5 | - |
150 | QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent()); | - |
151 | if (browser) | - |
152 | context = browser->source().toString(); | - |
153 | #endif | - |
154 | if (image.isNull()) { // try direct loading never evaluated: image.isNull() | 0 |
155 | name = format.name(); // remove qrc:/ prefix again never executed (the execution status of this line is deduced): name = format.name(); | - |
156 | if (name.isEmpty() || !image.load(name)) never evaluated: name.isEmpty() never evaluated: !image.load(name) | 0 |
157 | return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); never executed: return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); | 0 |
158 | } | 0 |
159 | doc->addResource(QTextDocument::ImageResource, url, image); never executed (the execution status of this line is deduced): doc->addResource(QTextDocument::ImageResource, url, image); | - |
160 | } | 0 |
161 | | - |
162 | return image; never executed: return image; | 0 |
163 | } | - |
164 | | - |
165 | static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format) | - |
166 | { | - |
167 | QImage image; never executed (the execution status of this line is deduced): QImage image; | - |
168 | | - |
169 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); never executed (the execution status of this line is deduced): const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
170 | const int width = qRound(format.width()); never executed (the execution status of this line is deduced): const int width = qRound(format.width()); | - |
171 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); never executed (the execution status of this line is deduced): const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
172 | const int height = qRound(format.height()); never executed (the execution status of this line is deduced): const int height = qRound(format.height()); | - |
173 | | - |
174 | QSize size(width, height); never executed (the execution status of this line is deduced): QSize size(width, height); | - |
175 | if (!hasWidth || !hasHeight) { never evaluated: !hasWidth never evaluated: !hasHeight | 0 |
176 | image = getImage(doc, format); never executed (the execution status of this line is deduced): image = getImage(doc, format); | - |
177 | if (!hasWidth) never evaluated: !hasWidth | 0 |
178 | size.setWidth(image.width()); never executed: size.setWidth(image.width()); | 0 |
179 | if (!hasHeight) never evaluated: !hasHeight | 0 |
180 | size.setHeight(image.height()); never executed: size.setHeight(image.height()); | 0 |
181 | } | 0 |
182 | | - |
183 | qreal scale = 1.0; never executed (the execution status of this line is deduced): qreal scale = 1.0; | - |
184 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); never executed (the execution status of this line is deduced): QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
185 | if (pdev) { | 0 |
186 | if (image.isNull()) never evaluated: image.isNull() | 0 |
187 | image = getImage(doc, format); never executed: image = getImage(doc, format); | 0 |
188 | if (!image.isNull()) never evaluated: !image.isNull() | 0 |
189 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); never executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | 0 |
190 | } | 0 |
191 | size *= scale; never executed (the execution status of this line is deduced): size *= scale; | - |
192 | | - |
193 | return size; never executed: return size; | 0 |
194 | } | - |
195 | | - |
196 | QTextImageHandler::QTextImageHandler(QObject *parent) | - |
197 | : QObject(parent) | - |
198 | { | - |
199 | } executed: } Execution Count:314 | 314 |
200 | | - |
201 | QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
202 | { | - |
203 | Q_UNUSED(posInDocument) executed (the execution status of this line is deduced): (void)posInDocument; | - |
204 | const QTextImageFormat imageFormat = format.toImageFormat(); executed (the execution status of this line is deduced): const QTextImageFormat imageFormat = format.toImageFormat(); | - |
205 | | - |
206 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) partially evaluated: QCoreApplication::instance()->thread() != QThread::currentThread() no Evaluation Count:0 | yes Evaluation Count:16 |
| 0-16 |
207 | return getImageSize(doc, imageFormat); never executed: return getImageSize(doc, imageFormat); | 0 |
208 | return getPixmapSize(doc, imageFormat); executed: return getPixmapSize(doc, imageFormat); Execution Count:16 | 16 |
209 | } | - |
210 | | - |
211 | QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat) | - |
212 | { | - |
213 | Q_ASSERT(doc != 0); never executed (the execution status of this line is deduced): qt_noop(); | - |
214 | | - |
215 | return getImage(doc, imageFormat); never executed: return getImage(doc, imageFormat); | 0 |
216 | } | - |
217 | | - |
218 | void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
219 | { | - |
220 | Q_UNUSED(posInDocument) executed (the execution status of this line is deduced): (void)posInDocument; | - |
221 | const QTextImageFormat imageFormat = format.toImageFormat(); executed (the execution status of this line is deduced): const QTextImageFormat imageFormat = format.toImageFormat(); | - |
222 | | - |
223 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) { partially evaluated: QCoreApplication::instance()->thread() != QThread::currentThread() no Evaluation Count:0 | yes Evaluation Count:2 |
| 0-2 |
224 | const QImage image = getImage(doc, imageFormat); never executed (the execution status of this line is deduced): const QImage image = getImage(doc, imageFormat); | - |
225 | p->drawImage(rect, image, image.rect()); never executed (the execution status of this line is deduced): p->drawImage(rect, image, image.rect()); | - |
226 | } else { | 0 |
227 | const QPixmap pixmap = getPixmap(doc, imageFormat); executed (the execution status of this line is deduced): const QPixmap pixmap = getPixmap(doc, imageFormat); | - |
228 | p->drawPixmap(rect, pixmap, pixmap.rect()); executed (the execution status of this line is deduced): p->drawPixmap(rect, pixmap, pixmap.rect()); | - |
229 | } executed: } Execution Count:2 | 2 |
230 | } | - |
231 | | - |
232 | QT_END_NAMESPACE | - |
233 | | - |
| | |