mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
IconUtils: add cache to avoid redundant I/O
This commit is contained in:
parent
52546e4664
commit
18ee02d920
@ -1,6 +1,5 @@
|
||||
#include "viconutils.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QRegExp>
|
||||
#include <QByteArray>
|
||||
#include <QPixmap>
|
||||
@ -8,13 +7,20 @@
|
||||
|
||||
#include "vutils.h"
|
||||
|
||||
QHash<QString, QIcon> VIconUtils::m_cache;
|
||||
|
||||
VIconUtils::VIconUtils()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QIcon VIconUtils::icon(const QString &p_file, const QString &p_fg, bool p_addDisabled)
|
||||
{
|
||||
const QString key = cacheKey(p_file, p_fg, p_addDisabled);
|
||||
auto it = m_cache.find(key);
|
||||
if (it != m_cache.end()) {
|
||||
return it.value();
|
||||
}
|
||||
|
||||
QFileInfo fi(p_file);
|
||||
bool isSvg = fi.suffix().toLower() == "svg";
|
||||
if (p_fg.isEmpty() || !isSvg) {
|
||||
@ -48,5 +54,8 @@ QIcon VIconUtils::icon(const QString &p_file, const QString &p_fg, bool p_addDis
|
||||
icon.addPixmap(disabledPixmap, QIcon::Disabled);
|
||||
}
|
||||
|
||||
// Add to cache.
|
||||
m_cache.insert(key, icon);
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
@ -3,12 +3,12 @@
|
||||
|
||||
#include <QString>
|
||||
#include <QIcon>
|
||||
#include <QHash>
|
||||
|
||||
#include "vpalette.h"
|
||||
|
||||
extern VPalette *g_palette;
|
||||
|
||||
|
||||
class VIconUtils
|
||||
{
|
||||
public:
|
||||
@ -94,6 +94,14 @@ public:
|
||||
|
||||
private:
|
||||
VIconUtils();
|
||||
|
||||
static QString cacheKey(const QString &p_file, const QString &p_fg, bool p_addDisabled)
|
||||
{
|
||||
return p_file + "_" + p_fg + "_" + (p_addDisabled ? "1" : "0");
|
||||
}
|
||||
|
||||
// file_fg_addDisabled as key.
|
||||
static QHash<QString, QIcon> m_cache;
|
||||
};
|
||||
|
||||
#endif // VICONUTILS_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user