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

refactor: Create ContentDialogManager

Move all static methods from ContentDialog to ContentDialogManger. Make
if singletone for the first time. Also move some methods to avoid
compile errors.
This commit is contained in:
Diadlo 2018-12-24 21:36:36 +03:00
parent a446d9b197
commit 7ad8607c43
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
9 changed files with 485 additions and 318 deletions

View File

@ -371,6 +371,8 @@ set(${PROJECT_NAME}_SOURCES
src/widget/circlewidget.h
src/widget/contentdialog.cpp
src/widget/contentdialog.h
src/widget/contentdialogmanager.cpp
src/widget/contentdialogmanager.h
src/widget/contentlayout.cpp
src/widget/contentlayout.h
src/widget/emoticonswidget.cpp

View File

@ -21,6 +21,7 @@
#include "splitterrestorer.h"
#include <QBoxLayout>
#include <QCloseEvent>
#include <QDragEnterEvent>
#include <QGuiApplication>
#include <QMimeData>
@ -44,10 +45,9 @@
#include "src/widget/translator.h"
#include "src/widget/widget.h"
QString ContentDialog::username = "";
ContentDialog* ContentDialog::currentDialog = nullptr;
QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::friendList;
QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::groupList;
// TODO: Remove. Never do this
// HACK: To check, if dialog contains widget
#include "src/widget/contentdialogmanager.h"
static const int minWidget = 220;
static const int minHeight = 220;
@ -125,7 +125,6 @@ ContentDialog::ContentDialog(QWidget* parent)
username = Core::getInstance()->getUsername();
currentDialog = this;
setAcceptDrops(true);
new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
@ -140,29 +139,17 @@ ContentDialog::ContentDialog(QWidget* parent)
Translator::registerHandler(std::bind(&ContentDialog::retranslateUi, this), this);
}
void ContentDialog::removeCurrent(QHash<int, ContactInfo>& infos)
{
for (auto it = infos.begin(); it != infos.end();) {
if (std::get<0>(*it) == this) {
it = infos.erase(it);
} else {
++it;
}
}
}
ContentDialog::~ContentDialog()
{
if (currentDialog == this) {
currentDialog = nullptr;
}
removeCurrent(friendList);
removeCurrent(groupList);
Translator::unregister(this);
}
void ContentDialog::closeEvent(QCloseEvent *event)
{
emit willClose();
event->accept();
}
FriendWidget* ContentDialog::addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form)
{
const auto compact = Settings::getInstance().getCompactLayout();
@ -176,12 +163,6 @@ FriendWidget* ContentDialog::addFriend(std::shared_ptr<FriendChatroom> chatroom,
connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget);
connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this, &ContentDialog::activate);
ContentDialog* lastDialog = getFriendDialog(friendId);
if (lastDialog) {
lastDialog->removeFriend(friendId);
}
friendList.insert(friendId, std::make_tuple(this, friendWidget));
// FIXME: emit should be removed
emit friendWidget->chatroomWidgetClicked(friendWidget);
@ -199,40 +180,29 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, this, &ContentDialog::activate);
ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog) {
lastDialog->removeGroup(groupId);
}
groupList.insert(groupId, std::make_tuple(this, groupWidget));
// FIXME: emit should be removed
emit groupWidget->chatroomWidgetClicked(groupWidget);
return groupWidget;
}
void ContentDialog::removeFriend(int friendId)
/**
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeFriend(FriendWidget* chatroomWidget)
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return;
}
FriendWidget* chatroomWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this,
&ContentDialog::updateFriendWidget);
// Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false);
cycleContacts(/* forward = */ true, /* inverse = */ false);
}
friendLayout->removeFriendWidget(chatroomWidget, Status::Offline);
friendLayout->removeFriendWidget(chatroomWidget, Status::Online);
chatroomWidget->deleteLater();
friendList.remove(friendId);
if (chatroomWidgetCount() == 0) {
contentLayout->clear();
@ -243,15 +213,11 @@ void ContentDialog::removeFriend(int friendId)
}
}
void ContentDialog::removeGroup(int groupId)
/**
* TODO: Pass id, store widgets in ContentDialog
*/
void ContentDialog::removeGroup(GroupWidget* chatroomWidget)
{
auto iter = groupList.find(groupId);
if (iter == groupList.end()) {
return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(iter.value());
// Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false);
@ -259,7 +225,6 @@ void ContentDialog::removeGroup(int groupId)
groupLayout.removeSortedWidget(chatroomWidget);
chatroomWidget->deleteLater();
groupList.remove(groupId);
if (chatroomWidgetCount() == 0) {
contentLayout->clear();
@ -270,16 +235,6 @@ void ContentDialog::removeGroup(int groupId)
}
}
bool ContentDialog::hasFriendWidget(int friendId, const GenericChatroomWidget* chatroomWidget) const
{
return hasWidget(friendId, chatroomWidget, friendList);
}
bool ContentDialog::hasGroupWidget(int groupId, const GenericChatroomWidget* chatroomWidget) const
{
return hasWidget(groupId, chatroomWidget, groupList);
}
int ContentDialog::chatroomWidgetCount() const
{
return friendLayout->friendTotalCount() + groupLayout.getLayout()->count();
@ -401,85 +356,6 @@ void ContentDialog::onVideoHide()
videoSurfaceSize = QSize();
}
ContentDialog* ContentDialog::current()
{
return currentDialog;
}
bool ContentDialog::friendWidgetExists(int friendId)
{
return existsWidget(friendId, friendList);
}
bool ContentDialog::groupWidgetExists(int groupId)
{
return existsWidget(groupId, groupList);
}
void ContentDialog::focusFriend(int friendId)
{
focusDialog(friendId, friendList);
}
void ContentDialog::focusGroup(int groupId)
{
focusDialog(groupId, groupList);
}
void ContentDialog::updateFriendStatus(int friendId)
{
updateStatus(friendId, friendList);
ContentDialog* contentDialog = getFriendDialog(friendId);
if (contentDialog) {
auto iter = friendList.find(friendId).value();
GenericChatroomWidget* widget = std::get<1>(iter);
FriendWidget* friendWidget = static_cast<FriendWidget*>(widget);
Friend* f = FriendList::findFriend(friendId);
contentDialog->friendLayout->addFriendWidget(friendWidget, f->getStatus());
}
}
/**
* @brief Update friend status message.
* @param friendId Id friend, whose status was changed.
* @param message Status message.
*/
void ContentDialog::updateFriendStatusMessage(int friendId, const QString& message)
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return;
}
std::get<1>(iter.value())->setStatusMsg(message);
}
void ContentDialog::updateGroupStatus(int groupId)
{
updateStatus(groupId, groupList);
}
bool ContentDialog::isFriendWidgetActive(int friendId)
{
return isWidgetActive(friendId, friendList);
}
bool ContentDialog::isGroupWidgetActive(int groupId)
{
return isWidgetActive(groupId, groupList);
}
ContentDialog* ContentDialog::getFriendDialog(int friendId)
{
return getDialog(friendId, friendList);
}
ContentDialog* ContentDialog::getGroupDialog(int groupId)
{
return getDialog(groupId, groupList);
}
/**
* @brief Update window title and icon.
@ -564,11 +440,7 @@ bool ContentDialog::event(QEvent* event)
}
}
currentDialog = this;
#ifdef Q_OS_MAC
emit activated();
#endif
default:
break;
}
@ -589,10 +461,9 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
}
int friendId = contact->getId();
auto iter = friendList.find(friendId);
// If friend is already in a dialog then you can't drop friend where it already is.
if (iter == friendList.end() || std::get<0>(iter.value()) != this) {
if (!ContentDialogManager::getInstance()->hasFriendWidget(this, friendId, frnd)) {
event->acceptProposedAction();
}
} else if (group) {
@ -606,8 +477,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return;
}
auto iter = groupList.find(groupId);
if (iter == groupList.end() || std::get<0>(iter.value()) != this) {
if (!ContentDialogManager::getInstance()->hasFriendWidget(this, groupId, group)) {
event->acceptProposedAction();
}
}
@ -626,11 +496,7 @@ void ContentDialog::dropEvent(QDropEvent* event)
}
int friendId = contact->getId();
auto iter = friendList.find(friendId);
if (iter != friendList.end()) {
std::get<0>(iter.value())->removeFriend(friendId);
}
ContentDialogManager::getInstance()->removeFriend(friendId);
Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible();
} else if (group) {
@ -644,11 +510,7 @@ void ContentDialog::dropEvent(QDropEvent* event)
return;
}
auto iter = friendList.find(groupId);
if (iter != friendList.end()) {
std::get<0>(iter.value())->removeGroup(groupId);
}
ContentDialogManager::getInstance()->removeGroup(groupId);
Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible();
}
@ -659,7 +521,7 @@ void ContentDialog::changeEvent(QEvent* event)
QWidget::changeEvent(event);
if (event->type() == QEvent::ActivationChange) {
if (isActiveWindow()) {
currentDialog = this;
emit activated();
}
}
}
@ -726,8 +588,8 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
{
Friend* f = FriendList::findFriend(friendId);
GenericChatroomWidget* widget = std::get<1>(friendList.find(friendId).value());
FriendWidget* friendWidget = static_cast<FriendWidget*>(widget);
// TODO: getFriendWidget from own container
FriendWidget* friendWidget = ContentDialogManager::getInstance()->getFriendWidget(friendId);
Status status = f->getStatus();
friendLayout->addFriendWidget(friendWidget, status);
@ -787,93 +649,6 @@ bool ContentDialog::hasWidget(int id, const GenericChatroomWidget* chatroomWidge
return std::get<0>(*iter) == this && std::get<1>(*iter) == chatroomWidget;
}
/**
* @brief Focus the dialog if it exists.
* @param id User Id.
* @param list List with contact info.
*/
void ContentDialog::focusDialog(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return;
}
ContentDialog* dialog = std::get<0>(*iter);
if (dialog->windowState() & Qt::WindowMinimized) {
dialog->showNormal();
}
dialog->raise();
dialog->activateWindow();
dialog->activate(std::get<1>(iter.value()));
}
/**
* @brief Check, if widget is exists.
* @param id User Id.
* @param list List with contact info.
* @return True is widget exists, false otherwise.
*/
bool ContentDialog::existsWidget(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
return iter != list.end();
}
/**
* @brief Update widget status and dialog title for current user.
* @param id User Id.
* @param list List with contact info.
*/
void ContentDialog::updateStatus(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(*iter);
chatroomWidget->updateStatusLight();
if (chatroomWidget->isActive()) {
ContentDialog* dialog = std::get<0>(*iter);
dialog->updateTitleAndStatusIcon();
}
}
/**
* @brief Check, if user dialog is active.
* @param id User Id.
* @param list List with contact info.
* @return True if user dialog is active, false otherwise.
*/
bool ContentDialog::isWidgetActive(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return false;
}
return std::get<0>(iter.value())->activeChatroomWidget == std::get<1>(iter.value());
}
/**
* @brief Select ContentDialog by id from the list.
* @param id User Id.
* @param list List with contact info.
* @return ContentDialog for user and nullptr if not found.
*/
ContentDialog* ContentDialog::getDialog(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return nullptr;
}
return std::get<0>(iter.value());
}
/**
* @brief Find the next or previous layout in layout list.
* @param layout Current layout.
@ -893,3 +668,13 @@ QLayout* ContentDialog::nextLayout(QLayout* layout, bool forward) const
return layouts[next];
}
void ContentDialog::addFriendWidget(FriendWidget* widget, Status status)
{
friendLayout->addFriendWidget(widget, status);
}
bool ContentDialog::isActiveWidget(GenericChatroomWidget* widget)
{
return activeChatroomWidget == widget;
}

View File

@ -22,6 +22,7 @@
#include "src/widget/genericchatitemlayout.h"
#include "src/widget/tool/activatedialog.h"
#include "src/core/core.h" // Status
#include <memory>
#include <tuple>
@ -42,6 +43,7 @@ class GenericChatroomWidget;
class Group;
class GroupChatroom;
class GroupWidget;
class QCloseEvent;
class QSplitter;
class QVBoxLayout;
@ -56,10 +58,8 @@ public:
FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
void removeFriend(int friendId);
void removeGroup(int groupId);
bool hasFriendWidget(int friendId, const GenericChatroomWidget* chatroomWidget) const;
bool hasGroupWidget(int groupId, const GenericChatroomWidget* chatroomWidget) const;
void removeFriend(FriendWidget* chatroomWidget);
void removeGroup(GroupWidget* chatroomWidget);
int chatroomWidgetCount() const;
void ensureSplitterVisible();
void updateTitleAndStatusIcon();
@ -68,23 +68,18 @@ public:
void onVideoShow(QSize size);
void onVideoHide();
static ContentDialog* current();
static bool friendWidgetExists(int friendId);
static bool groupWidgetExists(int groupId);
static void focusFriend(int friendId);
static void focusGroup(int groupId);
static void updateFriendStatus(int friendId);
static void updateFriendStatusMessage(int friendId, const QString& message);
static void updateGroupStatus(int groupId);
static bool isFriendWidgetActive(int friendId);
static bool isGroupWidgetActive(int groupId);
static ContentDialog* getFriendDialog(int friendId);
static ContentDialog* getGroupDialog(int groupId);
void addFriendWidget(FriendWidget* widget, Status status);
bool isActiveWidget(GenericChatroomWidget* widget);
bool hasWidget(int id, const GenericChatroomWidget* chatroomWidget,
const QHash<int, ContactInfo>& list) const;
signals:
void friendDialogShown(const Friend* f);
void groupDialogShown(Group* g);
void activated();
void willClose();
public slots:
void reorderLayouts(bool newGroupOnTop);
@ -101,26 +96,22 @@ protected:
void moveEvent(QMoveEvent* event) override;
void keyPressEvent(QKeyEvent* event) override;
public slots:
void activate(GenericChatroomWidget* widget);
private slots:
void updateFriendWidget(uint32_t friendId, QString alias);
void onGroupchatPositionChanged(bool top);
private:
void closeEvent(QCloseEvent* event) override;
void retranslateUi();
void saveDialogGeometry();
void saveSplitterState();
QLayout* nextLayout(QLayout* layout, bool forward) const;
int getCurrentLayout(QLayout*& layout);
bool hasWidget(int id, const GenericChatroomWidget* chatroomWidget,
const QHash<int, ContactInfo>& list) const;
void removeCurrent(QHash<int, ContactInfo>& infos);
static bool existsWidget(int id, const QHash<int, ContactInfo>& list);
static void focusDialog(int id, const QHash<int, ContactInfo>& list);
static void updateStatus(int id, const QHash<int, ContactInfo>& list);
static bool isWidgetActive(int id, const QHash<int, ContactInfo>& list);
static ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list);
QList<QLayout*> layouts;
QSplitter* splitter;
FriendListLayout* friendLayout;
@ -130,12 +121,10 @@ private:
QSize videoSurfaceSize;
int videoCount;
static QString username;
static ContentDialog* currentDialog;
static QHash<int, ContactInfo> friendList;
static QHash<int, ContactInfo> groupList;
QHash<int, GenericChatForm*> friendChatForms;
QHash<int, GenericChatForm*> groupChatForms;
QString username;
};
#endif // CONTENTDIALOG_H

View File

@ -0,0 +1,306 @@
#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 <tuple>
namespace
{
void removeDialog(ContentDialog* dialog, QHash<int, ContactInfo>& infos)
{
for (auto it = infos.begin(); it != infos.end();) {
if (std::get<0>(*it) == dialog) {
it = infos.erase(it);
} else {
++it;
}
}
}
}
ContentDialogManager* ContentDialogManager::instance;
ContentDialog* ContentDialogManager::current()
{
return currentDialog;
}
bool ContentDialogManager::friendWidgetExists(int friendId)
{
return existsWidget(friendId, friendList);
}
bool ContentDialogManager::groupWidgetExists(int groupId)
{
return existsWidget(groupId, groupList);
}
FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form)
{
auto friendWidget = dialog->addFriend(chatroom, form);
auto friendId = friendWidget->getFriend()->getId();
ContentDialog* lastDialog = getFriendDialog(friendId);
if (lastDialog) {
lastDialog->removeFriend(friendWidget);
}
friendList.insert(friendId, std::make_tuple(dialog, friendWidget));
return friendWidget;
}
GroupWidget* ContentDialogManager::addGroupToDialog(ContentDialog* dialog,
std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form)
{
auto groupWidget = dialog->addGroup(chatroom, form);
auto groupId = groupWidget->getGroup()->getId();
ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog) {
lastDialog->removeGroup(groupWidget);
}
groupList.insert(groupId, std::make_tuple(dialog, groupWidget));
return groupWidget;
}
// TODO: Remove method. Move logic in ContentDialog
void ContentDialogManager::removeFriend(int friendId)
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return;
}
auto friendWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
auto dialog = getFriendDialog(friendId);
dialog->removeFriend(friendWidget);
friendList.remove(friendId);
}
void ContentDialogManager::removeGroup(int groupId)
{
auto iter = friendList.find(groupId);
if (iter == friendList.end()) {
return;
}
auto groupWidget = static_cast<GroupWidget*>(std::get<1>(iter.value()));
auto dialog = getGroupDialog(groupId);
dialog->removeGroup(groupWidget);
groupList.remove(groupId);
}
/**
* @brief Check, if widget is exists.
* @param id User Id.
* @param list List with contact info.
* @return True is widget exists, false otherwise.
*/
bool ContentDialogManager::existsWidget(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
return iter != list.end();
}
void ContentDialogManager::focusFriend(int friendId)
{
focusDialog(friendId, friendList);
}
void ContentDialogManager::focusGroup(int groupId)
{
focusDialog(groupId, groupList);
}
/**
* @brief Focus the dialog if it exists.
* @param id User Id.
* @param list List with contact info.
*/
void ContentDialogManager::focusDialog(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return;
}
ContentDialog* dialog = std::get<0>(*iter);
if (dialog->windowState() & Qt::WindowMinimized) {
dialog->showNormal();
}
dialog->raise();
dialog->activateWindow();
dialog->activate(std::get<1>(iter.value()));
}
void ContentDialogManager::updateFriendStatus(int friendId)
{
updateStatus(friendId, friendList);
ContentDialog* contentDialog = getFriendDialog(friendId);
if (contentDialog) {
auto iter = friendList.find(friendId).value();
GenericChatroomWidget* widget = std::get<1>(iter);
FriendWidget* friendWidget = static_cast<FriendWidget*>(widget);
Friend* f = FriendList::findFriend(friendId);
contentDialog->addFriendWidget(friendWidget, f->getStatus());
}
}
/**
* @brief Update friend status message.
* @param friendId Id friend, whose status was changed.
* @param message Status message.
*/
void ContentDialogManager::updateFriendStatusMessage(int friendId, const QString& message)
{
auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
return;
}
std::get<1>(iter.value())->setStatusMsg(message);
}
void ContentDialogManager::updateGroupStatus(int groupId)
{
updateStatus(groupId, groupList);
}
bool ContentDialogManager::isFriendWidgetActive(int friendId)
{
return isWidgetActive(friendId, friendList);
}
bool ContentDialogManager::isGroupWidgetActive(int groupId)
{
return isWidgetActive(groupId, groupList);
}
/**
* @brief Check, if user dialog is active.
* @param id User Id.
* @param list List with contact info.
* @return True if user dialog is active, false otherwise.
*/
bool ContentDialogManager::isWidgetActive(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return false;
}
const auto dialog = std::get<0>(iter.value());
const auto widget = std::get<1>(iter.value());
return dialog->isActiveWidget(widget);
}
ContentDialog* ContentDialogManager::getFriendDialog(int friendId) const
{
return getDialog(friendId, friendList);
}
ContentDialog* ContentDialogManager::getGroupDialog(int groupId) const
{
return getDialog(groupId, groupList);
}
/**
* @brief Update widget status and dialog title for current user.
* @param id User Id.
* @param list List with contact info.
*/
void ContentDialogManager::updateStatus(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(*iter);
chatroomWidget->updateStatusLight();
if (chatroomWidget->isActive()) {
ContentDialog* dialog = std::get<0>(*iter);
dialog->updateTitleAndStatusIcon();
}
}
/**
* @brief Select ContentDialog by id from the list.
* @param id User Id.
* @param list List with contact info.
* @return ContentDialog for user and nullptr if not found.
*/
ContentDialog* ContentDialogManager::getDialog(int id, const QHash<int, ContactInfo>& list) const
{
auto iter = list.find(id);
if (iter == list.end()) {
return nullptr;
}
return std::get<0>(iter.value());
}
ContentDialogManager* ContentDialogManager::getInstance()
{
if (instance == nullptr) {
instance = new ContentDialogManager();
}
return instance;
}
void ContentDialogManager::addContentDialog(ContentDialog* dialog)
{
currentDialog = dialog;
connect(dialog, &ContentDialog::willClose, this, &ContentDialogManager::onDialogClose);
connect(dialog, &ContentDialog::activated, this, &ContentDialogManager::onDialogActivate);
}
void ContentDialogManager::onDialogActivate()
{
ContentDialog* dialog = qobject_cast<ContentDialog*>(sender());
currentDialog = dialog;
}
void ContentDialogManager::onDialogClose()
{
ContentDialog* dialog = qobject_cast<ContentDialog*>(sender());
if (currentDialog == dialog) {
currentDialog = nullptr;
}
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);
}

