From fe59969d239231ae1bfb33e702f58d124554db95 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Fri, 14 Aug 2009 16:04:35 +1000 Subject: Tidy up usage of QNetworkReply returned by QFxPixmapCache::get, update documentation. --- src/declarative/extra/qfxanimatedimageitem_p.h | 4 ++- src/declarative/extra/qfxparticles.cpp | 7 ++-- src/declarative/fx/qfxborderimage.cpp | 26 ++++++-------- src/declarative/fx/qfximagebase.cpp | 46 +++++++++++-------------- src/declarative/fx/qfximagebase_p.h | 1 - src/declarative/fx/qfxpixmapcache.cpp | 47 ++++++++++++++++++-------- 6 files changed, 69 insertions(+), 62 deletions(-) diff --git a/src/declarative/extra/qfxanimatedimageitem_p.h b/src/declarative/extra/qfxanimatedimageitem_p.h index 859f869..56252ca 100644 --- a/src/declarative/extra/qfxanimatedimageitem_p.h +++ b/src/declarative/extra/qfxanimatedimageitem_p.h @@ -58,6 +58,7 @@ QT_BEGIN_NAMESPACE class QMovie; +class QNetworkReply; class QFxAnimatedImageItemPrivate : public QFxImagePrivate { @@ -65,13 +66,14 @@ class QFxAnimatedImageItemPrivate : public QFxImagePrivate public: QFxAnimatedImageItemPrivate() - : playing(true), paused(false), _movie(0) + : playing(true), paused(false), _movie(0), reply(0) { } bool playing; bool paused; QMovie *_movie; + QNetworkReply *reply; }; QT_END_NAMESPACE diff --git a/src/declarative/extra/qfxparticles.cpp b/src/declarative/extra/qfxparticles.cpp index 0e94ea6..2380655 100644 --- a/src/declarative/extra/qfxparticles.cpp +++ b/src/declarative/extra/qfxparticles.cpp @@ -405,7 +405,6 @@ public: bool emitting; QFxParticleMotion *motion; QFxParticlesPainter *paintItem; - QPointer reply; QList particles; QTickAnimationProxy clock; @@ -660,9 +659,9 @@ void QFxParticles::setSource(const QUrl &name) } else { d->url = name; Q_ASSERT(!name.isRelative()); - d->reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->image); - if (d->reply) - connect(d->reply, SIGNAL(finished()), this, SLOT(imageLoaded())); + QNetworkReply *reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->image); + if (reply) + connect(reply, SIGNAL(finished()), this, SLOT(imageLoaded())); else { //### unify with imageLoaded d->paintItem->updateSize(); diff --git a/src/declarative/fx/qfxborderimage.cpp b/src/declarative/fx/qfxborderimage.cpp index 4920bf6..f8c79a6 100644 --- a/src/declarative/fx/qfxborderimage.cpp +++ b/src/declarative/fx/qfxborderimage.cpp @@ -205,10 +205,10 @@ void QFxBorderImage::setSource(const QUrl &url) this, SLOT(sciRequestFinished())); } } else { - d->reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->pix); - if (d->reply) { - connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished())); - connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), + QNetworkReply *reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->pix); + if (reply) { + connect(reply, SIGNAL(finished()), this, SLOT(requestFinished())); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(requestProgress(qint64,qint64))); } else { //### should be unified with requestFinished @@ -315,10 +315,10 @@ void QFxBorderImage::setGridScaledImage(const QFxGridScaledImage& sci) d->verticalTileMode = sci.verticalTileRule(); d->sciurl = d->url.resolved(QUrl(sci.pixmapUrl())); - d->reply = QFxPixmapCache::get(qmlEngine(this), d->sciurl, &d->pix); - if (d->reply) { - connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished())); - connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), + QNetworkReply *reply = QFxPixmapCache::get(qmlEngine(this), d->sciurl, &d->pix); + if (reply) { + connect(reply, SIGNAL(finished()), this, SLOT(requestFinished())); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(requestProgress(qint64,qint64))); } else { //### should be unified with requestFinished @@ -342,14 +342,8 @@ void QFxBorderImage::requestFinished() if (d->url.path().endsWith(QLatin1String(".sci"))) { QFxPixmapCache::find(d->sciurl, &d->pix); } else { - if (d->reply) { - //###disconnect really needed? - disconnect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), - this, SLOT(requestProgress(qint64,qint64))); - if (d->reply->error() != QNetworkReply::NoError) - d->status = Error; - } - QFxPixmapCache::find(d->url, &d->pix); + if (!QFxPixmapCache::find(d->url, &d->pix)) + d->status = Error; } setImplicitWidth(d->pix.width()); setImplicitHeight(d->pix.height()); diff --git a/src/declarative/fx/qfximagebase.cpp b/src/declarative/fx/qfximagebase.cpp index 66685c2..e3760f3 100644 --- a/src/declarative/fx/qfximagebase.cpp +++ b/src/declarative/fx/qfximagebase.cpp @@ -123,25 +123,25 @@ void QFxImageBase::setSource(const QUrl &url) update(); } else { d->status = Loading; - d->reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->pix); - if (d->reply) { - connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished())); - connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), - this, SLOT(requestProgress(qint64,qint64))); - } else { - //### should be unified with requestFinished - setImplicitWidth(d->pix.width()); - setImplicitHeight(d->pix.height()); - - if (d->status == Loading) - d->status = Ready; - d->progress = 1.0; - emit statusChanged(d->status); - emit sourceChanged(d->url); - emit progressChanged(1.0); - update(); - } + QNetworkReply *reply = QFxPixmapCache::get(qmlEngine(this), d->url, &d->pix); + if (reply) { + connect(reply, SIGNAL(finished()), this, SLOT(requestFinished())); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), + this, SLOT(requestProgress(qint64,qint64))); + } else { + //### should be unified with requestFinished + setImplicitWidth(d->pix.width()); + setImplicitHeight(d->pix.height()); + + if (d->status == Loading) + d->status = Ready; + d->progress = 1.0; + emit statusChanged(d->status); + emit sourceChanged(d->url); + emit progressChanged(1.0); + update(); } + } emit statusChanged(d->status); } @@ -149,14 +149,8 @@ void QFxImageBase::setSource(const QUrl &url) void QFxImageBase::requestFinished() { Q_D(QFxImageBase); - if (d->reply) { - //###disconnect really needed? - disconnect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), - this, SLOT(requestProgress(qint64,qint64))); - if (d->reply->error() != QNetworkReply::NoError) - d->status = Error; - } - QFxPixmapCache::find(d->url, &d->pix); + if (!QFxPixmapCache::find(d->url, &d->pix)) + d->status = Error; setImplicitWidth(d->pix.width()); setImplicitHeight(d->pix.height()); diff --git a/src/declarative/fx/qfximagebase_p.h b/src/declarative/fx/qfximagebase_p.h index b468b90..3f43f38 100644 --- a/src/declarative/fx/qfximagebase_p.h +++ b/src/declarative/fx/qfximagebase_p.h @@ -73,7 +73,6 @@ public: QPixmap pix; QFxImageBase::Status status; QUrl url; - QPointer reply; qreal progress; }; diff --git a/src/declarative/fx/qfxpixmapcache.cpp b/src/declarative/fx/qfxpixmapcache.cpp index 5220d15..0ca77c3 100644 --- a/src/declarative/fx/qfxpixmapcache.cpp +++ b/src/declarative/fx/qfxpixmapcache.cpp @@ -122,7 +122,14 @@ static bool readImage(QIODevice *dev, QPixmap *pixmap) This class is NOT reentrant. */ +/*! + Finds the cached pixmap corresponding to \a url. + A previous call to get() must have requested the URL, + and the QNetworkReply must have finished before calling + this function. + Returns true if the image was loaded without error. +*/ bool QFxPixmapCache::find(const QUrl& url, QPixmap *pixmap) { #ifdef Q_ENABLE_PERFORMANCE_LOG @@ -130,6 +137,7 @@ bool QFxPixmapCache::find(const QUrl& url, QPixmap *pixmap) #endif QString key = url.toString(); + bool ok = true; if (!QPixmapCache::find(key,pixmap)) { #ifndef QT_NO_LOCALFILE_OPTIMIZED_QML if (url.scheme()==QLatin1String("file")) { @@ -138,9 +146,12 @@ bool QFxPixmapCache::find(const QUrl& url, QPixmap *pixmap) if (!readImage(&f, pixmap)) { qWarning() << "Format error loading" << url; *pixmap = QPixmap(); + ok = false; } - } else + } else { *pixmap = QPixmap(); + ok = false; + } } else #endif { @@ -148,30 +159,36 @@ bool QFxPixmapCache::find(const QUrl& url, QPixmap *pixmap) if (iter == qfxActiveNetworkReplies.end()) { // API usage error qWarning() << "QFxPixmapCache: URL not loaded" << url; + ok = false; } else { if ((*iter)->reply->error()) { qWarning() << "Network error loading" << url << (*iter)->reply->errorString(); *pixmap = QPixmap(); - } else - if (!readImage((*iter)->reply, pixmap)) { - qWarning() << "Format error loading" << url; - *pixmap = QPixmap(); - } + ok = false; + } else if (!readImage((*iter)->reply, pixmap)) { + qWarning() << "Format error loading" << url; + *pixmap = QPixmap(); + ok = false; + } (*iter)->release(); } } QPixmapCache::insert(key, *pixmap); + } else { + ok = !pixmap->isNull(); } - return true; + return ok; } /*! - Starts a network request to load \a url. When the URL is loaded, - the given slot is invoked. Note that if the image is already cached, - the slot may be invoked immediately. + Starts a network request to load \a url. Returns a QNetworkReply if the image is not immediately available, otherwise - returns 0. The QNetworkReply must not be stored - it may be destroyed at any time. + returns 0. Caller should connect to QNetworkReply::finished() to then call + find() when the image is available. + + The returned QNetworkReply will be deleted when all get() calls are + matched by a corresponding find() call. */ QNetworkReply *QFxPixmapCache::get(QmlEngine *engine, const QUrl& url, QPixmap *pixmap) { @@ -212,10 +229,11 @@ QNetworkReply *QFxPixmapCache::get(QmlEngine *engine, const QUrl& url, QPixmap * } /*! - Stops the given slot being invoked if the given url finishes loading. + Cancels a previous call to get(). + May also cancel loading (eg. if no other pending request). - Any connections to the QNetworkReply returned by get() will be + Any connections from the QNetworkReply returned by get() to \a obj will be disconnected. */ void QFxPixmapCache::cancelGet(const QUrl& url, QObject* obj) @@ -224,7 +242,8 @@ void QFxPixmapCache::cancelGet(const QUrl& url, QObject* obj) QFxSharedNetworkReplyHash::Iterator iter = qfxActiveNetworkReplies.find(key); if (iter == qfxActiveNetworkReplies.end()) return; - QObject::disconnect((*iter)->reply, 0, obj, 0); + if (obj) + QObject::disconnect((*iter)->reply, 0, obj, 0); (*iter)->release(); } -- cgit v0.12