diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-03-28 22:57:03 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-03-28 22:57:03 (GMT) |
commit | a3868307c0e1584a1f694efc4a7b980b54c6e2b1 (patch) | |
tree | 54a19c5b8a357d057a8c9697b8429aed90973f60 | |
parent | 6b619af035a995cf16d977439995cf7c1c0d366e (diff) | |
download | Qt-a3868307c0e1584a1f694efc4a7b980b54c6e2b1.zip Qt-a3868307c0e1584a1f694efc4a7b980b54c6e2b1.tar.gz Qt-a3868307c0e1584a1f694efc4a7b980b54c6e2b1.tar.bz2 |
Source resizing for QDeclarativeImageProvider too.
(fixes test too)
6 files changed, 32 insertions, 16 deletions
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index ad26650..dea5a40 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -580,13 +580,13 @@ void QDeclarativeEngine::removeImageProvider(const QString &providerId) delete d->imageProviders.take(providerId); } -QImage QDeclarativeEnginePrivate::getImageFromProvider(const QUrl &url) +QImage QDeclarativeEnginePrivate::getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size) { QMutexLocker locker(&mutex); QImage image; QDeclarativeImageProvider *provider = imageProviders.value(url.host()); if (provider) - image = provider->request(url.path().mid(1)); + image = provider->request(url.path().mid(1), size, req_size); return image; } diff --git a/src/declarative/qml/qdeclarativeengine_p.h b/src/declarative/qml/qdeclarativeengine_p.h index 06b5027..84bf061 100644 --- a/src/declarative/qml/qdeclarativeengine_p.h +++ b/src/declarative/qml/qdeclarativeengine_p.h @@ -225,7 +225,7 @@ public: mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory; QHash<QString,QDeclarativeImageProvider*> imageProviders; - QImage getImageFromProvider(const QUrl &url); + QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size); mutable QMutex mutex; diff --git a/src/declarative/qml/qdeclarativeimageprovider.cpp b/src/declarative/qml/qdeclarativeimageprovider.cpp index 9ef8545..b992b9f 100644 --- a/src/declarative/qml/qdeclarativeimageprovider.cpp +++ b/src/declarative/qml/qdeclarativeimageprovider.cpp @@ -61,10 +61,14 @@ QDeclarativeImageProvider::~QDeclarativeImageProvider() } /*! - \fn QImage QDeclarativeImageProvider::request(const QString &id) + \fn QImage QDeclarativeImageProvider::request(const QString &id, QSize *size, const QSize& requested_size) Implement this method to return the image with \a id. + If \a requested_size is a valid size, resize the image to that size before returning. + + In any case, \a size must be set to the (original) size of the image. + Note: this method may be called by multiple threads, so ensure the implementation of this method is reentrant. */ diff --git a/src/declarative/qml/qdeclarativeimageprovider.h b/src/declarative/qml/qdeclarativeimageprovider.h index 6ee7bcf..50b73fe 100644 --- a/src/declarative/qml/qdeclarativeimageprovider.h +++ b/src/declarative/qml/qdeclarativeimageprovider.h @@ -54,7 +54,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeImageProvider { public: virtual ~QDeclarativeImageProvider(); - virtual QImage request(const QString &id) = 0; + virtual QImage request(const QString &id, QSize *size, const QSize& requested_size) = 0; }; QT_END_NAMESPACE diff --git a/src/declarative/util/qdeclarativepixmapcache.cpp b/src/declarative/util/qdeclarativepixmapcache.cpp index 54dccce..1d90bf8 100644 --- a/src/declarative/util/qdeclarativepixmapcache.cpp +++ b/src/declarative/util/qdeclarativepixmapcache.cpp @@ -255,7 +255,9 @@ bool QDeclarativeImageRequestHandler::event(QEvent *event) // fetch if (url.scheme() == QLatin1String("image")) { // Use QmlImageProvider - QImage image = QDeclarativeEnginePrivate::get(engine)->getImageFromProvider(url); + QSize read_impsize; + QImage image = QDeclarativeEnginePrivate::get(engine)->getImageFromProvider(url, &read_impsize, QSize(runningJob->forcedWidth(),runningJob->forcedHeight())); + qmlOriginalSizes()->insert(url, read_impsize); QDeclarativeImageReaderEvent::ReadError errorCode = QDeclarativeImageReaderEvent::NoError; QString errorStr; if (image.isNull()) { diff --git a/tests/auto/declarative/qdeclarativeimageprovider/tst_qdeclarativeimageprovider.cpp b/tests/auto/declarative/qdeclarativeimageprovider/tst_qdeclarativeimageprovider.cpp index c5bdfc8..fe5f5a2 100644 --- a/tests/auto/declarative/qdeclarativeimageprovider/tst_qdeclarativeimageprovider.cpp +++ b/tests/auto/declarative/qdeclarativeimageprovider/tst_qdeclarativeimageprovider.cpp @@ -43,6 +43,7 @@ #include <QtDeclarative/qdeclarativeengine.h> #include <QtDeclarative/qdeclarativeimageprovider.h> #include <private/qdeclarativeimage_p.h> +#include <QImageReader> // QDeclarativeImageProvider::request() is run in an idle thread where possible // Be generous in our timeout. @@ -76,28 +77,35 @@ private: class TestProvider : public QDeclarativeImageProvider { public: - QImage request(const QString &id) { - QImage image; - image.load(SRCDIR "/data/" + id); - return image; + QImage request(const QString &id, QSize *size, const QSize& requested_size) { + QImageReader io(SRCDIR "/data/" + id); + if (size) *size = io.size(); + if (requested_size.isValid()) + io.setScaledSize(requested_size); + return io.read(); } }; void tst_qdeclarativeimageprovider::imageSource_data() { QTest::addColumn<QString>("source"); + QTest::addColumn<QString>("properties"); + QTest::addColumn<QSize>("size"); QTest::addColumn<QString>("error"); - QTest::newRow("exists") << "image://test/exists.png" << ""; - QTest::newRow("missing") << "image://test/no-such-file.png" + QTest::newRow("exists") << "image://test/exists.png" << "" << QSize(100,100) << ""; + QTest::newRow("scaled") << "image://test/exists.png" << "sourceSize: \"80x30\"" << QSize(80,30) << ""; + QTest::newRow("missing") << "image://test/no-such-file.png" << "" << QSize() << "\"Failed to get image from provider: image://test/no-such-file.png\" "; - QTest::newRow("unknown provider") << "image://bogus/exists.png" + QTest::newRow("unknown provider") << "image://bogus/exists.png" << "" << QSize() << "\"Failed to get image from provider: image://bogus/exists.png\" "; } void tst_qdeclarativeimageprovider::imageSource() { QFETCH(QString, source); + QFETCH(QString, properties); + QFETCH(QSize, size); QFETCH(QString, error); if (!error.isEmpty()) @@ -106,7 +114,7 @@ void tst_qdeclarativeimageprovider::imageSource() engine.addImageProvider("test", new TestProvider); QVERIFY(engine.imageProvider("test") != 0); - QString componentStr = "import Qt 4.6\nImage { source: \"" + source + "\" }"; + QString componentStr = "import Qt 4.6\nImage { source: \"" + source + "\"; " + properties + " }"; QDeclarativeComponent component(&engine); component.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create()); @@ -118,8 +126,10 @@ void tst_qdeclarativeimageprovider::imageSource() if (error.isEmpty()) { TRY_WAIT(obj->status() == QDeclarativeImage::Ready); - QCOMPARE(obj->width(), 100.); - QCOMPARE(obj->height(), 100.); + QCOMPARE(obj->width(), 100.0); + QCOMPARE(obj->height(), 100.0); + QCOMPARE(obj->pixmap().width(), size.width()); + QCOMPARE(obj->pixmap().height(), size.height()); QCOMPARE(obj->fillMode(), QDeclarativeImage::Stretch); QCOMPARE(obj->progress(), 1.0); } else { |