on xenial help sign in
add missing test image; re-use QImageReaders more; fix test
997ff13Michael Terry
plugins/ImageCache/ImageCache.cpp+9−10
@@ -17,7 +17,7 @@
17#include <QDateTime>
17#include <QDateTime>
18#include <QDebug>
18#include <QDebug>
19#include <QDir>
19#include <QDir>
20−#include <QImageReader>
20+#include <QUrl>
21#include <QUrlQuery>
21#include <QUrlQuery>
22
22
23#include "ImageCache.h"
23#include "ImageCache.h"
@@ -52,7 +52,7 @@ QFileInfo ImageCache::imagePath(const QUrl &image)
52 return QFileInfo(imageCacheRoot() + name);
52 return QFileInfo(imageCacheRoot() + name);
53}
53}
54
54
55−bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &requestedSize, QSize &finalSize)
55+bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &imageSize, const QSize &requestedSize, QSize &finalSize)
56{
56{
57 if (!cachePath.exists())
57 if (!cachePath.exists())
58 return true;
58 return true;
@@ -62,7 +62,6 @@ bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, cons
62 return true;
62 return true;
63
63
64 QSize cacheSize(QImageReader(cachePath.filePath()).size());
64 QSize cacheSize(QImageReader(cachePath.filePath()).size());
65− QSize imageSize(QImageReader(imageInfo.filePath()).size());
66 finalSize = calculateSize(imageSize, requestedSize);
65 finalSize = calculateSize(imageSize, requestedSize);
67 if (finalSize.isValid() && cacheSize != finalSize)
66 if (finalSize.isValid() && cacheSize != finalSize)
68 return true;
67 return true;
@@ -83,16 +82,15 @@ QSize ImageCache::calculateSize(const QSize &imageSize, const QSize &requestedSi
83 return finalSize;
82 return finalSize;
84}
83}
85
84
86−QImage ImageCache::loadAndCacheImage(const QUrl &image, const QFileInfo &cachePath, const QSize &finalSize)
85+QImage ImageCache::loadAndCacheImage(QImageReader &reader, const QFileInfo &cachePath, const QSize &finalSize)
87{
86{
88− QImageReader reader(image.toLocalFile());
89 reader.setQuality(100);
87 reader.setQuality(100);
90 reader.setScaledSize(finalSize);
88 reader.setScaledSize(finalSize);
91 auto format = reader.format(); // can't get this after reading
89 auto format = reader.format(); // can't get this after reading
92
90
93 QImage loadedImage(reader.read());
91 QImage loadedImage(reader.read());
94 if (loadedImage.isNull()) {
92 if (loadedImage.isNull()) {
95− qWarning() << "ImageCache could not read image" << image.path();
93+ qWarning() << "ImageCache could not read image" << reader.fileName() << ":" << reader.errorString();
96 return QImage();
94 return QImage();
97 }
95 }
98
96
@@ -105,7 +103,8 @@ QImage ImageCache::loadAndCacheImage(const QUrl &image, const QFileInfo &cachePa
105QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
103QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
106{
104{
107 QUrl image(id);
105 QUrl image(id);
108− QSize imageSize(QImageReader(image.toLocalFile()).size());
106+ QImageReader imageReader(image.toLocalFile());
107+ QSize imageSize(imageReader.size());
109 QImage result;
108 QImage result;
110
109
111 // Early exit here, with no sourceSize, scaled-up sourceSize, or bad source image
110 // Early exit here, with no sourceSize, scaled-up sourceSize, or bad source image
@@ -114,7 +113,7 @@ QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &req
114 requestedSize.height() >= imageSize.height() ||
113 requestedSize.height() >= imageSize.height() ||
115 requestedSize.width() >= imageSize.width()) {
114 requestedSize.width() >= imageSize.width()) {
116 // We're only interested in scaling down, not up.
115 // We're only interested in scaling down, not up.
117− result = QImage(image.toLocalFile());
116+ result = imageReader.read();
118 *size = result.size();
117 *size = result.size();
119 return result;
118 return result;
120 }
119 }
@@ -122,11 +121,11 @@ QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &req
122 auto cachePath = imagePath(image);
121 auto cachePath = imagePath(image);
123 QSize finalSize;
122 QSize finalSize;
124
123
125− if (needsUpdate(image, cachePath, requestedSize, finalSize)) {
124+ if (needsUpdate(image, cachePath, imageSize, requestedSize, finalSize)) {
126 if (finalSize.isEmpty()) {
125 if (finalSize.isEmpty()) {
127 finalSize = calculateSize(imageSize, requestedSize);
126 finalSize = calculateSize(imageSize, requestedSize);
128 }
127 }
129− result = loadAndCacheImage(image, cachePath, finalSize);
128+ result = loadAndCacheImage(imageReader, cachePath, finalSize);
130 } else {
129 } else {
131 result = QImage(cachePath.filePath());
130 result = QImage(cachePath.filePath());
132 }
131 }
plugins/ImageCache/ImageCache.h+3−3
@@ -17,9 +17,9 @@
17#pragma once
17#pragma once
18
18
19#include <QFileInfo>
19#include <QFileInfo>
20+#include <QImageReader>
20#include <QQuickImageProvider>
21#include <QQuickImageProvider>
21#include <QSize>
22#include <QSize>
22−#include <QUrl>
23
23
24/**
24/**
25 * This class accepts an id formulated like a URL. So you'd use something like:
25 * This class accepts an id formulated like a URL. So you'd use something like:
@@ -47,7 +47,7 @@ public:
47private:
47private:
48 static QString imageCacheRoot();
48 static QString imageCacheRoot();
49 static QFileInfo imagePath(const QUrl &image);
49 static QFileInfo imagePath(const QUrl &image);
50− static bool needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &requestedSize, QSize &finalSize);
50+ static bool needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &imageSize, const QSize &requestedSize, QSize &finalSize);
51 static QSize calculateSize(const QSize &imageSize, const QSize &requestedSize);
51 static QSize calculateSize(const QSize &imageSize, const QSize &requestedSize);
52− static QImage loadAndCacheImage(const QUrl &image, const QFileInfo &cachePath, const QSize &finalSize);
52+ static QImage loadAndCacheImage(QImageReader &reader, const QFileInfo &cachePath, const QSize &finalSize);
53};
53};
tests/plugins/ImageCache/graphics/wide.jpgnew file

Binary file not shown.

tests/plugins/ImageCache/test.cpp+1−1
@@ -41,7 +41,7 @@ private:
41
41
42 void waitForImage(QQuickImage::Status status = QQuickImage::Ready)
42 void waitForImage(QQuickImage::Status status = QQuickImage::Ready)
43 {
43 {
44− QTRY_COMPARE(image->property("status"), QVariant::fromValue(status));
44+ QTRY_COMPARE(image->property("status"), QVariant((int)status));
45 }
45 }
46
46
47 QSize cachedImageSize(const QString &filename)
47 QSize cachedImageSize(const QString &filename)