Move pixmap caching to Metadata.

This commit is contained in:
Felix Geyer 2016-01-24 17:56:35 +01:00
parent 2d741afe3e
commit 4752adf9d3
6 changed files with 36 additions and 23 deletions

View File

@ -114,13 +114,12 @@ QPixmap Entry::iconPixmap() const
else {
Q_ASSERT(database());
QPixmap pixmap;
if (database() && !QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(database()->metadata()->customIcon(m_data.customIcon));
m_pixmapCacheKey = QPixmapCache::insert(pixmap);
if (database()) {
return database()->metadata()->customIconPixmap(m_data.customIcon);
}
else {
return QPixmap();
}
return pixmap;
}
}
@ -248,8 +247,6 @@ void Entry::setIcon(int iconNumber)
m_data.iconNumber = iconNumber;
m_data.customIcon = Uuid();
m_pixmapCacheKey = QPixmapCache::Key();
Q_EMIT modified();
emitDataChanged();
}
@ -263,8 +260,6 @@ void Entry::setIcon(const Uuid& uuid)
m_data.customIcon = uuid;
m_data.iconNumber = 0;
m_pixmapCacheKey = QPixmapCache::Key();
Q_EMIT modified();
emitDataChanged();
}

View File

@ -22,7 +22,6 @@
#include <QImage>
#include <QMap>
#include <QPixmap>
#include <QPixmapCache>
#include <QPointer>
#include <QSet>
#include <QUrl>
@ -169,7 +168,6 @@ private:
Entry* m_tmpHistoryItem;
bool m_modifiedSinceBegin;
QPointer<Group> m_group;
mutable QPixmapCache::Key m_pixmapCacheKey;
bool m_updateTimeinfo;
};

View File

@ -134,13 +134,12 @@ QPixmap Group::iconPixmap() const
else {
Q_ASSERT(m_db);
QPixmap pixmap;
if (m_db && !QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(m_db->metadata()->customIcon(m_data.customIcon));
m_pixmapCacheKey = QPixmapCache::insert(pixmap);
if (m_db) {
return m_db->metadata()->customIconPixmap(m_data.customIcon);
}
else {
return QPixmap();
}
return pixmap;
}
}
@ -214,8 +213,6 @@ void Group::setIcon(int iconNumber)
m_data.iconNumber = iconNumber;
m_data.customIcon = Uuid();
m_pixmapCacheKey = QPixmapCache::Key();
updateTimeinfo();
Q_EMIT modified();
Q_EMIT dataChanged(this);
@ -230,8 +227,6 @@ void Group::setIcon(const Uuid& uuid)
m_data.customIcon = uuid;
m_data.iconNumber = 0;
m_pixmapCacheKey = QPixmapCache::Key();
updateTimeinfo();
Q_EMIT modified();
Q_EMIT dataChanged(this);

View File

@ -155,7 +155,6 @@ private:
QList<Entry*> m_entries;
QPointer<Group> m_parent;
mutable QPixmapCache::Key m_pixmapCacheKey;
bool m_updateTimeinfo;

View File

@ -167,6 +167,24 @@ QImage Metadata::customIcon(const Uuid& uuid) const
return m_customIcons.value(uuid);
}
QPixmap Metadata::customIconPixmap(const Uuid& uuid) const
{
QPixmap pixmap;
if (!m_customIcons.contains(uuid)) {
return pixmap;
}
QPixmapCache::Key& cacheKey = m_customIconCacheKeys[uuid];
if (!QPixmapCache::find(cacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(m_customIcons.value(uuid));
cacheKey = QPixmapCache::insert(pixmap);
}
return pixmap;
}
bool Metadata::containsCustomIcon(const Uuid& uuid) const
{
return m_customIcons.contains(uuid);
@ -338,6 +356,8 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
Q_ASSERT(!m_customIcons.contains(uuid));
m_customIcons.insert(uuid, icon);
// reset cache in case there is also an icon with that uuid
m_customIconCacheKeys[uuid] = QPixmapCache::Key();
m_customIconsOrder.append(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();
@ -365,6 +385,8 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
Q_ASSERT(m_customIcons.contains(uuid));
m_customIcons.remove(uuid);
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
m_customIconCacheKeys.remove(uuid);
m_customIconsOrder.removeAll(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();

View File

@ -22,6 +22,8 @@
#include <QDateTime>
#include <QHash>
#include <QImage>
#include <QPixmap>
#include <QPixmapCache>
#include <QPointer>
#include "core/Global.h"
@ -78,6 +80,7 @@ public:
bool protectNotes() const;
// bool autoEnableVisualHiding() const;
QImage customIcon(const Uuid& uuid) const;
QPixmap customIconPixmap(const Uuid& uuid) const;
bool containsCustomIcon(const Uuid& uuid) const;
QHash<Uuid, QImage> customIcons() const;
QList<Uuid> customIconsOrder() const;
@ -153,6 +156,7 @@ private:
MetadataData m_data;
QHash<Uuid, QImage> m_customIcons;
mutable QHash<Uuid, QPixmapCache::Key> m_customIconCacheKeys;
QList<Uuid> m_customIconsOrder;
QPointer<Group> m_recycleBin;