New class for the work with emoticons - Emoticons

Redesigned emoticons window
- flat buttons
- 9 icons in a row
- moved the window to the direction of the input object

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3487 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-09-14 17:47:11 +00:00
parent 6e771af2b6
commit 0b63a4be86
15 changed files with 272 additions and 196 deletions

View File

@ -280,6 +280,7 @@ HEADERS += rshare.h \
gui/common/html.h \
gui/common/StatusDefs.h \
gui/common/TagDefs.h \
gui/common/Emoticons.h \
gui/MessagesDialog.h \
gui/help/browser/helpbrowser.h \
gui/help/browser/helptextbrowser.h \
@ -471,6 +472,7 @@ SOURCES += main.cpp \
gui/common/html.cpp \
gui/common/StatusDefs.cpp \
gui/common/TagDefs.cpp \
gui/common/Emoticons.cpp \
gui/settings/rsharesettings.cpp \
gui/settings/RsharePeerSettings.cpp \
gui/settings/rsettings.cpp \

View File

@ -26,12 +26,13 @@
#include <QItemDelegate>
#include "ForumsDialog.h"
#include "gui/RetroShareLink.h"
#include "gui/forums/CreateForum.h"
#include "gui/forums/CreateForumMsg.h"
#include "gui/forums/ForumDetails.h"
#include "RetroShareLink.h"
#include "forums/CreateForum.h"
#include "forums/CreateForumMsg.h"
#include "forums/ForumDetails.h"
#include "msgs/MessageComposer.h"
#include "gui/settings/rsharesettings.h"
#include "settings/rsharesettings.h"
#include "common/Emoticons.h"
#include <retroshare/rspeers.h>
#include <retroshare/rsforums.h>
@ -215,8 +216,6 @@ ForumsDialog::ForumsDialog(QWidget *parent)
ui.forumName->setFont(m_ForumNameFont);
ui.threadTitle->setFont(m_ForumNameFont);
style.loadEmoticons();
QMenu *forummenu = new QMenu();
forummenu->addAction(ui.actionCreate_Forum);
forummenu->addSeparator();
@ -1423,14 +1422,7 @@ void ForumsDialog::insertPost()
QString extraTxt;
extraTxt += QString::fromStdWString(msg.msg);
QHashIterator<QString, QString> i(style.smileys);
while(i.hasNext())
{
i.next();
foreach(QString code, i.key().split("|"))
extraTxt.replace(code, "<img src=\"" + i.value() + "\" />");
}
Emoticons::formatText(extraTxt);
ui.postText->setHtml(extraTxt);
ui.threadTitle->setText(QString::fromStdWString(msg.title));

View File

@ -24,7 +24,6 @@
#include "mainpage.h"
#include "RsAutoUpdatePage.h"
#include "chat/ChatStyle.h"
#include "ui_ForumsDialog.h"
class ForumsDialog : public RsAutoUpdatePage
@ -119,8 +118,6 @@ private:
int m_LastViewType;
std::string m_LastForumID;
ChatStyle style;
/** Qt Designer generated object */
Ui::ForumsDialog ui;
};

View File

@ -30,6 +30,7 @@
#include <QFileDialog>
#include "common/vmessagebox.h"
#include "common/StatusDefs.h"
#include "common/Emoticons.h"
#include <gui/mainpagestack.h>
#include "retroshare/rsinit.h"
@ -197,7 +198,6 @@ PeersDialog::PeersDialog(QWidget *parent)
ui.lineEdit->setFont(mCurrentFont);
style.setStyleFromSettings(ChatStyle::TYPE_PUBLIC);
style.loadEmoticons();
setChatInfo(tr("Welcome to RetroShare's group chat."), QString::fromUtf8("blue"));
@ -1387,7 +1387,7 @@ void PeersDialog::displayInfoChatMenu(const QPoint& pos)
void PeersDialog::smileyWidgetgroupchat()
{
style.showSmileyWidget(this, ui.emoticonBtn, SLOT(addSmileys()));
Emoticons::showSmileyWidget(this, ui.emoticonBtn, SLOT(addSmileys()), true);
}
void PeersDialog::addSmileys()

View File

