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

wire up the emote button

This commit is contained in:
krepa098 2014-07-30 22:07:26 +02:00
parent 0d493f4bcf
commit f7a357d671
4 changed files with 88 additions and 3 deletions

View File

@ -58,6 +58,7 @@ bool SmileyPack::load(const QString& filename)
// discard old data // discard old data
assignmentTable.clear(); assignmentTable.clear();
cache.clear(); cache.clear();
emoticons.clear();
// open emoticons.xml // open emoticons.xml
QFile xmlFile(filename); QFile xmlFile(filename);
@ -88,13 +89,17 @@ bool SmileyPack::load(const QString& filename)
QString file = emoticonElements.at(i).attributes().namedItem("file").nodeValue(); QString file = emoticonElements.at(i).attributes().namedItem("file").nodeValue();
QDomElement stringElement = emoticonElements.at(i).firstChildElement("string"); QDomElement stringElement = emoticonElements.at(i).firstChildElement("string");
QStringList emoticonSet; // { ":)", ":-)" } etc.
while (!stringElement.isNull()) while (!stringElement.isNull())
{ {
QString rune = stringElement.text(); QString emoticon = stringElement.text();
assignmentTable.insert(rune, file); assignmentTable.insert(emoticon, file);
emoticonSet.push_back(emoticon);
stringElement = stringElement.nextSibling().toElement(); stringElement = stringElement.nextSibling().toElement();
} }
emoticons.push_back(emoticonSet);
} }
path = QFileInfo(filename).absolutePath(); path = QFileInfo(filename).absolutePath();
@ -131,6 +136,26 @@ QString SmileyPack::replaceEmoticons(QString msg)
return msg; return msg;
} }
QList<QStringList> SmileyPack::getEmoticons() const
{
return emoticons;
}
QString SmileyPack::getRichText(const QString &key)
{
QString file = assignmentTable[key];
if (!cache.contains(file)) {
loadSmiley(file);
}
return "<img src=\"data:image/png;base64," % cache[file] % "\">";
}
QIcon SmileyPack::getIcon(const QString &key)
{
return QIcon(path + '/' + assignmentTable[key]);
}
void SmileyPack::loadSmiley(const QString &name) void SmileyPack::loadSmiley(const QString &name)
{ {
QSize size(16, 16); // TODO: adapt to text size QSize size(16, 16); // TODO: adapt to text size

View File

@ -18,8 +18,9 @@
#define SMILEYPACK_H #define SMILEYPACK_H
#include <QHash> #include <QHash>
#include <QString>
#include <QObject> #include <QObject>
#include <QString>
#include <QStringList>
//maps emoticons to smileys //maps emoticons to smileys
class SmileyPack : public QObject class SmileyPack : public QObject
@ -30,6 +31,9 @@ public:
bool load(const QString &filename); bool load(const QString &filename);
QString replaceEmoticons(QString msg); QString replaceEmoticons(QString msg);
QList<QStringList> getEmoticons() const;
QString getRichText(const QString& key);
QIcon getIcon(const QString& key);
static QStringList listSmileyPacks(const QString& path); static QStringList listSmileyPacks(const QString& path);
@ -46,6 +50,7 @@ private:
QHash<QString, QString> assignmentTable; // matches an emoticon to its corresponding smiley QHash<QString, QString> assignmentTable; // matches an emoticon to its corresponding smiley
QHash<QString, QString> cache; // base64 representation of a smiley QHash<QString, QString> cache; // base64 representation of a smiley
QString path; // directory containing the cfg file QString path; // directory containing the cfg file
QList<QStringList> emoticons;
}; };
#endif // SMILEYPACK_H #endif // SMILEYPACK_H

View File

@ -25,6 +25,8 @@
#include <QScrollBar> #include <QScrollBar>
#include <QFileDialog> #include <QFileDialog>
#include <QMenu> #include <QMenu>
#include <QWidgetAction>
#include <QGridLayout>
ChatForm::ChatForm(Friend* chatFriend) ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend), curRow{0}, lockSliderToBottom{true} : f(chatFriend), curRow{0}, lockSliderToBottom{true}
@ -188,6 +190,7 @@ ChatForm::ChatForm(Friend* chatFriend)
connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered())); connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered()));
connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged())); connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged()));
connect(chatArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); connect(chatArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
} }
ChatForm::~ChatForm() ChatForm::~ChatForm()
@ -652,3 +655,53 @@ void ChatForm::onSaveLogClicked()
file.write(log.toUtf8()); file.write(log.toUtf8());
file.close(); file.close();
} }
void ChatForm::onEmoteButtonClicked()
{
QList<QStringList> emoticons = SmileyPack::getInstance().getEmoticons();
QMenu menu;
QGridLayout* gridLayout = new QGridLayout;
menu.setLayout(gridLayout);
int colCount = sqrt(emoticons.size()) + 1;
int row = 0;
int col = 0;
for (QStringList set : emoticons)
{
QPushButton* button = new QPushButton;
button->setIcon(SmileyPack::getInstance().getIcon(set[0]));
button->setToolTip(set.join(" "));
button->setProperty("sequence", set[0]);
connect(button, &QPushButton::clicked, this, &ChatForm::onAddEmote);
gridLayout->addWidget(button, row, ++col);
if (col >= colCount)
{
col = 0;
row++;
}
}
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
if (sender)
{
QPoint pos(gridLayout->totalSizeHint().width() / 2, gridLayout->totalSizeHint().height());
menu.exec(sender->mapToGlobal(-pos));
}
}
void ChatForm::onAddEmote()
{
// hide the QMenu
QMenu* menu = qobject_cast<QMenu*>(QObject::sender()->parent());
if (menu)
menu->hide();
// insert the emoticon
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
if (sender)
msgEdit->insertPlainText(' ' + sender->property("sequence").toString() + ' ');
msgEdit->setFocus(); // refocus so that you can continue typing
}

View File

@ -83,6 +83,8 @@ private slots:
void onCancelCallTriggered(); void onCancelCallTriggered();
void onChatContextMenuRequested(QPoint pos); void onChatContextMenuRequested(QPoint pos);
void onSaveLogClicked(); void onSaveLogClicked();
void onEmoteButtonClicked();
void onAddEmote();
private: private:
Friend* f; Friend* f;