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

refactor(circlewidget): link CircleWidget ContentDialogs using signals

This commit is contained in:
jenli669 2019-06-29 12:53:49 +02:00
parent adab063a49
commit 7fca93bde9
No known key found for this signature in database
GPG Key ID: 8267F9F7C2BF7E5E
6 changed files with 51 additions and 37 deletions

View File

@ -113,8 +113,8 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* event)
circleList.remove(replacedCircle);
} else if (selectedItem == openAction) {
ContentDialog* dialog = Widget::getInstance()->createContentDialog();
ContentDialog* dialog = new ContentDialog();
emit newContentDialog(*dialog);
for (int i = 0; i < friendOnlineLayout()->count(); ++i) {
QWidget* const widget = friendOnlineLayout()->itemAt(i)->widget();
FriendWidget* const friendWidget = qobject_cast<FriendWidget*>(widget);

View File

@ -22,6 +22,8 @@
#include "categorywidget.h"
class ContentDialog;
class CircleWidget final : public CategoryWidget
{
Q_OBJECT
@ -35,6 +37,7 @@ public:
signals:
void renameRequested(CircleWidget* circleWidget, const QString& newName);
void searchCircle(CircleWidget& circletWidget);
void newContentDialog(ContentDialog& contentDialog);
protected:
void contextMenuEvent(QContextMenuEvent* event) final override;

View File

@ -19,17 +19,16 @@
#include "contentdialogmanager.h"
#include "src/widget/friendwidget.h"
#include "src/widget/groupwidget.h"
#include "src/friendlist.h"
#include "src/grouplist.h"
#include "src/model/friend.h"
#include "src/model/group.h"
#include "src/widget/friendwidget.h"
#include "src/widget/groupwidget.h"
#include <tuple>
namespace
{
namespace {
void removeDialog(ContentDialog* dialog, QHash<const ContactId&, ContentDialog*>& dialogs)
{
for (auto it = dialogs.begin(); it != dialogs.end();) {
@ -40,7 +39,7 @@ void removeDialog(ContentDialog* dialog, QHash<const ContactId&, ContentDialog*>
}
}
}
}
} // namespace
ContentDialogManager* ContentDialogManager::instance;
@ -60,7 +59,8 @@ bool ContentDialogManager::contactWidgetExists(const ContactId& contactId)
}
FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form)
std::shared_ptr<FriendChatroom> chatroom,
GenericChatForm* form)
{
auto friendWidget = dialog->addFriend(chatroom, form);
const auto friendPk = friendWidget->getFriend()->getPublicKey();
@ -75,7 +75,8 @@ FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
}
GroupWidget* ContentDialogManager::addGroupToDialog(ContentDialog* dialog,
std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form)
std::shared_ptr<GroupChatroom> chatroom,
GenericChatForm* form)
{
auto groupWidget = dialog->addGroup(chatroom, form);
const auto& groupId = groupWidget->getGroup()->getPersistentId();
@ -103,7 +104,8 @@ void ContentDialogManager::focusContact(const ContactId& contactId)
* @param list List with dialogs
* @return ContentDialog if found, nullptr otherwise
*/
ContentDialog* ContentDialogManager::focusDialog(const ContactId& id, const QHash<const ContactId&, ContentDialog*>& list)
ContentDialog* ContentDialogManager::focusDialog(const ContactId& id,
const QHash<const ContactId&, ContentDialog*>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
@ -178,11 +180,11 @@ ContentDialogManager* ContentDialogManager::getInstance()
return instance;
}
void ContentDialogManager::addContentDialog(ContentDialog* dialog)
void ContentDialogManager::addContentDialog(ContentDialog& dialog)
{
currentDialog = dialog;
connect(dialog, &ContentDialog::willClose, this, &ContentDialogManager::onDialogClose);
connect(dialog, &ContentDialog::activated, this, &ContentDialogManager::onDialogActivate);
currentDialog = &dialog;
connect(&dialog, &ContentDialog::willClose, this, &ContentDialogManager::onDialogClose);
connect(&dialog, &ContentDialog::activated, this, &ContentDialogManager::onDialogActivate);
}
void ContentDialogManager::onDialogActivate()
@ -203,10 +205,10 @@ void ContentDialogManager::onDialogClose()
IDialogs* ContentDialogManager::getFriendDialogs(const ToxPk& friendPk) const
{
return getFriendDialog(friendPk);
return getFriendDialog(friendPk);
}
IDialogs* ContentDialogManager::getGroupDialogs(const GroupId& groupId) const
{
return getGroupDialog(groupId);
return getGroupDialog(groupId);
}

View File

@ -20,11 +20,11 @@
#ifndef _CONTENT_DIALOG_MANAGER_H_
#define _CONTENT_DIALOG_MANAGER_H_
#include "src/core/contactid.h"
#include "src/core/toxpk.h"
#include "src/core/groupid.h"
#include "src/model/dialogs/idialogsmanager.h"
#include "contentdialog.h"
#include "src/core/contactid.h"
#include "src/core/groupid.h"
#include "src/core/toxpk.h"
#include "src/model/dialogs/idialogsmanager.h"
#include <QObject>
@ -47,10 +47,12 @@ public:
IDialogs* getFriendDialogs(const ToxPk& friendPk) const;
IDialogs* getGroupDialogs(const GroupId& groupId) const;
FriendWidget* addFriendToDialog(ContentDialog* dialog, std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroupToDialog(ContentDialog* dialog, std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
FriendWidget* addFriendToDialog(ContentDialog* dialog, std::shared_ptr<FriendChatroom> chatroom,
GenericChatForm* form);
GroupWidget* addGroupToDialog(ContentDialog* dialog, std::shared_ptr<GroupChatroom> chatroom,
GenericChatForm* form);
void addContentDialog(ContentDialog* dialog);
void addContentDialog(ContentDialog& dialog);
static ContentDialogManager* getInstance();
@ -59,7 +61,8 @@ private slots:
void onDialogActivate();
private:
ContentDialog* focusDialog(const ContactId& id, const QHash<const ContactId&, ContentDialog*>& list);
ContentDialog* focusDialog(const ContactId& id,
const QHash<const ContactId&, ContentDialog*>& list);
ContentDialog* currentDialog = nullptr;

View File

@ -1818,26 +1818,30 @@ void Widget::onUpdateAvailable(QString /*latestVersion*/, QUrl /*link*/)
ContentDialog* Widget::createContentDialog() const
{
ContentDialog* contentDialog = new ContentDialog();
ContentDialogManager::getInstance()->addContentDialog(contentDialog);
connect(contentDialog, &ContentDialog::friendDialogShown, this, &Widget::onFriendDialogShown);
connect(contentDialog, &ContentDialog::groupDialogShown, this, &Widget::onGroupDialogShown);
connect(core, &Core::usernameSet, contentDialog, &ContentDialog::setUsername);
connect(&settings, &Settings::groupchatPositionChanged, contentDialog,
registerContentDialog(*contentDialog);
return contentDialog;
}
void Widget::registerContentDialog(ContentDialog& contentDialog) const
{
ContentDialogManager::getInstance()->addContentDialog(contentDialog);
connect(&contentDialog, &ContentDialog::friendDialogShown, this, &Widget::onFriendDialogShown);
connect(&contentDialog, &ContentDialog::groupDialogShown, this, &Widget::onGroupDialogShown);
connect(core, &Core::usernameSet, &contentDialog, &ContentDialog::setUsername);
connect(&settings, &Settings::groupchatPositionChanged, &contentDialog,
&ContentDialog::reorderLayouts);
connect(contentDialog, &ContentDialog::addFriendDialog, this, &Widget::addFriendDialog);
connect(contentDialog, &ContentDialog::addGroupDialog, this, &Widget::addGroupDialog);
connect(contentDialog, &ContentDialog::connectFriendWidget, this, &Widget::connectFriendWidget);
connect(&contentDialog, &ContentDialog::addFriendDialog, this, &Widget::addFriendDialog);
connect(&contentDialog, &ContentDialog::addGroupDialog, this, &Widget::addGroupDialog);
connect(&contentDialog, &ContentDialog::connectFriendWidget, this, &Widget::connectFriendWidget);
#ifdef Q_OS_MAC
Nexus& n = Nexus::getInstance();
connect(contentDialog, &ContentDialog::destroyed, &n, &Nexus::updateWindowsClosed);
connect(contentDialog, &ContentDialog::windowStateChanged, &n, &Nexus::onWindowStateChanged);
connect(contentDialog->windowHandle(), &QWindow::windowTitleChanged, &n, &Nexus::updateWindows);
connect(&contentDialog, &ContentDialog::destroyed, &n, &Nexus::updateWindowsClosed);
connect(&contentDialog, &ContentDialog::windowStateChanged, &n, &Nexus::onWindowStateChanged);
connect(contentDialog.windowHandle(), &QWindow::windowTitleChanged, &n, &Nexus::updateWindows);
n.updateWindows();
#endif
return contentDialog;
}
ContentLayout* Widget::createContentDialog(DialogType type) const
@ -2702,6 +2706,7 @@ void Widget::refreshPeerListsLocal(const QString& username)
void Widget::connectCircleWidget(CircleWidget& circleWidget)
{
connect(&circleWidget, &CircleWidget::searchCircle, this, &Widget::searchCircle);
connect(&circleWidget, &CircleWidget::newContentDialog, this, &Widget::registerContentDialog);
}
void Widget::connectFriendWidget(FriendWidget& friendWidget)

View File

@ -243,6 +243,7 @@ private slots:
void connectFriendWidget(FriendWidget& friendWidget);
void searchCircle(CircleWidget& circleWidget);
void updateFriendActivity(const Friend& frnd);
void registerContentDialog(ContentDialog& contentDialog) const;
private:
// QMainWindow overrides