@ -21,18 +21,11 @@
****************************************************************/
#include <QApplication>
#include <QDesktopWidget>
#include <QWidget>
#include <QFile>
#include <QIcon>
#include <QPushButton>
#include <QXmlStreamReader>
#include <iostream>
#include "ChatStyle.h"
#include "gui/settings/rsharesettings.h"
#include "gui/notifyqt.h"
#include "gui/common/Emoticons.h"
#include <retroshare/rsinit.h>
@ -106,154 +99,6 @@ bool ChatStyle::setStyleFromSettings(enumStyleType styleType)
return result;
}
void ChatStyle::loadEmoticons()
{
QString sm_codes;
bool internalEmoticons = true;
#if defined(Q_OS_WIN32)
// first try external emoticons
QFile sm_file(QApplication::applicationDirPath() + "/emoticons/emotes.acs");
if(sm_file.open(QIODevice::ReadOnly))
{
internalEmoticons = false;
} else {
// then embedded emotions
sm_file.setFileName(":/smileys/emotes.acs");
if(!sm_file.open(QIODevice::ReadOnly))
{
std::cout << "error opening ressource file" << std::endl ;
return ;
}
}
#else
QFile sm_file(QString(":/smileys/emotes.acs"));
if(!sm_file.open(QIODevice::ReadOnly))
{
std::cout << "error opening ressource file" << std::endl ;
return ;
}
#endif
sm_codes = sm_file.readAll();
sm_file.close();
sm_codes.remove("\n");
sm_codes.remove("\r");
int i = 0;
QString smcode;
QString smfile;
while(sm_codes[i] != '{')
{
i++;
}
while (i < sm_codes.length()-2)
{
smcode = "";
smfile = "";
while(sm_codes[i] != '\"')
{
i++;
}
i++;
while (sm_codes[i] != '\"')
{
smcode += sm_codes[i];
i++;
}
i++;
while(sm_codes[i] != '\"')
{
i++;
}
i++;
while(sm_codes[i] != '\"' && sm_codes[i+1] != ';')
{
smfile += sm_codes[i];
i++;
}
i++;
if(!smcode.isEmpty() && !smfile.isEmpty()) {
if (internalEmoticons) {
smileys.insert(smcode, ":/"+smfile);
} else {
smileys.insert(smcode, smfile);
}
}
}
// init <img> embedder
defEmbedImg.InitFromAwkwardHash(smileys);
}
void ChatStyle::showSmileyWidget(QWidget *parent, QWidget *button, const char *slotAddMethod)
{
QWidget *smWidget = new QWidget(parent, Qt::Popup);
smWidget->setAttribute( Qt::WA_DeleteOnClose);
smWidget->setWindowTitle("Emoticons");
smWidget->setWindowIcon(QIcon(QString(":/images/rstray3.png")));
smWidget->setBaseSize(4*24, (smileys.size()/4)*24);
//Warning: this part of code was taken from kadu instant messenger;
// It was EmoticonSelector::alignTo(QWidget* w) function there
// comments are Polish, I dont' know how does it work...
// oblicz pozycj<63> widgetu do kt<6B>rego r<>wnamy
// oblicz rozmiar selektora
QPoint pos = button->mapToGlobal(QPoint(0,0));
QSize e_size = smWidget->sizeHint();
// oblicz rozmiar pulpitu
QSize s_size = QApplication::desktop()->size();
// oblicz dystanse od widgetu do lewego brzegu i do prawego
int l_dist = pos.x();
int r_dist = s_size.width() - (pos.x() + button->width());
// oblicz pozycj<63> w zale<6C>no<6E>ci od tego czy po lewej stronie
// jest wi<77>cej miejsca czy po prawej
int x;
if (l_dist >= r_dist)
x = pos.x() - e_size.width();
else
x = pos.x() + button->width();
// oblicz pozycj<63> y - centrujemy w pionie
int y = pos.y() + button->height()/2 - e_size.height()/2;
// je<6A>li wychodzi poza doln<6C> kraw<61>d<EFBFBD> to r<>wnamy do niej
if (y + e_size.height() > s_size.height())
y = s_size.height() - e_size.height();
// je<6A>li wychodzi poza g<>rn<72> kraw<61>d<EFBFBD> to r<>wnamy do niej
if (y < 0)
y = 0;
// ustawiamy selektor na wyliczonej pozycji
smWidget->move(x, y);
x = 0;
y = 0;
QHashIterator<QString, QString> i(smileys);
while(i.hasNext())
{
i.next();
QPushButton *smButton = new QPushButton("", smWidget);
smButton->setGeometry(x*24, y*24, 24,24);
smButton->setIconSize(QSize(24,24));
smButton->setIcon(QPixmap(i.value()));
smButton->setToolTip(i.key());
++x;
if(x > 4)
{
x = 0;
y++;
}
QObject::connect(smButton, SIGNAL(clicked()), parent, slotAddMethod);
QObject::connect(smButton, SIGNAL(clicked()), smWidget, SLOT(close()));
}
smWidget->show();
}
static QString getStyle(QDir &styleDir, QString styleVariant, enumGetStyle type)
{
QString style;
@ -323,7 +168,7 @@ QString ChatStyle::formatText(QString &message, unsigned int flag)
RsChat::embedHtml(doc, body, defEmbedAhref);
}
if (flag & CHAT_FORMATTEXT_EMBED_SMILEYS) {
RsChat::embedHtml(doc, body, defEmbedImg);
RsChat::embedHtml(doc, body, Emoticons::defEmbedImg);
}
return doc.toString(-1); // -1 removes any annoying carriage return misinterpreted by QTextEdit
@ -415,8 +260,6 @@ static bool getStyleInfo(QString stylePath, QString stylePathRelative, ChatStyle
return false;
}
QDir dir(QApplication::applicationDirPath());
QXmlStreamReader reader;
reader.setDevice(&xmlFile);

