| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qtextimagehandler.cpp |
| Switch to Source code | Preprocessed file |
| 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()
| 0 | ||||||||||||
| 15 | if (url->scheme() == QLatin1Literal("qrc")
| 0 | ||||||||||||
| 16 | fileName = fileName.right(fileName.length() - 3); | - | ||||||||||||
| 17 | } never executed: end of block | 0 | ||||||||||||
| 18 | else if (url->scheme() == QLatin1Literal("file")
| 0 | ||||||||||||
| 19 | fileName = url->toLocalFile(); | - | ||||||||||||
| 20 | } never executed: end of block | 0 | ||||||||||||
| 21 | } never executed: end of block | 0 | ||||||||||||
| 22 | - | |||||||||||||
| 23 | if (targetDevicePixelRatio <= 1.0
| 0 | ||||||||||||
| 24 | return never executed: fileName;return fileName;never executed: return fileName; | 0 | ||||||||||||
| 25 | - | |||||||||||||
| 26 | - | |||||||||||||
| 27 | return never executed: qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);never executed: return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio); | 0 | ||||||||||||
| 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(":/"))
| 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::Pixmap
| 0 | ||||||||||||
| 43 | pm = qvariant_cast<QPixmap>(data); | - | ||||||||||||
| 44 | } never executed: else if (data.type() == QVariant::ByteArrayend of block
| 0 | ||||||||||||
| 45 | pm.loadFromData(data.toByteArray()); | - | ||||||||||||
| 46 | } never executed: end of block | 0 | ||||||||||||
| 47 | - | |||||||||||||
| 48 | if (pm.isNull()
| 0 | ||||||||||||
| 49 | QImage img; | - | ||||||||||||
| 50 | if (name.isEmpty()
| 0 | ||||||||||||
| 51 | return never executed: QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));return 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(QLatin1String("@2x"))
| 0 | ||||||||||||
| 58 | pm.setDevicePixelRatio(sourcePixelRatio); never executed: pm.setDevicePixelRatio(sourcePixelRatio); | 0 | ||||||||||||
| 59 | - | |||||||||||||
| 60 | return never executed: pm;return 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
| 0 | ||||||||||||
| 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
| 0 | ||||||||||||
| 79 | if (!hasHeight
| 0 | ||||||||||||
| 80 | size.setWidth(pmWidth); never executed: size.setWidth(pmWidth); | 0 | ||||||||||||
| 81 | else | - | ||||||||||||
| 82 | size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight))); never executed: size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight))); | 0 | ||||||||||||
| 83 | } | - | ||||||||||||
| 84 | if (!hasHeight
| 0 | ||||||||||||
| 85 | if (!hasWidth
| 0 | ||||||||||||
| 86 | size.setHeight(pmHeight); never executed: size.setHeight(pmHeight); | 0 | ||||||||||||
| 87 | else | - | ||||||||||||
| 88 | size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth))); never executed: size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth))); | 0 | ||||||||||||
| 89 | } | - | ||||||||||||
| 90 | } never executed: end of block | 0 | ||||||||||||
| 91 | - | |||||||||||||
| 92 | qreal scale = 1.0; | - | ||||||||||||
| 93 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - | ||||||||||||
| 94 | if (pdev
| 0 | ||||||||||||
| 95 | if (pm.isNull()
| 0 | ||||||||||||
| 96 | pm = getPixmap(doc, format); never executed: pm = getPixmap(doc, format); | 0 | ||||||||||||
| 97 | if (!pm.isNull()
| 0 | ||||||||||||
| 98 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); never executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | 0 | ||||||||||||
| 99 | } never executed: end of block | 0 | ||||||||||||
| 100 | size *= scale; | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | return never executed: size;return size;never executed: return size; | 0 | ||||||||||||
| 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(":/"))
| 0 | ||||||||||||
| 111 | name.prepend(QLatin1String("qrc")); never executed: name.prepend(QLatin1String("qrc")); | 0 | ||||||||||||
| 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
| 0 | ||||||||||||
| 117 | image = qvariant_cast<QImage>(data); | - | ||||||||||||
| 118 | } never executed: else if (data.type() == QVariant::ByteArrayend of block
| 0 | ||||||||||||
| 119 | image.loadFromData(data.toByteArray()); | - | ||||||||||||
| 120 | } never executed: end of block | 0 | ||||||||||||
| 121 | - | |||||||||||||
| 122 | if (image.isNull()
| 0 | ||||||||||||
| 123 | if (name.isEmpty()
| 0 | ||||||||||||
| 124 | return never executed: QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));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 | ||||||||||||
| 125 | - | |||||||||||||
| 126 | doc->addResource(QTextDocument::ImageResource, url, image); | - | ||||||||||||
| 127 | } never executed: end of block | 0 | ||||||||||||
| 128 | - | |||||||||||||
| 129 | if (sourcePixelRatio != 1.0
| 0 | ||||||||||||
| 130 | image.setDevicePixelRatio(sourcePixelRatio); never executed: image.setDevicePixelRatio(sourcePixelRatio); | 0 | ||||||||||||
| 131 | - | |||||||||||||
| 132 | return never executed: image;return image;never executed: return image; | 0 | ||||||||||||
| 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
| 0 | ||||||||||||
| 146 | image = getImage(doc, format); | - | ||||||||||||
| 147 | if (!hasWidth
| 0 | ||||||||||||
| 148 | size.setWidth(image.width() / image.devicePixelRatio()); never executed: size.setWidth(image.width() / image.devicePixelRatio()); | 0 | ||||||||||||
| 149 | if (!hasHeight
| 0 | ||||||||||||
| 150 | size.setHeight(image.height() / image.devicePixelRatio()); never executed: size.setHeight(image.height() / image.devicePixelRatio()); | 0 | ||||||||||||
| 151 | } never executed: end of block | 0 | ||||||||||||
| 152 | - | |||||||||||||
| 153 | qreal scale = 1.0; | - | ||||||||||||
| 154 | QPaintDevice *pdev = doc->documentLayout()->paintDevice(); | - | ||||||||||||
| 155 | if (pdev
| 0 | ||||||||||||
| 156 | if (image.isNull()
| 0 | ||||||||||||
| 157 | image = getImage(doc, format); never executed: image = getImage(doc, format); | 0 | ||||||||||||
| 158 | if (!image.isNull()
| 0 | ||||||||||||
| 159 | scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); never executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi()); | 0 | ||||||||||||
| 160 | } never executed: end of block | 0 | ||||||||||||
| 161 | size *= scale; | - | ||||||||||||
| 162 | - | |||||||||||||
| 163 | return never executed: size;return size;never executed: return size; | 0 | ||||||||||||
| 164 | } | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | QTextImageHandler::QTextImageHandler(QObject *parent) | - | ||||||||||||
| 167 | : QObject(parent) | - | ||||||||||||
| 168 | { | - | ||||||||||||
| 169 | } never executed: end of block | 0 | ||||||||||||
| 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()
| 0 | ||||||||||||
| 177 | return never executed: getImageSize(doc, imageFormat);return getImageSize(doc, imageFormat);never executed: return getImageSize(doc, imageFormat); | 0 | ||||||||||||
| 178 | return never executed: getPixmapSize(doc, imageFormat);return getPixmapSize(doc, imageFormat);never executed: return getPixmapSize(doc, imageFormat); | 0 | ||||||||||||
| 179 | } | - | ||||||||||||
| 180 | - | |||||||||||||
| 181 | QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat) | - | ||||||||||||
| 182 | { | - | ||||||||||||
| 183 | ((!(doc != 0)) ? qt_assert("doc != 0",__FILE__,249) : qt_noop()); | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | return never executed: getImage(doc, imageFormat);return getImage(doc, imageFormat);never executed: return getImage(doc, imageFormat); | 0 | ||||||||||||
| 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()
| 0 | ||||||||||||
| 194 | const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatioF()); | - | ||||||||||||
| 195 | p->drawImage(rect, image, image.rect()); | - | ||||||||||||
| 196 | } never executed: else {end of block | 0 | ||||||||||||
| 197 | const QPixmap pixmap = getPixmap(doc, imageFormat, p->device()->devicePixelRatioF()); | - | ||||||||||||
| 198 | p->drawPixmap(rect, pixmap, pixmap.rect()); | - | ||||||||||||
| 199 | } never executed: end of block | 0 | ||||||||||||
| 200 | } | - | ||||||||||||
| 201 | - | |||||||||||||
| 202 | - | |||||||||||||
| Switch to Source code | Preprocessed file |