summaryrefslogtreecommitdiffstats
path: root/src/declarative/util
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-07-08 03:19:22 (GMT)
committerJason McDonald <jason.mcdonald@nokia.com>2010-07-13 04:39:09 (GMT)
commit7c1ff382b0927d8b003468c16e31333816c3ab6b (patch)
treebbab99dca0c96625f7a4194f2ffa03d492572dbf /src/declarative/util
parentca259d99473757190152e81e899aa200355235a5 (diff)
downloadQt-7c1ff382b0927d8b003468c16e31333816c3ab6b.zip
Qt-7c1ff382b0927d8b003468c16e31333816c3ab6b.tar.gz
Qt-7c1ff382b0927d8b003468c16e31333816c3ab6b.tar.bz2
Extend QDeclarativeImageProvider to support QPixmap loading and
synchronous loading of QImages. (QPixmaps can only be created in the main thread so they will always be loaded synchronously). This changes request() to requestImage() and adds requestPixmap() for pixmap support. Task-number: QTBUG-11989 (cherry picked from commit a258456bcb35ec4211751a702ea94a1881d82a07)
Diffstat (limited to 'src/declarative/util')
-rw-r--r--src/declarative/util/qdeclarativepixmapcache.cpp85
1 files changed, 67 insertions, 18 deletions
diff --git a/src/declarative/util/qdeclarativepixmapcache.cpp b/src/declarative/util/qdeclarativepixmapcache.cpp
index fdc2455..3f496b3 100644
--- a/src/declarative/util/qdeclarativepixmapcache.cpp
+++ b/src/declarative/util/qdeclarativepixmapcache.cpp
@@ -736,6 +736,58 @@ void QDeclarativePixmapData::removeFromCache()
}
}
+static QDeclarativePixmapData* createPixmapDataSync(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
+{
+ if (url.scheme() == QLatin1String("image")) {
+ QSize readSize;
+ QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
+ QDeclarativeImageProvider::ImageType imageType = ep->getImageProviderType(url);
+
+ switch (imageType) {
+ case QDeclarativeImageProvider::Image:
+ {
+ QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
+ if (!image.isNull()) {
+ *ok = true;
+ return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
+ }
+ }
+ case QDeclarativeImageProvider::Pixmap:
+ {
+ QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize);
+ if (!pixmap.isNull()) {
+ *ok = true;
+ return new QDeclarativePixmapData(url, pixmap, readSize, requestSize);
+ }
+ }
+ }
+
+ // no matching provider, or provider has bad image type, or provider returned null image
+ return new QDeclarativePixmapData(url, requestSize,
+ QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString()));
+ }
+
+ QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
+ if (localFile.isEmpty())
+ return 0;
+
+ QFile f(localFile);
+ QSize readSize;
+ QString errorString;
+
+ if (f.open(QIODevice::ReadOnly)) {
+ QImage image;
+ if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
+ *ok = true;
+ return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
+ }
+ } else {
+ errorString = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
+ }
+ return new QDeclarativePixmapData(url, requestSize, errorString);
+}
+
+
struct QDeclarativePixmapNull {
QUrl url;
QPixmap pixmap;
@@ -893,27 +945,24 @@ void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, const
QHash<QDeclarativePixmapKey, QDeclarativePixmapData *>::Iterator iter = store->m_cache.find(key);
if (iter == store->m_cache.end()) {
- if (!async) {
- QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
- if (!localFile.isEmpty()) {
- QFile f(localFile);
- QSize readSize;
- QString errorString;
-
- if (f.open(QIODevice::ReadOnly)) {
- QImage image;
- if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
- d = new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
- d->addToCache();
- return;
- }
- } else {
- errorString = tr("Cannot open: %1").arg(url.toString());
- }
+ if (async) {
+ if (url.scheme() == QLatin1String("image")
+ && QDeclarativeEnginePrivate::get(engine)->getImageProviderType(url) == QDeclarativeImageProvider::Pixmap) {
+ qWarning().nospace() << "Pixmaps must be loaded synchronously, ignoring asynchronous property for Image with source: "
+ << url.toString();
+ async = false;
+ }
+ }
- d = new QDeclarativePixmapData(url, requestSize, errorString);
+ if (!async) {
+ bool ok = false;
+ d = createPixmapDataSync(engine, url, requestSize, &ok);
+ if (ok) {
+ d->addToCache();
return;
}
+ if (d) // loadable, but encountered error while loading
+ return;
}
if (!engine)