mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
commit
d25a2b0ace
|
@ -37,15 +37,15 @@ SmileyPack& SmileyPack::getInstance()
|
||||||
bool SmileyPack::load(const QString& filename)
|
bool SmileyPack::load(const QString& filename)
|
||||||
{
|
{
|
||||||
// discard old data
|
// discard old data
|
||||||
lookupTable.clear();
|
assignmentTable.clear();
|
||||||
QDir::setSearchPaths("smiley", QStringList());
|
cache.clear();
|
||||||
|
|
||||||
// open emoticons.xml
|
// open emoticons.xml
|
||||||
QFile xmlFile(filename);
|
QFile xmlFile(filename);
|
||||||
if(!xmlFile.open(QIODevice::ReadOnly))
|
if(!xmlFile.open(QIODevice::ReadOnly))
|
||||||
return false; // cannot open file
|
return false; // cannot open file
|
||||||
|
|
||||||
/* parse the cfg document
|
/* parse the cfg file
|
||||||
* sample:
|
* sample:
|
||||||
* <?xml version='1.0'?>
|
* <?xml version='1.0'?>
|
||||||
* <messaging-emoticon-map>
|
* <messaging-emoticon-map>
|
||||||
|
@ -72,25 +72,21 @@ bool SmileyPack::load(const QString& filename)
|
||||||
while (!stringElement.isNull())
|
while (!stringElement.isNull())
|
||||||
{
|
{
|
||||||
QString rune = stringElement.text();
|
QString rune = stringElement.text();
|
||||||
lookupTable.insert(rune, file); // add it to the map
|
assignmentTable.insert(rune, file);
|
||||||
|
|
||||||
stringElement = stringElement.nextSibling().toElement();
|
stringElement = stringElement.nextSibling().toElement();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rich Text makes use of Qt's resource system, so
|
path = QFileInfo(filename).absolutePath();
|
||||||
// let Qt know about our smilies
|
|
||||||
QFileInfo info(filename);
|
|
||||||
QDir::setSearchPaths("smiley", QStringList() << info.absolutePath());
|
|
||||||
|
|
||||||
// success!
|
// success!
|
||||||
return true;
|
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 index = msg.indexOf(exp);
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
@ -99,21 +95,40 @@ QString SmileyPack::replaceEmoticons(const QString &msg) const
|
||||||
while (index >= 0)
|
while (index >= 0)
|
||||||
{
|
{
|
||||||
QString key = exp.cap();
|
QString key = exp.cap();
|
||||||
if (lookupTable.contains(key))
|
if (assignmentTable.contains(key))
|
||||||
{
|
{
|
||||||
QString width = QString::number(16);
|
QString file = assignmentTable[key];
|
||||||
QString height = QString::number(16);
|
if (!cache.contains(file)) {
|
||||||
|
loadSmiley(file);
|
||||||
|
}
|
||||||
|
|
||||||
QString img = lookupTable[key];
|
QString imgRichText = "<img src=\"data:image/png;base64," % cache[file] % "\">";
|
||||||
QString imgRt = "<img src=\"smiley:" + img + "\" width=\"" + width + "\" height=\"" + height + "\">";
|
|
||||||
|
|
||||||
out.replace(index + offset, key.length(), imgRt);
|
msg.replace(index + offset, key.length(), imgRichText);
|
||||||
offset += imgRt.length() - key.length();
|
index += imgRichText.length() - key.length();
|
||||||
}
|
}
|
||||||
index = msg.indexOf(exp, index + exp.matchedLength() + 1);
|
index = msg.indexOf(exp, index + key.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
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()
|
void SmileyPack::onSmileyPackChanged()
|
||||||
|
|
|
@ -29,7 +29,7 @@ public:
|
||||||
static SmileyPack& getInstance();
|
static SmileyPack& getInstance();
|
||||||
|
|
||||||
bool load(const QString &filename);
|
bool load(const QString &filename);
|
||||||
QString replaceEmoticons(const QString& msg) const;
|
QString replaceEmoticons(QString msg);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onSmileyPackChanged();
|
void onSmileyPackChanged();
|
||||||
|
@ -39,7 +39,11 @@ private:
|
||||||
SmileyPack(SmileyPack&) = delete;
|
SmileyPack(SmileyPack&) = delete;
|
||||||
SmileyPack& operator=(const 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
|
#endif // SMILEYPACK_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user