thumbnailmodel.cpp Example File
mediabrowser/thumbnailmodel.cpp
#include "thumbnailmodel.h"
#include <QtCore/qcryptographichash.h>
#include <QtCore/qdir.h>
#include <QtCore/qtconcurrentrun.h>
#include <QtCore/qurl.h>
#include <QtGui/qimagereader.h>
#if defined(Q_WS_MAEMO_5)
const QSize ThumbnailModel::thumbnailSize(124, 124);
#else
const QSize ThumbnailModel::thumbnailSize(128, 128);
#endif
ThumbnailModel::ThumbnailModel(QAbstractGallery *gallery, QObject *parent)
: QGalleryQueryModel(gallery, parent)
{
}
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();
}
QString ThumbnailModel::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();
}
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