mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: Use loaded icons as cache, make getAsIcon const
This commit is contained in:
parent
fa21594902
commit
47da91d74f
|
@ -111,7 +111,7 @@ SmileyPack::SmileyPack()
|
||||||
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
||||||
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this,
|
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this,
|
||||||
&SmileyPack::onSmileyPackChanged);
|
&SmileyPack::onSmileyPackChanged);
|
||||||
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanup);
|
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanupIconsCache);
|
||||||
cleanupTimer->start(CLEANUP_TIMEOUT);
|
cleanupTimer->start(CLEANUP_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,13 +120,13 @@ SmileyPack::~SmileyPack()
|
||||||
delete cleanupTimer;
|
delete cleanupTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SmileyPack::cleanup()
|
void SmileyPack::cleanupIconsCache()
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&loadingMutex);
|
QMutexLocker locker(&loadingMutex);
|
||||||
for (auto it = emoticonToIcon.begin(); it != emoticonToIcon.end();) {
|
for (auto it = cachedIcon.begin(); it != cachedIcon.end();) {
|
||||||
std::shared_ptr<QIcon>& icon = it->second;
|
std::shared_ptr<QIcon>& icon = it->second;
|
||||||
if (icon.use_count() == 1) {
|
if (icon.use_count() == 1) {
|
||||||
it = emoticonToIcon.erase(it);
|
it = cachedIcon.erase(it);
|
||||||
icon.reset();
|
icon.reset();
|
||||||
} else {
|
} else {
|
||||||
++it;
|
++it;
|
||||||
|
@ -226,8 +226,8 @@ bool SmileyPack::load(const QString& filename)
|
||||||
const QString childName = QStringLiteral("string");
|
const QString childName = QStringLiteral("string");
|
||||||
const int iconsCount = emoticonElements.size();
|
const int iconsCount = emoticonElements.size();
|
||||||
emoticons.clear();
|
emoticons.clear();
|
||||||
emoticonToIcon.clear();
|
|
||||||
emoticonToPath.clear();
|
emoticonToPath.clear();
|
||||||
|
cachedIcon.clear();
|
||||||
|
|
||||||
for (int i = 0; i < iconsCount; ++i) {
|
for (int i = 0; i < iconsCount; ++i) {
|
||||||
QDomNode node = emoticonElements.at(i);
|
QDomNode node = emoticonElements.at(i);
|
||||||
|
@ -290,11 +290,12 @@ QList<QStringList> SmileyPack::getEmoticons() const
|
||||||
* @param emoticon Passed emoticon
|
* @param emoticon Passed emoticon
|
||||||
* @return Returns cached icon according to passed emoticon, null if no icon mapped to this emoticon
|
* @return Returns cached icon according to passed emoticon, null if no icon mapped to this emoticon
|
||||||
*/
|
*/
|
||||||
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
|
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon) const
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&loadingMutex);
|
QMutexLocker locker(&loadingMutex);
|
||||||
if (emoticonToIcon.find(emoticon) != emoticonToIcon.end())
|
if (cachedIcon.find(emoticon) != cachedIcon.end()) {
|
||||||
return emoticonToIcon[emoticon];
|
return cachedIcon[emoticon];
|
||||||
|
}
|
||||||
|
|
||||||
const auto iconPathIt = emoticonToPath.find(emoticon);
|
const auto iconPathIt = emoticonToPath.find(emoticon);
|
||||||
if (iconPathIt == emoticonToPath.end()) {
|
if (iconPathIt == emoticonToPath.end()) {
|
||||||
|
@ -303,7 +304,7 @@ std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
|
||||||
|
|
||||||
const QString& iconPath = iconPathIt.value();
|
const QString& iconPath = iconPathIt.value();
|
||||||
auto icon = std::make_shared<QIcon>(iconPath);
|
auto icon = std::make_shared<QIcon>(iconPath);
|
||||||
emoticonToIcon[emoticon] = icon;
|
cachedIcon[emoticon] = icon;
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,11 +39,11 @@ public:
|
||||||
|
|
||||||
QString smileyfied(const QString& msg);
|
QString smileyfied(const QString& msg);
|
||||||
QList<QStringList> getEmoticons() const;
|
QList<QStringList> getEmoticons() const;
|
||||||
std::shared_ptr<QIcon> getAsIcon(const QString& key);
|
std::shared_ptr<QIcon> getAsIcon(const QString& key) const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onSmileyPackChanged();
|
void onSmileyPackChanged();
|
||||||
void cleanup();
|
void cleanupIconsCache();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SmileyPack();
|
SmileyPack();
|
||||||
|
@ -53,7 +53,7 @@ private:
|
||||||
|
|
||||||
bool load(const QString& filename);
|
bool load(const QString& filename);
|
||||||
|
|
||||||
std::map<QString, std::shared_ptr<QIcon>> emoticonToIcon;
|
mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
|
||||||
QHash<QString, QString> emoticonToPath;
|
QHash<QString, QString> emoticonToPath;
|
||||||
QList<QStringList> emoticons;
|
QList<QStringList> emoticons;
|
||||||
QString path;
|
QString path;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user