diff --git a/smileypack.cpp b/smileypack.cpp
index 1e73ea2d0..0ccc87af0 100644
--- a/smileypack.cpp
+++ b/smileypack.cpp
@@ -56,9 +56,10 @@ QStringList SmileyPack::listSmileyPacks(const QString &path)
bool SmileyPack::load(const QString& filename)
{
// discard old data
- assignmentTable.clear();
+ filenameTable.clear();
cache.clear();
emoticons.clear();
+ path.clear();
// open emoticons.xml
QFile xmlFile(filename);
@@ -80,6 +81,8 @@ bool SmileyPack::load(const QString& filename)
*
*/
+ path = QFileInfo(filename).absolutePath();
+
QDomDocument doc;
doc.setContent(xmlFile.readAll());
@@ -94,16 +97,15 @@ bool SmileyPack::load(const QString& filename)
while (!stringElement.isNull())
{
QString emoticon = stringElement.text();
- assignmentTable.insert(emoticon, file);
+ filenameTable.insert(emoticon, file);
emoticonSet.push_back(emoticon);
+ cacheSmiley(file); // preload all smileys
stringElement = stringElement.nextSibling().toElement();
}
emoticons.push_back(emoticonSet);
}
- path = QFileInfo(filename).absolutePath();
-
// success!
return true;
}
@@ -118,14 +120,9 @@ QString SmileyPack::replaceEmoticons(QString msg)
while (index >= 0)
{
QString key = exp.cap();
- if (assignmentTable.contains(key))
+ if (filenameTable.contains(key))
{
- QString file = assignmentTable[key];
- if (!cache.contains(file)) {
- loadSmiley(file);
- }
-
- QString imgRichText = "";
+ QString imgRichText = getSmileyAsRichText(key);
msg.replace(index, key.length(), imgRichText);
index += imgRichText.length() - key.length();
@@ -141,22 +138,20 @@ QList SmileyPack::getEmoticons() const
return emoticons;
}
-QString SmileyPack::getRichText(const QString &key)
+QString SmileyPack::getSmileyAsRichText(const QString &key)
{
- QString file = assignmentTable[key];
- if (!cache.contains(file)) {
- loadSmiley(file);
- }
-
- return "";
+ return "";
}
QIcon SmileyPack::getIcon(const QString &key)
{
- return QIcon(path + '/' + assignmentTable[key]);
+ QPixmap pm;
+ pm.loadFromData(getCachedSmiley(key), "PNG");
+
+ return QIcon(pm);
}
-void SmileyPack::loadSmiley(const QString &name)
+void SmileyPack::cacheSmiley(const QString &name)
{
QSize size(16, 16); // TODO: adapt to text size
QString filename = path % '/' % name;
@@ -170,10 +165,25 @@ void SmileyPack::loadSmiley(const QString &name)
QBuffer buffer(&scaledImgData);
scaledImg.save(&buffer, "PNG");
- cache.insert(name, scaledImgData.toBase64());
+ cache.insert(name, scaledImgData);
}
}
+QByteArray SmileyPack::getCachedSmiley(const QString &key)
+{
+ // valid key?
+ if (!filenameTable.contains(key))
+ return QByteArray();
+
+ // cache it if needed
+ QString file = filenameTable.value(key);
+ if (!cache.contains(file)) {
+ cacheSmiley(file);
+ }
+
+ return cache.value(file);
+}
+
void SmileyPack::onSmileyPackChanged()
{
load(Settings::getInstance().getSmileyPack());
diff --git a/smileypack.h b/smileypack.h
index 69ea898cf..b315d60d6 100644
--- a/smileypack.h
+++ b/smileypack.h
@@ -28,14 +28,13 @@ class SmileyPack : public QObject
Q_OBJECT
public:
static SmileyPack& getInstance();
+ static QStringList listSmileyPacks(const QString& path);
bool load(const QString &filename);
QString replaceEmoticons(QString msg);
QList getEmoticons() const;
- QString getRichText(const QString& key);
- QIcon getIcon(const QString& key);
-
- static QStringList listSmileyPacks(const QString& path);
+ QString getSmileyAsRichText(const QString& key);
+ QIcon getIcon(const QString& key);
private slots:
void onSmileyPackChanged();
@@ -45,11 +44,12 @@ private:
SmileyPack(SmileyPack&) = delete;
SmileyPack& operator=(const SmileyPack&) = delete;
- void loadSmiley(const QString& name);
+ void cacheSmiley(const QString& name);
+ QByteArray getCachedSmiley(const QString& key);
- QHash assignmentTable; // matches an emoticon to its corresponding smiley
- QHash cache; // base64 representation of a smiley
- QString path; // directory containing the cfg file
+ QHash filenameTable; // matches an emoticon to its corresponding smiley ie. ":)" -> "happy.png"
+ QHash cache; // (scaled) representation of a smiley ie. "happy.png" -> data
+ QString path; // directory containing the cfg and image files
QList emoticons;
};
diff --git a/widget/form/chatform.cpp b/widget/form/chatform.cpp
index 896f1c960..7dfdaf1d4 100644
--- a/widget/form/chatform.cpp
+++ b/widget/form/chatform.cpp
@@ -667,7 +667,7 @@ void ChatForm::onEmoteButtonClicked()
int colCount = sqrt(emoticons.size()) + 1;
int row = 0;
int col = 0;
- for (QStringList set : emoticons)
+ for (const QStringList& set : emoticons)
{
QPushButton* button = new QPushButton;
button->setIcon(SmileyPack::getInstance().getIcon(set[0]));