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

possiblity to setup alias

This commit is contained in:
apprb 2014-11-07 23:55:32 +09:00
parent b316f6e13f
commit af8df8e707
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
6 changed files with 81 additions and 24 deletions

View File

@ -18,35 +18,65 @@
#include "friendlist.h"
#include <QMenu>
#include <QDebug>
#include <QHash>
QList<Friend*> FriendList::friendList;
QHash<int, Friend*> FriendList::friendList;
QHash<QString, int> FriendList::tox2id;
Friend* FriendList::addFriend(int friendId, const QString& userId)
{
for (Friend* f : friendList)
if (f->getFriendID() == friendId)
qWarning() << "FriendList::addFriend: friendId already taken";
auto friendChecker = friendList.find(friendId);
if (friendChecker != friendList.end())
qWarning() << "FriendList::addFriend: friendId already taken";
Friend* newfriend = new Friend(friendId, userId);
friendList.append(newfriend);
friendList[friendId] = newfriend;
tox2id[userId] = friendId;
return newfriend;
}
Friend* FriendList::findFriend(int friendId)
{
for (Friend* f : friendList)
if (f->getFriendID() == friendId)
return f;
auto f_it = friendList.find(friendId);
if (f_it != friendList.end())
return *f_it;
return nullptr;
}
void FriendList::removeFriend(int friendId)
{
for (int i=0; i<friendList.size(); i++)
{
if (friendList[i]->getFriendID() == friendId)
{
friendList.removeAt(i);
return;
}
}
auto f_it = friendList.find(friendId);
if (f_it != friendList.end())
friendList.erase(f_it);
}
void FriendList::clear()
{
for (auto friendptr : friendList)
delete friendptr;
}
Friend* FriendList::findFriend(QString userId)
{
auto id = tox2id.find(userId);
if (id != tox2id.end())
{
Friend *f = findFriend(*id);
if (f->getToxID() == ToxID::fromString(userId))
return f;
}
return nullptr;
}
QList<Friend*> FriendList::getAllFriends()
{
QList<Friend*> res;
for (auto it : friendList)
res.append(it);
return res;
}

View File

@ -18,6 +18,7 @@
#define FRIENDLIST_H
template <class T> class QList;
template <class A, class B> class QHash;
struct Friend;
class QString;
@ -27,10 +28,14 @@ public:
FriendList();
static Friend* addFriend(int friendId, const QString& userId);
static Friend* findFriend(int friendId);
static Friend* findFriend(QString userId);
static QList<Friend*> getAllFriends();
static void removeFriend(int friendId);
static void clear();
public:
static QList<Friend*> friendList;
private:
static QHash<int, Friend*> friendList;
static QHash<QString, int> tox2id;
};
#endif // FRIENDLIST_H

View File

@ -31,6 +31,8 @@
#include "src/widget/tool/chattextedit.h"
#include "src/widget/maskablepixmapwidget.h"
#include "src/core.h"
#include "src/friendlist.h"
#include "src/friend.h"
GenericChatForm::GenericChatForm(QWidget *parent) :
QWidget(parent),
@ -342,8 +344,13 @@ ChatActionPtr GenericChatForm::genMessageActionAction(const ToxID& author, QStri
QString authorStr;
if (isMe)
authorStr = core->getUsername();
else
authorStr = core->getPeerName(author);
else {
Friend *f = FriendList::findFriend(author.publicKey);
if (f)
authorStr = f->getDisplayedName();
else
authorStr = core->getPeerName(author);
}
if (authorStr.isEmpty()) // Fallback if we can't find a username
authorStr = author.toString();

View File

@ -42,6 +42,10 @@ FriendWidget::FriendWidget(int FriendId, QString id)
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
nameLabel->setText(id);
nameLabel->setAttribute(Qt::WA_NoMousePropagation);
nameLabel->setEditable(true);
connect(nameLabel, SIGNAL(textChanged(QString,QString)), this, SLOT(onFriendAliasChange(QString,QString)));
}
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
@ -210,3 +214,9 @@ void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}
void FriendWidget::onFriendAliasChange(QString newText, QString)
{
Friend* f = FriendList::findFriend(friendId);
f->setAlias(newText);
}

View File

@ -45,6 +45,9 @@ public slots:
void onAvatarChange(int FriendId, const QPixmap& pic);
void onAvatarRemoved(int FriendId);
private slots:
void onFriendAliasChange(QString newText, QString oldText);
protected:
void mousePressEvent(QMouseEvent* ev);
void mouseMoveEvent(QMouseEvent* ev);

View File

@ -276,9 +276,7 @@ Widget::~Widget()
delete addFriendForm;
delete filesForm;
for (Friend* f : FriendList::friendList)
delete f;
FriendList::friendList.clear();
FriendList::clear();
for (Group* g : GroupList::groupList)
delete g;
GroupList::groupList.clear();
@ -604,6 +602,9 @@ void Widget::addFriend(int friendId, const QString &userId)
Friend* newfriend = FriendList::addFriend(friendId, userId);
QLayout* layout = contactListWidget->getFriendLayout(Status::Offline);
layout->addWidget(newfriend->getFriendWidget());
newfriend->setAlias("");
connect(newfriend->getFriendWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*)));
connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));
connect(newfriend->getFriendWidget(), SIGNAL(copyFriendIdToClipboard(int)), this, SLOT(copyFriendIdToClipboard(int)));
@ -820,7 +821,8 @@ void Widget::removeFriend(int friendId)
void Widget::clearContactsList()
{
for (Friend* f : FriendList::friendList)
QList<Friend*> friends = FriendList::getAllFriends();
for (Friend* f : friends)
removeFriend(f);
for (Group* g : GroupList::groupList)
removeGroup(g);