Skip to content

Commit

Permalink
refactor(drag&drop): Remove FriendWidget dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Diadlo committed Nov 5, 2016
1 parent 89138bb commit 2da9de0
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 95 deletions.
5 changes: 4 additions & 1 deletion src/widget/categorywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ void CategoryWidget::removeFriendWidget(FriendWidget* w, Status s)

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
Expand Down
70 changes: 40 additions & 30 deletions src/widget/circlewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
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 <QBoxLayout>
#include <QMouseEvent>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QMenu>

#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;

CircleWidget::CircleWidget(FriendListWidget* parent, int id)
Expand Down Expand Up @@ -150,7 +153,9 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* 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();

setContainerAttribute(Qt::WA_UnderMouse, true); // Simulate hover.
Expand All @@ -163,31 +168,34 @@ void CircleWidget::dragLeaveEvent(QDragLeaveEvent* )

void CircleWidget::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasFormat("friend"))
{
setExpanded(true, false);

int friendId = event->mimeData()->data("friend").toInt();
Friend* f = FriendList::findFriend(friendId);
assert(f != nullptr);
setExpanded(true, false);

FriendWidget* widget = f->getFriendWidget();
assert(widget != nullptr);
// Check, that the element is dropped from qTox
QObject *o = event->source();
FriendWidget *widget = qobject_cast<FriendWidget*>(o);
if (!widget)
return;

// Update old circle after moved.
CircleWidget* circleWidget = getFromID(Settings::getInstance().getFriendCircleID(f->getToxId()));
// Check, that the user has a friend with the same ToxId
ToxId toxId(event->mimeData()->text());
Friend *f = FriendList::findFriend(toxId);
if (!f)
return;

addFriendWidget(widget, f->getStatus());
Settings::getInstance().savePersonal();
// Save CircleWidget before changing the Id
int circleId = Settings::getInstance().getFriendCircleID(toxId);
CircleWidget* circleWidget = getFromID(circleId);

if (circleWidget != nullptr)
{
circleWidget->updateStatus();
Widget::getInstance()->searchCircle(circleWidget);
}
addFriendWidget(widget, f->getStatus());
Settings::getInstance().savePersonal();

setContainerAttribute(Qt::WA_UnderMouse, false);
if (circleWidget != nullptr)
{
circleWidget->updateStatus();
Widget::getInstance()->searchCircle(circleWidget);
}

setContainerAttribute(Qt::WA_UnderMouse, false);
}

void CircleWidget::onSetName()
Expand All @@ -203,7 +211,9 @@ void CircleWidget::onExpand()

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)
Expand Down
51 changes: 38 additions & 13 deletions src/widget/contentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,48 +543,73 @@ bool ContentDialog::event(QEvent* 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);

// 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)
event->acceptProposedAction();
}
else if (event->mimeData()->hasFormat("group"))
else if (group)
{
int groupId = event->mimeData()->data("group").toInt();
auto iter = groupList.find(groupId);
if (!event->mimeData()->hasFormat("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)
event->acceptProposedAction();
}
}

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();
auto iter = friendList.find(friendId);
ToxId toxId(event->mimeData()->text());
Friend *contact = FriendList::findFriend(toxId);
if (!contact)
return;

int friendId = contact->getFriendID();
auto iter = friendList.find(friendId);
if (iter != friendList.end())
std::get<0>(iter.value())->removeFriend(friendId);

Friend* contact = FriendList::findFriend(friendId);
Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible();
}
else if (event->mimeData()->hasFormat("group"))
else if (group)
{
int groupId = event->mimeData()->data("group").toInt();
auto iter = friendList.find(groupId);
if (!event->mimeData()->hasFormat("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())
std::get<0>(iter.value())->removeGroup(groupId);

Group* contact = GroupList::findGroup(groupId);
Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible();
}
Expand Down
31 changes: 18 additions & 13 deletions src/widget/form/groupchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
*/

#include "groupchatform.h"

#include <QDragEnterEvent>
#include <QMimeData>
#include <QTimer>

#include "tabcompleter.h"
#include "src/group.h"
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/widget/groupwidget.h"
#include "src/widget/tool/chattextedit.h"
#include "src/widget/tool/croppinglabel.h"
#include "src/widget/maskablepixmapwidget.h"
#include "src/core/core.h"
Expand All @@ -31,12 +37,6 @@
#include "src/widget/translator.h"
#include "src/widget/form/chatform.h"
#include "src/video/groupnetcamview.h"
#include <QDebug>
#include <QTimer>
#include <QPushButton>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QtAlgorithms>

/**
* @var QList<QLabel*> GroupChatForm::peerLabels
Expand Down Expand Up @@ -279,17 +279,22 @@ void GroupChatForm::peerAudioPlaying(int peer)

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();
}

void GroupChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasFormat("friend"))
{
int friendId = ev->mimeData()->data("friend").toInt();
Core::getInstance()->groupInviteFriend(friendId, group->getGroupId());
}
ToxId toxId = ToxId(ev->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (!frnd)
return;

int friendId = frnd->getFriendID();
int groupId = group->getGroupId();
Core::getInstance()->groupInviteFriend(friendId, groupId);
}

void GroupChatForm::onMicMuteToggle()
Expand Down
33 changes: 19 additions & 14 deletions src/widget/friendlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,29 +607,34 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget

void FriendListWidget::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasFormat("friend"))
ToxId toxId(event->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (frnd)
event->acceptProposedAction();
}

void FriendListWidget::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasFormat("friend"))
{
int friendId = event->mimeData()->data("friend").toInt();
Friend* f = FriendList::findFriend(friendId);
assert(f != nullptr);
// Check, that the element is dropped from qTox
QObject *o = event->source();
FriendWidget *widget = qobject_cast<FriendWidget*>(o);
if (!widget)
return;

FriendWidget* widget = f->getFriendWidget();
assert(widget != nullptr);
// Check, that the user has a friend with the same ToxId
ToxId toxId(event->mimeData()->text());
Friend *f = FriendList::findFriend(toxId);
if (!f)
return;

// Update old circle after moved.
CircleWidget* circleWidget = CircleWidget::getFromID(Settings::getInstance().getFriendCircleID(f->getToxId()));
// Save CircleWidget before changing the Id
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)
circleWidget->updateStatus();
}
if (circleWidget)
circleWidget->updateStatus();
}

void FriendListWidget::dayTimeout()
Expand Down
5 changes: 2 additions & 3 deletions src/widget/friendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,12 @@ void FriendWidget::mouseMoveEvent(QMouseEvent *ev)

if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{
QDrag* drag = new QDrag(this);
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->setPixmap(avatar->getPixmap());

drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}
Expand Down
Loading

0 comments on commit 2da9de0

Please sign in to comment.