summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew den Exter <andrew.den-exter@nokia.com>2010-10-22 14:49:56 +1000
committerAndrew den Exter <andrew.den-exter@nokia.com>2010-10-25 11:11:16 +1000
commit5f1884dd01581412da4721f9d7fe2694cc37a9e5 (patch)
tree40c1bc5142d26baaca31fba06f10c6daaa199545
parent85add5d14e5d126e8dbd5625ebdad70c056b5136 (diff)
Enable image thumnbails in mediabrowser on symbian.
Use QThread instead of QtConcurrent to handle the loading of images in mediabrowser. QFuture isn't enabled on symbian so the old solution had to be disabled. Using QThread also means we can lower the priority of the image loading thread which improves the responsiveness of the UI on other platforms. Task-number: MOBILITY-1816 Reviewed-by: Justin McPherson
-rw-r--r--examples/mediabrowser/coverartmodel.cpp6
-rw-r--r--examples/mediabrowser/coverartmodel.h4
-rw-r--r--examples/mediabrowser/mediabrowser.pro2
-rw-r--r--examples/mediabrowser/thumbnailcache.cpp212
-rw-r--r--examples/mediabrowser/thumbnailcache.h87
-rw-r--r--examples/mediabrowser/thumbnailmodel.cpp90
-rw-r--r--examples/mediabrowser/thumbnailmodel.h11
7 files changed, 314 insertions, 98 deletions
diff --git a/examples/mediabrowser/coverartmodel.cpp b/examples/mediabrowser/coverartmodel.cpp
index 29739da802..0e97501ad1 100644
--- a/examples/mediabrowser/coverartmodel.cpp
+++ b/examples/mediabrowser/coverartmodel.cpp
@@ -58,9 +58,8 @@ CoverArtModel::CoverArtModel(QAbstractGallery *gallery, QObject *parent)
{
}
-#ifndef QT_NO_QFUTURE
#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
-QString CoverArtModel::imagePath(const QModelIndex &index) const
+QUrl CoverArtModel::imageUrl(const QModelIndex &index) const
{
QString title = index.data(Qt::DisplayRole).toString();
QString artist = index.data(AlbumDelegate::Artist).toString();
@@ -72,7 +71,7 @@ QString CoverArtModel::imagePath(const QModelIndex &index) const
+ hash(title)
+ QLatin1String(".jpeg");
- return thumbnailPath(QUrl::fromLocalFile(fileName));
+ return QUrl::fromLocalFile(fileName);
}
QString CoverArtModel::hash(const QString &identifier) const
@@ -86,4 +85,3 @@ QString CoverArtModel::hash(const QString &identifier) const
}
}
#endif
-#endif
diff --git a/examples/mediabrowser/coverartmodel.h b/examples/mediabrowser/coverartmodel.h
index 7e1926a537..0fb6027d83 100644
--- a/examples/mediabrowser/coverartmodel.h
+++ b/examples/mediabrowser/coverartmodel.h
@@ -53,10 +53,9 @@ class CoverArtModel : public ThumbnailModel
public:
CoverArtModel(QAbstractGallery *gallery, QObject *parent = 0);
-#ifndef QT_NO_QFUTURE
#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
protected:
- QString imagePath(const QModelIndex &index) const;
+ QUrl imageUrl(const QModelIndex &index) const;
private:
QString hash(const QString &identifier) const;
@@ -64,7 +63,6 @@ private:
QRegExp illegalCharacters;
QString whitespace;
#endif
-#endif
};
#endif
diff --git a/examples/mediabrowser/mediabrowser.pro b/examples/mediabrowser/mediabrowser.pro
index fe629c5025..1a6d44d4f9 100644
--- a/examples/mediabrowser/mediabrowser.pro
+++ b/examples/mediabrowser/mediabrowser.pro
@@ -19,6 +19,7 @@ HEADERS = \
photodelegate.h \
photoview.h \
songview.h \
+ thumbnailcache.h \
thumbnailmodel.h
SOURCES = \
@@ -33,6 +34,7 @@ SOURCES = \
photodelegate.cpp \
photoview.cpp \
songview.cpp \
+ thumbnailcache.cpp \
thumbnailmodel.cpp
symbian: {
diff --git a/examples/mediabrowser/thumbnailcache.cpp b/examples/mediabrowser/thumbnailcache.cpp
new file mode 100644
index 0000000000..d59a5d7524
--- /dev/null
+++ b/examples/mediabrowser/thumbnailcache.cpp
@@ -0,0 +1,212 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Qt Mobility Components.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "thumbnailcache.h"
+
+#include "thumbnailmodel.h"
+
+#include <QtCore/qcryptographichash.h>
+#include <QtCore/qcoreapplication.h>
+#include <QtCore/qcoreevent.h>
+#include <QtCore/qdir.h>
+#include <QtCore/qurl.h>
+#include <QtGui/qimagereader.h>
+#include <QtGui/qpixmap.h>
+
+class Thumbnail
+{
+public:
+ Thumbnail(const QUrl &url)
+ : url(url)
+ {
+ }
+
+ const QUrl url;
+ QPixmap pixmap;
+
+private:
+ ThumbnailCache *cache;
+};
+
+class ThumbnailEvent : public QEvent
+{
+public:
+ ThumbnailEvent(const QUrl &url, const QImage &image)
+ : QEvent(QEvent::User)
+ , url(url)
+ , image(image)
+ {
+ }
+
+ const QUrl url;
+ const QImage image;
+};
+
+ThumbnailCache::ThumbnailCache(QObject *parent)
+ : QThread(parent)
+#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
+ , m_cache(25)
+#else
+ , m_cache(150)
+#endif
+ , cancelled(false)
+{
+ start(QThread::LowestPriority);
+}
+
+ThumbnailCache::~ThumbnailCache()
+{
+ {
+ QMutexLocker locker(&mutex);
+
+ cancelled = true;
+
+ waitCondition.wakeOne();
+ }
+ wait();
+}
+
+QPixmap ThumbnailCache::thumbnail(const QUrl &url)
+{
+ QMutexLocker locker(&mutex);
+
+ if (Thumbnail *thumbnail = cache.object(url)) {
+ return thumbnail->pixmap;
+ } else {
+ pendingUrls.enqueue(url);
+
+ cache.insert(url, new Thumbnail(url));
+
+ waitCondition.wakeOne();
+
+ return QPixmap();
+ }
+}
+
+bool ThumbnailCache::event(QEvent *event)
+{
+ if (event->type() == QEvent::User) {
+ ThumbnailEvent *thumbnailEvent = static_cast<ThumbnailEvent *>(event);
+
+ QMutexLocker locker(&mutex);
+
+ if (Thumbnail *thumbnail = cache.object(thumbnailEvent->url)) {
+ thumbnail->pixmap = QPixmap::fromImage(thumbnailEvent->image);
+
+ locker.unlock();
+
+ emit thumbnailReady();
+ }
+ return true;
+ } else {
+ return QThread::event(event);
+ }
+}
+
+void ThumbnailCache::run()
+{
+ QMutexLocker locker(&mutex);
+
+ while(!cancelled) {
+ if (!pendingUrls.isEmpty()) {
+ const QUrl url = pendingUrls.dequeue();
+
+ if (cache.contains(url)) {
+ locker.unlock();
+ QImage image = loadImage(url);
+ locker.relock();
+
+ if (!image.isNull())
+ QCoreApplication::postEvent(this, new ThumbnailEvent(url, image));
+ }
+ } else {
+ waitCondition.wait(&mutex);
+ }
+ }
+}
+
+QImage ThumbnailCache::loadImage(const QUrl &url) const
+{
+ const QString fileName = thumbnailPath(url);
+
+ QImageReader reader(fileName);
+ reader.setQuality(25);
+
+ if (reader.supportsOption(QImageIOHandler::Size)) {
+ QSize size = reader.size();
+
+ if (!reader.supportsOption(QImageIOHandler::ScaledSize)
+ && (size.width() > 1280 || size.height() > 1280)) {
+ return QImage();
+ }
+
+ if (size.width() > ThumbnailModel::thumbnailSize.width()
+ || size.height() > ThumbnailModel::thumbnailSize.height()) {
+ size.scale(ThumbnailModel::thumbnailSize, Qt::KeepAspectRatio);
+ }
+
+ reader.setScaledSize(size);
+ } else {
+ reader.setScaledSize(ThumbnailModel::thumbnailSize);
+ }
+
+ return reader.read();
+}
+
+QString ThumbnailCache::thumbnailPath(const QUrl &url) const
+{
+#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
+#if defined(Q_WS_MAEMO_5)
+ QString thumbnailPath = QDir::homePath()
+ + QLatin1String("/.thumbnails/cropped/")
+ + QCryptographicHash::hash(url.toString().toUtf8(), QCryptographicHash::Md5).toHex()
+ + QLatin1String(".jpeg");
+#else
+ QString thumbnailPath = QDir::homePath()
+ + QLatin1String("/.thumbnails/normal/")
+ + QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex()
+ + QLatin1String(".png");
+#endif
+ if (QFile::exists(thumbnailPath))
+ return thumbnailPath;
+#endif
+
+ return url.path();
+}
diff --git a/examples/mediabrowser/thumbnailcache.h b/examples/mediabrowser/thumbnailcache.h
new file mode 100644
index 0000000000..9640e8130c
--- /dev/null
+++ b/examples/mediabrowser/thumbnailcache.h
@@ -0,0 +1,87 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Qt Mobility Components.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef THUMBNAILCACHE_H
+#define THUMBNAILCACHE_H
+
+#include <QtCore/qcache.h>
+#include <QtCore/qmutex.h>
+#include <QtCore/qqueue.h>
+#include <QtCore/qthread.h>
+#include <QtCore/qwaitcondition.h>
+
+
+QT_BEGIN_NAMESPACE
+class QImage;
+class QPixmap;
+class QUrl;
+QT_END_NAMESPACE
+
+class Thumbnail;
+
+class ThumbnailCache : public QThread
+{
+ Q_OBJECT
+public:
+ ThumbnailCache(QObject *parent = 0);
+ ~ThumbnailCache();
+
+ QPixmap thumbnail(const QUrl &url);
+
+ bool event(QEvent *event);
+
+signals:
+ void thumbnailReady();
+
+protected:
+ void run();
+
+private:
+ QImage loadImage(const QUrl &url) const;
+ QString thumbnailPath(const QUrl &url) const;
+
+ QMutex mutex;
+ QWaitCondition waitCondition;
+ QCache<QUrl, Thumbnail> cache;
+ QQueue<QUrl> pendingUrls;
+ bool cancelled;
+};
+
+#endif
diff --git a/examples/mediabrowser/thumbnailmodel.cpp b/examples/mediabrowser/thumbnailmodel.cpp
index b880faef1a..cc9d76846a 100644
--- a/examples/mediabrowser/thumbnailmodel.cpp
+++ b/examples/mediabrowser/thumbnailmodel.cpp
@@ -40,11 +40,8 @@
#include "thumbnailmodel.h"
-#include <QtCore/qcryptographichash.h>
-#include <QtCore/qdir.h>
-#include <QtCore/qtconcurrentrun.h>
#include <QtCore/qurl.h>
-#include <QtGui/qimagereader.h>
+#include <QtGui/qpixmap.h>
#if defined(Q_WS_MAEMO_5)
const QSize ThumbnailModel::thumbnailSize(124, 124);
@@ -55,99 +52,26 @@ const QSize ThumbnailModel::thumbnailSize(128, 128);
ThumbnailModel::ThumbnailModel(QAbstractGallery *gallery, QObject *parent)
: QGalleryQueryModel(gallery, parent)
{
+ connect(&cache, SIGNAL(thumbnailReady()), this, SLOT(thumbnailLoaded()));
}
ThumbnailModel::~ThumbnailModel()
{
}
-#ifndef QT_NO_QFUTURE
-
QVariant ThumbnailModel::data(const QModelIndex &index, int role) const
{
- if (role == Qt::DecorationRole && index.isValid()) {
- QString id = itemId(index).toString();
-
- QFutureWatcher<QImage> *future = cache.object(id);
-
- if (!future) {
- future = new QFutureWatcher<QImage>;
-
- QString path = imagePath(index);
-
- if (!path.isEmpty()) {
- future->setFuture(QtConcurrent::run(ThumbnailModel::load, path));
-
- connect(future, SIGNAL(finished()), this, SLOT(thumbnailLoaded()));
- }
-
- cache.insert(id, future);
- }
-
- return !future->isCanceled()
- ? future->result()
- : QVariant();
- } else {
- return QGalleryQueryModel::data(index, role);
- }
-}
-
-QString ThumbnailModel::imagePath(const QModelIndex &index) const
-{
- QUrl url = itemUrl(index);
-
- return url.isValid() ? thumbnailPath(url) : QString();
+ return role == Qt::DecorationRole && index.isValid()
+ ? cache.thumbnail(imageUrl(index))
+ : QGalleryQueryModel::data(index, role);
}
-QString ThumbnailModel::thumbnailPath(const QUrl &url) const
+QUrl ThumbnailModel::imageUrl(const QModelIndex &index) const
{
-#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
-#if defined(Q_WS_MAEMO_5)
- QString thumbnailPath = QDir::homePath()
- + QLatin1String("/.thumbnails/cropped/")
- + QCryptographicHash::hash(url.toString().toUtf8(), QCryptographicHash::Md5).toHex()
- + QLatin1String(".jpeg");
-#else
- QString thumbnailPath = QDir::homePath()
- + QLatin1String("/.thumbnails/normal/")
- + QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex()
- + QLatin1String(".png");
-#endif
- if (QFile::exists(thumbnailPath))
- return thumbnailPath;
-#endif
-
- return url.path();
+ return itemUrl(index);
}
void ThumbnailModel::thumbnailLoaded()
{
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() -1));
}
-
-QImage ThumbnailModel::load(const QString &fileName)
-{
- QImageReader reader(fileName);
- reader.setQuality(25);
-
- if (reader.supportsOption(QImageIOHandler::Size)) {
- QSize size = reader.size();
-
- if (!reader.supportsOption(QImageIOHandler::ScaledSize)
- && (size.width() > 1280 || size.height() > 1280)) {
- return QImage();
- }
-
- if (size.width() > thumbnailSize.width() || size.height() > thumbnailSize.height())
- size.scale(thumbnailSize, Qt::KeepAspectRatio);
-
- reader.setScaledSize(size);
- } else {
- reader.setScaledSize(thumbnailSize);
- }
-
- return reader.read();
-}
-
-#endif
-
diff --git a/examples/mediabrowser/thumbnailmodel.h b/examples/mediabrowser/thumbnailmodel.h
index 574f22c28e..961c604678 100644
--- a/examples/mediabrowser/thumbnailmodel.h
+++ b/examples/mediabrowser/thumbnailmodel.h
@@ -43,8 +43,7 @@
#include <qgalleryquerymodel.h>
-#include <QtCore/qcache.h>
-#include <QtCore/qfuturewatcher.h>
+#include "thumbnailcache.h"
QTM_USE_NAMESPACE
@@ -57,21 +56,17 @@ public:
ThumbnailModel(QAbstractGallery *gallery, QObject *parent = 0);
~ThumbnailModel();
-#ifndef QT_NO_QFUTURE
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
protected:
- virtual QString imagePath(const QModelIndex &index) const;
+ virtual QUrl imageUrl(const QModelIndex &index) const;
- QString thumbnailPath(const QUrl &url) const;
private slots:
void thumbnailLoaded();
private:
- static QImage load(const QString &fileName);
+ mutable ThumbnailCache cache;
- mutable QCache<QString, QFutureWatcher<QImage> > cache;
-#endif
};
#endif