Make qt_findAtNxFile return the source device pixel ratio
QTextImageHandler needs it. And QQuickImageBase will also need it. Change-Id: Ica5d17519c4c6c06e25bab7d2b1d3733f4058425 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@theqtcompany.com>
This commit is contained in:
parent
45be405408
commit
d6b45853f5
@ -1378,8 +1378,12 @@ QDebug operator<<(QDebug dbg, const QIcon &i)
|
|||||||
|
|
||||||
Given base foo.png and a target dpr of 2.5, this function will look for
|
Given base foo.png and a target dpr of 2.5, this function will look for
|
||||||
foo@3x.png, then foo@2x, then fall back to foo.png if not found.
|
foo@3x.png, then foo@2x, then fall back to foo.png if not found.
|
||||||
|
|
||||||
|
\a sourceDevicePixelRatio will be set to the value of N if the argument is
|
||||||
|
a non-null pointer
|
||||||
*/
|
*/
|
||||||
QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio)
|
QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
|
||||||
|
qreal *sourceDevicePixelRatio)
|
||||||
{
|
{
|
||||||
if (targetDevicePixelRatio <= 1.0)
|
if (targetDevicePixelRatio <= 1.0)
|
||||||
return baseFileName;
|
return baseFileName;
|
||||||
@ -1397,9 +1401,12 @@ QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRati
|
|||||||
for (int n = qCeil(targetDevicePixelRatio); n > 1; --n) {
|
for (int n = qCeil(targetDevicePixelRatio); n > 1; --n) {
|
||||||
QString atNxfileName = baseFileName;
|
QString atNxfileName = baseFileName;
|
||||||
atNxfileName.insert(dotIndex, atNx.arg(n));
|
atNxfileName.insert(dotIndex, atNx.arg(n));
|
||||||
if (QFile::exists(atNxfileName))
|
if (QFile::exists(atNxfileName)) {
|
||||||
|
if (sourceDevicePixelRatio)
|
||||||
|
*sourceDevicePixelRatio = n;
|
||||||
return atNxfileName;
|
return atNxfileName;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return baseFileName;
|
return baseFileName;
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,8 @@ Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QIcon &);
|
|||||||
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QIcon &);
|
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QIcon &);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Q_GUI_EXPORT QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio);
|
Q_GUI_EXPORT QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
|
||||||
|
qreal *sourceDevicePixelRatio = Q_NULLPTR);
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
@ -44,8 +44,10 @@
|
|||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio);
|
extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
|
||||||
static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio)
|
qreal *sourceDevicePixelRatio);
|
||||||
|
static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio,
|
||||||
|
qreal *sourceDevicePixelRatio)
|
||||||
{
|
{
|
||||||
// We might use the fileName for loading if url loading fails
|
// We might use the fileName for loading if url loading fails
|
||||||
// try to make sure it is a valid file path.
|
// try to make sure it is a valid file path.
|
||||||
@ -64,7 +66,7 @@ static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePi
|
|||||||
return fileName;
|
return fileName;
|
||||||
|
|
||||||
// try to find a Nx version
|
// try to find a Nx version
|
||||||
return qt_findAtNxFile(fileName, targetDevicePixelRatio);
|
return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -76,7 +78,8 @@ static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, con
|
|||||||
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources and convert them to url
|
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources and convert them to url
|
||||||
name.prepend(QLatin1String("qrc"));
|
name.prepend(QLatin1String("qrc"));
|
||||||
QUrl url = QUrl(name);
|
QUrl url = QUrl(name);
|
||||||
name = resolveFileName(name, &url, devicePixelRatio);
|
qreal sourcePixelRatio = 1.0;
|
||||||
|
name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio);
|
||||||
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
|
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
|
||||||
if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
|
if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
|
||||||
pm = qvariant_cast<QPixmap>(data);
|
pm = qvariant_cast<QPixmap>(data);
|
||||||
@ -102,7 +105,7 @@ static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, con
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (name.contains(QStringLiteral("@2x")))
|
if (name.contains(QStringLiteral("@2x")))
|
||||||
pm.setDevicePixelRatio(2.0);
|
pm.setDevicePixelRatio(sourcePixelRatio);
|
||||||
|
|
||||||
return pm;
|
return pm;
|
||||||
}
|
}
|
||||||
@ -157,7 +160,8 @@ static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const
|
|||||||
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
|
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
|
||||||
name.prepend(QLatin1String("qrc"));
|
name.prepend(QLatin1String("qrc"));
|
||||||
QUrl url = QUrl(name);
|
QUrl url = QUrl(name);
|
||||||
name = resolveFileName(name, &url, devicePixelRatio);
|
qreal sourcePixelRatio = 1.0;
|
||||||
|
name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio);
|
||||||
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
|
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
|
||||||
if (data.type() == QVariant::Image) {
|
if (data.type() == QVariant::Image) {
|
||||||
image = qvariant_cast<QImage>(data);
|
image = qvariant_cast<QImage>(data);
|
||||||
@ -181,8 +185,8 @@ static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const
|
|||||||
doc->addResource(QTextDocument::ImageResource, url, image);
|
doc->addResource(QTextDocument::ImageResource, url, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.contains(QStringLiteral("@2x")))
|
if (sourcePixelRatio != 1.0)
|
||||||
image.setDevicePixelRatio(2.0);
|
image.setDevicePixelRatio(sourcePixelRatio);
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user