From 4752adf9d3347f73cfedbec589bf3ffbe43a81c8 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sun, 24 Jan 2016 17:56:35 +0100 Subject: [PATCH] Move pixmap caching to Metadata. --- src/core/Entry.cpp | 15 +++++---------- src/core/Entry.h | 2 -- src/core/Group.cpp | 15 +++++---------- src/core/Group.h | 1 - src/core/Metadata.cpp | 22 ++++++++++++++++++++++ src/core/Metadata.h | 4 ++++ 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 4f977915b..0634e20b5 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -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(); } diff --git a/src/core/Entry.h b/src/core/Entry.h index ae07ed453..4b3502f74 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -169,7 +168,6 @@ private: Entry* m_tmpHistoryItem; bool m_modifiedSinceBegin; QPointer m_group; - mutable QPixmapCache::Key m_pixmapCacheKey; bool m_updateTimeinfo; }; diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 57e0bae95..ece40df74 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -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); diff --git a/src/core/Group.h b/src/core/Group.h index 3d3618044..2bc20b7b0 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -155,7 +155,6 @@ private: QList m_entries; QPointer m_parent; - mutable QPixmapCache::Key m_pixmapCacheKey; bool m_updateTimeinfo; diff --git a/src/core/Metadata.cpp b/src/core/Metadata.cpp index 3256ca05e..73074e827 100644 --- a/src/core/Metadata.cpp +++ b/src/core/Metadata.cpp @@ -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(); diff --git a/src/core/Metadata.h b/src/core/Metadata.h index 4164fb63e..a15e3e0e7 100644 --- a/src/core/Metadata.h +++ b/src/core/Metadata.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #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 customIcons() const; QList customIconsOrder() const; @@ -153,6 +156,7 @@ private: MetadataData m_data; QHash m_customIcons; + mutable QHash m_customIconCacheKeys; QList m_customIconsOrder; QPointer m_recycleBin;