View File

@ -74,8 +74,6 @@ public:
TYPE_HISTORY
};
QHash<QString, QString> smileys;
public:
/* Default constructor */
ChatStyle();
@ -84,12 +82,10 @@ public:
bool setStylePath(const QString &stylePath, const QString &styleVariant);
bool setStyleFromSettings(enumStyleType styleType);
void loadEmoticons();
QString formatMessage(enumFormatMessage type, QString &name, QDateTime &timestamp, QString &message, unsigned int flag);
QString formatText(QString &message, unsigned int flag);
void showSmileyWidget(QWidget *parent, QWidget *button, const char *slotAddMethod);
static bool getAvailableStyles(enumStyleType styleType, QList<ChatStyleInfo> &styles);
static bool getAvailableVariants(const QString &stylePath, QStringList &variants);
@ -103,7 +99,6 @@ private:
/** store default information for embedding HTML */
RsChat::EmbedInHtmlAhref defEmbedAhref;
RsChat::EmbedInHtmlImg defEmbedImg;
};
#endif // _CHATSTYLE_H

View File

@ -49,6 +49,7 @@
#include "gui/notifyqt.h"
#include "../RsAutoUpdatePage.h"
#include "gui/common/StatusDefs.h"
#include "gui/common/Emoticons.h"
#include "gui/feeds/AttachFileItem.h"
#include "gui/msgs/MessageComposer.h"
@ -105,7 +106,6 @@ PopupChatDialog::PopupChatDialog(std::string id, std::string name,
last_status_send_time = 0 ;
style.setStyleFromSettings(ChatStyle::TYPE_PRIVATE);
style.loadEmoticons();
/* Hide or show the frames */
showAvatarFrame(true);
@ -716,7 +716,7 @@ void PopupChatDialog::setFont()
void PopupChatDialog::smileyWidget()
{
style.showSmileyWidget(this, ui.emoteiconButton, SLOT(addSmiley()));
Emoticons::showSmileyWidget(this, ui.emoteiconButton, SLOT(addSmiley()), true);
}
//============================================================================

View File

@ -0,0 +1,207 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2010, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QHash>
#include <QFile>
#include <QApplication>
#include <QWidget>
#include <QFile>
#include <QIcon>
#include <QDesktopWidget>
#include <QPushButton>
#include <iostream>
#include "Emoticons.h"
static QHash<QString, QString> Smileys;
RsChat::EmbedInHtmlImg Emoticons::defEmbedImg;
void Emoticons::load()
{
QString sm_codes;
bool internalEmoticons = true;
#if defined(Q_OS_WIN32)
// first try external emoticons
QFile sm_file(QApplication::applicationDirPath() + "/emoticons/emotes.acs");
if(sm_file.open(QIODevice::ReadOnly))
{
internalEmoticons = false;
} else {
// then embedded emotions
sm_file.setFileName(":/smileys/emotes.acs");
if(!sm_file.open(QIODevice::ReadOnly))
{
std::cout << "error opening ressource file" << std::endl ;
return ;
}
}
#else
QFile sm_file(QString(":/smileys/emotes.acs"));
if(!sm_file.open(QIODevice::ReadOnly))
{
std::cout << "error opening ressource file" << std::endl ;
return ;
}
#endif
sm_codes = sm_file.readAll();
sm_file.close();
sm_codes.remove("\n");
sm_codes.remove("\r");
int i = 0;
QString smcode;
QString smfile;
while(sm_codes[i] != '{')
{
i++;
}
while (i < sm_codes.length()-2)
{
smcode = "";
smfile = "";
while(sm_codes[i] != '\"')
{
i++;
}
i++;
while (sm_codes[i] != '\"')
{
smcode += sm_codes[i];
i++;
}
i++;
while(sm_codes[i] != '\"')
{
i++;
}
i++;
while(sm_codes[i] != '\"' && sm_codes[i+1] != ';')
{
smfile += sm_codes[i];
i++;
}
i++;
if(!smcode.isEmpty() && !smfile.isEmpty()) {
if (internalEmoticons) {
Smileys.insert(smcode, ":/"+smfile);
} else {
Smileys.insert(smcode, smfile);
}
}
}
// init <img> embedder
defEmbedImg.InitFromAwkwardHash(Smileys);
}
void Emoticons::showSmileyWidget(QWidget *parent, QWidget *button, const char *slotAddMethod, bool above)
{
QWidget *smWidget = new QWidget(parent, Qt::Popup);
const int buttonWidth = 24;
const int buttonHeight = 24;
const int countPerLine = 9;
int rowCount = (Smileys.size()/countPerLine) + ((Smileys.size() % countPerLine) ? 1 : 0);
smWidget->setAttribute( Qt::WA_DeleteOnClose);
smWidget->setWindowTitle("Emoticons");
smWidget->setWindowIcon(QIcon(QString(":/images/rstray3.png")));
smWidget->setBaseSize(countPerLine*buttonWidth, rowCount*buttonHeight);
//Warning: this part of code was taken from kadu instant messenger;
// It was EmoticonSelector::alignTo(QWidget* w) function there
// comments are Polish, I dont' know how does it work...
// oblicz pozycj<63> widgetu do kt<6B>rego r<>wnamy
// oblicz rozmiar selektora
QPoint pos = button->mapToGlobal(QPoint(0,0));
QSize e_size = smWidget->sizeHint();
// oblicz rozmiar pulpitu
QSize s_size = QApplication::desktop()->size();
// oblicz dystanse od widgetu do lewego brzegu i do prawego
int l_dist = pos.x();
int r_dist = s_size.width() - (pos.x() + button->width());
// oblicz pozycj<63> w zale<6C>no<6E>ci od tego czy po lewej stronie
// jest wi<77>cej miejsca czy po prawej
int x;
if (l_dist >= r_dist)
x = pos.x() - e_size.width();
else
x = pos.x() + button->width();
// oblicz pozycj<63> y - centrujemy w pionie
int y = pos.y() + button->height()/2 - e_size.height()/2;
// je<6A>li wychodzi poza doln<6C> kraw<61>d<EFBFBD> to r<>wnamy do niej
if (y + e_size.height() > s_size.height())
y = s_size.height() - e_size.height();
if (above) {
y -= rowCount*buttonHeight;
}
// je<6A>li wychodzi poza g<>rn<72> kraw<61>d<EFBFBD> to r<>wnamy do niej
if (y < 0)
y = 0;
// ustawiamy selektor na wyliczonej pozycji
smWidget->move(x, y);
x = 0;
y = 0;
QHashIterator<QString, QString> i(Smileys);
while(i.hasNext())
{
i.next();
QPushButton *smButton = new QPushButton("", smWidget);
smButton->setGeometry(x*buttonWidth, y*buttonHeight, buttonWidth, buttonHeight);
smButton->setIconSize(QSize(buttonWidth, buttonHeight));
smButton->setIcon(QPixmap(i.value()));
smButton->setToolTip(i.key());
smButton->setFlat(true);
++x;
if(x >= countPerLine)
{
x = 0;
y++;
}
QObject::connect(smButton, SIGNAL(clicked()), parent, slotAddMethod);
QObject::connect(smButton, SIGNAL(clicked()), smWidget, SLOT(close()));
}
smWidget->show();
}
void Emoticons::formatText(QString &text)
{
QHashIterator<QString, QString> i(Smileys);
while(i.hasNext()) {
i.next();
foreach (QString code, i.key().split("|")) {
text.replace(code, "<img src=\"" + i.value() + "\" />");
}
}
}

View File

@ -0,0 +1,45 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2010, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _EMOTICONS_H
#define _EMOTICONS_H
#include "gui/chat/HandleRichText.h"
class QWidget;
class QString;
class Emoticons
{
public:
static void load();
static void showSmileyWidget(QWidget *parent, QWidget *button, const char *slotAddMethod, bool above);
static void formatText(QString &text);
public:
static RsChat::EmbedInHtmlImg defEmbedImg;
};
#endif

View File

@ -154,7 +154,6 @@ void ChatMsgItem::insertChat(std::string &message)
// embed smileys ?
if (Settings->valueFromGroup(QString("Chat"), QString::fromUtf8("Emoteicons_GroupChat"), true).toBool()) {
style.loadEmoticons();
formatFlag |= CHAT_FORMATTEXT_EMBED_SMILEYS;
}

View File

@ -35,6 +35,7 @@
#include "gui/settings/rsharesettings.h"
#include "gui/RetroShareLink.h"
#include "gui/feeds/AttachFileItem.h"
#include "gui/common/Emoticons.h"
#include <sys/stat.h>
@ -59,8 +60,6 @@ CreateForumMsg::CreateForumMsg(std::string fId, std::string pId)
connect( ui.pastersButton, SIGNAL(clicked() ), this , SLOT(pasteLink()));
newMsg();
style.loadEmoticons();
}
/** context menu searchTablewidget2 **/
@ -189,7 +188,7 @@ void CreateForumMsg::cancelMsg()
void CreateForumMsg::smileyWidgetForums()
{
style.showSmileyWidget(this, ui.emoticonButton, SLOT(addSmileys()));
Emoticons::showSmileyWidget(this, ui.emoticonButton, SLOT(addSmileys()), false);
}
void CreateForumMsg::addSmileys()

