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

refactor: Move some methods back to ContentDialog

Store in ContentDialog list of widgets and list of ContentDialogs in
ContentDialogManger
This commit is contained in:
Diadlo 2018-12-24 21:36:39 +03:00
parent 7ad8607c43
commit ee5a7d8e94
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
7 changed files with 73 additions and 74 deletions

View File

@ -156,6 +156,7 @@ FriendWidget* ContentDialog::addFriend(std::shared_ptr<FriendChatroom> chatroom,
auto frnd = chatroom->getFriend(); auto frnd = chatroom->getFriend();
auto friendId = frnd->getId(); auto friendId = frnd->getId();
auto friendWidget = new FriendWidget(chatroom, compact); auto friendWidget = new FriendWidget(chatroom, compact);
friendWidgets[friendId] = friendWidget;
friendLayout->addFriendWidget(friendWidget, frnd->getStatus()); friendLayout->addFriendWidget(friendWidget, frnd->getStatus());
friendChatForms[friendId] = form; friendChatForms[friendId] = form;
@ -174,7 +175,8 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
const auto g = chatroom->getGroup(); const auto g = chatroom->getGroup();
const auto groupId = g->getId(); const auto groupId = g->getId();
const auto compact = Settings::getInstance().getCompactLayout(); const auto compact = Settings::getInstance().getCompactLayout();
GroupWidget* groupWidget = new GroupWidget(chatroom, compact); auto groupWidget = new GroupWidget(chatroom, compact);
groupWidgets[groupId] = groupWidget;
groupLayout.addSortedWidget(groupWidget); groupLayout.addSortedWidget(groupWidget);
groupChatForms[groupId] = form; groupChatForms[groupId] = form;
@ -186,11 +188,9 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
return groupWidget; return groupWidget;
} }
/** void ContentDialog::removeFriend(int friendId)
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeFriend(FriendWidget* chatroomWidget)
{ {
auto chatroomWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this, disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this,
&ContentDialog::updateFriendWidget); &ContentDialog::updateFriendWidget);
@ -211,13 +211,15 @@ void ContentDialog::removeFriend(FriendWidget* chatroomWidget)
} else { } else {
update(); update();
} }
friendWidgets.remove(friendId);
friendChatForms.remove(friendId);
closeIfEmpty();
} }
/** void ContentDialog::removeGroup(int groupId)
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeGroup(GroupWidget* chatroomWidget)
{ {
auto chatroomWidget = qobject_cast<GroupWidget*>(groupWidgets[groupId]);
// Need to find replacement to show here instead. // Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) { if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false); cycleContacts(true, false);
@ -233,6 +235,17 @@ void ContentDialog::removeGroup(GroupWidget* chatroomWidget)
} else { } else {
update(); update();
} }
groupWidgets.remove(groupId);
groupChatForms.remove(groupId);
closeIfEmpty();
}
void ContentDialog::closeIfEmpty()
{
if (friendWidgets.isEmpty() && groupWidgets.isEmpty()) {
close();
}
} }
int ContentDialog::chatroomWidgetCount() const int ContentDialog::chatroomWidgetCount() const
@ -463,10 +476,11 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
int friendId = contact->getId(); int friendId = contact->getId();
// If friend is already in a dialog then you can't drop friend where it already is. // If friend is already in a dialog then you can't drop friend where it already is.
if (!ContentDialogManager::getInstance()->hasFriendWidget(this, friendId, frnd)) { if (!hasFriendWidget(friendId)) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
} else if (group) { } else if (group) {
qDebug() << event->mimeData()->formats();
if (!event->mimeData()->hasFormat("groupId")) { if (!event->mimeData()->hasFormat("groupId")) {
return; return;
} }
@ -477,7 +491,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return; return;
} }
if (!ContentDialogManager::getInstance()->hasFriendWidget(this, groupId, group)) { if (!hasGroupWidget(groupId)) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
} }
@ -495,8 +509,6 @@ void ContentDialog::dropEvent(QDropEvent* event)
return; return;
} }
int friendId = contact->getId();
ContentDialogManager::getInstance()->removeFriend(friendId);
Widget::getInstance()->addFriendDialog(contact, this); Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
} else if (group) { } else if (group) {
@ -510,7 +522,6 @@ void ContentDialog::dropEvent(QDropEvent* event)
return; return;
} }
ContentDialogManager::getInstance()->removeGroup(groupId);
Widget::getInstance()->addGroupDialog(contact, this); Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
} }
@ -588,8 +599,7 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias) void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
// TODO: getFriendWidget from own container FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
FriendWidget* friendWidget = ContentDialogManager::getInstance()->getFriendWidget(friendId);
Status status = f->getStatus(); Status status = f->getStatus();
friendLayout->addFriendWidget(friendWidget, status); friendLayout->addFriendWidget(friendWidget, status);
@ -631,22 +641,14 @@ void ContentDialog::saveSplitterState()
Settings::getInstance().setDialogSplitterState(splitter->saveState()); Settings::getInstance().setDialogSplitterState(splitter->saveState());
} }
/** bool ContentDialog::hasFriendWidget(int friendId) const
* @brief Check if current ContentDialog instance and chatroom widget associated with user.
* @param id User Id.
* @param chatroomWidget Widget which should be a pair for current dialog.
* @param list List with contact info.
* @return True, if chatroomWidget is pair for current instance.
*/
bool ContentDialog::hasWidget(int id, const GenericChatroomWidget* chatroomWidget,
const QHash<int, ContactInfo>& list) const
{ {
auto iter = list.find(id); return friendWidgets.contains(friendId);
if (iter == list.end()) {
return false;
} }
return std::get<0>(*iter) == this && std::get<1>(*iter) == chatroomWidget; bool ContentDialog::hasGroupWidget(int groupId) const
{
return groupWidgets.contains(groupId);
} }
/** /**

View File

@ -58,8 +58,8 @@ public:
FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form); FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form); GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
void removeFriend(FriendWidget* chatroomWidget); void removeFriend(int friendId);
void removeGroup(GroupWidget* chatroomWidget); void removeGroup(int groupId);
int chatroomWidgetCount() const; int chatroomWidgetCount() const;
void ensureSplitterVisible(); void ensureSplitterVisible();
void updateTitleAndStatusIcon(); void updateTitleAndStatusIcon();
@ -71,9 +71,8 @@ public:
void addFriendWidget(FriendWidget* widget, Status status); void addFriendWidget(FriendWidget* widget, Status status);
bool isActiveWidget(GenericChatroomWidget* widget); bool isActiveWidget(GenericChatroomWidget* widget);
bool hasWidget(int id, const GenericChatroomWidget* chatroomWidget, bool hasFriendWidget(int friendId) const;
const QHash<int, ContactInfo>& list) const; bool hasGroupWidget(int groupId) const;
signals: signals:
void friendDialogShown(const Friend* f); void friendDialogShown(const Friend* f);
@ -104,6 +103,7 @@ private slots:
void onGroupchatPositionChanged(bool top); void onGroupchatPositionChanged(bool top);
private: private:
void closeIfEmpty();
void closeEvent(QCloseEvent* event) override; void closeEvent(QCloseEvent* event) override;
void retranslateUi(); void retranslateUi();
@ -121,6 +121,9 @@ private:
QSize videoSurfaceSize; QSize videoSurfaceSize;
int videoCount; int videoCount;
QHash<int, GenericChatroomWidget*> friendWidgets;
QHash<int, GenericChatroomWidget*> groupWidgets;
QHash<int, GenericChatForm*> friendChatForms; QHash<int, GenericChatForm*> friendChatForms;
QHash<int, GenericChatForm*> groupChatForms; QHash<int, GenericChatForm*> groupChatForms;

View File

@ -21,6 +21,17 @@ void removeDialog(ContentDialog* dialog, QHash<int, ContactInfo>& infos)
} }
} }
} }
void removeDialog(ContentDialog* dialog, QHash<int, ContentDialog*>& dialogs)
{
for (auto it = dialogs.begin(); it != dialogs.end();) {
if (*it == dialog) {
it = dialogs.erase(it);
} else {
++it;
}
}
}
} }
ContentDialogManager* ContentDialogManager::instance; ContentDialogManager* ContentDialogManager::instance;
@ -48,9 +59,10 @@ FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
ContentDialog* lastDialog = getFriendDialog(friendId); ContentDialog* lastDialog = getFriendDialog(friendId);
if (lastDialog) { if (lastDialog) {
lastDialog->removeFriend(friendWidget); lastDialog->removeFriend(friendId);
} }
friendDialogs[friendId] = dialog;
friendList.insert(friendId, std::make_tuple(dialog, friendWidget)); friendList.insert(friendId, std::make_tuple(dialog, friendWidget));
return friendWidget; return friendWidget;
} }
@ -63,9 +75,10 @@ GroupWidget* ContentDialogManager::addGroupToDialog(ContentDialog* dialog,
ContentDialog* lastDialog = getGroupDialog(groupId); ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog) { if (lastDialog) {
lastDialog->removeGroup(groupWidget); lastDialog->removeGroup(groupId);
} }
groupDialogs[groupId] = dialog;
groupList.insert(groupId, std::make_tuple(dialog, groupWidget)); groupList.insert(groupId, std::make_tuple(dialog, groupWidget));
return groupWidget; return groupWidget;
} }
@ -80,7 +93,7 @@ void ContentDialogManager::removeFriend(int friendId)
auto friendWidget = static_cast<FriendWidget*>(std::get<1>(iter.value())); auto friendWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
auto dialog = getFriendDialog(friendId); auto dialog = getFriendDialog(friendId);
dialog->removeFriend(friendWidget); dialog->removeFriend(friendId);
friendList.remove(friendId); friendList.remove(friendId);
} }
@ -93,7 +106,7 @@ void ContentDialogManager::removeGroup(int groupId)
auto groupWidget = static_cast<GroupWidget*>(std::get<1>(iter.value())); auto groupWidget = static_cast<GroupWidget*>(std::get<1>(iter.value()));
auto dialog = getGroupDialog(groupId); auto dialog = getGroupDialog(groupId);
dialog->removeGroup(groupWidget); dialog->removeGroup(groupId);
groupList.remove(groupId); groupList.remove(groupId);
} }
@ -163,7 +176,6 @@ void ContentDialogManager::updateFriendStatus(int friendId)
void ContentDialogManager::updateFriendStatusMessage(int friendId, const QString& message) void ContentDialogManager::updateFriendStatusMessage(int friendId, const QString& message)
{ {
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
if (iter == friendList.end()) { if (iter == friendList.end()) {
return; return;
} }
@ -282,25 +294,7 @@ void ContentDialogManager::onDialogClose()
removeDialog(dialog, friendList); removeDialog(dialog, friendList);
removeDialog(dialog, groupList); removeDialog(dialog, groupList);
}
bool ContentDialogManager::hasFriendWidget(ContentDialog* dialog, int friendId, const GenericChatroomWidget* chatroomWidget) const removeDialog(dialog, friendDialogs);
{ removeDialog(dialog, groupDialogs);
return dialog->hasWidget(friendId, chatroomWidget, friendList);
}
bool ContentDialogManager::hasGroupWidget(ContentDialog* dialog, int groupId, const GenericChatroomWidget* chatroomWidget) const
{
return dialog->hasWidget(groupId, chatroomWidget, groupList);
}
FriendWidget* ContentDialogManager::getFriendWidget(int friendId) const
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return nullptr;
}
GenericChatroomWidget* widget = std::get<1>(*iter);
return qobject_cast<FriendWidget*>(widget);
} }

View File

@ -53,11 +53,6 @@ public:
static ContentDialogManager* getInstance(); static ContentDialogManager* getInstance();
bool hasFriendWidget(ContentDialog* dialog, int friendId, const GenericChatroomWidget* chatroomWidget) const;
bool hasGroupWidget(ContentDialog* dialog, int groupId, const GenericChatroomWidget* chatroomWidget) const;
FriendWidget* getFriendWidget(int friendId) const;
private slots: private slots:
void onDialogClose(); void onDialogClose();
void onDialogActivate(); void onDialogActivate();
@ -70,6 +65,10 @@ private:
ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list) const; ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list) const;
ContentDialog* currentDialog = nullptr; ContentDialog* currentDialog = nullptr;
QHash<int, ContentDialog*> friendDialogs;
QHash<int, ContentDialog*> groupDialogs;
QHash<int, ContactInfo> friendList; QHash<int, ContactInfo> friendList;
QHash<int, ContactInfo> groupList; QHash<int, ContactInfo> groupList;

View File

@ -115,7 +115,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
} }
// TODO: move to model // TODO: move to model
if (contentDialog && ContentDialogManager::getInstance()->hasFriendWidget(contentDialog, friendId, this)) { if (contentDialog && contentDialog->hasFriendWidget(friendId)) {
const auto removeChatWindow = menu.addAction(tr("Remove chat from this window")); const auto removeChatWindow = menu.addAction(tr("Remove chat from this window"));
connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow); connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow);
} }
@ -170,7 +170,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
menu.addSeparator(); menu.addSeparator();
// TODO: move to model // TODO: move to model
if (!contentDialog || !ContentDialogManager::getInstance()->hasFriendWidget(contentDialog, friendId, this)) { if (!contentDialog || !contentDialog->hasFriendWidget(friendId)) {
const auto removeAction = const auto removeAction =
menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist")); menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendId); }, connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendId); },

View File

@ -95,7 +95,7 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
openChatWindow = menu.addAction(tr("Open chat in new window")); openChatWindow = menu.addAction(tr("Open chat in new window"));
} }
if (contentDialog && ContentDialogManager::getInstance()->hasGroupWidget(contentDialog, groupId, this)) { if (contentDialog && contentDialog->hasGroupWidget(groupId)) {
removeChatWindow = menu.addAction(tr("Remove chat from this window")); removeChatWindow = menu.addAction(tr("Remove chat from this window"));
} }

View File

@ -1131,12 +1131,12 @@ void Widget::onFriendAliasChanged(uint32_t friendId, const QString& alias)
void Widget::onChatroomWidgetClicked(GenericChatroomWidget* widget) void Widget::onChatroomWidgetClicked(GenericChatroomWidget* widget)
{ {
openDialog(widget, false); openDialog(widget, /* newWindow = */ false);
} }
void Widget::openNewDialog(GenericChatroomWidget* widget) void Widget::openNewDialog(GenericChatroomWidget* widget)
{ {
openDialog(widget, true); openDialog(widget, /* newWindow = */ true);
} }
void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow) void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
@ -1248,7 +1248,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
auto form = chatForms[friendId]; auto form = chatForms[friendId];
auto chatroom = friendChatrooms[friendId]; auto chatroom = friendChatrooms[friendId];
FriendWidget* friendWidget = dialog->addFriend(chatroom, form); FriendWidget* friendWidget = ContentDialogManager::getInstance()->addFriendToDialog(dialog, chatroom, form);
friendWidget->setStatusMsg(widget->getStatusMsg()); friendWidget->setStatusMsg(widget->getStatusMsg());
@ -1259,7 +1259,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
#endif #endif
connect(friendWidget, &FriendWidget::removeFriend, this, widgetRemoveFriend); connect(friendWidget, &FriendWidget::removeFriend, this, widgetRemoveFriend);
connect(friendWidget, &FriendWidget::middleMouseClicked, dialog, connect(friendWidget, &FriendWidget::middleMouseClicked, dialog,
[=]() { ContentDialogManager::getInstance()->removeFriend(friendId); }); [=]() { dialog->removeFriend(friendId); });
connect(friendWidget, &FriendWidget::copyFriendIdToClipboard, this, connect(friendWidget, &FriendWidget::copyFriendIdToClipboard, this,
&Widget::copyFriendIdToClipboard); &Widget::copyFriendIdToClipboard);
connect(friendWidget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog); connect(friendWidget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog);
@ -1304,7 +1304,8 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
auto chatForm = groupChatForms[groupId].data(); auto chatForm = groupChatForms[groupId].data();
auto chatroom = groupChatrooms[groupId]; auto chatroom = groupChatrooms[groupId];
auto groupWidget = dialog->addGroup(chatroom, chatForm); auto groupWidget = ContentDialogManager::getInstance()->addGroupToDialog(dialog, chatroom, chatForm);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
auto removeGroup = QOverload<int>::of(&Widget::removeGroup); auto removeGroup = QOverload<int>::of(&Widget::removeGroup);
#else #else
@ -1313,7 +1314,7 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
connect(groupWidget, &GroupWidget::removeGroup, this, removeGroup); connect(groupWidget, &GroupWidget::removeGroup, this, removeGroup);
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &GroupChatForm::focusInput); connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &GroupChatForm::focusInput);
connect(groupWidget, &GroupWidget::middleMouseClicked, dialog, connect(groupWidget, &GroupWidget::middleMouseClicked, dialog,
[=]() { ContentDialogManager::getInstance()->removeGroup(groupId); }); [=]() { dialog->removeGroup(groupId); });
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &ChatForm::focusInput); connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &ChatForm::focusInput);
connect(groupWidget, &GroupWidget::newWindowOpened, this, &Widget::openNewDialog); connect(groupWidget, &GroupWidget::newWindowOpened, this, &Widget::openNewDialog);