mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
smooth scaling
This commit is contained in:
parent
e4f897221f
commit
f9cc5dbccd
|
@ -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,22 +72,19 @@ 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(const QString &msg)
|
||||||
{
|
{
|
||||||
QString out = msg;
|
QString out = msg;
|
||||||
QRegExp exp("\\S*"); // matches words
|
QRegExp exp("\\S*"); // matches words
|
||||||
|
@ -99,16 +96,17 @@ 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);
|
out.replace(index + offset, key.length(), imgRichText);
|
||||||
offset += imgRt.length() - key.length();
|
offset += imgRichText.length() - key.length();
|
||||||
}
|
}
|
||||||
index = msg.indexOf(exp, index + exp.matchedLength() + 1);
|
index = msg.indexOf(exp, index + exp.matchedLength() + 1);
|
||||||
}
|
}
|
||||||
|
@ -116,6 +114,24 @@ QString SmileyPack::replaceEmoticons(const QString &msg) const
|
||||||
return out;
|
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()
|
void SmileyPack::onSmileyPackChanged()
|
||||||
{
|
{
|
||||||
load(Settings::getInstance().getSmileyPack());
|
load(Settings::getInstance().getSmileyPack());
|
||||||
|
|
|
@ -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(const 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, QByteArray> cache;
|
||||||
|
QString path; // directory containing the cfg file
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SMILEYPACK_H
|
#endif // SMILEYPACK_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user