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

Adds functionality for sorting FriendWidgets alphabetically in the

friends list. Widgets are sorted upon being added to the friends list in
the first place, and re-sorted if a user changes their name, or if a
user is given an alias.

-Friend now inherits from QObject
-Friend objects now emit a signal when their display name is changed
-FriendListWidget::moveWidget() is now a slot
This commit is contained in:
zero-one 2015-03-11 17:23:22 -07:00
parent 4f0798bde7
commit fdb4fef374
5 changed files with 30 additions and 9 deletions

View File

@ -53,6 +53,8 @@ void Friend::setName(QString name)
if (widget->isActive())
GUI::setWindowTitle(name);
emit displayedNameChanged(getFriendWidget(), getStatus(), hasNewEvents);
}
}
@ -66,6 +68,8 @@ void Friend::setAlias(QString name)
if (widget->isActive())
GUI::setWindowTitle(dispName);
emit displayedNameChanged(getFriendWidget(), getStatus(), hasNewEvents);
}
void Friend::setStatusMessage(QString message)

View File

@ -17,14 +17,16 @@
#ifndef FRIEND_H
#define FRIEND_H
#include <QObject>
#include <QString>
#include "corestructs.h"
struct FriendWidget;
class ChatForm;
struct Friend
struct Friend : QObject
{
Q_OBJECT
public:
Friend(int FriendId, const ToxID &UserId);
Friend(const Friend& other)=delete;
@ -49,6 +51,9 @@ public:
ChatForm *getChatForm();
FriendWidget *getFriendWidget();
signals:
void displayedNameChanged(FriendWidget* widget, Status s, int hasNewEvents);
private:
QString userAlias, userName;
ToxID userID;

View File

@ -16,6 +16,9 @@
#include "friendlistwidget.h"
#include <QDebug>
#include <QGridLayout>
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/widget/friendwidget.h"
FriendListWidget::FriendListWidget(QWidget *parent) :
QWidget(parent)
@ -63,9 +66,18 @@ QVBoxLayout* FriendListWidget::getFriendLayout(Status s)
void FriendListWidget::moveWidget(QWidget *w, Status s, int hasNewEvents)
{
mainLayout->removeWidget(w);
if (hasNewEvents == 0)
getFriendLayout(s)->addWidget(w);
else
getFriendLayout(s)->insertWidget(0, w);
getFriendLayout(s)->removeWidget(w);
QVBoxLayout* l = getFriendLayout(s);
Friend* g = FriendList::findFriend(dynamic_cast<FriendWidget*>(w)->friendId);
for(int i = 0; i < l->count(); i++){
FriendWidget* w1 = dynamic_cast<FriendWidget*>(l->itemAt(i)->widget());
if(w1 != NULL){
Friend* f = FriendList::findFriend(w1->friendId);
if(f->getDisplayedName().localeAwareCompare(g->getDisplayedName()) > 0){
l->insertWidget(i,w);
return;
}
}
}
l->addWidget(w);
}

View File

@ -33,11 +33,11 @@ public:
QVBoxLayout* getGroupLayout();
QVBoxLayout* getFriendLayout(Status s);
void moveWidget(QWidget *w, Status s, int hasNewEvents);
signals:
public slots:
void moveWidget(QWidget *w, Status s, int hasNewEvents);
private:
QHash<int, QVBoxLayout*> layouts;

View File

@ -559,10 +559,10 @@ void Widget::addFriend(int friendId, const QString &userId)
//qDebug() << "Widget: Adding friend with id" << userId;
ToxID userToxId = ToxID::fromString(userId);
Friend* newfriend = FriendList::addFriend(friendId, userToxId);
QLayout* layout = contactListWidget->getFriendLayout(Status::Offline);
layout->addWidget(newfriend->getFriendWidget());
contactListWidget->moveWidget(newfriend->getFriendWidget(),Status::Offline,0);
Core* core = Nexus::getCore();
connect(newfriend, &Friend::displayedNameChanged, contactListWidget, &FriendListWidget::moveWidget);
connect(settingsWidget, &SettingsWidget::compactToggled, newfriend->getFriendWidget(), &GenericChatroomWidget::onCompactChanged);
connect(newfriend->getFriendWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*)));
connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));