View File

@ -25,8 +25,6 @@
#include "ui_CreateForumMsg.h"
#include "gui/chat/ChatStyle.h"
class AttachFileItem;
class CreateForumMsg : public QMainWindow
@ -64,8 +62,6 @@ private:
std::string mForumId;
std::string mParentId;
ChatStyle style;
/** Qt Designer generated object */
Ui::CreateForumMsg ui;
};

View File

@ -71,7 +71,6 @@ ImHistoryBrowser::ImHistoryBrowser(bool isPrivateChatIn, IMHistoryKeeper &histKe
}
style.setStyleFromSettings(ChatStyle::TYPE_HISTORY);
style.loadEmoticons();
ui.listWidget->setItemDelegate(new IMHistoryItemDelegate);

View File

@ -198,7 +198,6 @@ void ChatPage::setPreviewMessages(QString &stylePath, QString styleVariant, QTex
{
ChatStyle style;
style.setStylePath(stylePath, styleVariant);
style.loadEmoticons();
textBrowser->clear();

View File

@ -39,6 +39,7 @@
#include "gui/settings/RsharePeerSettings.h"
#include "gui/connect/ConfCertDialog.h"
#include "idle/idle.h"
#include "gui/common/Emoticons.h"
/*** WINDOWS DON'T LIKE THIS - REDEFINES VER numbers.
#include <gui/qskinobject/qskinobject.h>
@ -168,6 +169,8 @@ int main(int argc, char *argv[])
rshare.setQuitOnLastWindowClosed(true);
#else
Emoticons::load();
MainWindow *w = MainWindow::Create ();
// I'm using a signal to transfer the hashing info to the mainwindow, because Qt schedules signals properly to