on xenial help sign in
add missing test image; re-use QImageReaders more; fix test
commit997ff13e7daf419c2f917208dbda21be89e65520 authorMichael Terry <michael.terry@canonical.com> dateTue 30 Aug 2016, 14:05 (10 years ago) parent8e94487
4 files changed+13−14
plugins/ImageCache/ImageCache.cpp+9−10 plugins/ImageCache/ImageCache.h+3−3 tests/plugins/ImageCache/graphics/wide.jpg tests/plugins/ImageCache/test.cpp+1−1
plugins/ImageCache/ImageCache.cpp+9−10
@@ -17,7 +17,7 @@
1717#include <QDateTime>
1818#include <QDebug>
1919#include <QDir>
20−#include <QImageReader>
20+#include <QUrl>
2121#include <QUrlQuery>
2222
2323#include "ImageCache.h"
@@ -52,7 +52,7 @@ QFileInfo ImageCache::imagePath(const QUrl &image)
5252 return QFileInfo(imageCacheRoot() + name);
5353}
5454
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)
5656{
5757 if (!cachePath.exists())
5858 return true;
@@ -62,7 +62,6 @@ bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, cons
6262 return true;
6363
6464 QSize cacheSize(QImageReader(cachePath.filePath()).size());
65− QSize imageSize(QImageReader(imageInfo.filePath()).size());
6665 finalSize = calculateSize(imageSize, requestedSize);
6766 if (finalSize.isValid() && cacheSize != finalSize)
6867 return true;
@@ -83,16 +82,15 @@ QSize ImageCache::calculateSize(const QSize &imageSize, const QSize &requestedSi
8382 return finalSize;
8483}
8584
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)
8786{
88− QImageReader reader(image.toLocalFile());
8987 reader.setQuality(100);
9088 reader.setScaledSize(finalSize);
9189 auto format = reader.format(); // can't get this after reading
9290
9391 QImage loadedImage(reader.read());
9492 if (loadedImage.isNull()) {
95− qWarning() << "ImageCache could not read image" << image.path();
93+ qWarning() << "ImageCache could not read image" << reader.fileName() << ":" << reader.errorString();
9694 return QImage();
9795 }
9896
@@ -105,7 +103,8 @@ QImage ImageCache::loadAndCacheImage(const QUrl &image, const QFileInfo &cachePa
105103QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
106104{
107105 QUrl image(id);
108− QSize imageSize(QImageReader(image.toLocalFile()).size());
106+ QImageReader imageReader(image.toLocalFile());
107+ QSize imageSize(imageReader.size());
109108 QImage result;
110109
111110 // 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
114113 requestedSize.height() >= imageSize.height() ||
115114 requestedSize.width() >= imageSize.width()) {
116115 // We're only interested in scaling down, not up.
117− result = QImage(image.toLocalFile());
116+ result = imageReader.read();
118117 *size = result.size();
119118 return result;
120119 }
@@ -122,11 +121,11 @@ QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &req
122121 auto cachePath = imagePath(image);
123122 QSize finalSize;
124123
125− if (needsUpdate(image, cachePath, requestedSize, finalSize)) {
124+ if (needsUpdate(image, cachePath, imageSize, requestedSize, finalSize)) {
126125 if (finalSize.isEmpty()) {
127126 finalSize = calculateSize(imageSize, requestedSize);
128127 }
129− result = loadAndCacheImage(image, cachePath, finalSize);
128+ result = loadAndCacheImage(imageReader, cachePath, finalSize);
130129 } else {
131130 result = QImage(cachePath.filePath());
132131 }
plugins/ImageCache/ImageCache.h+3−3
@@ -17,9 +17,9 @@
1717#pragma once
1818
1919#include <QFileInfo>
20+#include <QImageReader>
2021#include <QQuickImageProvider>
2122#include <QSize>
22−#include <QUrl>
2323
2424/**
2525 * This class accepts an id formulated like a URL. So you'd use something like:
@@ -47,7 +47,7 @@ public:
4747private:
4848 static QString imageCacheRoot();
4949 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);
5151 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);
5353};
tests/plugins/ImageCache/graphics/wide.jpgnew file

Binary file not shown.

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