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

smooth scaling

This commit is contained in:
krepa098 2014-07-29 10:50:39 +02:00
parent e4f897221f
commit f9cc5dbccd
2 changed files with 38 additions and 18 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,22 +72,19 @@ 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(const QString &msg)
{
QString out = msg;
QRegExp exp("\\S*"); // matches words
@ -99,16 +96,17 @@ 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 file = assignmentTable[key];
if (!cache.contains(file)) {
loadSmiley(file);
}
QString img = lookupTable[key];
QString imgRt = "<img src=\"smiley:" + img + "\" width=\"" + width + "\" height=\"" + height + "\">";
QString imgRichText = "<img src=\"data:image/png;base64," + cache[file] + "\">";
out.replace(index + offset, key.length(), imgRt);
offset += imgRt.length() - key.length();
out.replace(index + offset, key.length(), imgRichText);
offset += imgRichText.length() - key.length();
}
index = msg.indexOf(exp, index + exp.matchedLength() + 1);
}
@ -116,6 +114,24 @@ QString SmileyPack::replaceEmoticons(const QString &msg) const
return out;
}
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()
{
load(Settings::getInstance().getSmileyPack());

View File

@ -29,7 +29,7 @@ public:
static SmileyPack& getInstance();
bool load(const QString &filename);
QString replaceEmoticons(const QString& msg) const;
QString replaceEmoticons(const 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, QByteArray> cache;
QString path; // directory containing the cfg file
};
#endif // SMILEYPACK_H