Add a cache for FilePath::icon().

This commit is contained in:
Felix Geyer 2012-07-18 21:06:46 +02:00
parent f01076856b
commit 4152e93bb7
2 changed files with 18 additions and 7 deletions

View File

@ -70,28 +70,37 @@ QIcon FilePath::applicationIcon()
QIcon FilePath::icon(const QString& category, const QString& name, bool fromTheme)
{
QIcon icon;
QString combinedName = category + "/" + name;
QIcon icon = m_iconCache.value(combinedName);
if (!icon.isNull()) {
return icon;
}
if (fromTheme) {
icon = QIcon::fromTheme(name);
}
if (icon.isNull()) {
QStringList pngSizes;
pngSizes << "16" << "22" << "24" << "32" << "48" << "64" << "128";
QList<int> pngSizes;
pngSizes << 16 << 22 << 24 << 32 << 48 << 64 << 128;
QString filename;
Q_FOREACH (const QString& size, pngSizes) {
filename = QString("%1/icons/application/%2x%2/%3/%4.png").arg(m_dataPath, size, category, name);
if (QFile::exists(filename)) {
icon.addFile(filename);
}
}
filename = QString("%1/icons/application/scalable/%3/%4.svgz").arg(m_dataPath, category, name);
Q_FOREACH (int size, pngSizes) {
filename = QString("%1/icons/application/%2x%2/%3.png").arg(m_dataPath, QString::number(size),
combinedName);
if (QFile::exists(filename)) {
icon.addFile(filename, QSize(size, size));
}
}
filename = QString("%1/icons/application/scalable/%3.svgz").arg(m_dataPath, combinedName);
if (QFile::exists(filename)) {
icon.addFile(filename);
}
}
m_iconCache.insert(combinedName, icon);
return icon;
}

View File

@ -18,6 +18,7 @@
#ifndef KEEPASSX_FILEPATH_H
#define KEEPASSX_FILEPATH_H
#include <QtCore/QHash>
#include <QtCore/QString>
#include <QtGui/QIcon>
@ -40,6 +41,7 @@ private:
static FilePath* m_instance;
QString m_dataPath;
QHash<QString, QIcon> m_iconCache;
Q_DISABLE_COPY(FilePath)
};