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 friendId = frnd->getId();
auto friendWidget = new FriendWidget(chatroom, compact);
friendWidgets[friendId] = friendWidget;
friendLayout->addFriendWidget(friendWidget, frnd->getStatus());
friendChatForms[friendId] = form;
@ -174,7 +175,8 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
const auto g = chatroom->getGroup();
const auto groupId = g->getId();
const auto compact = Settings::getInstance().getCompactLayout();
GroupWidget* groupWidget = new GroupWidget(chatroom, compact);
auto groupWidget = new GroupWidget(chatroom, compact);
groupWidgets[groupId] = groupWidget;
groupLayout.addSortedWidget(groupWidget);
groupChatForms[groupId] = form;
@ -186,11 +188,9 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
return groupWidget;
}
/**
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeFriend(FriendWidget* chatroomWidget)
void ContentDialog::removeFriend(int friendId)
{
auto chatroomWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this,
&ContentDialog::updateFriendWidget);
@ -211,13 +211,15 @@ void ContentDialog::removeFriend(FriendWidget* chatroomWidget)
} else {
update();
}
friendWidgets.remove(friendId);
friendChatForms.remove(friendId);
closeIfEmpty();
}
/**
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeGroup(GroupWidget* chatroomWidget)
void ContentDialog::removeGroup(int groupId)
{
auto chatroomWidget = qobject_cast<GroupWidget*>(groupWidgets[groupId]);
// Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false);
@ -233,6 +235,17 @@ void ContentDialog::removeGroup(GroupWidget* chatroomWidget)
} else {
update();
}
groupWidgets.remove(groupId);
groupChatForms.remove(groupId);
closeIfEmpty();
}
void ContentDialog::closeIfEmpty()
{
if (friendWidgets.isEmpty() && groupWidgets.isEmpty()) {
close();
}
}
int ContentDialog::chatroomWidgetCount() const
@ -463,10 +476,11 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
int friendId = contact->getId();
// 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();
}
} else if (group) {
qDebug() << event->mimeData()->formats();
if (!event->mimeData()->hasFormat("groupId")) {
return;
}
@ -477,7 +491,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return;
}
if (!ContentDialogManager::getInstance()->hasFriendWidget(this, groupId, group)) {
if (!hasGroupWidget(groupId)) {
event->acceptProposedAction();
}
}
@ -495,8 +509,6 @@ void ContentDialog::dropEvent(QDropEvent* event)
return;
}
int friendId = contact->getId();
ContentDialogManager::getInstance()->removeFriend(friendId);
Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible();
} else if (group) {
@ -510,7 +522,6 @@ void ContentDialog::dropEvent(QDropEvent* event)
return;
}
ContentDialogManager::getInstance()->removeGroup(groupId);
Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible();
}
@ -588,8 +599,7 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
{
Friend* f = FriendList::findFriend(friendId);
// TODO: getFriendWidget from own container
FriendWidget* friendWidget = ContentDialogManager::getInstance()->getFriendWidget(friendId);
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
Status status = f->getStatus();
friendLayout->addFriendWidget(friendWidget, status);
@ -631,22 +641,14 @@ void ContentDialog::saveSplitterState()
Settings::getInstance().setDialogSplitterState(splitter->saveState());
}
/**
* @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
bool ContentDialog::hasFriendWidget(int friendId) const
{
auto iter = list.find(id);
if (iter == list.end()) {
return false;
return friendWidgets.contains(friendId);
}
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);
GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
void removeFriend(FriendWidget* chatroomWidget);
void removeGroup(GroupWidget* chatroomWidget);
void removeFriend(int friendId);
void removeGroup(int groupId);
int chatroomWidgetCount() const;
void ensureSplitterVisible();
void updateTitleAndStatusIcon();
@ -71,9 +71,8 @@ public:
void addFriendWidget(FriendWidget* widget, Status status);
bool isActiveWidget(GenericChatroomWidget* widget);
bool hasWidget(int id, const GenericChatroomWidget* chatroomWidget,
const QHash<int, ContactInfo>& list) const;
bool hasFriendWidget(int friendId) const;
bool hasGroupWidget(int groupId) const;
signals:
void friendDialogShown(const Friend* f);
@ -104,6 +103,7 @@ private slots:
void onGroupchatPositionChanged(bool top);
private:
void closeIfEmpty();
void closeEvent(QCloseEvent* event) override;
void retranslateUi();
@ -121,6 +121,9 @@ private:
QSize videoSurfaceSize;
int videoCount;
QHash<int, GenericChatroomWidget*> friendWidgets;
QHash<int, GenericChatroomWidget*> groupWidgets;
QHash<int, GenericChatForm*> friendChatForms;
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;
@ -48,9 +59,10 @@ FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
ContentDialog* lastDialog = getFriendDialog(friendId);
if (lastDialog) {
lastDialog->removeFriend(friendWidget);
lastDialog->removeFriend(friendId);
}
friendDialogs[friendId] = dialog;
friendList.insert(friendId, std::make_tuple(dialog, friendWidget));
return friendWidget;
}
@ -63,9 +75,10 @@ GroupWidget* ContentDialogManager::addGroupToDialog(ContentDialog* dialog,
ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog) {
lastDialog->removeGroup(groupWidget);
lastDialog->removeGroup(groupId);
}
groupDialogs[groupId] = dialog;
groupList.insert(groupId, std::make_tuple(dialog, groupWidget));
return groupWidget;
}
@ -80,7 +93,7 @@ void ContentDialogManager::removeFriend(int friendId)
auto friendWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
auto dialog = getFriendDialog(friendId);
dialog->removeFriend(friendWidget);
dialog->removeFriend(friendId);
friendList.remove(friendId);
}
@ -93,7 +106,7 @@ void ContentDialogManager::removeGroup(int groupId)
auto groupWidget = static_cast<GroupWidget*>(std::get<1>(iter.value()));
auto dialog = getGroupDialog(groupId);
dialog->removeGroup(groupWidget);
dialog->removeGroup(groupId);
groupList.remove(groupId);
}
@ -163,7 +176,6 @@ void ContentDialogManager::updateFriendStatus(int friendId)
void ContentDialogManager::updateFriendStatusMessage(int friendId, const QString& message)
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return;
}
@ -282,25 +294,7 @@ void ContentDialogManager::onDialogClose()
removeDialog(dialog, friendList);
removeDialog(dialog, groupList);
}
bool ContentDialogManager::hasFriendWidget(ContentDialog* dialog, int friendId, const GenericChatroomWidget* chatroomWidget) const
{
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);
removeDialog(dialog, friendDialogs);
removeDialog(dialog, groupDialogs);
}

View File

@ -53,11 +53,6 @@ public:
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:
void onDialogClose();
void onDialogActivate();
@ -70,6 +65,10 @@ private:
ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list) const;
ContentDialog* currentDialog = nullptr;
QHash<int, ContentDialog*> friendDialogs;
QHash<int, ContentDialog*> groupDialogs;
QHash<int, ContactInfo> friendList;
QHash<int, ContactInfo> groupList;

View File

@ -115,7 +115,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
}
// 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"));
connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow);
}
@ -170,7 +170,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
menu.addSeparator();
// TODO: move to model
if (!contentDialog || !ContentDialogManager::getInstance()->hasFriendWidget(contentDialog, friendId, this)) {
if (!contentDialog || !contentDialog->hasFriendWidget(friendId)) {
const auto removeAction =
menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
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"));
}
if (contentDialog && ContentDialogManager::getInstance()->hasGroupWidget(contentDialog, groupId, this)) {
if (contentDialog && contentDialog->hasGroupWidget(groupId)) {
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)
{
openDialog(widget, false);
openDialog(widget, /* newWindow = */ false);
}
void Widget::openNewDialog(GenericChatroomWidget* widget)
{
openDialog(widget, true);
openDialog(widget, /* newWindow = */ true);
}
void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
@ -1248,7 +1248,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
auto form = chatForms[friendId];
auto chatroom = friendChatrooms[friendId];
FriendWidget* friendWidget = dialog->addFriend(chatroom, form);
FriendWidget* friendWidget = ContentDialogManager::getInstance()->addFriendToDialog(dialog, chatroom, form);
friendWidget->setStatusMsg(widget->getStatusMsg());
@ -1259,7 +1259,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
#endif
connect(friendWidget, &FriendWidget::removeFriend, this, widgetRemoveFriend);
connect(friendWidget, &FriendWidget::middleMouseClicked, dialog,
[=]() { ContentDialogManager::getInstance()->removeFriend(friendId); });
[=]() { dialog->removeFriend(friendId); });
connect(friendWidget, &FriendWidget::copyFriendIdToClipboard, this,
&Widget::copyFriendIdToClipboard);
connect(friendWidget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog);
@ -1304,7 +1304,8 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
auto chatForm = groupChatForms[groupId].data();
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))
auto removeGroup = QOverload<int>::of(&Widget::removeGroup);
#else
@ -1313,7 +1314,7 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
connect(groupWidget, &GroupWidget::removeGroup, this, removeGroup);
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &GroupChatForm::focusInput);
connect(groupWidget, &GroupWidget::middleMouseClicked, dialog,
[=]() { ContentDialogManager::getInstance()->removeGroup(groupId); });
[=]() { dialog->removeGroup(groupId); });
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &ChatForm::focusInput);
connect(groupWidget, &GroupWidget::newWindowOpened, this, &Widget::openNewDialog);