From d0649a1b457f321cfafee1aaf443543fd11a7112 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Mon, 9 Jan 2017 15:22:22 +0100 Subject: Make Item::grabToImage work in more cases, and improve test coverage This fixes a regression caused by 9c50216c7bbbdb2bb51d4485286bf09e12fb5b62 when the Image specified fillMode: aspectRatioCrop/Fit. [ChangeLog][QtQuick][Image] Item::grabToImage on an Image element will now work regardless of the Image's sourceSize or cache properties. Change-Id: I225854c48f0c35a3cb2ef0dd56bf51bd88c31779 Reviewed-by: Robin Burchell --- src/quick/items/qquickitemgrabresult.cpp | 2 +- src/quick/util/qquickpixmapcache.cpp | 12 ++++++++++-- src/quick/util/qquickpixmapcache_p.h | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src/quick') diff --git a/src/quick/items/qquickitemgrabresult.cpp b/src/quick/items/qquickitemgrabresult.cpp index 07c2cb607c..12bcd43076 100644 --- a/src/quick/items/qquickitemgrabresult.cpp +++ b/src/quick/items/qquickitemgrabresult.cpp @@ -75,7 +75,7 @@ public: void ensureImageInCache() const { if (url.isEmpty() && !image.isNull()) { - url.setScheme(QStringLiteral("ItemGrabber")); + url.setScheme(QQuickPixmap::itemGrabberScheme); url.setPath(QVariant::fromValue(item.data()).toString()); static uint counter = 0; url.setFragment(QString::number(++counter)); diff --git a/src/quick/util/qquickpixmapcache.cpp b/src/quick/util/qquickpixmapcache.cpp index 1c6b2afb54..be6d4d18bd 100644 --- a/src/quick/util/qquickpixmapcache.cpp +++ b/src/quick/util/qquickpixmapcache.cpp @@ -84,6 +84,7 @@ QT_BEGIN_NAMESPACE +const QLatin1String QQuickPixmap::itemGrabberScheme = QLatin1String("itemgrabber"); #ifndef QT_NO_DEBUG static const bool qsg_leak_check = !qEnvironmentVariableIsEmpty("QML_LEAK_CHECK"); @@ -1462,8 +1463,15 @@ void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url, const QSize &reques QHash::Iterator iter = store->m_cache.end(); // If Cache is disabled, the pixmap will always be loaded, even if there is an existing - // cached version. - if (options & QQuickPixmap::Cache) + // cached version. Unless it's an itemgrabber url, since the cache is used to pass + // the result between QQuickItemGrabResult and QQuickImage. + if (url.scheme() == itemGrabberScheme) { + QSize dummy; + if (requestSize != dummy) + qWarning() << "Ignoring sourceSize request for image url that came from grabToImage. Use the targetSize parameter of the grabToImage() function instead."; + const QQuickPixmapKey grabberKey = { &url, &dummy, QQuickImageProviderOptions() }; + iter = store->m_cache.find(grabberKey); + } else if (options & QQuickPixmap::Cache) iter = store->m_cache.find(key); if (iter == store->m_cache.end()) { diff --git a/src/quick/util/qquickpixmapcache_p.h b/src/quick/util/qquickpixmapcache_p.h index f7cdfa7d07..a867771755 100644 --- a/src/quick/util/qquickpixmapcache_p.h +++ b/src/quick/util/qquickpixmapcache_p.h @@ -177,6 +177,8 @@ public: static void purgeCache(); static bool isCached(const QUrl &url, const QSize &requestSize); + static const QLatin1String itemGrabberScheme; + private: Q_DISABLE_COPY(QQuickPixmap) QQuickPixmapData *d; -- cgit v1.2.3