mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
cleanup
This commit is contained in:
parent
f7a357d671
commit
f0306bb25f
|
@ -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)
|
|||
* </messaging-emoticon-map>
|
||||
*/
|
||||
|
||||
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 = "<img src=\"data:image/png;base64," % cache[file] % "\">";
|
||||
QString imgRichText = getSmileyAsRichText(key);
|
||||
|
||||
msg.replace(index, key.length(), imgRichText);
|
||||
index += imgRichText.length() - key.length();
|
||||
|
@ -141,22 +138,20 @@ QList<QStringList> 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 "<img src=\"data:image/png;base64," % cache[file] % "\">";
|
||||
return "<img src=\"data:image/png;base64," % QString(getCachedSmiley(key).toBase64()) % "\">";
|
||||
}
|
||||
|
||||
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());
|
||||
|
|
14
smileypack.h
14
smileypack.h
|
@ -28,15 +28,14 @@ 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<QStringList> getEmoticons() const;
|
||||
QString getRichText(const QString& key);
|
||||
QString getSmileyAsRichText(const QString& key);
|
||||
QIcon getIcon(const QString& key);
|
||||
|
||||
static QStringList listSmileyPacks(const QString& path);
|
||||
|
||||
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<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
|
||||
QHash<QString, QString> filenameTable; // matches an emoticon to its corresponding smiley ie. ":)" -> "happy.png"
|
||||
QHash<QString, QByteArray> cache; // (scaled) representation of a smiley ie. "happy.png" -> data
|
||||
QString path; // directory containing the cfg and image files
|
||||
QList<QStringList> emoticons;
|
||||
};
|
||||
|
||||
|
|
|
@ -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]));
|
||||
|
|
Loading…
Reference in New Issue
Block a user