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

refactor(drag&drop): Remove FriendWidget dependency

This commit is contained in:
Diadlo 2016-08-06 22:28:40 +03:00
parent 89138bb52b
commit 2da9de096f
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
7 changed files with 154 additions and 99 deletions

View File

@ -137,7 +137,10 @@ void CategoryWidget::removeFriendWidget(FriendWidget* w, Status s)
void CategoryWidget::updateStatus() void CategoryWidget::updateStatus()
{ {
statusLabel->setText(QString::number(listLayout->friendOnlineCount()) + QStringLiteral(" / ") + QString::number(listLayout->friendTotalCount())); QString online = QString::number(listLayout->friendOnlineCount());
QString offline = QString::number(listLayout->friendTotalCount());
QString text = online + QStringLiteral(" / ") + offline;
statusLabel->setText(text);
} }
bool CategoryWidget::hasChatrooms() const bool CategoryWidget::hasChatrooms() const

View File

@ -17,23 +17,26 @@
along with qTox. If not, see <http://www.gnu.org/licenses/>. along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "circlewidget.h"
#include "friendwidget.h"
#include "friendlistwidget.h"
#include "tool/croppinglabel.h"
#include "src/persistence/settings.h"
#include "src/friendlist.h"
#include "src/friend.h"
#include "src/widget/contentdialog.h"
#include "widget.h"
#include <QVariant> #include <QVariant>
#include <QBoxLayout> #include <QBoxLayout>
#include <QMouseEvent> #include <QMouseEvent>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QMimeData> #include <QMimeData>
#include <QMenu> #include <QMenu>
#include <cassert> #include <cassert>
#include "circlewidget.h"
#include "contentdialog.h"
#include "friendwidget.h"
#include "friendlistwidget.h"
#include "tool/croppinglabel.h"
#include "widget.h"
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/persistence/settings.h"
QHash<int, CircleWidget*> CircleWidget::circleList; QHash<int, CircleWidget*> CircleWidget::circleList;
CircleWidget::CircleWidget(FriendListWidget* parent, int id) CircleWidget::CircleWidget(FriendListWidget* parent, int id)
@ -150,7 +153,9 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* event)
void CircleWidget::dragEnterEvent(QDragEnterEvent* event) void CircleWidget::dragEnterEvent(QDragEnterEvent* event)
{ {
if (event->mimeData()->hasFormat("friend")) ToxId toxId(event->mimeData()->text());
Friend *f = FriendList::findFriend(toxId);
if (f != nullptr)
event->acceptProposedAction(); event->acceptProposedAction();
setContainerAttribute(Qt::WA_UnderMouse, true); // Simulate hover. setContainerAttribute(Qt::WA_UnderMouse, true); // Simulate hover.
@ -162,20 +167,24 @@ void CircleWidget::dragLeaveEvent(QDragLeaveEvent* )
} }
void CircleWidget::dropEvent(QDropEvent* event) void CircleWidget::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasFormat("friend"))
{ {
setExpanded(true, false); setExpanded(true, false);
int friendId = event->mimeData()->data("friend").toInt(); // Check, that the element is dropped from qTox
Friend* f = FriendList::findFriend(friendId); QObject *o = event->source();
assert(f != nullptr); FriendWidget *widget = qobject_cast<FriendWidget*>(o);
if (!widget)
return;
FriendWidget* widget = f->getFriendWidget(); // Check, that the user has a friend with the same ToxId
assert(widget != nullptr); ToxId toxId(event->mimeData()->text());
Friend *f = FriendList::findFriend(toxId);
if (!f)
return;
// Update old circle after moved. // Save CircleWidget before changing the Id
CircleWidget* circleWidget = getFromID(Settings::getInstance().getFriendCircleID(f->getToxId())); int circleId = Settings::getInstance().getFriendCircleID(toxId);
CircleWidget* circleWidget = getFromID(circleId);
addFriendWidget(widget, f->getStatus()); addFriendWidget(widget, f->getStatus());
Settings::getInstance().savePersonal(); Settings::getInstance().savePersonal();
@ -188,7 +197,6 @@ void CircleWidget::dropEvent(QDropEvent* event)
setContainerAttribute(Qt::WA_UnderMouse, false); setContainerAttribute(Qt::WA_UnderMouse, false);
} }
}
void CircleWidget::onSetName() void CircleWidget::onSetName()
{ {
@ -203,7 +211,9 @@ void CircleWidget::onExpand()
void CircleWidget::onAddFriendWidget(FriendWidget* w) void CircleWidget::onAddFriendWidget(FriendWidget* w)
{ {
Settings::getInstance().setFriendCircleID(FriendList::findFriend(w->friendId)->getToxId(), id); Friend* f = FriendList::findFriend(w->friendId);
ToxId toxId = f->getToxId();
Settings::getInstance().setFriendCircleID(toxId, id);
} }
void CircleWidget::updateID(int index) void CircleWidget::updateID(int index)

View File

@ -543,20 +543,34 @@ bool ContentDialog::event(QEvent* event)
void ContentDialog::dragEnterEvent(QDragEnterEvent *event) void ContentDialog::dragEnterEvent(QDragEnterEvent *event)
{ {
if (event->mimeData()->hasFormat("friend")) QObject *o = event->source();
FriendWidget *frnd = qobject_cast<FriendWidget*>(o);
GroupWidget *group = qobject_cast<GroupWidget*>(o);
if (frnd)
{ {
int friendId = event->mimeData()->data("friend").toInt(); ToxId toxId(event->mimeData()->text());
Friend *contact = FriendList::findFriend(toxId);
if (!contact)
return;
int friendId = contact->getFriendID();
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
// 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 (iter == friendList.end() || std::get<0>(iter.value()) != this) if (iter == friendList.end() || std::get<0>(iter.value()) != this)
event->acceptProposedAction(); event->acceptProposedAction();
} }
else if (event->mimeData()->hasFormat("group")) else if (group)
{ {
int groupId = event->mimeData()->data("group").toInt(); if (!event->mimeData()->hasFormat("groupId"))
auto iter = groupList.find(groupId); return;
int groupId = event->mimeData()->data("groupId").toInt();
Group *contact = GroupList::findGroup(groupId);
if (!contact)
return;
auto iter = groupList.find(groupId);
if (iter == groupList.end() || std::get<0>(iter.value()) != this) if (iter == groupList.end() || std::get<0>(iter.value()) != this)
event->acceptProposedAction(); event->acceptProposedAction();
} }
@ -564,27 +578,38 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent *event)
void ContentDialog::dropEvent(QDropEvent *event) void ContentDialog::dropEvent(QDropEvent *event)
{ {
if (event->mimeData()->hasFormat("friend")) QObject *o = event->source();
FriendWidget *frnd = qobject_cast<FriendWidget*>(o);
GroupWidget *group = qobject_cast<GroupWidget*>(o);
if (frnd)
{ {
int friendId = event->mimeData()->data("friend").toInt(); ToxId toxId(event->mimeData()->text());
auto iter = friendList.find(friendId); Friend *contact = FriendList::findFriend(toxId);
if (!contact)
return;
int friendId = contact->getFriendID();
auto iter = friendList.find(friendId);
if (iter != friendList.end()) if (iter != friendList.end())
std::get<0>(iter.value())->removeFriend(friendId); std::get<0>(iter.value())->removeFriend(friendId);
Friend* contact = FriendList::findFriend(friendId);
Widget::getInstance()->addFriendDialog(contact, this); Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
} }
else if (event->mimeData()->hasFormat("group")) else if (group)
{ {
int groupId = event->mimeData()->data("group").toInt(); if (!event->mimeData()->hasFormat("groupId"))
auto iter = friendList.find(groupId); return;
int groupId = event->mimeData()->data("groupId").toInt();
Group *contact = GroupList::findGroup(groupId);
if (!contact)
return;
auto iter = friendList.find(groupId);
if (iter != friendList.end()) if (iter != friendList.end())
std::get<0>(iter.value())->removeGroup(groupId); std::get<0>(iter.value())->removeGroup(groupId);
Group* contact = GroupList::findGroup(groupId);
Widget::getInstance()->addGroupDialog(contact, this); Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
} }

View File

@ -18,10 +18,16 @@
*/ */
#include "groupchatform.h" #include "groupchatform.h"
#include <QDragEnterEvent>
#include <QMimeData>
#include <QTimer>
#include "tabcompleter.h" #include "tabcompleter.h"
#include "src/group.h" #include "src/group.h"
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/widget/groupwidget.h" #include "src/widget/groupwidget.h"
#include "src/widget/tool/chattextedit.h"
#include "src/widget/tool/croppinglabel.h" #include "src/widget/tool/croppinglabel.h"
#include "src/widget/maskablepixmapwidget.h" #include "src/widget/maskablepixmapwidget.h"
#include "src/core/core.h" #include "src/core/core.h"
@ -31,12 +37,6 @@
#include "src/widget/translator.h" #include "src/widget/translator.h"
#include "src/widget/form/chatform.h" #include "src/widget/form/chatform.h"
#include "src/video/groupnetcamview.h" #include "src/video/groupnetcamview.h"
#include <QDebug>
#include <QTimer>
#include <QPushButton>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QtAlgorithms>
/** /**
* @var QList<QLabel*> GroupChatForm::peerLabels * @var QList<QLabel*> GroupChatForm::peerLabels
@ -279,17 +279,22 @@ void GroupChatForm::peerAudioPlaying(int peer)
void GroupChatForm::dragEnterEvent(QDragEnterEvent *ev) void GroupChatForm::dragEnterEvent(QDragEnterEvent *ev)
{ {
if (ev->mimeData()->hasFormat("friend")) ToxId toxId = ToxId(ev->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (frnd)
ev->acceptProposedAction(); ev->acceptProposedAction();
} }
void GroupChatForm::dropEvent(QDropEvent *ev) void GroupChatForm::dropEvent(QDropEvent *ev)
{ {
if (ev->mimeData()->hasFormat("friend")) ToxId toxId = ToxId(ev->mimeData()->text());
{ Friend *frnd = FriendList::findFriend(toxId);
int friendId = ev->mimeData()->data("friend").toInt(); if (!frnd)
Core::getInstance()->groupInviteFriend(friendId, group->getGroupId()); return;
}
int friendId = frnd->getFriendID();
int groupId = group->getGroupId();
Core::getInstance()->groupInviteFriend(friendId, groupId);
} }
void GroupChatForm::onMicMuteToggle() void GroupChatForm::onMicMuteToggle()

View File

@ -607,30 +607,35 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget
void FriendListWidget::dragEnterEvent(QDragEnterEvent* event) void FriendListWidget::dragEnterEvent(QDragEnterEvent* event)
{ {
if (event->mimeData()->hasFormat("friend")) ToxId toxId(event->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (frnd)
event->acceptProposedAction(); event->acceptProposedAction();
} }
void FriendListWidget::dropEvent(QDropEvent* event) void FriendListWidget::dropEvent(QDropEvent* event)
{ {
if (event->mimeData()->hasFormat("friend")) // Check, that the element is dropped from qTox
{ QObject *o = event->source();
int friendId = event->mimeData()->data("friend").toInt(); FriendWidget *widget = qobject_cast<FriendWidget*>(o);
Friend* f = FriendList::findFriend(friendId); if (!widget)
assert(f != nullptr); return;
FriendWidget* widget = f->getFriendWidget(); // Check, that the user has a friend with the same ToxId
assert(widget != nullptr); ToxId toxId(event->mimeData()->text());
Friend *f = FriendList::findFriend(toxId);
if (!f)
return;
// Update old circle after moved. // Save CircleWidget before changing the Id
CircleWidget* circleWidget = CircleWidget::getFromID(Settings::getInstance().getFriendCircleID(f->getToxId())); int circleId = Settings::getInstance().getFriendCircleID(f->getToxId());
CircleWidget* circleWidget = CircleWidget::getFromID(circleId);
moveWidget(widget, f->getStatus(), true); moveWidget(widget, f->getStatus(), true);
if (circleWidget != nullptr) if (circleWidget)
circleWidget->updateStatus(); circleWidget->updateStatus();
} }
}
void FriendListWidget::dayTimeout() void FriendListWidget::dayTimeout()
{ {

View File

@ -409,13 +409,12 @@ void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{ {
QDrag* drag = new QDrag(this);
QMimeData* mdata = new QMimeData; QMimeData* mdata = new QMimeData;
mdata->setData("friend", QString::number(friendId).toLatin1()); mdata->setText(getFriend()->getToxId().toString());
QDrag* drag = new QDrag(this);
drag->setMimeData(mdata); drag->setMimeData(mdata);
drag->setPixmap(avatar->getPixmap()); drag->setPixmap(avatar->getPixmap());
drag->exec(Qt::CopyAction | Qt::MoveAction); drag->exec(Qt::CopyAction | Qt::MoveAction);
} }
} }

View File

@ -18,16 +18,7 @@
*/ */
#include "groupwidget.h" #include "groupwidget.h"
#include "maskablepixmapwidget.h"
#include "contentdialog.h"
#include "src/grouplist.h"
#include "src/group.h"
#include "src/persistence/settings.h"
#include "form/groupchatform.h"
#include "src/widget/style.h"
#include "src/core/core.h"
#include "tool/croppinglabel.h"
#include "src/widget/translator.h"
#include <QPalette> #include <QPalette>
#include <QMenu> #include <QMenu>
#include <QContextMenuEvent> #include <QContextMenuEvent>
@ -36,6 +27,20 @@
#include <QApplication> #include <QApplication>
#include <QDrag> #include <QDrag>
#include "contentdialog.h"
#include "form/groupchatform.h"
#include "maskablepixmapwidget.h"
#include "src/core/core.h"
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/group.h"
#include "src/grouplist.h"
#include "src/persistence/settings.h"
#include "src/widget/friendwidget.h"
#include "src/widget/style.h"
#include "src/widget/translator.h"
#include "tool/croppinglabel.h"
GroupWidget::GroupWidget(int GroupId, QString Name) GroupWidget::GroupWidget(int GroupId, QString Name)
: groupId{GroupId} : groupId{GroupId}
{ {
@ -137,13 +142,12 @@ void GroupWidget::mouseMoveEvent(QMouseEvent *ev)
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{ {
QDrag* drag = new QDrag(this);
QMimeData* mdata = new QMimeData; QMimeData* mdata = new QMimeData;
mdata->setData("group", QString::number(groupId).toLatin1()); mdata->setText(getGroup()->getName());
QDrag* drag = new QDrag(this);
drag->setMimeData(mdata); drag->setMimeData(mdata);
drag->setPixmap(avatar->getPixmap()); drag->setPixmap(avatar->getPixmap());
drag->exec(Qt::CopyAction | Qt::MoveAction); drag->exec(Qt::CopyAction | Qt::MoveAction);
} }
} }
@ -231,7 +235,9 @@ void GroupWidget::resetEventFlags()
void GroupWidget::dragEnterEvent(QDragEnterEvent *ev) void GroupWidget::dragEnterEvent(QDragEnterEvent *ev)
{ {
if (ev->mimeData()->hasFormat("friend")) ToxId toxId = ToxId(ev->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (frnd)
ev->acceptProposedAction(); ev->acceptProposedAction();
if (!active) if (!active)
@ -246,15 +252,17 @@ void GroupWidget::dragLeaveEvent(QDragLeaveEvent *)
void GroupWidget::dropEvent(QDropEvent *ev) void GroupWidget::dropEvent(QDropEvent *ev)
{ {
if (ev->mimeData()->hasFormat("friend")) ToxId toxId = ToxId(ev->mimeData()->text());
{ Friend *frnd = FriendList::findFriend(toxId);
int friendId = ev->mimeData()->data("friend").toInt(); if (!frnd)
return;
int friendId = frnd->getFriendID();
Core::getInstance()->groupInviteFriend(friendId, groupId); Core::getInstance()->groupInviteFriend(friendId, groupId);
if (!active) if (!active)
setBackgroundRole(QPalette::Window); setBackgroundRole(QPalette::Window);
} }
}
void GroupWidget::setName(const QString& name) void GroupWidget::setName(const QString& name)
{ {