Skip to content

Commit

Permalink
Add icon size parameter to Metadata::customIconsScaledPixmap(s)
Browse files Browse the repository at this point in the history
Previously all they could return was a 16x16 icon, now they have
a parameter for the caller to specify the icon size (must be
square).

Implements keepassxreboot#4071
  • Loading branch information
wolframroesler committed May 9, 2020
1 parent e93c2ec commit ba91abb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/core/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ void Metadata::clear()
init();
m_customIcons.clear();
m_customIconCacheKeys.clear();
m_customIconScaledCacheKeys.clear();
m_customIconsOrder.clear();
m_customIconsHashes.clear();
m_customData->clear();
Expand Down Expand Up @@ -199,16 +198,21 @@ QPixmap Metadata::customIconPixmap(const QUuid& uuid) const
return pixmap;
}

QPixmap Metadata::customIconScaledPixmap(const QUuid& uuid, const QSize& size) const
QPixmap Metadata::customIconScaledPixmap(const QUuid& uuid, int size /*=16*/) const
{
QPixmap pixmap;

if (!m_customIcons.contains(uuid)) {
return pixmap;
}

QImage image = m_customIcons.value(uuid).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
pixmap = QPixmap::fromImage(image);
const auto cacheKey = QString("%1-%2").arg(uuid.toString(), size);

if (!QPixmapCache::find(cacheKey, &pixmap)) {
QImage image = m_customIcons.value(uuid).scaled(size, size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
pixmap = QPixmap::fromImage(image);
QPixmapCache::insert(cacheKey, pixmap);
}

return pixmap;
}
Expand All @@ -223,7 +227,7 @@ QHash<QUuid, QImage> Metadata::customIcons() const
return m_customIcons;
}

QHash<QUuid, QPixmap> Metadata::customIconsScaledPixmaps(const QSize& size) const
QHash<QUuid, QPixmap> Metadata::customIconsScaledPixmaps(int size /*=16*/) const
{
QHash<QUuid, QPixmap> result;

Expand Down Expand Up @@ -395,7 +399,6 @@ void Metadata::addCustomIcon(const QUuid& uuid, const QImage& icon)
m_customIcons[uuid] = icon;
// reset cache in case there is also an icon with that uuid
m_customIconCacheKeys[uuid] = QPixmapCache::Key();
m_customIconScaledCacheKeys[uuid] = QPixmapCache::Key();
// remove all uuids to prevent duplicates in release mode
m_customIconsOrder.removeAll(uuid);
m_customIconsOrder.append(uuid);
Expand Down Expand Up @@ -434,8 +437,6 @@ void Metadata::removeCustomIcon(const QUuid& uuid)
m_customIcons.remove(uuid);
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
m_customIconCacheKeys.remove(uuid);
QPixmapCache::remove(m_customIconScaledCacheKeys.value(uuid));
m_customIconScaledCacheKeys.remove(uuid);
m_customIconsOrder.removeAll(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
emit metadataModified();
Expand Down
5 changes: 2 additions & 3 deletions src/core/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class Metadata : public QObject
bool protectNotes() const;
QImage customIcon(const QUuid& uuid) const;
QPixmap customIconPixmap(const QUuid& uuid) const;
QPixmap customIconScaledPixmap(const QUuid& uuid, const QSize& size = {16, 16}) const;
QPixmap customIconScaledPixmap(const QUuid& uuid, int size=16) const;
bool containsCustomIcon(const QUuid& uuid) const;
QHash<QUuid, QImage> customIcons() const;
QList<QUuid> customIconsOrder() const;
bool recycleBinEnabled() const;
QHash<QUuid, QPixmap> customIconsScaledPixmaps(const QSize& size = {16, 16}) const;
QHash<QUuid, QPixmap> customIconsScaledPixmaps(int size=16) const;
Group* recycleBin();
const Group* recycleBin() const;
QDateTime recycleBinChanged() const;
Expand Down Expand Up @@ -163,7 +163,6 @@ class Metadata : public QObject

QHash<QUuid, QImage> m_customIcons;
mutable QHash<QUuid, QPixmapCache::Key> m_customIconCacheKeys;
mutable QHash<QUuid, QPixmapCache::Key> m_customIconScaledCacheKeys;
QList<QUuid> m_customIconsOrder;
QHash<QByteArray, QUuid> m_customIconsHashes;

Expand Down

0 comments on commit ba91abb

Please sign in to comment.