View File

@ -0,0 +1,79 @@
/*
Copyright © 2018 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _CONTENT_DIALOG_MANAGER_H_
#define _CONTENT_DIALOG_MANAGER_H_
#include <QObject>
#include "contentdialog.h"
/**
* @breaf Manage all content dialogs
*/
class ContentDialogManager : public QObject
{
Q_OBJECT
public:
ContentDialog* current();
bool friendWidgetExists(int friendId);
bool groupWidgetExists(int groupId);
void focusFriend(int friendId);
void focusGroup(int groupId);
void updateFriendStatus(int friendId);
void updateFriendStatusMessage(int friendId, const QString& message);
void updateGroupStatus(int groupId);
bool isFriendWidgetActive(int friendId);
bool isGroupWidgetActive(int groupId);
ContentDialog* getFriendDialog(int friendId) const;
ContentDialog* getGroupDialog(int groupId) const;
void removeFriend(int friendId);
void removeGroup(int groupId);
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);
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();
private:
bool existsWidget(int id, const QHash<int, ContactInfo>& list);
void focusDialog(int id, const QHash<int, ContactInfo>& list);
void updateStatus(int id, const QHash<int, ContactInfo>& list);
bool isWidgetActive(int id, const QHash<int, ContactInfo>& list);
ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list) const;
ContentDialog* currentDialog = nullptr;
QHash<int, ContactInfo> friendList;
QHash<int, ContactInfo> groupList;
static ContentDialogManager* instance;
};
#endif // _CONTENT_DIALOG_MANAGER_H_

