summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brasser <michael.brasser@nokia.com>2009-08-07 05:04:13 (GMT)
committerMichael Brasser <michael.brasser@nokia.com>2009-08-07 05:04:13 (GMT)
commit53d7bcc2f535807ec6edd1df20194675d3cc357e (patch)
tree2afd5dc4dc74fc56850c93a2cccf5286d40f7ef2 /src
parentd6843baff3f344de4c63c5559d23595dbf2bdfea (diff)
downloadQt-53d7bcc2f535807ec6edd1df20194675d3cc357e.zip
Qt-53d7bcc2f535807ec6edd1df20194675d3cc357e.tar.gz
Qt-53d7bcc2f535807ec6edd1df20194675d3cc357e.tar.bz2
Start refactoring QFxPixmap so it isn't as slow to use.
Diffstat (limited to 'src')
-rw-r--r--src/declarative/extra/qfxparticles.cpp15
-rw-r--r--src/declarative/fx/qfximage.cpp36
-rw-r--r--src/declarative/fx/qfxpixmap.cpp100
-rw-r--r--src/declarative/fx/qfxpixmap.h22
4 files changed, 77 insertions, 96 deletions
diff --git a/src/declarative/extra/qfxparticles.cpp b/src/declarative/extra/qfxparticles.cpp
index 26c176b..f05f233 100644
--- a/src/declarative/extra/qfxparticles.cpp
+++ b/src/declarative/extra/qfxparticles.cpp
@@ -53,6 +53,7 @@
#include <qfxpixmap.h>
#include <private/qfxperf_p.h>
#include <private/qmlanimation_p.h>
+#include <QNetworkReply>
#include "qfxparticles.h"
#include <QPainter>
@@ -404,6 +405,7 @@ public:
bool emitting;
QFxParticleMotion *motion;
QFxParticlesPainter *paintItem;
+ QPointer<QNetworkReply> reply;
QList<QFxParticle> particles;
QTickAnimationProxy<QFxParticlesPrivate, &QFxParticlesPrivate::tick> clock;
@@ -636,7 +638,7 @@ QUrl QFxParticles::source() const
void QFxParticles::imageLoaded()
{
Q_D(QFxParticles);
- d->image = QFxPixmap(d->url);
+ QFxPixmap::find(d->url, &d->image);
d->paintItem->updateSize();
d->paintItem->update();
}
@@ -645,7 +647,7 @@ void QFxParticles::setSource(const QUrl &name)
{
Q_D(QFxParticles);
- if (name == d->url)
+ if ((d->url.isEmpty() == name.isEmpty()) && name == d->url)
return;
if (!d->url.isEmpty())
@@ -658,7 +660,14 @@ void QFxParticles::setSource(const QUrl &name)
} else {
d->url = name;
Q_ASSERT(!name.isRelative());
- QFxPixmap::get(qmlEngine(this), d->url, this, SLOT(imageLoaded()));
+ d->reply = QFxPixmap::get(qmlEngine(this), d->url, &d->image);
+ if (d->reply)
+ connect(d->reply, SIGNAL(finished()), this, SLOT(imageLoaded()));
+ else {
+ //### unify with imageLoaded
+ d->paintItem->updateSize();
+ d->paintItem->update();
+ }
}
}
diff --git a/src/declarative/fx/qfximage.cpp b/src/declarative/fx/qfximage.cpp
index 792b015..26e2727 100644
--- a/src/declarative/fx/qfximage.cpp
+++ b/src/declarative/fx/qfximage.cpp
@@ -437,7 +437,8 @@ void QFxImage::setSource(const QUrl &url)
QFxPerfTimer<QFxPerf::PixmapLoad> perf;
#endif
Q_D(QFxImage);
- if (url == d->url)
+ //equality is fairly expensive, so we bypass for simple, common case
+ if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
return;
if (d->sciReply) {
@@ -485,13 +486,23 @@ void QFxImage::setSource(const QUrl &url)
this, SLOT(sciRequestFinished()));
}
} else {
- d->reply = QFxPixmap::get(qmlEngine(this), d->url, this, SLOT(requestFinished()));
+ d->reply = QFxPixmap::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 progressChanged(d->progress);
+ emit statusChanged(d->status);
+ emit sourceChanged(d->url);
+ emit progressChanged(1.0);
+ update();
}
}
}
@@ -503,15 +514,16 @@ void QFxImage::requestFinished()
{
Q_D(QFxImage);
if (d->url.path().endsWith(QLatin1String(".sci"))) {
- d->pix = QFxPixmap(d->sciurl);
+ QFxPixmap::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;
}
- d->pix = QFxPixmap(d->url);
+ QFxPixmap::find(d->url, &d->pix);
}
setImplicitWidth(d->pix.width());
setImplicitHeight(d->pix.height());
@@ -566,13 +578,23 @@ void QFxImage::setGridScaledImage(const QFxGridScaledImage& sci)
sg->setVerticalTileRule(sci.verticalTileRule());
d->sciurl = d->url.resolved(QUrl(sci.pixmapUrl()));
- d->reply = QFxPixmap::get(qmlEngine(this), d->sciurl, this, SLOT(requestFinished()));
+ d->reply = QFxPixmap::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)),
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 progressChanged(d->progress);
+ emit statusChanged(d->status);
+ emit sourceChanged(d->url);
+ emit progressChanged(1.0);
+ update();
}
}
}
diff --git a/src/declarative/fx/qfxpixmap.cpp b/src/declarative/fx/qfxpixmap.cpp
index 6647b21..99b8128 100644
--- a/src/declarative/fx/qfxpixmap.cpp
+++ b/src/declarative/fx/qfxpixmap.cpp
@@ -81,14 +81,7 @@ public:
}
};
-class QFxPixmapPrivate
-{
-public:
- QFxPixmapPrivate() {}
-
- QPixmap pixmap;
-
- bool readImage(QIODevice *dev)
+static bool readImage(QIODevice *dev, QPixmap *pixmap)
{
QImageReader imgio(dev);
@@ -114,13 +107,12 @@ public:
if (!sz.isValid())
img = img.scaled(limit,Qt::KeepAspectRatio);
#endif
- pixmap = QPixmap::fromImage(img);
+ *pixmap = QPixmap::fromImage(img);
return true;
} else {
return false;
}
}
-};
/*!
\internal
@@ -131,25 +123,26 @@ public:
This class is NOT reentrant.
The pixmap cache will grow indefinately.
*/
-QFxPixmap::QFxPixmap()
-: d(new QFxPixmapPrivate)
-{
-}
-QFxPixmap::QFxPixmap(const QUrl &url)
-: d(new QFxPixmapPrivate)
+
+bool QFxPixmap::find(const QUrl& url, QPixmap *pixmap)
{
#ifdef Q_ENABLE_PERFORMANCE_LOG
QFxPerfTimer<QFxPerf::PixmapLoad> perf;
#endif
+
QString key = url.toString();
- if (!QPixmapCache::find(key,&d->pixmap)) {
+ if (!QPixmapCache::find(key,pixmap)) {
#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
if (url.scheme()==QLatin1String("file")) {
QFile f(url.toLocalFile());
- if (f.open(QIODevice::ReadOnly))
- if (!d->readImage(&f))
+ if (f.open(QIODevice::ReadOnly)) {
+ if (!readImage(&f, pixmap)) {
qWarning() << "Format error loading" << url;
+ *pixmap = QPixmap();
+ }
+ } else
+ *pixmap = QPixmap();
} else
#endif
{
@@ -158,55 +151,21 @@ QFxPixmap::QFxPixmap(const QUrl &url)
// API usage error
qWarning() << "QFxPixmap: URL not loaded" << url;
} else {
- if ((*iter)->reply->error())
+ if ((*iter)->reply->error()) {
qWarning() << "Network error loading" << url << (*iter)->reply->errorString();
- else
- if (!d->readImage((*iter)->reply))
+ *pixmap = QPixmap();
+ } else
+ if (!readImage((*iter)->reply, pixmap)) {
qWarning() << "Format error loading" << url;
+ *pixmap = QPixmap();
+ }
(*iter)->release();
}
}
- QPixmapCache::insert(key, d->pixmap);
+ QPixmapCache::insert(key, *pixmap);
}
}
-QFxPixmap::QFxPixmap(const QFxPixmap &o)
-: d(new QFxPixmapPrivate)
-{
- d->pixmap = o.d->pixmap;
-}
-
-QFxPixmap::~QFxPixmap()
-{
- delete d;
-}
-
-QFxPixmap &QFxPixmap::operator=(const QFxPixmap &o)
-{
- d->pixmap = o.d->pixmap;
- return *this;
-}
-
-bool QFxPixmap::isNull() const
-{
- return d->pixmap.isNull();
-}
-
-int QFxPixmap::width() const
-{
- return d->pixmap.width();
-}
-
-int QFxPixmap::height() const
-{
- return d->pixmap.height();
-}
-
-QFxPixmap::operator const QPixmap &() const
-{
- return d->pixmap;
-}
-
/*!
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,
@@ -215,20 +174,28 @@ QFxPixmap::operator const QPixmap &() const
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.
*/
-QNetworkReply *QFxPixmap::get(QmlEngine *engine, const QUrl& url, QObject* obj, const char* slot)
+QNetworkReply *QFxPixmap::get(QmlEngine *engine, const QUrl& url, QPixmap *pixmap)
{
#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
if (url.scheme()==QLatin1String("file")) {
- QObject dummy;
- QObject::connect(&dummy, SIGNAL(destroyed()), obj, slot);
+ QString key = url.toString();
+ if (!QPixmapCache::find(key,pixmap)) {
+ QFile f(url.toLocalFile());
+ if (f.open(QIODevice::ReadOnly)) {
+ if (!readImage(&f, pixmap)) {
+ qWarning() << "Format error loading" << url;
+ *pixmap = QPixmap();
+ }
+ } else
+ *pixmap = QPixmap();
+ QPixmapCache::insert(key, *pixmap);
+ }
return 0;
}
#endif
QString key = url.toString();
- if (QPixmapCache::find(key,0)) {
- QObject dummy;
- QObject::connect(&dummy, SIGNAL(destroyed()), obj, slot);
+ if (QPixmapCache::find(key,pixmap)) {
return 0;
}
@@ -242,7 +209,6 @@ QNetworkReply *QFxPixmap::get(QmlEngine *engine, const QUrl& url, QObject* obj,
(*iter)->addRef();
}
- QObject::connect((*iter)->reply, SIGNAL(finished()), obj, slot);
return (*iter)->reply;
}
diff --git a/src/declarative/fx/qfxpixmap.h b/src/declarative/fx/qfxpixmap.h
index f63299b..ec8d2be 100644
--- a/src/declarative/fx/qfxpixmap.h
+++ b/src/declarative/fx/qfxpixmap.h
@@ -54,29 +54,13 @@ QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class QmlEngine;
class QNetworkReply;
-class QFxPixmapPrivate;
-class Q_DECLARATIVE_EXPORT QFxPixmap
+class Q_DECLARATIVE_EXPORT QFxPixmap //### rename QFxPixmapCache
{
public:
- QFxPixmap();
- QFxPixmap(const QUrl& url); // url must have been passed to QFxPixmap::get, and finished.
- QFxPixmap(const QFxPixmap &);
- virtual ~QFxPixmap();
-
- QFxPixmap &operator=(const QFxPixmap &);
-
- static QNetworkReply *get(QmlEngine *, const QUrl& url, QObject*, const char* slot);
+ static QNetworkReply *get(QmlEngine *, const QUrl& url, QPixmap *pixmap);
static void cancelGet(const QUrl& url, QObject* obj);
- bool isNull() const;
-
- int width() const;
- int height() const;
-
- operator const QPixmap &() const;
-
-private:
- QFxPixmapPrivate *d;
+ static bool find(const QUrl& url, QPixmap *pixmap); // url must have been passed to QFxPixmap::get, and finished. Or must be a local file.
};