1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qdeclarativepixmapcache_p.h"
43 #include "qdeclarativenetworkaccessmanagerfactory.h"
44 #include "qdeclarativeimageprovider.h"
46 #include <qdeclarativeengine.h>
47 #include <private/qdeclarativeglobal_p.h>
48 #include <private/qdeclarativeengine_p.h>
50 #include <QCoreApplication>
51 #include <QImageReader>
53 #include <QNetworkReply>
54 #include <QPixmapCache>
58 #include <QMutexLocker>
59 #include <QWaitCondition>
61 #include <QWaitCondition>
62 #include <QtCore/qdebug.h>
63 #include <private/qobject_p.h>
66 #define IMAGEREQUEST_MAX_REQUEST_COUNT 8
67 #define IMAGEREQUEST_MAX_REDIRECT_RECURSION 16
68 #define CACHE_EXPIRE_TIME 30
69 #define CACHE_REMOVAL_FRACTION 4
73 // The cache limit describes the maximum "junk" in the cache.
74 // These are the same defaults as QPixmapCache
75 #if defined(Q_WS_QWS) || defined(Q_WS_WINCE)
76 static int cache_limit = 2048 * 1024; // 2048 KB cache limit for embedded
78 static int cache_limit = 10240 * 1024; // 10 MB cache limit for desktop
81 class QDeclarativePixmapReader;
82 class QDeclarativePixmapData;
83 class QDeclarativePixmapReply : public QObject
87 enum ReadError { NoError, Loading, Decoding };
89 QDeclarativePixmapReply(QDeclarativePixmapData *);
90 ~QDeclarativePixmapReply();
92 QDeclarativePixmapData *data;
93 QDeclarativePixmapReader *reader;
99 class Event : public QEvent {
101 Event(ReadError, const QString &, const QSize &, const QImage &);
108 void postReply(ReadError, const QString &, const QSize &, const QImage &);
113 void downloadProgress(qint64, qint64);
116 bool event(QEvent *event);
119 Q_DISABLE_COPY(QDeclarativePixmapReply)
122 static int finishedIndex;
123 static int downloadProgressIndex;
126 class QDeclarativePixmapReaderThreadObject : public QObject {
129 QDeclarativePixmapReaderThreadObject(QDeclarativePixmapReader *);
131 virtual bool event(QEvent *e);
133 void networkRequestDone();
135 QDeclarativePixmapReader *reader;
138 class QDeclarativePixmapData;
139 class QDeclarativePixmapReader : public QThread
143 QDeclarativePixmapReader(QDeclarativeEngine *eng);
144 ~QDeclarativePixmapReader();
146 QDeclarativePixmapReply *getImage(QDeclarativePixmapData *);
147 void cancel(QDeclarativePixmapReply *rep);
149 static QDeclarativePixmapReader *instance(QDeclarativeEngine *engine);
155 friend class QDeclarativePixmapReaderThreadObject;
157 void processJob(QDeclarativePixmapReply *, const QUrl &, const QSize &);
158 void networkRequestDone(QNetworkReply *);
160 QList<QDeclarativePixmapReply*> jobs;
161 QList<QDeclarativePixmapReply*> cancelled;
162 QDeclarativeEngine *engine;
163 QObject *eventLoopQuitHack;
166 QDeclarativePixmapReaderThreadObject *threadObject;
167 QWaitCondition waitCondition;
169 QNetworkAccessManager *networkAccessManager();
170 QNetworkAccessManager *accessManager;
172 QHash<QNetworkReply*,QDeclarativePixmapReply*> replies;
174 static int replyDownloadProgress;
175 static int replyFinished;
176 static int downloadProgress;
177 static int threadNetworkRequestDone;
178 static QHash<QDeclarativeEngine *,QDeclarativePixmapReader*> readers;
179 static QMutex readerMutex;
182 class QDeclarativePixmapData
185 QDeclarativePixmapData(const QUrl &u, const QSize &s, const QString &e)
186 : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Error),
187 url(u), errorString(e), requestSize(s), reply(0), prevUnreferenced(0),
188 prevUnreferencedPtr(0), nextUnreferenced(0)
192 QDeclarativePixmapData(const QUrl &u, const QSize &r)
193 : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Loading),
194 url(u), requestSize(r), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0),
199 QDeclarativePixmapData(const QUrl &u, const QPixmap &p, const QSize &s, const QSize &r)
200 : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QDeclarativePixmap::Ready),
201 url(u), pixmap(p), implicitSize(s), requestSize(r), reply(0), prevUnreferenced(0),
202 prevUnreferencedPtr(0), nextUnreferenced(0)
206 QDeclarativePixmapData(const QPixmap &p)
207 : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(QDeclarativePixmap::Ready),
208 pixmap(p), implicitSize(p.size()), requestSize(p.size()), reply(0), prevUnreferenced(0),
209 prevUnreferencedPtr(0), nextUnreferenced(0)
217 void removeFromCache();
222 bool privatePixmap:1;
224 QDeclarativePixmap::Status pixmapStatus;
231 QDeclarativePixmapReply *reply;
233 QDeclarativePixmapData *prevUnreferenced;
234 QDeclarativePixmapData**prevUnreferencedPtr;
235 QDeclarativePixmapData *nextUnreferenced;
238 int QDeclarativePixmapReply::finishedIndex = -1;
239 int QDeclarativePixmapReply::downloadProgressIndex = -1;
242 QHash<QDeclarativeEngine *,QDeclarativePixmapReader*> QDeclarativePixmapReader::readers;
243 QMutex QDeclarativePixmapReader::readerMutex;
245 int QDeclarativePixmapReader::replyDownloadProgress = -1;
246 int QDeclarativePixmapReader::replyFinished = -1;
247 int QDeclarativePixmapReader::downloadProgress = -1;
248 int QDeclarativePixmapReader::threadNetworkRequestDone = -1;
251 void QDeclarativePixmapReply::postReply(ReadError error, const QString &errorString,
252 const QSize &implicitSize, const QImage &image)
255 QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, image));
258 QDeclarativePixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, const QImage &i)
259 : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i)
263 QNetworkAccessManager *QDeclarativePixmapReader::networkAccessManager()
265 if (!accessManager) {
266 Q_ASSERT(threadObject);
267 accessManager = QDeclarativeEnginePrivate::get(engine)->createNetworkAccessManager(threadObject);
269 return accessManager;
272 static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize,
273 const QSize &requestSize)
275 QImageReader imgio(dev);
277 bool force_scale = false;
278 if (url.path().endsWith(QLatin1String(".svg"),Qt::CaseInsensitive)) {
279 imgio.setFormat("svg"); // QSvgPlugin::capabilities bug QTBUG-9053
284 if (requestSize.width() > 0 || requestSize.height() > 0) {
285 QSize s = imgio.size();
286 if (requestSize.width() && (force_scale || requestSize.width() < s.width())) {
287 if (requestSize.height() <= 0)
288 s.setHeight(s.height()*requestSize.width()/s.width());
289 s.setWidth(requestSize.width()); scaled = true;
291 if (requestSize.height() && (force_scale || requestSize.height() < s.height())) {
292 if (requestSize.width() <= 0)
293 s.setWidth(s.width()*requestSize.height()/s.height());
294 s.setHeight(requestSize.height()); scaled = true;
296 if (scaled) { imgio.setScaledSize(s); }
300 *impsize = imgio.size();
302 if (imgio.read(image)) {
303 if (impsize && impsize->width() < 0)
304 *impsize = image->size();
308 *errorString = QDeclarativePixmap::tr("Error decoding: %1: %2").arg(url.toString())
309 .arg(imgio.errorString());
314 QDeclarativePixmapReader::QDeclarativePixmapReader(QDeclarativeEngine *eng)
315 : QThread(eng), engine(eng), threadObject(0), accessManager(0)
317 eventLoopQuitHack = new QObject;
318 eventLoopQuitHack->moveToThread(this);
319 connect(eventLoopQuitHack, SIGNAL(destroyed(QObject*)), SLOT(quit()), Qt::DirectConnection);
320 start(QThread::IdlePriority);
323 QDeclarativePixmapReader::~QDeclarativePixmapReader()
326 readers.remove(engine);
327 readerMutex.unlock();
329 eventLoopQuitHack->deleteLater();
333 void QDeclarativePixmapReader::networkRequestDone(QNetworkReply *reply)
335 QDeclarativePixmapReply *job = replies.take(reply);
338 job->redirectCount++;
339 if (job->redirectCount < IMAGEREQUEST_MAX_REDIRECT_RECURSION) {
340 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
341 if (redirect.isValid()) {
342 QUrl url = reply->url().resolved(redirect.toUrl());
343 QNetworkRequest req(url);
344 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
346 reply->deleteLater();
347 reply = networkAccessManager()->get(req);
349 QMetaObject::connect(reply, replyDownloadProgress, job, downloadProgress);
350 QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);
352 replies.insert(reply, job);
358 QDeclarativePixmapReply::ReadError error = QDeclarativePixmapReply::NoError;
361 if (reply->error()) {
362 error = QDeclarativePixmapReply::Loading;
363 errorString = reply->errorString();
365 QByteArray all = reply->readAll();
367 buff.open(QIODevice::ReadOnly);
368 if (!readImage(reply->url(), &buff, &image, &errorString, &readSize, job->requestSize)) {
369 error = QDeclarativePixmapReply::Decoding;
372 // send completion event to the QDeclarativePixmapReply
374 if (!cancelled.contains(job)) job->postReply(error, errorString, readSize, image);
377 reply->deleteLater();
379 // kick off event loop again incase we have dropped below max request count
380 threadObject->processJobs();
383 QDeclarativePixmapReaderThreadObject::QDeclarativePixmapReaderThreadObject(QDeclarativePixmapReader *i)
388 void QDeclarativePixmapReaderThreadObject::processJobs()
390 QCoreApplication::postEvent(this, new QEvent(QEvent::User));
393 bool QDeclarativePixmapReaderThreadObject::event(QEvent *e)
395 if (e->type() == QEvent::User) {
396 reader->processJobs();
399 return QObject::event(e);
403 void QDeclarativePixmapReaderThreadObject::networkRequestDone()
405 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
406 reader->networkRequestDone(reply);
409 void QDeclarativePixmapReader::processJobs()
411 QMutexLocker locker(&mutex);
414 if (cancelled.isEmpty() && (jobs.isEmpty() || replies.count() >= IMAGEREQUEST_MAX_REQUEST_COUNT))
415 return; // Nothing else to do
417 // Clean cancelled jobs
418 if (cancelled.count()) {
419 for (int i = 0; i < cancelled.count(); ++i) {
420 QDeclarativePixmapReply *job = cancelled.at(i);
421 QNetworkReply *reply = replies.key(job, 0);
422 if (reply && reply->isRunning()) {
423 // cancel any jobs already started
424 replies.remove(reply);
427 // deleteLater, since not owned by this thread
433 if (!jobs.isEmpty() && replies.count() < IMAGEREQUEST_MAX_REQUEST_COUNT) {
434 QDeclarativePixmapReply *runningJob = jobs.takeLast();
435 runningJob->loading = true;
437 QUrl url = runningJob->data->url;
438 QSize requestSize = runningJob->data->requestSize;
440 processJob(runningJob, url, requestSize);
446 void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, const QUrl &url,
447 const QSize &requestSize)
450 if (url.scheme() == QLatin1String("image")) {
451 // Use QmlImageProvider
453 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
454 QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
456 QDeclarativePixmapReply::ReadError errorCode = QDeclarativePixmapReply::NoError;
458 if (image.isNull()) {
459 errorCode = QDeclarativePixmapReply::Loading;
460 errorStr = QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString());
464 if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
467 QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
469 // Image is local - load/decode immediately
471 QDeclarativePixmapReply::ReadError errorCode = QDeclarativePixmapReply::NoError;
475 if (f.open(QIODevice::ReadOnly)) {
476 if (!readImage(url, &f, &image, &errorStr, &readSize, requestSize))
477 errorCode = QDeclarativePixmapReply::Loading;
479 errorStr = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
480 errorCode = QDeclarativePixmapReply::Loading;
483 if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
487 QNetworkRequest req(url);
488 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
489 QNetworkReply *reply = networkAccessManager()->get(req);
491 QMetaObject::connect(reply, replyDownloadProgress, runningJob, downloadProgress);
492 QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);
494 replies.insert(reply, runningJob);
499 QDeclarativePixmapReader *QDeclarativePixmapReader::instance(QDeclarativeEngine *engine)
502 QDeclarativePixmapReader *reader = readers.value(engine);
504 reader = new QDeclarativePixmapReader(engine);
505 readers.insert(engine, reader);
507 readerMutex.unlock();
512 QDeclarativePixmapReply *QDeclarativePixmapReader::getImage(QDeclarativePixmapData *data)
515 QDeclarativePixmapReply *reply = new QDeclarativePixmapReply(data);
516 reply->reader = this;
519 if (threadObject) threadObject->processJobs();
524 void QDeclarativePixmapReader::cancel(QDeclarativePixmapReply *reply)
527 if (reply->loading) {
528 cancelled.append(reply);
531 if (threadObject) threadObject->processJobs();
533 jobs.removeAll(reply);
539 void QDeclarativePixmapReader::run()
541 if (replyDownloadProgress == -1) {
542 const QMetaObject *nr = &QNetworkReply::staticMetaObject;
543 const QMetaObject *pr = &QDeclarativePixmapReply::staticMetaObject;
544 const QMetaObject *ir = &QDeclarativePixmapReaderThreadObject::staticMetaObject;
545 replyDownloadProgress = nr->indexOfSignal("downloadProgress(qint64,qint64)");
546 replyFinished = nr->indexOfSignal("finished()");
547 downloadProgress = pr->indexOfSignal("downloadProgress(qint64,qint64)");
548 threadNetworkRequestDone = ir->indexOfSlot("networkRequestDone()");
552 threadObject = new QDeclarativePixmapReaderThreadObject(this);
562 class QDeclarativePixmapKey
569 inline bool operator==(const QDeclarativePixmapKey &lhs, const QDeclarativePixmapKey &rhs)
571 return *lhs.size == *rhs.size && *lhs.url == *rhs.url;
574 inline uint qHash(const QDeclarativePixmapKey &key)
576 return qHash(*key.url) ^ key.size->width() ^ key.size->height();
579 class QDeclarativePixmapStore : public QObject
583 QDeclarativePixmapStore();
585 void unreferencePixmap(QDeclarativePixmapData *);
586 void referencePixmap(QDeclarativePixmapData *);
589 virtual void timerEvent(QTimerEvent *);
592 QHash<QDeclarativePixmapKey, QDeclarativePixmapData *> m_cache;
595 void shrinkCache(int remove);
597 QDeclarativePixmapData *m_unreferencedPixmaps;
598 QDeclarativePixmapData *m_lastUnreferencedPixmap;
600 int m_unreferencedCost;
603 Q_GLOBAL_STATIC(QDeclarativePixmapStore, pixmapStore);
605 QDeclarativePixmapStore::QDeclarativePixmapStore()
606 : m_unreferencedPixmaps(0), m_lastUnreferencedPixmap(0), m_unreferencedCost(0), m_timerId(-1)
610 void QDeclarativePixmapStore::unreferencePixmap(QDeclarativePixmapData *data)
612 Q_ASSERT(data->prevUnreferenced == 0);
613 Q_ASSERT(data->prevUnreferencedPtr == 0);
614 Q_ASSERT(data->nextUnreferenced == 0);
616 data->nextUnreferenced = m_unreferencedPixmaps;
617 data->prevUnreferencedPtr = &m_unreferencedPixmaps;
619 m_unreferencedPixmaps = data;
620 if (m_unreferencedPixmaps->nextUnreferenced) {
621 m_unreferencedPixmaps->nextUnreferenced->prevUnreferenced = m_unreferencedPixmaps;
622 m_unreferencedPixmaps->nextUnreferenced->prevUnreferencedPtr = &m_unreferencedPixmaps->nextUnreferenced;
625 if (!m_lastUnreferencedPixmap)
626 m_lastUnreferencedPixmap = data;
628 m_unreferencedCost += data->cost();
630 shrinkCache(-1); // Shrink the cache incase it has become larger than cache_limit
632 if (m_timerId == -1 && m_unreferencedPixmaps)
633 m_timerId = startTimer(CACHE_EXPIRE_TIME * 1000);
636 void QDeclarativePixmapStore::referencePixmap(QDeclarativePixmapData *data)
638 Q_ASSERT(data->prevUnreferencedPtr);
640 *data->prevUnreferencedPtr = data->nextUnreferenced;
641 if (data->nextUnreferenced) {
642 data->nextUnreferenced->prevUnreferencedPtr = data->prevUnreferencedPtr;
643 data->nextUnreferenced->prevUnreferenced = data->prevUnreferenced;
645 if (m_lastUnreferencedPixmap == data)
646 m_lastUnreferencedPixmap = data->prevUnreferenced;
648 data->nextUnreferenced = 0;
649 data->prevUnreferencedPtr = 0;
650 data->prevUnreferenced = 0;
652 m_unreferencedCost -= data->cost();
655 void QDeclarativePixmapStore::shrinkCache(int remove)
657 while ((remove > 0 || m_unreferencedCost > cache_limit) && m_lastUnreferencedPixmap) {
658 QDeclarativePixmapData *data = m_lastUnreferencedPixmap;
659 Q_ASSERT(data->nextUnreferenced == 0);
661 *data->prevUnreferencedPtr = 0;
662 m_lastUnreferencedPixmap = data->prevUnreferenced;
663 data->prevUnreferencedPtr = 0;
664 data->prevUnreferenced = 0;
666 remove -= data->cost();
667 m_unreferencedCost -= data->cost();
668 data->removeFromCache();
673 void QDeclarativePixmapStore::timerEvent(QTimerEvent *)
675 int removalCost = m_unreferencedCost / CACHE_REMOVAL_FRACTION;
677 shrinkCache(removalCost);
679 if (m_unreferencedPixmaps == 0) {
680 killTimer(m_timerId);
685 QDeclarativePixmapReply::QDeclarativePixmapReply(QDeclarativePixmapData *d)
686 : data(d), reader(0), requestSize(d->requestSize), loading(false), redirectCount(0)
688 if (finishedIndex == -1) {
689 finishedIndex = QDeclarativePixmapReply::staticMetaObject.indexOfSignal("finished()");
690 downloadProgressIndex = QDeclarativePixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
694 QDeclarativePixmapReply::~QDeclarativePixmapReply()
698 bool QDeclarativePixmapReply::event(QEvent *event)
700 if (event->type() == QEvent::User) {
703 Event *de = static_cast<Event *>(event);
704 data->pixmapStatus = (de->error == NoError) ? QDeclarativePixmap::Ready : QDeclarativePixmap::Error;
706 if (data->pixmapStatus == QDeclarativePixmap::Ready) {
707 data->pixmap = QPixmap::fromImage(de->image);
708 data->implicitSize = de->implicitSize;
710 data->errorString = de->errorString;
711 data->removeFromCache(); // We don't continue to cache error'd pixmaps
721 return QObject::event(event);
725 int QDeclarativePixmapData::cost() const
727 return (pixmap.width() * pixmap.height() * pixmap.depth()) / 8;
730 void QDeclarativePixmapData::addref()
733 if (prevUnreferencedPtr)
734 pixmapStore()->referencePixmap(this);
737 void QDeclarativePixmapData::release()
739 Q_ASSERT(refCount > 0);
744 reply->reader->cancel(reply);
748 if (pixmapStatus == QDeclarativePixmap::Ready) {
749 pixmapStore()->unreferencePixmap(this);
757 void QDeclarativePixmapData::addToCache()
760 QDeclarativePixmapKey key = { &url, &requestSize };
761 pixmapStore()->m_cache.insert(key, this);
766 void QDeclarativePixmapData::removeFromCache()
769 QDeclarativePixmapKey key = { &url, &requestSize };
770 pixmapStore()->m_cache.remove(key);
775 static QDeclarativePixmapData* createPixmapDataSync(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
777 if (url.scheme() == QLatin1String("image")) {
779 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
780 QDeclarativeImageProvider::ImageType imageType = ep->getImageProviderType(url);
783 case QDeclarativeImageProvider::Image:
785 QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
786 if (!image.isNull()) {
788 return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
791 case QDeclarativeImageProvider::Pixmap:
793 QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize);
794 if (!pixmap.isNull()) {
796 return new QDeclarativePixmapData(url, pixmap, readSize, requestSize);
801 // no matching provider, or provider has bad image type, or provider returned null image
802 return new QDeclarativePixmapData(url, requestSize,
803 QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString()));
806 QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
807 if (localFile.isEmpty())
814 if (f.open(QIODevice::ReadOnly)) {
816 if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
818 return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
821 errorString = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
823 return new QDeclarativePixmapData(url, requestSize, errorString);
827 struct QDeclarativePixmapNull {
832 Q_GLOBAL_STATIC(QDeclarativePixmapNull, nullPixmap);
834 QDeclarativePixmap::QDeclarativePixmap()
839 QDeclarativePixmap::QDeclarativePixmap(QDeclarativeEngine *engine, const QUrl &url)
845 QDeclarativePixmap::QDeclarativePixmap(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
848 load(engine, url, size);
851 QDeclarativePixmap::~QDeclarativePixmap()
859 bool QDeclarativePixmap::isNull() const
864 bool QDeclarativePixmap::isReady() const
866 return status() == Ready;
869 bool QDeclarativePixmap::isError() const
871 return status() == Error;
874 bool QDeclarativePixmap::isLoading() const
876 return status() == Loading;
879 QString QDeclarativePixmap::error() const
882 return d->errorString;
887 QDeclarativePixmap::Status QDeclarativePixmap::status() const
890 return d->pixmapStatus;
895 const QUrl &QDeclarativePixmap::url() const
900 return nullPixmap()->url;
903 const QSize &QDeclarativePixmap::implicitSize() const
906 return d->implicitSize;
908 return nullPixmap()->size;
911 const QSize &QDeclarativePixmap::requestSize() const
914 return d->requestSize;
916 return nullPixmap()->size;
919 const QPixmap &QDeclarativePixmap::pixmap() const
924 return nullPixmap()->pixmap;
927 void QDeclarativePixmap::setPixmap(const QPixmap &p)
932 d = new QDeclarativePixmapData(p);
935 int QDeclarativePixmap::width() const
938 return d->pixmap.width();
943 int QDeclarativePixmap::height() const
946 return d->pixmap.height();
951 QRect QDeclarativePixmap::rect() const
954 return d->pixmap.rect();
959 void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url)
961 load(engine, url, QSize(), QDeclarativePixmap::Cache);
964 void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, QDeclarativePixmap::Options options)
966 load(engine, url, QSize(), options);
969 void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
971 load(engine, url, size, QDeclarativePixmap::Cache);
974 void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, QDeclarativePixmap::Options options)
976 if (d) { d->release(); d = 0; }
978 QDeclarativePixmapKey key = { &url, &requestSize };
979 QDeclarativePixmapStore *store = pixmapStore();
981 QHash<QDeclarativePixmapKey, QDeclarativePixmapData *>::Iterator iter = store->m_cache.find(key);
983 if (iter == store->m_cache.end()) {
984 if (options & QDeclarativePixmap::Asynchronous) {
985 // pixmaps can only be loaded synchronously
986 if (url.scheme() == QLatin1String("image")
987 && QDeclarativeEnginePrivate::get(engine)->getImageProviderType(url) == QDeclarativeImageProvider::Pixmap) {
988 options &= ~QDeclarativePixmap::Asynchronous;
992 if (!(options & QDeclarativePixmap::Asynchronous)) {
994 d = createPixmapDataSync(engine, url, requestSize, &ok);
996 if (options & QDeclarativePixmap::Cache)
1000 if (d) // loadable, but encountered error while loading
1007 QDeclarativePixmapReader *reader = QDeclarativePixmapReader::instance(engine);
1009 d = new QDeclarativePixmapData(url, requestSize);
1010 if (options & QDeclarativePixmap::Cache)
1013 d->reply = reader->getImage(d);
1020 void QDeclarativePixmap::clear()
1028 void QDeclarativePixmap::clear(QObject *obj)
1032 QObject::disconnect(d->reply, 0, obj, 0);
1038 bool QDeclarativePixmap::connectFinished(QObject *object, const char *method)
1040 if (!d || !d->reply) {
1041 qWarning("QDeclarativePixmap: connectFinished() called when not loading.");
1045 return QObject::connect(d->reply, SIGNAL(finished()), object, method);
1048 bool QDeclarativePixmap::connectFinished(QObject *object, int method)
1050 if (!d || !d->reply) {
1051 qWarning("QDeclarativePixmap: connectFinished() called when not loading.");
1055 return QMetaObject::connect(d->reply, QDeclarativePixmapReply::finishedIndex, object, method);
1058 bool QDeclarativePixmap::connectDownloadProgress(QObject *object, const char *method)
1060 if (!d || !d->reply) {
1061 qWarning("QDeclarativePixmap: connectDownloadProgress() called when not loading.");
1065 return QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), object, method);
1068 bool QDeclarativePixmap::connectDownloadProgress(QObject *object, int method)
1070 if (!d || !d->reply) {
1071 qWarning("QDeclarativePixmap: connectDownloadProgress() called when not loading.");
1075 return QMetaObject::connect(d->reply, QDeclarativePixmapReply::downloadProgressIndex, object, method);
1080 #include <qdeclarativepixmapcache.moc>