mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-02-02 09:34:58 -05:00
Store database icons as QImage instead of QIcon.
This has the advantage that they can be used without a running X server. Add methods to retrieve QPixmaps that are converted from the stored QImages and cached by QPixmapCache.
This commit is contained in:
parent
fdf600e09a
commit
00aafa69f5
@ -20,7 +20,6 @@
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QHash>
|
||||
#include <QtGui/QIcon>
|
||||
|
||||
#include "core/Uuid.h"
|
||||
#include "keys/CompositeKey.h"
|
||||
|
@ -19,20 +19,40 @@
|
||||
|
||||
#include "core/DataPath.h"
|
||||
|
||||
DatabaseIcons* DatabaseIcons::m_instance(0);
|
||||
|
||||
QIcon DatabaseIcons::icon(int index)
|
||||
QImage DatabaseIcons::icon(int index)
|
||||
{
|
||||
if (index < 0 || index >= iconCount()) {
|
||||
qWarning("DatabaseIcons::icon: invalid icon index %d", index);
|
||||
return QIcon();
|
||||
return QImage();
|
||||
}
|
||||
|
||||
if (!m_instance) {
|
||||
m_instance = new DatabaseIcons();
|
||||
if (!m_iconCache[index].isNull()) {
|
||||
return m_iconCache[index];
|
||||
}
|
||||
else {
|
||||
QString iconPath = QString("icons/database/").append(m_indexToName.at(index));
|
||||
QImage icon(DataPath::getPath(iconPath));
|
||||
|
||||
m_iconCache[index] = icon;
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap DatabaseIcons::iconPixmap(int index)
|
||||
{
|
||||
if (index < 0 || index >= iconCount()) {
|
||||
qWarning("DatabaseIcons::iconPixmap: invalid icon index %d", index);
|
||||
return QPixmap();
|
||||
}
|
||||
|
||||
return m_instance->getIconInternal(index);
|
||||
QPixmap pixmap;
|
||||
|
||||
if (!QPixmapCache::find(m_pixmapCacheKeys[index], &pixmap)) {
|
||||
pixmap = QPixmap::fromImage(icon(index));
|
||||
m_pixmapCacheKeys[index] = QPixmapCache::insert(pixmap);
|
||||
}
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
int DatabaseIcons::iconCount()
|
||||
@ -42,6 +62,7 @@ int DatabaseIcons::iconCount()
|
||||
|
||||
DatabaseIcons::DatabaseIcons()
|
||||
{
|
||||
m_indexToName.reserve(iconCount());
|
||||
m_indexToName.append("C00_Password.png");
|
||||
m_indexToName.append("C01_Package_Network.png");
|
||||
m_indexToName.append("C02_MessageBox_Warning.png");
|
||||
@ -113,18 +134,20 @@ DatabaseIcons::DatabaseIcons()
|
||||
m_indexToName.append("C68_BlackBerry.png");
|
||||
|
||||
Q_ASSERT(m_indexToName.size() == iconCount());
|
||||
|
||||
m_iconCache.reserve(iconCount());
|
||||
m_iconCache.resize(iconCount());
|
||||
m_pixmapCacheKeys.reserve(iconCount());
|
||||
m_pixmapCacheKeys.resize(iconCount());
|
||||
}
|
||||
|
||||
QIcon DatabaseIcons::getIconInternal(int index)
|
||||
DatabaseIcons* databaseIcons()
|
||||
{
|
||||
if (m_iconCache.contains(index)) {
|
||||
return m_iconCache.value(index);
|
||||
}
|
||||
else {
|
||||
QString iconPath = QString("icons/database/").append(m_indexToName.at(index));
|
||||
QIcon icon(DataPath::getPath(iconPath));
|
||||
static DatabaseIcons* instance(0);
|
||||
|
||||
m_iconCache.insert(index, icon);
|
||||
return icon;
|
||||
if (!instance) {
|
||||
instance = new DatabaseIcons();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
@ -18,25 +18,28 @@
|
||||
#ifndef KEEPASSX_DATABASEICONS_H
|
||||
#define KEEPASSX_DATABASEICONS_H
|
||||
|
||||
#include <QtCore/QHash>
|
||||
#include <QtGui/QIcon>
|
||||
#include <QtCore/QVector>
|
||||
#include <QtGui/QImage>
|
||||
#include <QtGui/QPixmap>
|
||||
#include <QtGui/QPixmapCache>
|
||||
|
||||
class DatabaseIcons
|
||||
{
|
||||
public:
|
||||
static QIcon icon(int index);
|
||||
static int iconCount();
|
||||
|
||||
private:
|
||||
static DatabaseIcons* m_instance;
|
||||
QImage icon(int index);
|
||||
QPixmap iconPixmap(int index);
|
||||
int iconCount();
|
||||
|
||||
private:
|
||||
DatabaseIcons();
|
||||
QIcon getIconInternal(int index);
|
||||
|
||||
bool m_initalized;
|
||||
QList<QString> m_indexToName;
|
||||
QHash<int,QIcon> m_iconCache;
|
||||
QVector<QString> m_indexToName;
|
||||
QVector<QImage> m_iconCache;
|
||||
QVector<QPixmapCache::Key> m_pixmapCacheKeys;
|
||||
|
||||
friend DatabaseIcons* databaseIcons();
|
||||
};
|
||||
|
||||
DatabaseIcons* databaseIcons();
|
||||
|
||||
#endif // KEEPASSX_DATABASEICONS_H
|
||||
|
@ -52,10 +52,10 @@ Uuid Entry::uuid() const
|
||||
return m_uuid;
|
||||
}
|
||||
|
||||
QIcon Entry::icon() const
|
||||
QImage Entry::icon() const
|
||||
{
|
||||
if (m_customIcon.isNull()) {
|
||||
return DatabaseIcons::icon(m_iconNumber);
|
||||
return databaseIcons()->icon(m_iconNumber);
|
||||
}
|
||||
else {
|
||||
// TODO check if m_db is 0
|
||||
@ -63,6 +63,23 @@ QIcon Entry::icon() const
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap Entry::iconPixmap() const
|
||||
{
|
||||
if (m_customIcon.isNull()) {
|
||||
return databaseIcons()->iconPixmap(m_iconNumber);
|
||||
}
|
||||
else {
|
||||
QPixmap pixmap;
|
||||
if (!QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
|
||||
// TODO check if m_db is 0
|
||||
pixmap = QPixmap::fromImage(m_db->metadata()->customIcon(m_customIcon));
|
||||
*const_cast<QPixmapCache::Key*>(&m_pixmapCacheKey) = QPixmapCache::insert(pixmap);
|
||||
}
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
}
|
||||
|
||||
int Entry::iconNumber() const
|
||||
{
|
||||
return m_iconNumber;
|
||||
@ -176,6 +193,8 @@ void Entry::setIcon(int iconNumber)
|
||||
|
||||
m_iconNumber = iconNumber;
|
||||
m_customIcon = Uuid();
|
||||
|
||||
m_pixmapCacheKey = QPixmapCache::Key();
|
||||
}
|
||||
|
||||
void Entry::setIcon(const Uuid& uuid)
|
||||
@ -184,6 +203,8 @@ void Entry::setIcon(const Uuid& uuid)
|
||||
|
||||
m_iconNumber = 0;
|
||||
m_customIcon = uuid;
|
||||
|
||||
m_pixmapCacheKey = QPixmapCache::Key();
|
||||
}
|
||||
|
||||
void Entry::setForegroundColor(const QColor& color)
|
||||
|
@ -23,7 +23,9 @@
|
||||
#include <QtCore/QSet>
|
||||
#include <QtCore/QUrl>
|
||||
#include <QtGui/QColor>
|
||||
#include <QtGui/QIcon>
|
||||
#include <QtGui/QImage>
|
||||
#include <QtGui/QPixmap>
|
||||
#include <QtGui/QPixmapCache>
|
||||
|
||||
#include "core/TimeInfo.h"
|
||||
#include "core/Uuid.h"
|
||||
@ -45,7 +47,8 @@ public:
|
||||
Entry();
|
||||
~Entry();
|
||||
Uuid uuid() const;
|
||||
QIcon icon() const;
|
||||
QImage icon() const;
|
||||
QPixmap iconPixmap() const;
|
||||
int iconNumber() const;
|
||||
Uuid iconUuid() const;
|
||||
QColor foregroundColor() const;
|
||||
@ -125,6 +128,7 @@ private:
|
||||
QList<Entry*> m_history;
|
||||
QPointer<Group> m_group;
|
||||
const Database* m_db;
|
||||
QPixmapCache::Key m_pixmapCacheKey;
|
||||
const static QStringList m_defaultAttibutes;
|
||||
};
|
||||
|
||||
|
@ -55,16 +55,34 @@ QString Group::notes() const
|
||||
return m_notes;
|
||||
}
|
||||
|
||||
QIcon Group::icon() const
|
||||
QImage Group::icon() const
|
||||
{
|
||||
if (m_customIcon.isNull()) {
|
||||
return DatabaseIcons::icon(m_iconNumber);
|
||||
return databaseIcons()->icon(m_iconNumber);
|
||||
}
|
||||
else {
|
||||
// TODO check if m_db is 0
|
||||
return m_db->metadata()->customIcon(m_customIcon);
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap Group::iconPixmap() const
|
||||
{
|
||||
if (m_customIcon.isNull()) {
|
||||
return databaseIcons()->iconPixmap(m_iconNumber);
|
||||
}
|
||||
else {
|
||||
QPixmap pixmap;
|
||||
if (!QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
|
||||
// TODO check if m_db is 0
|
||||
pixmap = QPixmap::fromImage(m_db->metadata()->customIcon(m_customIcon));
|
||||
*const_cast<QPixmapCache::Key*>(&m_pixmapCacheKey) = QPixmapCache::insert(pixmap);
|
||||
}
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
}
|
||||
|
||||
int Group::iconNumber() const
|
||||
{
|
||||
return m_iconNumber;
|
||||
@ -129,6 +147,8 @@ void Group::setIcon(int iconNumber)
|
||||
m_iconNumber = iconNumber;
|
||||
m_customIcon = Uuid();
|
||||
|
||||
m_pixmapCacheKey = QPixmapCache::Key();
|
||||
|
||||
Q_EMIT dataChanged(this);
|
||||
}
|
||||
|
||||
@ -139,6 +159,8 @@ void Group::setIcon(const Uuid& uuid)
|
||||
m_iconNumber = 0;
|
||||
m_customIcon = uuid;
|
||||
|
||||
m_pixmapCacheKey = QPixmapCache::Key();
|
||||
|
||||
Q_EMIT dataChanged(this);
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,9 @@
|
||||
#define KEEPASSX_GROUP_H
|
||||
|
||||
#include <QtCore/QPointer>
|
||||
#include <QtGui/QIcon>
|
||||
#include <QtGui/QImage>
|
||||
#include <QtGui/QPixmap>
|
||||
#include <QtGui/QPixmapCache>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "core/Entry.h"
|
||||
@ -38,7 +40,8 @@ public:
|
||||
Uuid uuid() const;
|
||||
QString name() const;
|
||||
QString notes() const;
|
||||
QIcon icon() const;
|
||||
QImage icon() const;
|
||||
QPixmap iconPixmap() const;
|
||||
int iconNumber() const;
|
||||
Uuid iconUuid() const;
|
||||
TimeInfo timeInfo() const;
|
||||
@ -109,6 +112,7 @@ private:
|
||||
QList<Entry*> m_entries;
|
||||
|
||||
QPointer<Group> m_parent;
|
||||
QPixmapCache::Key m_pixmapCacheKey;
|
||||
|
||||
friend void Database::setRootGroup(Group* group);
|
||||
friend Entry::~Entry();
|
||||
|
@ -120,12 +120,12 @@ bool Metadata::autoEnableVisualHiding() const
|
||||
return m_autoEnableVisualHiding;
|
||||
}
|
||||
|
||||
QIcon Metadata::customIcon(const Uuid& uuid) const
|
||||
QImage Metadata::customIcon(const Uuid& uuid) const
|
||||
{
|
||||
return m_customIcons.value(uuid);
|
||||
}
|
||||
|
||||
QHash<Uuid, QIcon> Metadata::customIcons() const
|
||||
QHash<Uuid, QImage> Metadata::customIcons() const
|
||||
{
|
||||
return m_customIcons;
|
||||
}
|
||||
@ -257,7 +257,7 @@ void Metadata::setAutoEnableVisualHiding(bool value)
|
||||
m_autoEnableVisualHiding = value;
|
||||
}
|
||||
|
||||
void Metadata::addCustomIcon(const Uuid& uuid, const QIcon& icon)
|
||||
void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
|
||||
{
|
||||
Q_ASSERT(!uuid.isNull());
|
||||
Q_ASSERT(!m_customIcons.contains(uuid));
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QHash>
|
||||
#include <QtGui/QIcon>
|
||||
#include <QtGui/QImage>
|
||||
|
||||
#include "core/Uuid.h"
|
||||
|
||||
@ -48,8 +48,8 @@ public:
|
||||
bool protectUrl() const;
|
||||
bool protectNotes() const;
|
||||
bool autoEnableVisualHiding() const;
|
||||
QIcon customIcon(const Uuid& uuid) const;
|
||||
QHash<Uuid, QIcon> customIcons() const;
|
||||
QImage customIcon(const Uuid& uuid) const;
|
||||
QHash<Uuid, QImage> customIcons() const;
|
||||
bool recycleBinEnabled() const;
|
||||
const Group* recycleBin() const;
|
||||
QDateTime recycleBinChanged() const;
|
||||
@ -76,7 +76,7 @@ public:
|
||||
void setProtectUrl(bool value);
|
||||
void setProtectNotes(bool value);
|
||||
void setAutoEnableVisualHiding(bool value);
|
||||
void addCustomIcon(const Uuid& uuid, const QIcon& icon);
|
||||
void addCustomIcon(const Uuid& uuid, const QImage& icon);
|
||||
void removeCustomIcon(const Uuid& uuid);
|
||||
void setRecycleBinEnabled(bool value);
|
||||
void setRecycleBin(Group* group);
|
||||
@ -113,7 +113,7 @@ private:
|
||||
bool m_protectNotes;
|
||||
bool m_autoEnableVisualHiding;
|
||||
|
||||
QHash<Uuid, QIcon> m_customIcons;
|
||||
QHash<Uuid, QImage> m_customIcons;
|
||||
|
||||
bool m_recycleBinEnabled;
|
||||
Group* m_recycleBin;
|
||||
|
@ -227,9 +227,9 @@ void KeePass2XmlReader::parseIcon()
|
||||
uuid = readUuid();
|
||||
}
|
||||
else if (m_xml.name() == "Data") {
|
||||
QPixmap pixmap;
|
||||
pixmap.loadFromData(readBinary());
|
||||
m_meta->addCustomIcon(uuid, QIcon(pixmap));
|
||||
QImage icon;
|
||||
icon.loadFromData(readBinary());
|
||||
m_meta->addCustomIcon(uuid, icon);
|
||||
}
|
||||
else {
|
||||
skipCurrentElement();
|
||||
@ -318,7 +318,7 @@ Group* KeePass2XmlReader::parseGroup()
|
||||
raiseError();
|
||||
}
|
||||
else {
|
||||
if (iconId >= DatabaseIcons::iconCount()) {
|
||||
if (iconId >= databaseIcons()->iconCount()) {
|
||||
qWarning("KeePass2XmlReader::parseGroup: icon id \"%d\" not supported", iconId);
|
||||
}
|
||||
group->setIcon(iconId);
|
||||
|
@ -107,7 +107,7 @@ void KeePass2XmlWriter::writeCustomIcons()
|
||||
{
|
||||
m_xml.writeStartElement("CustomIcons");
|
||||
|
||||
QHash<Uuid, QIcon> customIcons = m_meta->customIcons();
|
||||
QHash<Uuid, QImage> customIcons = m_meta->customIcons();
|
||||
Q_FOREACH (const Uuid& uuid, customIcons.keys()) {
|
||||
writeIcon(uuid, customIcons.value(uuid));
|
||||
}
|
||||
@ -115,18 +115,17 @@ void KeePass2XmlWriter::writeCustomIcons()
|
||||
m_xml.writeEndElement();
|
||||
}
|
||||
|
||||
void KeePass2XmlWriter::writeIcon(const Uuid& uuid, const QIcon& icon)
|
||||
void KeePass2XmlWriter::writeIcon(const Uuid& uuid, const QImage& icon)
|
||||
{
|
||||
m_xml.writeStartElement("Icon");
|
||||
|
||||
writeUuid("UUID", uuid);
|
||||
|
||||
QPixmap pixmap = icon.pixmap(16, 16);
|
||||
|
||||
QByteArray ba;
|
||||
QBuffer buffer(&ba);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
pixmap.save(&buffer, "PNG");
|
||||
// TODO check !icon.save()
|
||||
icon.save(&buffer, "PNG");
|
||||
buffer.close();
|
||||
writeBinary("Data", ba);
|
||||
|
||||
|
@ -21,7 +21,7 @@
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QXmlStreamWriter>
|
||||
#include <QtGui/QColor>
|
||||
#include <QtGui/QIcon>
|
||||
#include <QtGui/QImage>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "core/Entry.h"
|
||||
@ -46,7 +46,7 @@ private:
|
||||
void writeMetadata();
|
||||
void writeMemoryProtection();
|
||||
void writeCustomIcons();
|
||||
void writeIcon(const Uuid& uuid, const QIcon& icon);
|
||||
void writeIcon(const Uuid& uuid, const QImage& icon);
|
||||
void writeCustomData();
|
||||
void writeCustomDataItem(const QString& key, const QString& value);
|
||||
void writeRoot();
|
||||
|
@ -85,7 +85,7 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
|
||||
}
|
||||
}
|
||||
else if ((role == Qt::DecorationRole) && (index.column() == 0)) {
|
||||
return entry->icon();
|
||||
return entry->iconPixmap();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
|
@ -109,7 +109,7 @@ QVariant GroupModel::data(const QModelIndex& index, int role) const
|
||||
return group->name();
|
||||
}
|
||||
else if (role == Qt::DecorationRole) {
|
||||
return group->icon();
|
||||
return group->iconPixmap();
|
||||
}
|
||||
else {
|
||||
return QVariant();
|
||||
|
@ -105,11 +105,13 @@ void TestKeePass2XmlReader::testCustomIcons()
|
||||
QCOMPARE(m_db->metadata()->customIcons().size(), 1);
|
||||
Uuid uuid = Uuid::fromBase64("++vyI+daLk6omox4a6kQGA==");
|
||||
QVERIFY(m_db->metadata()->customIcons().contains(uuid));
|
||||
QIcon icon = m_db->metadata()->customIcon(uuid);
|
||||
QImage img = icon.pixmap(16, 16).toImage();
|
||||
QImage icon = m_db->metadata()->customIcon(uuid);
|
||||
QCOMPARE(icon.width(), 16);
|
||||
QCOMPARE(icon.height(), 16);
|
||||
|
||||
for (int x=0; x<16; x++) {
|
||||
for (int y=0; y<16; y++) {
|
||||
QRgb rgb = img.pixel(x, y);
|
||||
QRgb rgb = icon.pixel(x, y);
|
||||
QCOMPARE(qRed(rgb), 128);
|
||||
QCOMPARE(qGreen(rgb), 0);
|
||||
QCOMPARE(qBlue(rgb), 128);
|
||||
|
Loading…
x
Reference in New Issue
Block a user