View File

@ -31,6 +31,7 @@
#include "src/video/genericnetcamview.h"
#include "src/widget/chatformheader.h"
#include "src/widget/contentdialog.h"
#include "src/widget/contentdialogmanager.h"
#include "src/widget/contentlayout.h"
#include "src/widget/emoticonswidget.h"
#include "src/widget/maskablepixmapwidget.h"
@ -1014,7 +1015,7 @@ void GenericChatForm::showNetcam()
bodySplitter->setCollapsible(0, false);
QSize minSize = netcam->getSurfaceMinSize();
ContentDialog* current = ContentDialog::current();
ContentDialog* current = ContentDialogManager::getInstance()->current();
if (current)
current->onVideoShow(minSize);
}
@ -1024,7 +1025,7 @@ void GenericChatForm::hideNetcam()
if (!netcam)
return;
ContentDialog* current = ContentDialog::current();
ContentDialog* current = ContentDialogManager::getInstance()->current();
if (current)
current->onVideoHide();

View File

@ -31,6 +31,7 @@
#include "src/model/group.h"
#include "src/persistence/settings.h"
#include "src/widget/about/aboutfriendform.h"
#include "src/widget/contentdialogmanager.h"
#include "src/widget/form/chatform.h"
#include "src/widget/style.h"
#include "src/widget/tool/croppinglabel.h"
@ -105,7 +106,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
const auto frnd = chatroom->getFriend();
const auto friendId = frnd->getId();
const auto contentDialog = ContentDialog::getFriendDialog(friendId);
const auto contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
// TODO: move to model
if (!contentDialog || contentDialog->chatroomWidgetCount() > 1) {
@ -114,7 +115,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
}
// TODO: move to model
if (contentDialog && contentDialog->hasFriendWidget(friendId, this)) {
if (contentDialog && ContentDialogManager::getInstance()->hasFriendWidget(contentDialog, friendId, this)) {
const auto removeChatWindow = menu.addAction(tr("Remove chat from this window"));
connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow);
}
@ -169,7 +170,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
menu.addSeparator();
// TODO: move to model
if (!contentDialog || !contentDialog->hasFriendWidget(friendId, this)) {
if (!contentDialog || !ContentDialogManager::getInstance()->hasFriendWidget(contentDialog, friendId, this)) {
const auto removeAction =
menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendId); },
@ -194,8 +195,8 @@ void FriendWidget::removeChatWindow()
{
const auto frnd = chatroom->getFriend();
const auto friendId = frnd->getId();
ContentDialog* contentDialog = ContentDialog::getFriendDialog(friendId);
contentDialog->removeFriend(friendId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
ContentDialogManager::getInstance()->removeFriend(friendId);
}
namespace {

View File

@ -35,6 +35,7 @@
#include "src/friendlist.h"
#include "src/model/group.h"
#include "src/grouplist.h"
#include "src/widget/contentdialogmanager.h"
#include "src/widget/friendwidget.h"
#include "src/widget/style.h"
#include "src/widget/translator.h"
@ -87,14 +88,14 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
QAction* removeChatWindow = nullptr;
// TODO: Move to model
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
const bool notAlone = contentDialog != nullptr && contentDialog->chatroomWidgetCount() > 1;
if (contentDialog == nullptr || notAlone) {
openChatWindow = menu.addAction(tr("Open chat in new window"));
}
if (contentDialog && contentDialog->hasGroupWidget(groupId, this)) {
if (contentDialog && ContentDialogManager::getInstance()->hasGroupWidget(contentDialog, groupId, this)) {
removeChatWindow = menu.addAction(tr("Remove chat from this window"));
}
@ -121,8 +122,8 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
emit newWindowOpened(this);
} else if (selectedItem == removeChatWindow) {
// TODO: move to model
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
contentDialog->removeGroup(groupId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
ContentDialogManager::getInstance()->removeGroup(groupId);
} else if (selectedItem == setTitle) {
editName();
}

View File

@ -66,6 +66,7 @@
#include "src/persistence/profile.h"
#include "src/persistence/settings.h"
#include "src/platform/timer.h"
#include "src/widget/contentdialogmanager.h"
#include "src/widget/form/addfriendform.h"
#include "src/widget/form/chatform.h"
#include "src/widget/form/filesform.h"
@ -310,8 +311,8 @@ void Widget::init()
Nexus::getInstance().windowMenu->insertAction(frontAction, nextConversationAction);
nextConversationAction->setShortcut(QKeySequence::SelectNextPage);
connect(nextConversationAction, &QAction::triggered, [this]() {
if (ContentDialog::current() == QApplication::activeWindow())
ContentDialog::current()->cycleContacts(true);
if (ContentDialogManager::getInstance()->current() == QApplication::activeWindow())
ContentDialogManager::getInstance()->current()->cycleContacts(true);
else if (QApplication::activeWindow() == this)
cycleContacts(true);
});
@ -320,8 +321,8 @@ void Widget::init()
Nexus::getInstance().windowMenu->insertAction(frontAction, previousConversationAction);
previousConversationAction->setShortcut(QKeySequence::SelectPreviousPage);
connect(previousConversationAction, &QAction::triggered, [this] {
if (ContentDialog::current() == QApplication::activeWindow())
ContentDialog::current()->cycleContacts(false);
if (ContentDialogManager::getInstance()->current() == QApplication::activeWindow())
ContentDialogManager::getInstance()->current()->cycleContacts(false);
else if (QApplication::activeWindow() == this)
cycleContacts(false);
});
@ -1068,7 +1069,7 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
setWindowTitle(widget->getTitle());
}
ContentDialog::updateFriendStatus(friendId);
ContentDialogManager::getInstance()->updateFriendStatus(friendId);
}
void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
@ -1085,7 +1086,7 @@ void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
friendWidgets[friendId]->setStatusMsg(str);
chatForms[friendId]->setStatusMessage(str);
ContentDialog::updateFriendStatusMessage(friendId, message);
ContentDialogManager::getInstance()->updateFriendStatusMessage(friendId, message);
}
void Widget::onFriendDisplayedNameChanged(const QString& displayed)
@ -1157,11 +1158,11 @@ void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
bool chatFormIsSet;
if (frnd) {
ContentDialog::focusFriend(id);
chatFormIsSet = ContentDialog::friendWidgetExists(id);
ContentDialogManager::getInstance()->focusFriend(id);
chatFormIsSet = ContentDialogManager::getInstance()->friendWidgetExists(id);
} else {
ContentDialog::focusGroup(id);
chatFormIsSet = ContentDialog::groupWidgetExists(id);
ContentDialogManager::getInstance()->focusGroup(id);
chatFormIsSet = ContentDialogManager::getInstance()->groupWidgetExists(id);
}
if ((chatFormIsSet || form->isVisible()) && !newWindow) {
@ -1172,7 +1173,7 @@ void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
ContentDialog* dialog = nullptr;
if (!Settings::getInstance().getDontGroupWindows() && !newWindow) {
dialog = ContentDialog::current();
dialog = ContentDialogManager::getInstance()->current();
}
if (dialog == nullptr) {
@ -1237,7 +1238,7 @@ void Widget::onReceiptRecieved(int friendId, int receipt)
void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
{
uint32_t friendId = frnd->getId();
ContentDialog* contentDialog = ContentDialog::getFriendDialog(friendId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
bool isSeparate = Settings::getInstance().getSeparateWindow();
FriendWidget* widget = friendWidgets[friendId];
bool isCurrent = activeChatroomWidget == widget;
@ -1258,7 +1259,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
#endif
connect(friendWidget, &FriendWidget::removeFriend, this, widgetRemoveFriend);
connect(friendWidget, &FriendWidget::middleMouseClicked, dialog,
[=]() { dialog->removeFriend(friendId); });
[=]() { ContentDialogManager::getInstance()->removeFriend(friendId); });
connect(friendWidget, &FriendWidget::copyFriendIdToClipboard, this,
&Widget::copyFriendIdToClipboard);
connect(friendWidget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog);
@ -1293,7 +1294,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
{
int groupId = group->getId();
ContentDialog* groupDialog = ContentDialog::getGroupDialog(groupId);
ContentDialog* groupDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
bool separated = Settings::getInstance().getSeparateWindow();
GroupWidget* widget = groupWidgets[groupId];
bool isCurrentWindow = activeChatroomWidget == widget;
@ -1312,7 +1313,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,
[=]() { dialog->removeGroup(groupId); });
[=]() { ContentDialogManager::getInstance()->removeGroup(groupId); });
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &ChatForm::focusInput);
connect(groupWidget, &GroupWidget::newWindowOpened, this, &Widget::openNewDialog);
@ -1337,18 +1338,18 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
{
bool hasActive;
QWidget* currentWindow;
ContentDialog* contentDialog = ContentDialog::getFriendDialog(friendId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
Friend* f = FriendList::findFriend(friendId);
if (contentDialog != nullptr) {
currentWindow = contentDialog->window();
hasActive = ContentDialog::isFriendWidgetActive(friendId);
hasActive = ContentDialogManager::getInstance()->isFriendWidgetActive(friendId);
} else {
if (Settings::getInstance().getSeparateWindow() && Settings::getInstance().getShowWindow()) {
if (Settings::getInstance().getDontGroupWindows()) {
contentDialog = createContentDialog();
} else {
contentDialog = ContentDialog::current();
contentDialog = ContentDialogManager::getInstance()->current();
if (!contentDialog) {
contentDialog = createContentDialog();
}
@ -1356,7 +1357,7 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
addFriendDialog(f, contentDialog);
currentWindow = contentDialog->window();
hasActive = ContentDialog::isFriendWidgetActive(friendId);
hasActive = ContentDialogManager::getInstance()->isFriendWidgetActive(friendId);
} else {
currentWindow = window();
FriendWidget* widget = friendWidgets[friendId];
@ -1375,7 +1376,7 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
setWindowTitle(widget->getTitle());
}
} else {
ContentDialog::updateFriendStatus(friendId);
ContentDialogManager::getInstance()->updateFriendStatus(friendId);
}
return true;
@ -1388,13 +1389,13 @@ bool Widget::newGroupMessageAlert(int groupId, bool notify)
{
bool hasActive;
QWidget* currentWindow;
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
Group* g = GroupList::findGroup(groupId);
GroupWidget* widget = groupWidgets[groupId];
if (contentDialog != nullptr) {
currentWindow = contentDialog->window();
hasActive = ContentDialog::isGroupWidgetActive(groupId);
hasActive = ContentDialogManager::getInstance()->isGroupWidgetActive(groupId);
} else {
currentWindow = window();
hasActive = widget == activeChatroomWidget;
@ -1412,7 +1413,7 @@ bool Widget::newGroupMessageAlert(int groupId, bool notify)
setWindowTitle(widget->getTitle());
}
} else {
ContentDialog::updateGroupStatus(groupId);
ContentDialogManager::getInstance()->updateGroupStatus(groupId);
}
return true;
@ -1516,10 +1517,10 @@ void Widget::removeFriend(Friend* f, bool fake)
contactListWidget->removeFriendWidget(widget);
ContentDialog* lastDialog = ContentDialog::getFriendDialog(friendId);
ContentDialog* lastDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
if (lastDialog != nullptr) {
lastDialog->removeFriend(friendId);
ContentDialogManager::getInstance()->removeFriend(friendId);
}
FriendList::removeFriend(friendId, fake);
@ -1593,6 +1594,8 @@ void Widget::toggleFullscreen()
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::getInstance(), &Core::usernameSet, contentDialog, &ContentDialog::setUsername);
@ -1836,9 +1839,9 @@ void Widget::removeGroup(Group* g, bool fake)
}
GroupList::removeGroup(groupId, fake);
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
if (contentDialog != nullptr) {
contentDialog->removeGroup(groupId);
ContentDialogManager::getInstance()->removeGroup(groupId);
}
Nexus::getCore()->removeGroup(groupId, fake);