Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, | - |
6 | qreal *sourceDevicePixelRatio); | - |
7 | static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio, | - |
8 | qreal *sourceDevicePixelRatio) | - |
9 | { | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | if (url->isValid()) { | - |
15 | if (url->scheme() == QLatin1Literal("qrc")) { | - |
16 | fileName = fileName.right(fileName.length() - 3); | - |
17 | } | - |
18 | else if (url->scheme() == QLatin1Literal("file")) { | - |
19 | fileName = url->toLocalFile(); | - |
20 | } | - |
21 | } | - |
22 | | - |
23 | if (targetDevicePixelRatio <= 1.0) | - |
24 | return fileName; | - |
25 | | - |
26 | | - |
27 | return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio); | - |
28 | } | - |
29 | | - |
30 | | - |
31 | static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0) | - |
32 | { | - |
33 | QPixmap pm; | - |
34 | | - |
35 | QString name = format.name(); | - |
36 | if (name.startsWith(QLatin1String(":/"))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
37 | name.prepend(QLatin1String("qrc")); never executed: name.prepend(QLatin1String("qrc")); | 0 |
38 | QUrl url = QUrl(name); | - |
39 | qreal sourcePixelRatio = 1.0; | - |
40 | name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio); | - |
41 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
42 | if (data.type() == QVariant::PixmapTRUE | never evaluated | FALSE | never evaluated |
|| data.type() == QVariant::ImageTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
43 | pm = qvariant_cast<QPixmap>(data); | - |
44 | } never executed: end of block else if (data.type() == QVariant::ByteArrayTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
45 | pm.loadFromData(data.toByteArray()); | - |
46 | } never executed: end of block | 0 |
47 | | - |
48 | if (pm.isNull()TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
49 | QImage img; | - |
50 | if (name.isEmpty()TRUE | never evaluated | FALSE | never evaluated |
|| !img.load(name)TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
51 | return never executed: return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));never executed: return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); | 0 |
52 | | - |
53 | pm = QPixmap::fromImage(img); | - |
54 | doc->addResource(QTextDocument::ImageResource, url, pm); | - |
55 | } never executed: end of block | 0 |
56 | | - |
57 | if (name.contains(([]() -> QString { enum { Size = sizeof(u"" "@2x")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeofQLatin1String(QStringData) }, u"" "@2x"}; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())))))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
58 | pm.setDevicePixelRatio(sourcePixelRatio); never executed: pm.setDevicePixelRatio(sourcePixelRatio); | 0 |
59 | | - |
60 | return never executed: return pm; pm;never executed: return pm; | 0 |
61 | } | - |
62 | | - |
63 | static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format) | - |
64 | { | - |
65 | QPixmap pm; | - |
66 | | - |
67 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
68 | const int width = qRound(format.width()); | - |
69 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
70 | const int height = qRound(format.height()); | - |
71 | | - |
72 | QSize size(width, height); | - |
73 | if (!hasWidth || !hasHeight) { | - |
74 | pm = getPixmap(doc, format); | - |
75 | const int pmWidth = pm.width() / pm.devicePixelRatio(); | - |
76 | const int pmHeight = pm.height() / pm.devicePixelRatio(); | - |
77 | | - |
78 | if (!hasWidth) { | - |
79 | if (!hasHeight) | - |
80 | size.setWidth(pmWidth); | - |
81 | else | - |
82 | size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight))); | - |
83 | } | - |
84 | if (!hasHeight) { | - |
85 | if (!hasWidth) | - |
86 | size.setHeight(pmHeight); | - |
87 | else | - |
88 | size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth))); | - |
89 | } | - |
90 | } | - |
91 | | - |
92 | qreal scale = 1.0; | - |
93 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
94 | if (pdev) { | - |
95 | if (pm.isNull()) | - |
96 | pm = getPixmap(doc, format); | - |
97 | if (!pm.isNull()) | - |
98 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | - |
99 | } | - |
100 | size *= scale; | - |
101 | | - |
102 | return size; | - |
103 | } | - |
104 | | - |
105 | static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0) | - |
106 | { | - |
107 | QImage image; | - |
108 | | - |
109 | QString name = format.name(); | - |
110 | if (name.startsWith(QLatin1String(":/"))) | - |
111 | name.prepend(QLatin1String("qrc")); | - |
112 | QUrl url = QUrl(name); | - |
113 | qreal sourcePixelRatio = 1.0; | - |
114 | name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio); | - |
115 | const QVariant data = doc->resource(QTextDocument::ImageResource, url); | - |
116 | if (data.type() == QVariant::Image) { | - |
117 | image = qvariant_cast<QImage>(data); | - |
118 | } else if (data.type() == QVariant::ByteArray) { | - |
119 | image.loadFromData(data.toByteArray()); | - |
120 | } | - |
121 | | - |
122 | if (image.isNull()) { | - |
123 | if (name.isEmpty() || !image.load(name)) | - |
124 | return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png")); | - |
125 | | - |
126 | doc->addResource(QTextDocument::ImageResource, url, image); | - |
127 | } | - |
128 | | - |
129 | if (sourcePixelRatio != 1.0) | - |
130 | image.setDevicePixelRatio(sourcePixelRatio); | - |
131 | | - |
132 | return image; | - |
133 | } | - |
134 | | - |
135 | static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format) | - |
136 | { | - |
137 | QImage image; | - |
138 | | - |
139 | const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); | - |
140 | const int width = qRound(format.width()); | - |
141 | const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); | - |
142 | const int height = qRound(format.height()); | - |
143 | | - |
144 | QSize size(width, height); | - |
145 | if (!hasWidth || !hasHeight) { | - |
146 | image = getImage(doc, format); | - |
147 | if (!hasWidth) | - |
148 | size.setWidth(image.width() / image.devicePixelRatio()); | - |
149 | if (!hasHeight) | - |
150 | size.setHeight(image.height() / image.devicePixelRatio()); | - |
151 | } | - |
152 | | - |
153 | qreal scale = 1.0; | - |
154 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - |
155 | if (pdev) { | - |
156 | if (image.isNull()) | - |
157 | image = getImage(doc, format); | - |
158 | if (!image.isNull()) | - |
159 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | - |
160 | } | - |
161 | size *= scale; | - |
162 | | - |
163 | return size; | - |
164 | } | - |
165 | | - |
166 | QTextImageHandler::QTextImageHandler(QObject *parent) | - |
167 | : QObject(parent) | - |
168 | { | - |
169 | } | - |
170 | | - |
171 | QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
172 | { | - |
173 | (void)posInDocument; | - |
174 | const QTextImageFormat imageFormat = format.toImageFormat(); | - |
175 | | - |
176 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) | - |
177 | return getImageSize(doc, imageFormat); | - |
178 | return getPixmapSize(doc, imageFormat); | - |
179 | } | - |
180 | | - |
181 | QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat) | - |
182 | { | - |
183 | ((!(doc != 0)) ? qt_assert("doc != 0",__FILE__,242249) : qt_noop()); | - |
184 | | - |
185 | return getImage(doc, imageFormat); | - |
186 | } | - |
187 | | - |
188 | void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) | - |
189 | { | - |
190 | (void)posInDocument; | - |
191 | const QTextImageFormat imageFormat = format.toImageFormat(); | - |
192 | | - |
193 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) { | - |
194 | const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatioF()); | - |
195 | p->drawImage(rect, image, image.rect()); | - |
196 | } else { | - |
197 | const QPixmap pixmap = getPixmap(doc, imageFormat, p->device()->devicePixelRatioF()); | - |
198 | p->drawPixmap(rect, pixmap, pixmap.rect()); | - |
199 | } | - |
200 | } | - |
201 | | - |
202 | | - |
| | |