1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativepixmapcache_p.h"
43 #include "QtDeclarative/qdeclarativenetworkaccessmanagerfactory.h"
44 #include "QtDeclarative/qdeclarativeimageprovider.h"
46 #include <QtDeclarative/qdeclarativeengine.h>
47 #include <QtDeclarative/private/qdeclarativeglobal_p.h>
48 #include <QtDeclarative/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
75 // The cache limit describes the maximum "junk" in the cache.
76 // These are the same defaults as QPixmapCache
77 #if defined(Q_WS_QWS) || defined(Q_WS_WINCE)
78 static int cache_limit = 2048 * 1024; // 2048 KB cache limit for embedded
80 static int cache_limit = 10240 * 1024; // 10 MB cache limit for desktop
83 class QDeclarative1PixmapReader;
84 class QDeclarative1PixmapData;
85 class QDeclarative1PixmapReply : public QObject
89 enum ReadError { NoError, Loading, Decoding };
91 QDeclarative1PixmapReply(QDeclarative1PixmapData *);
92 ~QDeclarative1PixmapReply();
94 QDeclarative1PixmapData *data;
95 QDeclarative1PixmapReader *reader;
101 class Event : public QEvent {
103 Event(ReadError, const QString &, const QSize &, const QImage &);
110 void postReply(ReadError, const QString &, const QSize &, const QImage &);
115 void downloadProgress(qint64, qint64);
118 bool event(QEvent *event);
121 Q_DISABLE_COPY(QDeclarative1PixmapReply)
124 static int finishedIndex;
125 static int downloadProgressIndex;
128 class QDeclarative1PixmapReaderThreadObject : public QObject {
131 QDeclarative1PixmapReaderThreadObject(QDeclarative1PixmapReader *);
133 virtual bool event(QEvent *e);
135 void networkRequestDone();
137 QDeclarative1PixmapReader *reader;
140 class QDeclarative1PixmapData;
141 class QDeclarative1PixmapReader : public QThread
145 QDeclarative1PixmapReader(QDeclarativeEngine *eng);
146 ~QDeclarative1PixmapReader();
148 QDeclarative1PixmapReply *getImage(QDeclarative1PixmapData *);
149 void cancel(QDeclarative1PixmapReply *rep);
151 static QDeclarative1PixmapReader *instance(QDeclarativeEngine *engine);
157 friend class QDeclarative1PixmapReaderThreadObject;
159 void processJob(QDeclarative1PixmapReply *, const QUrl &, const QSize &);
160 void networkRequestDone(QNetworkReply *);
162 QList<QDeclarative1PixmapReply*> jobs;
163 QList<QDeclarative1PixmapReply*> cancelled;
164 QDeclarativeEngine *engine;
165 QObject *eventLoopQuitHack;
168 QDeclarative1PixmapReaderThreadObject *threadObject;
169 QWaitCondition waitCondition;
171 QNetworkAccessManager *networkAccessManager();
172 QNetworkAccessManager *accessManager;
174 QHash<QNetworkReply*,QDeclarative1PixmapReply*> replies;
176 static int replyDownloadProgress;
177 static int replyFinished;
178 static int downloadProgress;
179 static int threadNetworkRequestDone;
180 static QHash<QDeclarativeEngine *,QDeclarative1PixmapReader*> readers;
181 static QMutex readerMutex;
184 class QDeclarative1PixmapData
187 QDeclarative1PixmapData(const QUrl &u, const QSize &s, const QString &e)
188 : refCount(1), inCache(false), pixmapStatus(QDeclarative1Pixmap::Error),
189 url(u), errorString(e), requestSize(s), reply(0), prevUnreferenced(0),
190 prevUnreferencedPtr(0), nextUnreferenced(0)
194 QDeclarative1PixmapData(const QUrl &u, const QSize &r)
195 : refCount(1), inCache(false), pixmapStatus(QDeclarative1Pixmap::Loading),
196 url(u), requestSize(r), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0),
201 QDeclarative1PixmapData(const QUrl &u, const QPixmap &p, const QSize &s, const QSize &r)
202 : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QDeclarative1Pixmap::Ready),
203 url(u), pixmap(p), implicitSize(s), requestSize(r), reply(0), prevUnreferenced(0),
204 prevUnreferencedPtr(0), nextUnreferenced(0)
208 QDeclarative1PixmapData(const QPixmap &p)
209 : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(QDeclarative1Pixmap::Ready),
210 pixmap(p), implicitSize(p.size()), requestSize(p.size()), reply(0), prevUnreferenced(0),
211 prevUnreferencedPtr(0), nextUnreferenced(0)
219 void removeFromCache();
224 bool privatePixmap:1;
226 QDeclarative1Pixmap::Status pixmapStatus;
233 QDeclarative1PixmapReply *reply;
235 QDeclarative1PixmapData *prevUnreferenced;
236 QDeclarative1PixmapData**prevUnreferencedPtr;
237 QDeclarative1PixmapData *nextUnreferenced;
240 int QDeclarative1PixmapReply::finishedIndex = -1;
241 int QDeclarative1PixmapReply::downloadProgressIndex = -1;
244 QHash<QDeclarativeEngine *,QDeclarative1PixmapReader*> QDeclarative1PixmapReader::readers;
245 QMutex QDeclarative1PixmapReader::readerMutex;
247 int QDeclarative1PixmapReader::replyDownloadProgress = -1;
248 int QDeclarative1PixmapReader::replyFinished = -1;
249 int QDeclarative1PixmapReader::downloadProgress = -1;
250 int QDeclarative1PixmapReader::threadNetworkRequestDone = -1;
253 void QDeclarative1PixmapReply::postReply(ReadError error, const QString &errorString,
254 const QSize &implicitSize, const QImage &image)
257 QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, image));
260 QDeclarative1PixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, const QImage &i)
261 : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i)
265 QNetworkAccessManager *QDeclarative1PixmapReader::networkAccessManager()
267 if (!accessManager) {
268 Q_ASSERT(threadObject);
269 accessManager = QDeclarativeEnginePrivate::get(engine)->createNetworkAccessManager(threadObject);
271 return accessManager;
274 static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize,
275 const QSize &requestSize)
277 QImageReader imgio(dev);
279 bool force_scale = false;
280 if (url.path().endsWith(QLatin1String(".svg"),Qt::CaseInsensitive)) {
281 imgio.setFormat("svg"); // QSvgPlugin::capabilities bug QTBUG-9053
286 if (requestSize.width() > 0 || requestSize.height() > 0) {
287 QSize s = imgio.size();
288 if (requestSize.width() && (force_scale || requestSize.width() < s.width())) {
289 if (requestSize.height() <= 0)
290 s.setHeight(s.height()*requestSize.width()/s.width());
291 s.setWidth(requestSize.width()); scaled = true;
293 if (requestSize.height() && (force_scale || requestSize.height() < s.height())) {
294 if (requestSize.width() <= 0)
295 s.setWidth(s.width()*requestSize.height()/s.height());
296 s.setHeight(requestSize.height()); scaled = true;
298 if (scaled) { imgio.setScaledSize(s); }
302 *impsize = imgio.size();
304 if (imgio.read(image)) {
305 if (impsize && impsize->width() < 0)
306 *impsize = image->size();
310 *errorString = QDeclarative1Pixmap::tr("Error decoding: %1: %2").arg(url.toString())
311 .arg(imgio.errorString());
316 QDeclarative1PixmapReader::QDeclarative1PixmapReader(QDeclarativeEngine *eng)
317 : QThread(eng), engine(eng), threadObject(0), accessManager(0)
319 eventLoopQuitHack = new QObject;
320 eventLoopQuitHack->moveToThread(this);
321 connect(eventLoopQuitHack, SIGNAL(destroyed(QObject*)), SLOT(quit()), Qt::DirectConnection);
322 start(QThread::IdlePriority);
325 QDeclarative1PixmapReader::~QDeclarative1PixmapReader()
328 readers.remove(engine);
329 readerMutex.unlock();
331 eventLoopQuitHack->deleteLater();
335 void QDeclarative1PixmapReader::networkRequestDone(QNetworkReply *reply)
337 QDeclarative1PixmapReply *job = replies.take(reply);
340 job->redirectCount++;
341 if (job->redirectCount < IMAGEREQUEST_MAX_REDIRECT_RECURSION) {
342 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
343 if (redirect.isValid()) {
344 QUrl url = reply->url().resolved(redirect.toUrl());
345 QNetworkRequest req(url);
346 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
348 reply->deleteLater();
349 reply = networkAccessManager()->get(req);
351 QMetaObject::connect(reply, replyDownloadProgress, job, downloadProgress);
352 QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);
354 replies.insert(reply, job);
360 QDeclarative1PixmapReply::ReadError error = QDeclarative1PixmapReply::NoError;
363 if (reply->error()) {
364 error = QDeclarative1PixmapReply::Loading;
365 errorString = reply->errorString();
367 QByteArray all = reply->readAll();
369 buff.open(QIODevice::ReadOnly);
370 if (!readImage(reply->url(), &buff, &image, &errorString, &readSize, job->requestSize)) {
371 error = QDeclarative1PixmapReply::Decoding;
374 // send completion event to the QDeclarative1PixmapReply
376 if (!cancelled.contains(job)) job->postReply(error, errorString, readSize, image);
379 reply->deleteLater();
381 // kick off event loop again incase we have dropped below max request count
382 threadObject->processJobs();
385 QDeclarative1PixmapReaderThreadObject::QDeclarative1PixmapReaderThreadObject(QDeclarative1PixmapReader *i)
390 void QDeclarative1PixmapReaderThreadObject::processJobs()
392 QCoreApplication::postEvent(this, new QEvent(QEvent::User));
395 bool QDeclarative1PixmapReaderThreadObject::event(QEvent *e)
397 if (e->type() == QEvent::User) {
398 reader->processJobs();
401 return QObject::event(e);
405 void QDeclarative1PixmapReaderThreadObject::networkRequestDone()
407 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
408 reader->networkRequestDone(reply);
411 void QDeclarative1PixmapReader::processJobs()
413 QMutexLocker locker(&mutex);
416 if (cancelled.isEmpty() && (jobs.isEmpty() || replies.count() >= IMAGEREQUEST_MAX_REQUEST_COUNT))
417 return; // Nothing else to do
419 // Clean cancelled jobs
420 if (cancelled.count()) {
421 for (int i = 0; i < cancelled.count(); ++i) {
422 QDeclarative1PixmapReply *job = cancelled.at(i);
423 QNetworkReply *reply = replies.key(job, 0);
424 if (reply && reply->isRunning()) {
425 // cancel any jobs already started
426 replies.remove(reply);
429 // deleteLater, since not owned by this thread
435 if (!jobs.isEmpty() && replies.count() < IMAGEREQUEST_MAX_REQUEST_COUNT) {
436 QDeclarative1PixmapReply *runningJob = jobs.takeLast();
437 runningJob->loading = true;
439 QUrl url = runningJob->data->url;
440 QSize requestSize = runningJob->data->requestSize;
442 processJob(runningJob, url, requestSize);
448 void QDeclarative1PixmapReader::processJob(QDeclarative1PixmapReply *runningJob, const QUrl &url,
449 const QSize &requestSize)
452 if (url.scheme() == QLatin1String("image")) {
453 // Use QmlImageProvider
455 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
456 QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
458 QDeclarative1PixmapReply::ReadError errorCode = QDeclarative1PixmapReply::NoError;
460 if (image.isNull()) {
461 errorCode = QDeclarative1PixmapReply::Loading;
462 errorStr = QDeclarative1Pixmap::tr("Failed to get image from provider: %1").arg(url.toString());
466 if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
469 QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
471 // Image is local - load/decode immediately
473 QDeclarative1PixmapReply::ReadError errorCode = QDeclarative1PixmapReply::NoError;
477 if (f.open(QIODevice::ReadOnly)) {
478 if (!readImage(url, &f, &image, &errorStr, &readSize, requestSize))
479 errorCode = QDeclarative1PixmapReply::Loading;
481 errorStr = QDeclarative1Pixmap::tr("Cannot open: %1").arg(url.toString());
482 errorCode = QDeclarative1PixmapReply::Loading;
485 if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
489 QNetworkRequest req(url);
490 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
491 QNetworkReply *reply = networkAccessManager()->get(req);
493 QMetaObject::connect(reply, replyDownloadProgress, runningJob, downloadProgress);
494 QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);
496 replies.insert(reply, runningJob);
501 QDeclarative1PixmapReader *QDeclarative1PixmapReader::instance(QDeclarativeEngine *engine)
504 QDeclarative1PixmapReader *reader = readers.value(engine);
506 reader = new QDeclarative1PixmapReader(engine);
507 readers.insert(engine, reader);
509 readerMutex.unlock();
514 QDeclarative1PixmapReply *QDeclarative1PixmapReader::getImage(QDeclarative1PixmapData *data)
517 QDeclarative1PixmapReply *reply = new QDeclarative1PixmapReply(data);
518 reply->reader = this;
521 if (threadObject) threadObject->processJobs();
526 void QDeclarative1PixmapReader::cancel(QDeclarative1PixmapReply *reply)
529 if (reply->loading) {
530 cancelled.append(reply);
533 if (threadObject) threadObject->processJobs();
535 jobs.removeAll(reply);
541 void QDeclarative1PixmapReader::run()
543 if (replyDownloadProgress == -1) {
544 const QMetaObject *nr = &QNetworkReply::staticMetaObject;
545 const QMetaObject *pr = &QDeclarative1PixmapReply::staticMetaObject;
546 const QMetaObject *ir = &QDeclarative1PixmapReaderThreadObject::staticMetaObject;
547 replyDownloadProgress = nr->indexOfSignal("downloadProgress(qint64,qint64)");
548 replyFinished = nr->indexOfSignal("finished()");
549 downloadProgress = pr->indexOfSignal("downloadProgress(qint64,qint64)");
550 threadNetworkRequestDone = ir->indexOfSlot("networkRequestDone()");
554 threadObject = new QDeclarative1PixmapReaderThreadObject(this);
564 class QDeclarative1PixmapKey
571 inline bool operator==(const QDeclarative1PixmapKey &lhs, const QDeclarative1PixmapKey &rhs)
573 return *lhs.size == *rhs.size && *lhs.url == *rhs.url;
576 inline uint qHash(const QDeclarative1PixmapKey &key)
578 return qHash(*key.url) ^ key.size->width() ^ key.size->height();
581 class QDeclarative1PixmapStore : public QObject
585 QDeclarative1PixmapStore();
587 void unreferencePixmap(QDeclarative1PixmapData *);
588 void referencePixmap(QDeclarative1PixmapData *);
591 virtual void timerEvent(QTimerEvent *);
594 QHash<QDeclarative1PixmapKey, QDeclarative1PixmapData *> m_cache;
597 void shrinkCache(int remove);
599 QDeclarative1PixmapData *m_unreferencedPixmaps;
600 QDeclarative1PixmapData *m_lastUnreferencedPixmap;
602 int m_unreferencedCost;
606 Q_GLOBAL_STATIC(QDeclarative1PixmapStore, pixmapStore)
608 QDeclarative1PixmapStore::QDeclarative1PixmapStore()
609 : m_unreferencedPixmaps(0), m_lastUnreferencedPixmap(0), m_unreferencedCost(0), m_timerId(-1)
613 void QDeclarative1PixmapStore::unreferencePixmap(QDeclarative1PixmapData *data)
615 Q_ASSERT(data->prevUnreferenced == 0);
616 Q_ASSERT(data->prevUnreferencedPtr == 0);
617 Q_ASSERT(data->nextUnreferenced == 0);
619 data->nextUnreferenced = m_unreferencedPixmaps;
620 data->prevUnreferencedPtr = &m_unreferencedPixmaps;
622 m_unreferencedPixmaps = data;
623 if (m_unreferencedPixmaps->nextUnreferenced) {
624 m_unreferencedPixmaps->nextUnreferenced->prevUnreferenced = m_unreferencedPixmaps;
625 m_unreferencedPixmaps->nextUnreferenced->prevUnreferencedPtr = &m_unreferencedPixmaps->nextUnreferenced;
628 if (!m_lastUnreferencedPixmap)
629 m_lastUnreferencedPixmap = data;
631 m_unreferencedCost += data->cost();
633 shrinkCache(-1); // Shrink the cache incase it has become larger than cache_limit
635 if (m_timerId == -1 && m_unreferencedPixmaps)
636 m_timerId = startTimer(CACHE_EXPIRE_TIME * 1000);
639 void QDeclarative1PixmapStore::referencePixmap(QDeclarative1PixmapData *data)
641 Q_ASSERT(data->prevUnreferencedPtr);
643 *data->prevUnreferencedPtr = data->nextUnreferenced;
644 if (data->nextUnreferenced) {
645 data->nextUnreferenced->prevUnreferencedPtr = data->prevUnreferencedPtr;
646 data->nextUnreferenced->prevUnreferenced = data->prevUnreferenced;
648 if (m_lastUnreferencedPixmap == data)
649 m_lastUnreferencedPixmap = data->prevUnreferenced;
651 data->nextUnreferenced = 0;
652 data->prevUnreferencedPtr = 0;
653 data->prevUnreferenced = 0;
655 m_unreferencedCost -= data->cost();
658 void QDeclarative1PixmapStore::shrinkCache(int remove)
660 while ((remove > 0 || m_unreferencedCost > cache_limit) && m_lastUnreferencedPixmap) {
661 QDeclarative1PixmapData *data = m_lastUnreferencedPixmap;
662 Q_ASSERT(data->nextUnreferenced == 0);
664 *data->prevUnreferencedPtr = 0;
665 m_lastUnreferencedPixmap = data->prevUnreferenced;
666 data->prevUnreferencedPtr = 0;
667 data->prevUnreferenced = 0;
669 remove -= data->cost();
670 m_unreferencedCost -= data->cost();
671 data->removeFromCache();
676 void QDeclarative1PixmapStore::timerEvent(QTimerEvent *)
678 int removalCost = m_unreferencedCost / CACHE_REMOVAL_FRACTION;
680 shrinkCache(removalCost);
682 if (m_unreferencedPixmaps == 0) {
683 killTimer(m_timerId);
688 QDeclarative1PixmapReply::QDeclarative1PixmapReply(QDeclarative1PixmapData *d)
689 : data(d), reader(0), requestSize(d->requestSize), loading(false), redirectCount(0)
691 if (finishedIndex == -1) {
692 finishedIndex = QDeclarative1PixmapReply::staticMetaObject.indexOfSignal("finished()");
693 downloadProgressIndex = QDeclarative1PixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
697 QDeclarative1PixmapReply::~QDeclarative1PixmapReply()
701 bool QDeclarative1PixmapReply::event(QEvent *event)
703 if (event->type() == QEvent::User) {
706 Event *de = static_cast<Event *>(event);
707 data->pixmapStatus = (de->error == NoError) ? QDeclarative1Pixmap::Ready : QDeclarative1Pixmap::Error;
709 if (data->pixmapStatus == QDeclarative1Pixmap::Ready) {
710 data->pixmap = QPixmap::fromImage(de->image);
711 data->implicitSize = de->implicitSize;
713 data->errorString = de->errorString;
714 data->removeFromCache(); // We don't continue to cache error'd pixmaps
724 return QObject::event(event);
728 int QDeclarative1PixmapData::cost() const
730 return (pixmap.width() * pixmap.height() * pixmap.depth()) / 8;
733 void QDeclarative1PixmapData::addref()
736 if (prevUnreferencedPtr)
737 pixmapStore()->referencePixmap(this);
740 void QDeclarative1PixmapData::release()
742 Q_ASSERT(refCount > 0);
747 reply->reader->cancel(reply);
751 if (pixmapStatus == QDeclarative1Pixmap::Ready) {
752 pixmapStore()->unreferencePixmap(this);
760 void QDeclarative1PixmapData::addToCache()
763 QDeclarative1PixmapKey key = { &url, &requestSize };
764 pixmapStore()->m_cache.insert(key, this);
769 void QDeclarative1PixmapData::removeFromCache()
772 QDeclarative1PixmapKey key = { &url, &requestSize };
773 pixmapStore()->m_cache.remove(key);
778 static QDeclarative1PixmapData* createPixmapDataSync(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
780 if (url.scheme() == QLatin1String("image")) {
782 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
783 QDeclarativeImageProvider::ImageType imageType = ep->getImageProviderType(url);
786 case QDeclarativeImageProvider::Image:
788 QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
789 if (!image.isNull()) {
791 return new QDeclarative1PixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
795 case QDeclarativeImageProvider::Pixmap:
797 QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize);
798 if (!pixmap.isNull()) {
800 return new QDeclarative1PixmapData(url, pixmap, readSize, requestSize);
804 case QDeclarativeImageProvider::Texture:
805 case QDeclarativeImageProvider::Invalid:
809 // no matching provider, or provider has bad image type, or provider returned null image
810 return new QDeclarative1PixmapData(url, requestSize,
811 QDeclarative1Pixmap::tr("Failed to get image from provider: %1").arg(url.toString()));
814 QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
815 if (localFile.isEmpty())
822 if (f.open(QIODevice::ReadOnly)) {
824 if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
826 return new QDeclarative1PixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
829 errorString = QDeclarative1Pixmap::tr("Cannot open: %1").arg(url.toString());
831 return new QDeclarative1PixmapData(url, requestSize, errorString);
835 struct QDeclarative1PixmapNull {
840 Q_GLOBAL_STATIC(QDeclarative1PixmapNull, nullPixmap);
842 QDeclarative1Pixmap::QDeclarative1Pixmap()
847 QDeclarative1Pixmap::QDeclarative1Pixmap(QDeclarativeEngine *engine, const QUrl &url)
853 QDeclarative1Pixmap::QDeclarative1Pixmap(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
856 load(engine, url, size);
859 QDeclarative1Pixmap::~QDeclarative1Pixmap()
867 bool QDeclarative1Pixmap::isNull() const
872 bool QDeclarative1Pixmap::isReady() const
874 return status() == Ready;
877 bool QDeclarative1Pixmap::isError() const
879 return status() == Error;
882 bool QDeclarative1Pixmap::isLoading() const
884 return status() == Loading;
887 QString QDeclarative1Pixmap::error() const
890 return d->errorString;
895 QDeclarative1Pixmap::Status QDeclarative1Pixmap::status() const
898 return d->pixmapStatus;
903 const QUrl &QDeclarative1Pixmap::url() const
908 return nullPixmap()->url;
911 const QSize &QDeclarative1Pixmap::implicitSize() const
914 return d->implicitSize;
916 return nullPixmap()->size;
919 const QSize &QDeclarative1Pixmap::requestSize() const
922 return d->requestSize;
924 return nullPixmap()->size;
927 const QPixmap &QDeclarative1Pixmap::pixmap() const
932 return nullPixmap()->pixmap;
935 void QDeclarative1Pixmap::setPixmap(const QPixmap &p)
940 d = new QDeclarative1PixmapData(p);
943 int QDeclarative1Pixmap::width() const
946 return d->pixmap.width();
951 int QDeclarative1Pixmap::height() const
954 return d->pixmap.height();
959 QRect QDeclarative1Pixmap::rect() const
962 return d->pixmap.rect();
967 void QDeclarative1Pixmap::load(QDeclarativeEngine *engine, const QUrl &url)
969 load(engine, url, QSize(), QDeclarative1Pixmap::Cache);
972 void QDeclarative1Pixmap::load(QDeclarativeEngine *engine, const QUrl &url, QDeclarative1Pixmap::Options options)
974 load(engine, url, QSize(), options);
977 void QDeclarative1Pixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
979 load(engine, url, size, QDeclarative1Pixmap::Cache);
982 void QDeclarative1Pixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, QDeclarative1Pixmap::Options options)
984 if (d) { d->release(); d = 0; }
986 QDeclarative1PixmapKey key = { &url, &requestSize };
987 QDeclarative1PixmapStore *store = pixmapStore();
989 QHash<QDeclarative1PixmapKey, QDeclarative1PixmapData *>::Iterator iter = store->m_cache.find(key);
991 if (iter == store->m_cache.end()) {
992 if (options & QDeclarative1Pixmap::Asynchronous) {
993 // pixmaps can only be loaded synchronously
994 if (url.scheme() == QLatin1String("image")
995 && QDeclarativeEnginePrivate::get(engine)->getImageProviderType(url) == QDeclarativeImageProvider::Pixmap) {
996 options &= ~QDeclarative1Pixmap::Asynchronous;
1000 if (!(options & QDeclarative1Pixmap::Asynchronous)) {
1002 d = createPixmapDataSync(engine, url, requestSize, &ok);
1004 if (options & QDeclarative1Pixmap::Cache)
1008 if (d) // loadable, but encountered error while loading
1015 QDeclarative1PixmapReader *reader = QDeclarative1PixmapReader::instance(engine);
1017 d = new QDeclarative1PixmapData(url, requestSize);
1018 if (options & QDeclarative1Pixmap::Cache)
1021 d->reply = reader->getImage(d);
1028 void QDeclarative1Pixmap::clear()
1036 void QDeclarative1Pixmap::clear(QObject *obj)
1040 QObject::disconnect(d->reply, 0, obj, 0);
1046 bool QDeclarative1Pixmap::connectFinished(QObject *object, const char *method)
1048 if (!d || !d->reply) {
1049 qWarning("QDeclarative1Pixmap: connectFinished() called when not loading.");
1053 return QObject::connect(d->reply, SIGNAL(finished()), object, method);
1056 bool QDeclarative1Pixmap::connectFinished(QObject *object, int method)
1058 if (!d || !d->reply) {
1059 qWarning("QDeclarative1Pixmap: connectFinished() called when not loading.");
1063 return QMetaObject::connect(d->reply, QDeclarative1PixmapReply::finishedIndex, object, method);
1066 bool QDeclarative1Pixmap::connectDownloadProgress(QObject *object, const char *method)
1068 if (!d || !d->reply) {
1069 qWarning("QDeclarative1Pixmap: connectDownloadProgress() called when not loading.");
1073 return QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), object, method);
1076 bool QDeclarative1Pixmap::connectDownloadProgress(QObject *object, int method)
1078 if (!d || !d->reply) {
1079 qWarning("QDeclarative1Pixmap: connectDownloadProgress() called when not loading.");
1083 return QMetaObject::connect(d->reply, QDeclarative1PixmapReply::downloadProgressIndex, object, method);
1090 #include <qdeclarativepixmapcache.moc>