From 0d74134dc1359890483f8dab205ff442430d2ba8 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sun, 4 Mar 2018 19:44:59 +0100 Subject: [PATCH] refactor(group): use displayedNameChanged signal --- src/model/contact.h | 3 +++ src/model/friend.h | 1 - src/model/group.cpp | 16 +++++++++------- src/model/group.h | 4 +--- src/widget/form/chatform.cpp | 5 ++--- src/widget/form/genericchatform.cpp | 5 ++++- src/widget/form/genericchatform.h | 3 ++- src/widget/form/groupchatform.cpp | 8 +++----- src/widget/friendlistwidget.cpp | 1 - src/widget/widget.cpp | 2 +- 10 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/model/contact.h b/src/model/contact.h index 8cf903bdb..e466261b6 100644 --- a/src/model/contact.h +++ b/src/model/contact.h @@ -35,6 +35,9 @@ public: virtual void setEventFlag(bool flag) = 0; virtual bool getEventFlag() const = 0; + +signals: + void displayedNameChanged(const QString& newName); }; #endif // CONTACT_H diff --git a/src/model/friend.h b/src/model/friend.h index 10ef932b3..2dd36dd92 100644 --- a/src/model/friend.h +++ b/src/model/friend.h @@ -52,7 +52,6 @@ public: Status getStatus() const; signals: - void displayedNameChanged(const QString& newName); void nameChanged(uint32_t friendId, const QString& name); void aliasChanged(uint32_t friendId, QString alias); void statusChanged(uint32_t friendId, Status status); diff --git a/src/model/group.cpp b/src/model/group.cpp index 0e6696ad6..08a44aea4 100644 --- a/src/model/group.cpp +++ b/src/model/group.cpp @@ -24,9 +24,7 @@ #include "src/persistence/settings.h" #include "src/widget/form/groupchatform.h" #include "src/widget/groupwidget.h" -#include "src/widget/gui.h" #include -#include static const int MAX_GROUP_TITLE_LENGTH = 128; @@ -64,17 +62,21 @@ void Group::updatePeer(int peerId, QString name) void Group::setName(const QString& newTitle) { - if (!newTitle.isEmpty() && title != newTitle) { - title = newTitle.left(MAX_GROUP_TITLE_LENGTH); + const QString shortTitle = newTitle.left(MAX_GROUP_TITLE_LENGTH); + if (!shortTitle.isEmpty() && title != shortTitle) { + title = shortTitle; + emit displayedNameChanged(title); emit titleChangedByUser(groupId, title); emit titleChanged(groupId, selfName, title); } } -void Group::onTitleChanged(const QString& author, const QString& newTitle) +void Group::setTitle(const QString& author, const QString& newTitle) { - if (!newTitle.isEmpty() && title != newTitle) { - title = newTitle.left(MAX_GROUP_TITLE_LENGTH); + const QString shortTitle = newTitle.left(MAX_GROUP_TITLE_LENGTH); + if (!shortTitle.isEmpty() && title != shortTitle) { + title = shortTitle; + emit displayedNameChanged(title); emit titleChanged(groupId, author, title); } } diff --git a/src/model/group.h b/src/model/group.h index d0ad73532..44bb3c430 100644 --- a/src/model/group.h +++ b/src/model/group.h @@ -27,8 +27,6 @@ #define RETRY_PEER_INFO_INTERVAL 500 -class Friend; -class GroupChatForm; class ToxPk; class Group : public Contact @@ -52,7 +50,7 @@ public: void updatePeer(int peerId, QString newName); void setName(const QString& newTitle) override; - void onTitleChanged(const QString& author, const QString& newTitle); + void setTitle(const QString& author, const QString& newTitle); QString getName() const; QString getDisplayedName() const override; diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 52e091147..32f566bd7 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -118,7 +118,8 @@ QString secondsToDHMS(quint32 duration) ChatForm::ChatForm(Friend* chatFriend, History* history) - : f(chatFriend) + : GenericChatForm(chatFriend) + , f(chatFriend) , history{history} , isTyping{false} , lastCallIsVideo{false} @@ -194,8 +195,6 @@ ChatForm::ChatForm(Friend* chatFriend, History* history) }); // reflect name changes in the header - // TODO(sudden6): check if this creates a cycle when the alias is changed - connect(f, &Friend::displayedNameChanged, this, &ChatForm::setName); connect(headWidget, &ChatFormHeader::nameChanged, this, [=](const QString& newName) { f->setAlias(newName); }); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index da4508ccb..251f50368 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -129,7 +129,7 @@ QPushButton* createButton(const QString& name, T* self, Fun onClickSlot) } -GenericChatForm::GenericChatForm(QWidget* parent) +GenericChatForm::GenericChatForm(const Contact* contact, QWidget* parent) : QWidget(parent, Qt::Window) , audioInputFlag(false) , audioOutputFlag(false) @@ -242,6 +242,9 @@ GenericChatForm::GenericChatForm(QWidget* parent) retranslateUi(); Translator::registerHandler(std::bind(&GenericChatForm::retranslateUi, this), this); + // update header on name/title change + connect(contact, &Contact::displayedNameChanged, this, &GenericChatForm::setName); + netcam = nullptr; } diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 099d745c3..d458f0342 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -35,6 +35,7 @@ class ChatFormHeader; class ChatLog; class ChatTextEdit; +class Contact; class ContentLayout; class CroppingLabel; class FlyoutOverlayWidget; @@ -57,7 +58,7 @@ class GenericChatForm : public QWidget { Q_OBJECT public: - explicit GenericChatForm(QWidget* parent = nullptr); + explicit GenericChatForm(const Contact* contact, QWidget* parent = nullptr); ~GenericChatForm() override; void setName(const QString& newName); diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index 6f8bc7ce3..76e0f05d6 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -73,7 +73,8 @@ QString editName(const QString& name) */ GroupChatForm::GroupChatForm(Group* chatGroup) - : group(chatGroup) + : GenericChatForm (chatGroup) + , group(chatGroup) , inCall(false) { nusersLabel = new QLabel(); @@ -114,9 +115,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) connect(headWidget, &ChatFormHeader::callTriggered, this, &GroupChatForm::onCallClicked); connect(headWidget, &ChatFormHeader::micMuteToggle, this, &GroupChatForm::onMicMuteToggle); connect(headWidget, &ChatFormHeader::volMuteToggle, this, &GroupChatForm::onVolMuteToggle); - connect(headWidget, &ChatFormHeader::nameChanged, this, [=](const QString& newName) { - chatGroup->setName(newName); - }); + connect(headWidget, &ChatFormHeader::nameChanged, chatGroup, &Group::setName); connect(group, &Group::userListChanged, this, &GroupChatForm::onUserListChanged); connect(group, &Group::titleChanged, this, &GroupChatForm::onTitleChanged); connect(&Settings::getInstance(), &Settings::blackListChanged, this, &GroupChatForm::updateUserNames); @@ -182,7 +181,6 @@ void GroupChatForm::onUserListChanged() void GroupChatForm::onTitleChanged(uint32_t groupId, const QString& author, const QString& title) { Q_UNUSED(groupId); - setName(title); if (author.isEmpty()) { return; } diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index b643811fd..d78ac7366 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -385,7 +385,6 @@ void FriendListWidget::searchChatrooms(const QString& searchString, bool hideOnl void FriendListWidget::renameGroupWidget(GroupWidget* groupWidget, const QString& newName) { groupLayout.removeSortedWidget(groupWidget); - groupWidget->setName(newName); groupLayout.addSortedWidget(groupWidget); } diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 344ea147b..cb40ba27d 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -1823,7 +1823,7 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q GUI::setWindowTitle(title); } - g->onTitleChanged(author, title); + g->setTitle(author, title); FilterCriteria filter = getFilterCriteria(); widget->searchName(ui->searchContactText->text(), filterGroups(filter)); }