Commit fc091bae authored by Robby Stephenson's avatar Robby Stephenson
Browse files

Add icon cache to improve icon view performance.

Initial cache size is 1000 and can be changed with
"Icon Cache Size" general config option.

Patch from Pedro Miguel Carvalho.
BUG:272583
parent 91faa1a3
2011-05-06 Robby Stephenson <robby@periapsis.org>
* Added icon cache to improve performance with large images
(Patch from Pedro Miguel Carvalho, Bug 272583).
2011-05-01 Robby Stephenson <robby@periapsis.org>
* Changed image loading to use known id instead of recalculating, improves performance.
......
......@@ -110,6 +110,9 @@
<entry key="Max Icon Size" type="Int">
<default>96</default>
</entry>
<entry key="Icon Cache Size" type="Int">
<default>1000</default>
</entry>
<entry key="Image Cache Size" type="Int">
<default code="true">(64 * 1024 * 1024)</default>
</entry>
......
/***************************************************************************
Copyright (C) 2008-2009 Robby Stephenson <robby@periapsis.org>
Copyright (C) 2011 Pedro Miguel Carvalho <kde@pmc.com.pt>
***************************************************************************/
/***************************************************************************
......@@ -29,13 +30,13 @@
#include "../entry.h"
#include "../field.h"
#include "../images/image.h"
#include "../core/tellico_config.h"
#include "../tellico_debug.h"
#include <kicon.h>
using Tellico::EntryTitleModel;
EntryTitleModel::EntryTitleModel(QObject* parent) : AbstractEntryModel(parent) {
m_iconCache.setMaxCost(Config::iconCacheSize());
}
EntryTitleModel::~EntryTitleModel() {
......@@ -46,7 +47,7 @@ int EntryTitleModel::columnCount(const QModelIndex&) const {
return rowCount() > 0 ? entry(createIndex(0, 0))->collection()->fields().count() : 1;
}
// TODO: combine this with EntryModel::headerData inso AnstractEntryModel
// TODO: combine this with EntryModel::headerData into AnstractEntryModel
QVariant EntryTitleModel::headerData(int section_, Qt::Orientation orientation_, int role_) const {
if(section_ < 0 || section_ >= columnCount() || orientation_ != Qt::Horizontal) {
return QVariant();
......@@ -96,14 +97,20 @@ QVariant EntryTitleModel::data(const QModelIndex& index_, int role_) const {
QString fieldName = imageField(entry->collection());
if(fieldName.isEmpty()) {
return defaultIcon(entry->collection());
} else {
const Data::Image& img = ImageFactory::imageById(entry->field(fieldName));
if(img.isNull()) {
return defaultIcon(entry->collection());
}
return KIcon(QPixmap::fromImage(img));
}
return QVariant();
const QString id = entry->field(fieldName);
KIcon* icon = m_iconCache.object( id );
if(icon) {
return KIcon(*icon);
}
const Data::Image& img = ImageFactory::imageById(id);
if(img.isNull()) {
return defaultIcon(entry->collection());
}
icon = new KIcon(QPixmap::fromImage(img));
m_iconCache.insert(id, icon);
return KIcon(*icon);
}
case EntryPtrRole:
return qVariantFromValue(entry);
......@@ -137,4 +144,9 @@ QString EntryTitleModel::imageField(Tellico::Data::CollPtr coll_) const {
return m_imageFields.value(coll_->id());
}
void EntryTitleModel::clear() {
m_iconCache.clear();
AbstractEntryModel::clear();
}
#include "entrytitlemodel.moc"
/***************************************************************************
Copyright (C) 2008-2009 Robby Stephenson <robby@periapsis.org>
Copyright (C) 2011 Pedro Miguel Carvalho <kde@pmc.com.pt>
***************************************************************************/
/***************************************************************************
......@@ -27,9 +28,10 @@
#include "abstractentrymodel.h"
#include <QHash>
#include <KIcon>
class KIcon;
#include <QHash>
#include <QCache>
namespace Tellico {
......@@ -47,12 +49,15 @@ public:
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual void clear();
private:
const KIcon& defaultIcon(Data::CollPtr coll) const;
QString imageField(Data::CollPtr coll) const;
mutable QHash<int, KIcon*> m_defaultIcons;
mutable QHash<long, QString> m_imageFields;
mutable QCache<QString, KIcon> m_iconCache;
};
} // end namespace
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment