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

View File

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

View File

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

View File

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

View File

@ -167,6 +167,24 @@ QImage Metadata::customIcon(const Uuid& uuid) const
return m_customIcons.value(uuid); 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 bool Metadata::containsCustomIcon(const Uuid& uuid) const
{ {
return m_customIcons.contains(uuid); return m_customIcons.contains(uuid);
@ -338,6 +356,8 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
Q_ASSERT(!m_customIcons.contains(uuid)); Q_ASSERT(!m_customIcons.contains(uuid));
m_customIcons.insert(uuid, icon); 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); m_customIconsOrder.append(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count()); Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified(); Q_EMIT modified();
@ -365,6 +385,8 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
Q_ASSERT(m_customIcons.contains(uuid)); Q_ASSERT(m_customIcons.contains(uuid));
m_customIcons.remove(uuid); m_customIcons.remove(uuid);
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
m_customIconCacheKeys.remove(uuid);
m_customIconsOrder.removeAll(uuid); m_customIconsOrder.removeAll(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count()); Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified(); Q_EMIT modified();

View File

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