1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
This commit is contained in:
krepa098 2014-07-31 17:36:55 +02:00
parent f0306bb25f
commit 6fed143048
5 changed files with 30 additions and 24 deletions

View File

@ -34,9 +34,9 @@ SmileyPack& SmileyPack::getInstance()
return smileyPack;
}
QStringList SmileyPack::listSmileyPacks(const QString &path)
QList<QPair<QString, QString> > SmileyPack::listSmileyPacks(const QString &path)
{
QStringList smileyPacks;
QList<QPair<QString, QString> > smileyPacks;
QDir dir(path);
foreach (const QString& subdirectory, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
@ -45,7 +45,11 @@ QStringList SmileyPack::listSmileyPacks(const QString &path)
QFileInfoList entries = dir.entryInfoList(QStringList() << "emoticons.xml", QDir::Files);
if (entries.size() > 0) // does it contain a file called emoticons.xml?
smileyPacks << QDir(QCoreApplication::applicationDirPath()).relativeFilePath(entries[0].absoluteFilePath());
{
QString packageName = dir.dirName();
QString relPath = QDir(QCoreApplication::applicationDirPath()).relativeFilePath(entries[0].absoluteFilePath());
smileyPacks << QPair<QString, QString>(packageName, relPath);
}
dir.cdUp();
}
@ -57,7 +61,7 @@ bool SmileyPack::load(const QString& filename)
{
// discard old data
filenameTable.clear();
cache.clear();
imgCache.clear();
emoticons.clear();
path.clear();
@ -110,7 +114,7 @@ bool SmileyPack::load(const QString& filename)
return true;
}
QString SmileyPack::replaceEmoticons(QString msg)
QString SmileyPack::smileyfied(QString msg)
{
QRegExp exp("\\S+"); // matches words
@ -122,7 +126,7 @@ QString SmileyPack::replaceEmoticons(QString msg)
QString key = exp.cap();
if (filenameTable.contains(key))
{
QString imgRichText = getSmileyAsRichText(key);
QString imgRichText = getAsRichText(key);
msg.replace(index, key.length(), imgRichText);
index += imgRichText.length() - key.length();
@ -138,12 +142,12 @@ QList<QStringList> SmileyPack::getEmoticons() const
return emoticons;
}
QString SmileyPack::getSmileyAsRichText(const QString &key)
QString SmileyPack::getAsRichText(const QString &key)
{
return "<img src=\"data:image/png;base64," % QString(getCachedSmiley(key).toBase64()) % "\">";
}
QIcon SmileyPack::getIcon(const QString &key)
QIcon SmileyPack::getAsIcon(const QString &key)
{
QPixmap pm;
pm.loadFromData(getCachedSmiley(key), "PNG");
@ -165,7 +169,7 @@ void SmileyPack::cacheSmiley(const QString &name)
QBuffer buffer(&scaledImgData);
scaledImg.save(&buffer, "PNG");
cache.insert(name, scaledImgData);
imgCache.insert(name, scaledImgData);
}
}
@ -177,11 +181,11 @@ QByteArray SmileyPack::getCachedSmiley(const QString &key)
// cache it if needed
QString file = filenameTable.value(key);
if (!cache.contains(file)) {
if (!imgCache.contains(file)) {
cacheSmiley(file);
}
return cache.value(file);
return imgCache.value(file);
}
void SmileyPack::onSmileyPackChanged()

View File

@ -28,13 +28,13 @@ class SmileyPack : public QObject
Q_OBJECT
public:
static SmileyPack& getInstance();
static QStringList listSmileyPacks(const QString& path);
static QList<QPair<QString, QString>> listSmileyPacks(const QString& path);
bool load(const QString &filename);
QString replaceEmoticons(QString msg);
QString smileyfied(QString msg);
QList<QStringList> getEmoticons() const;
QString getSmileyAsRichText(const QString& key);
QIcon getIcon(const QString& key);
QString getAsRichText(const QString& key);
QIcon getAsIcon(const QString& key);
private slots:
void onSmileyPackChanged();
@ -48,9 +48,9 @@ private:
QByteArray getCachedSmiley(const QString& key);
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
QHash<QString, QByteArray> imgCache; // (scaled) representation of a smiley ie. "happy.png" -> data
QList<QStringList> emoticons; // {{ ":)", ":-)" }, {":(", ...}, ... }
QString path; // directory containing the cfg and image files
QList<QStringList> emoticons;
};
#endif // SMILEYPACK_H

View File

@ -242,7 +242,7 @@ void ChatForm::addFriendMessage(QString message)
void ChatForm::addMessage(QString author, QString message, QString date)
{
message = SmileyPack::getInstance().replaceEmoticons(message);
message = SmileyPack::getInstance().smileyfied(message);
addMessage(new QLabel(author), new QLabel(message), new QLabel(date));
}
@ -670,7 +670,7 @@ void ChatForm::onEmoteButtonClicked()
for (const QStringList& set : emoticons)
{
QPushButton* button = new QPushButton;
button->setIcon(SmileyPack::getInstance().getIcon(set[0]));
button->setIcon(SmileyPack::getInstance().getAsIcon(set[0]));
button->setToolTip(set.join(" "));
button->setProperty("sequence", set[0]);
connect(button, &QPushButton::clicked, this, &ChatForm::onAddEmote);

View File

@ -53,8 +53,9 @@ SettingsForm::SettingsForm()
makeToxPortable.setChecked(Settings::getInstance().getMakeToxPortable());
smileyPackLabel.setText(tr("Smiley Pack", "Text on smiley pack label"));
smileyPackBrowser.addItems(SmileyPack::listSmileyPacks("./smileys"));
smileyPackBrowser.setCurrentText(Settings::getInstance().getSmileyPack());
for (auto entry : SmileyPack::listSmileyPacks("./smileys"))
smileyPackBrowser.addItem(entry.first, entry.second);
smileyPackBrowser.setCurrentIndex(smileyPackBrowser.findData(Settings::getInstance().getSmileyPack()));
main->setLayout(&layout);
layout.addWidget(&nameLabel);
@ -79,7 +80,7 @@ SettingsForm::SettingsForm()
connect(&useTranslations, SIGNAL(stateChanged(int)), this, SLOT(onUseTranslationUpdated()));
connect(&makeToxPortable, SIGNAL(stateChanged(int)), this, SLOT(onMakeToxPortableUpdated()));
connect(&idLabel, SIGNAL(clicked()), this, SLOT(copyIdClicked()));
connect(&smileyPackBrowser, SIGNAL(currentTextChanged(QString)), this, SLOT(onSmileyBrowserTextChanged(QString)));
connect(&smileyPackBrowser, SIGNAL(currentIndexChanged(int)), this, SLOT(onSmileyBrowserIndexChanged(int)));
}
SettingsForm::~SettingsForm()
@ -127,7 +128,8 @@ void SettingsForm::onMakeToxPortableUpdated()
Settings::getInstance().setMakeToxPortable(makeToxPortable.isChecked());
}
void SettingsForm::onSmileyBrowserTextChanged(const QString &filename)
void SettingsForm::onSmileyBrowserIndexChanged(int index)
{
QString filename = smileyPackBrowser.itemData(index).toString();
Settings::getInstance().setSmileyPack(filename);
}

View File

@ -48,7 +48,7 @@ private slots:
void onEnableIPv6Updated();
void onUseTranslationUpdated();
void onMakeToxPortableUpdated();
void onSmileyBrowserTextChanged(const QString& filename);
void onSmileyBrowserIndexChanged(int index);
void copyIdClicked();
private: