1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #168 from krepa098/smileys

Smileys
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-07-29 13:09:22 +02:00
commit d25a2b0ace
2 changed files with 41 additions and 22 deletions

View File

@ -37,15 +37,15 @@ SmileyPack& SmileyPack::getInstance()
bool SmileyPack::load(const QString& filename)
{
// discard old data
lookupTable.clear();
QDir::setSearchPaths("smiley", QStringList());
assignmentTable.clear();
cache.clear();
// open emoticons.xml
QFile xmlFile(filename);
if(!xmlFile.open(QIODevice::ReadOnly))
return false; // cannot open file
/* parse the cfg document
/* parse the cfg file
* sample:
* <?xml version='1.0'?>
* <messaging-emoticon-map>
@ -72,25 +72,21 @@ bool SmileyPack::load(const QString& filename)
while (!stringElement.isNull())
{
QString rune = stringElement.text();
lookupTable.insert(rune, file); // add it to the map
assignmentTable.insert(rune, file);
stringElement = stringElement.nextSibling().toElement();
}
}
// Rich Text makes use of Qt's resource system, so
// let Qt know about our smilies
QFileInfo info(filename);
QDir::setSearchPaths("smiley", QStringList() << info.absolutePath());
path = QFileInfo(filename).absolutePath();
// success!
return true;
}
QString SmileyPack::replaceEmoticons(const QString &msg) const
QString SmileyPack::replaceEmoticons(QString msg)
{
QString out = msg;
QRegExp exp("\\S*"); // matches words
QRegExp exp("\\S+"); // matches words
int index = msg.indexOf(exp);
int offset = 0;
@ -99,21 +95,40 @@ QString SmileyPack::replaceEmoticons(const QString &msg) const
while (index >= 0)
{
QString key = exp.cap();
if (lookupTable.contains(key))
if (assignmentTable.contains(key))
{
QString width = QString::number(16);
QString height = QString::number(16);
QString img = lookupTable[key];
QString imgRt = "<img src=\"smiley:" + img + "\" width=\"" + width + "\" height=\"" + height + "\">";
out.replace(index + offset, key.length(), imgRt);
offset += imgRt.length() - key.length();
}
index = msg.indexOf(exp, index + exp.matchedLength() + 1);
QString file = assignmentTable[key];
if (!cache.contains(file)) {
loadSmiley(file);
}
return out;
QString imgRichText = "<img src=\"data:image/png;base64," % cache[file] % "\">";
msg.replace(index + offset, key.length(), imgRichText);
index += imgRichText.length() - key.length();
}
index = msg.indexOf(exp, index + key.length());
}
return msg;
}
void SmileyPack::loadSmiley(const QString &name)
{
QSize size(16, 16); // TODO: adapt to text size
QString filename = path % '/' % name;
QImage img(filename);
if (!img.isNull())
{
QImage scaledImg = img.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QByteArray scaledImgData;
QBuffer buffer(&scaledImgData);
scaledImg.save(&buffer, "PNG");
cache.insert(name, scaledImgData.toBase64());
}
}
void SmileyPack::onSmileyPackChanged()

View File

@ -29,7 +29,7 @@ public:
static SmileyPack& getInstance();
bool load(const QString &filename);
QString replaceEmoticons(const QString& msg) const;
QString replaceEmoticons(QString msg);
private slots:
void onSmileyPackChanged();
@ -39,7 +39,11 @@ private:
SmileyPack(SmileyPack&) = delete;
SmileyPack& operator=(const SmileyPack&) = delete;
QHash<QString, QString> lookupTable; // matches an emoticon to its corresponding smiley
void loadSmiley(const QString& name);
QHash<QString, QString> assignmentTable; // matches an emoticon to its corresponding smiley
QHash<QString, QString> cache; // base64 representation of a smiley
QString path; // directory containing the cfg file
};
#endif // SMILEYPACK_H