summaryrefslogtreecommitdiffstats
path: root/src/declarative/util/qmlpixmapcache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/util/qmlpixmapcache.cpp')
-rw-r--r--src/declarative/util/qmlpixmapcache.cpp68
1 files changed, 42 insertions, 26 deletions
diff --git a/src/declarative/util/qmlpixmapcache.cpp b/src/declarative/util/qmlpixmapcache.cpp
index 6f36cad..8836c06 100644
--- a/src/declarative/util/qmlpixmapcache.cpp
+++ b/src/declarative/util/qmlpixmapcache.cpp
@@ -60,6 +60,13 @@
QT_BEGIN_NAMESPACE
+#if (QT_VERSION < QT_VERSION_CHECK(4, 6, 2))
+static uint qHash(const QUrl &u)
+{
+ return qHash(u.toString());
+}
+#endif
+
class QmlImageReader : public QThread
{
Q_OBJECT
@@ -215,20 +222,20 @@ static QString toLocalFileOrQrc(const QUrl& url)
return r;
}
-typedef QHash<QString, QmlPixmapReply *> QmlPixmapReplyHash;
-static QmlPixmapReplyHash qmlActivePixmapReplies;
+typedef QHash<QUrl, QmlPixmapReply *> QmlPixmapReplyHash;
+Q_GLOBAL_STATIC(QmlPixmapReplyHash, qmlActivePixmapReplies);
class QmlPixmapReplyPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QmlPixmapReply)
public:
- QmlPixmapReplyPrivate(const QString &url, QNetworkReply *r)
- : QObjectPrivate(), refCount(1), urlKey(url), reply(r), status(QmlPixmapReply::Loading) {
+ QmlPixmapReplyPrivate(const QUrl &u, QNetworkReply *r)
+ : QObjectPrivate(), refCount(1), url(u), reply(r), status(QmlPixmapReply::Loading) {
}
int refCount;
- QString urlKey;
+ QUrl url;
QNetworkReply *reply;
QPixmap pixmap; // ensure reference to pixmap so QPixmapCache does not discard
QImage image;
@@ -236,8 +243,8 @@ public:
};
-QmlPixmapReply::QmlPixmapReply(const QString &key, QNetworkReply *reply)
- : QObject(*new QmlPixmapReplyPrivate(key, reply), 0)
+QmlPixmapReply::QmlPixmapReply(const QUrl &url, QNetworkReply *reply)
+ : QObject(*new QmlPixmapReplyPrivate(url, reply), 0)
{
Q_D(QmlPixmapReply);
@@ -265,14 +272,22 @@ QmlPixmapReply::~QmlPixmapReply()
delete d->reply;
}
+const QUrl &QmlPixmapReply::url() const
+{
+ Q_D(const QmlPixmapReply);
+ return d->url;
+}
+
void QmlPixmapReply::networkRequestDone()
{
Q_D(QmlPixmapReply);
if (d->reply->error()) {
d->pixmap = QPixmap();
d->status = Error;
- QPixmapCache::insert(d->urlKey, d->pixmap);
- qWarning() << "Network error loading" << d->urlKey << d->reply->errorString();
+ QByteArray key = d->url.toEncoded(QUrl::FormattingOption(0x100));
+ QString strKey = QString::fromLatin1(key.constData(), key.count());
+ QPixmapCache::insert(strKey, d->pixmap);
+ qWarning() << "Network error loading" << d->url << d->reply->errorString();
emit finished();
} else {
qmlImageReader()->read(this);
@@ -291,9 +306,11 @@ bool QmlPixmapReply::event(QEvent *event)
d->pixmap = QPixmap::fromImage(de->image);
d->image = QImage();
} else {
- qWarning() << "Error decoding" << d->urlKey;
+ qWarning() << "Error decoding" << d->url;
}
- QPixmapCache::insert(d->urlKey, d->pixmap);
+ QByteArray key = d->url.toEncoded(QUrl::FormattingOption(0x100));
+ QString strKey = QString::fromLatin1(key.constData(), key.count());
+ QPixmapCache::insert(strKey, d->pixmap);
emit finished();
}
return true;
@@ -326,7 +343,7 @@ bool QmlPixmapReply::release(bool defer)
Q_ASSERT(d->refCount > 0);
--d->refCount;
if (d->refCount == 0) {
- qmlActivePixmapReplies.remove(d->urlKey);
+ qmlActivePixmapReplies()->remove(d->url);
if (defer)
deleteLater();
else
@@ -373,16 +390,17 @@ QmlPixmapReply::Status QmlPixmapCache::get(const QUrl& url, QPixmap *pixmap)
}
#endif
- QString key = url.toString();
- QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies.find(key);
- if (QPixmapCache::find(key, pixmap)) {
- if (iter != qmlActivePixmapReplies.end()) {
+ QByteArray key = url.toEncoded(QUrl::FormattingOption(0x100));
+ QString strKey = QString::fromLatin1(key.constData(), key.count());
+ QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
+ if (QPixmapCache::find(strKey, pixmap)) {
+ if (iter != qmlActivePixmapReplies()->end()) {
status = (*iter)->status();
(*iter)->release();
} else {
status = pixmap->isNull() ? QmlPixmapReply::Error : QmlPixmapReply::Ready;
}
- } else if (iter != qmlActivePixmapReplies.end()) {
+ } else if (iter != qmlActivePixmapReplies()->end()) {
status = QmlPixmapReply::Loading;
}
@@ -400,13 +418,12 @@ QmlPixmapReply::Status QmlPixmapCache::get(const QUrl& url, QPixmap *pixmap)
*/
QmlPixmapReply *QmlPixmapCache::request(QmlEngine *engine, const QUrl &url)
{
- QString key = url.toString();
- QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies.find(key);
- if (iter == qmlActivePixmapReplies.end()) {
+ QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
+ if (iter == qmlActivePixmapReplies()->end()) {
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
- QmlPixmapReply *item = new QmlPixmapReply(key, engine->networkAccessManager()->get(req));
- iter = qmlActivePixmapReplies.insert(key, item);
+ QmlPixmapReply *item = new QmlPixmapReply(url, engine->networkAccessManager()->get(req));
+ iter = qmlActivePixmapReplies()->insert(url, item);
} else {
(*iter)->addRef();
}
@@ -424,9 +441,8 @@ QmlPixmapReply *QmlPixmapCache::request(QmlEngine *engine, const QUrl &url)
*/
void QmlPixmapCache::cancel(const QUrl& url, QObject *obj)
{
- QString key = url.toString();
- QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies.find(key);
- if (iter == qmlActivePixmapReplies.end())
+ QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
+ if (iter == qmlActivePixmapReplies()->end())
return;
QmlPixmapReply *reply = *iter;
@@ -441,7 +457,7 @@ void QmlPixmapCache::cancel(const QUrl& url, QObject *obj)
*/
int QmlPixmapCache::pendingRequests()
{
- return qmlActivePixmapReplies.count();
+ return qmlActivePixmapReplies()->count();
}
#include <qmlpixmapcache.moc>