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

refactor: Remove using getFriendWidget from FriendWidget

This commit is contained in:
Diadlo 2016-08-24 02:51:38 +03:00
parent 4d806a3661
commit 7d1564e586
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
3 changed files with 123 additions and 89 deletions

View File

@ -48,6 +48,14 @@
#include <cassert>
/**
* @class FriendWidget
*
* Widget, which displays brief information about friend.
* For example, used on friend list.
* When you click should open the chat with friend. Widget has a context menu.
*/
FriendWidget::FriendWidget(int FriendId, QString id)
: friendId(FriendId)
, isDefaultAvatar{true}
@ -62,7 +70,25 @@ FriendWidget::FriendWidget(int FriendId, QString id)
statusMessageLabel->setTextFormat(Qt::PlainText);
}
/**
* @brief FriendWidget::contextMenuEvent
* @param event Describe a context menu event
*
* Default context menu event handler.
* Redirect all event information to the signal.
*/
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
{
emit contextMenuCalled(event);
}
/**
* @brief FriendWidget::onContextMenuCalled
* @param event Redirected from native contextMenuEvent
*
* Context menu handler. Always should be called to FriendWidget from FriendList
*/
void FriendWidget::onContextMenuCalled(QContextMenuEvent *event)
{
if (!active)
setBackgroundRole(QPalette::Highlight);
@ -114,7 +140,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
FriendListWidget *friendList;
if (circleWidget == nullptr)
friendList = qobject_cast<FriendListWidget*>(FriendList::findFriend(friendId)->getFriendWidget()->parentWidget());
friendList = qobject_cast<FriendListWidget*>(this->parentWidget());
else
friendList = qobject_cast<FriendListWidget*>(circleWidget->parentWidget());
@ -170,8 +196,9 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
if (!active)
setBackgroundRole(QPalette::Window);
if (selectedItem)
{
if (!selectedItem)
return;
if (selectedItem == setAlias)
{
nameLabel->editBegin();
@ -179,18 +206,15 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
else if (selectedItem == removeFriendAction)
{
emit removeFriend(friendId);
return;
}
else if (selectedItem == openChatWindow)
{
emit chatroomWidgetClicked(this, true);
return;
}
else if (selectedItem == removeChatWindow)
{
ContentDialog* contentDialog = ContentDialog::getFriendDialog(friendId);
contentDialog->removeFriend(friendId);
return;
}
else if (selectedItem == autoAccept)
{
@ -203,12 +227,12 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
}
else if (autoAccept->isChecked())
{
dir = QFileDialog::getExistingDirectory(0,
tr("Choose an auto accept directory","popup title"),
dir,
QFileDialog::DontUseNativeDialog);
dir = QFileDialog::getExistingDirectory(
0, tr("Choose an auto accept directory", "popup title"),
dir, QFileDialog::DontUseNativeDialog);
autoAccept->setChecked(true);
qDebug() << "setting auto accept dir for" << friendId << "to" << dir;
qDebug() << "Setting auto accept dir for" << friendId << "to" << dir;
Settings::getInstance().setAutoAcceptDir(id, dir);
}
}
@ -229,7 +253,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
circleWidget->updateStatus();
if (friendList != nullptr)
friendList->addCircleWidget(FriendList::findFriend(friendId)->getFriendWidget());
friendList->addCircleWidget(this);
else
Settings::getInstance().setFriendCircleID(id, Settings::getInstance().addCircle());
}
@ -240,8 +264,8 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
}
else if (removeCircleAction != nullptr && selectedItem == removeCircleAction)
{
if (friendList != nullptr)
friendList->moveWidget(FriendList::findFriend(friendId)->getFriendWidget(), FriendList::findFriend(friendId)->getStatus(), true);
if (friendList)
friendList->moveWidget(this, FriendList::findFriend(friendId)->getStatus(), true);
else
Settings::getInstance().setFriendCircleID(id, -1);
@ -255,24 +279,25 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
{
CircleWidget* circle = CircleWidget::getFromID(circleActions[selectedItem]);
if (circle != nullptr)
if (circle)
{
circle->addFriendWidget(FriendList::findFriend(friendId)->getFriendWidget(), FriendList::findFriend(friendId)->getStatus());
circle->addFriendWidget(this, FriendList::findFriend(friendId)->getStatus());
circle->setExpanded(true);
Widget::getInstance()->searchCircle(circle);
Settings::getInstance().savePersonal();
}
else
{
Settings::getInstance().setFriendCircleID(id, circleActions[selectedItem]);
}
if (circleWidget != nullptr)
if (circleWidget)
{
circleWidget->updateStatus();
Widget::getInstance()->searchCircle(circleWidget);
}
}
}
}
void FriendWidget::setAsActiveChatroom()
{

View File

@ -43,11 +43,13 @@ signals:
void friendWidgetClicked(FriendWidget* widget);
void removeFriend(int friendId);
void copyFriendIdToClipboard(int friendId);
void contextMenuCalled(QContextMenuEvent * event);
public slots:
void onAvatarChange(int FriendId, const QPixmap& pic);
void onAvatarRemoved(int FriendId);
void setAlias(const QString& alias);
void onContextMenuCalled(QContextMenuEvent * event);
protected:
virtual void mousePressEvent(QMouseEvent* ev) override;

View File

@ -1021,6 +1021,7 @@ void Widget::addFriend(int friendId, const ToxPk& friendPk)
connect(widget, &FriendWidget::chatroomWidgetClicked, this, &Widget::onChatroomWidgetClicked);
connect(widget, &FriendWidget::chatroomWidgetClicked, friendForm, &ChatForm::focusInput);
connect(widget, &FriendWidget::copyFriendIdToClipboard, this, &Widget::copyFriendIdToClipboard);
connect(widget, &FriendWidget::contextMenuCalled, widget, &FriendWidget::onContextMenuCalled);
connect(widget, SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));
// Try to get the avatar from the cache
@ -1274,6 +1275,12 @@ void Widget::addFriendDialog(Friend *frnd, ContentDialog *dialog)
// Signal transmission from the created `friendWidget` (which shown in
// ContentDialog) to the `widget` (which shown in main widget)
// FIXME: emit should be removed
connect(friendWidget, &FriendWidget::contextMenuCalled,
widget, [=](QContextMenuEvent * event)
{
emit widget->contextMenuCalled(event);
});
connect(friendWidget, &FriendWidget::chatroomWidgetClicked,
this, [=](GenericChatroomWidget *w, bool group)
{