From dca1f953264a373cbbe42adccb93fea84b96af51 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Thu, 16 Feb 2017 12:38:51 +0100 Subject: QQuickImageBase: allow derived classes to affect devicePixelRatio usage QQuickImageBase currently sets its d->devicePixelRatio member in three scenarios: - The URL uses the image: scheme (an image provider) - The URL is an svg/svgz - The URL contains the "@2x" syntax In all other cases, it sets d->devicePixelRatio to 1.0. QQuickIconImage derives from QQuickImageBase, and can display theme icons in addition to regular images. Theme icons have their own directory structure that determines what is and is not a high DPI image: https://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html In particular, they use e.g. a 22x22@2 directory to indicate that the icons contained within it are high DPI images, as opposed to storing that information in the file name. To account for these images, QQuickIconImage must be able to affect the decision making progress that determines whether or not an image is loaded at a higher DPI. This patch adds a virtual function to QQuickImageBasePrivate that allows subclasses to have some control over this. Change-Id: Id45e0580c8e2ea9a96a41c54ef5fa765ce5922cf Reviewed-by: J-P Nurmi Reviewed-by: Robin Burchell --- src/quick/items/qquickimagebase.cpp | 47 ++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'src/quick/items/qquickimagebase.cpp') diff --git a/src/quick/items/qquickimagebase.cpp b/src/quick/items/qquickimagebase.cpp index 22d631e917..33d69f5032 100644 --- a/src/quick/items/qquickimagebase.cpp +++ b/src/quick/items/qquickimagebase.cpp @@ -49,6 +49,30 @@ QT_BEGIN_NAMESPACE +// This function gives derived classes the chance set the devicePixelRatio +// if they're not happy with our implementation of it. +bool QQuickImageBasePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio) +{ + // QQuickImageProvider and SVG can generate a high resolution image when + // sourceSize is set (this function is only called if it's set). + // If sourceSize is not set then the provider default size will be used, as usual. + bool setDevicePixelRatio = false; + if (url.scheme() == QLatin1String("image")) { + setDevicePixelRatio = true; + } else { + QString stringUrl = url.path(QUrl::PrettyDecoded); + if (stringUrl.endsWith(QLatin1String("svg")) || + stringUrl.endsWith(QLatin1String("svgz"))) { + setDevicePixelRatio = true; + } + } + + if (setDevicePixelRatio) + devicePixelRatio = targetDevicePixelRatio; + + return setDevicePixelRatio; +} + QQuickImageBase::QQuickImageBase(QQuickItem *parent) : QQuickImplicitSizeItem(*(new QQuickImageBasePrivate), parent) { @@ -221,26 +245,11 @@ void QQuickImageBase::load() QUrl loadUrl = d->url; - // QQuickImageProvider and SVG can generate a high resolution image when - // sourceSize is set. If sourceSize is not set then the provider default size - // will be used, as usual. - bool setDevicePixelRatio = false; - if (d->sourcesize.isValid()) { - if (loadUrl.scheme() == QLatin1String("image")) { - setDevicePixelRatio = true; - } else { - QString stringUrl = loadUrl.path(QUrl::PrettyDecoded); - if (stringUrl.endsWith(QLatin1String("svg")) || - stringUrl.endsWith(QLatin1String("svgz"))) { - setDevicePixelRatio = true; - } - } - - if (setDevicePixelRatio) - d->devicePixelRatio = targetDevicePixelRatio; - } + bool updatedDevicePixelRatio = false; + if (d->sourcesize.isValid()) + updatedDevicePixelRatio = d->updateDevicePixelRatio(targetDevicePixelRatio); - if (!setDevicePixelRatio) { + if (!updatedDevicePixelRatio) { // (possible) local file: loadUrl and d->devicePixelRatio will be modified if // an "@2x" file is found. resolve2xLocalFile(d->url, targetDevicePixelRatio, &loadUrl, &d->devicePixelRatio); -- cgit v1.2.3