2015-05-28 01:17:12 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2015 by The qTox Project Contributors
|
2015-06-12 03:27:54 +08:00
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2015-05-28 01:17:12 +08:00
|
|
|
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.
|
2015-06-12 03:27:54 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2015-05-28 01:17:12 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-12 03:27:54 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2015-05-28 01:17:12 +08:00
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2015-05-28 01:17:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QBoxLayout>
|
2015-05-28 22:28:11 +08:00
|
|
|
#include <QMouseEvent>
|
2015-05-28 01:17:12 +08:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QMimeData>
|
2015-05-29 06:36:21 +08:00
|
|
|
#include <QMenu>
|
2016-08-07 03:28:40 +08:00
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
#include "circlewidget.h"
|
|
|
|
#include "contentdialog.h"
|
|
|
|
#include "friendwidget.h"
|
|
|
|
#include "friendlistwidget.h"
|
|
|
|
#include "tool/croppinglabel.h"
|
|
|
|
#include "widget.h"
|
|
|
|
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/friend.h"
|
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/persistence/settings.h"
|
2016-08-07 03:28:40 +08:00
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
QHash<int, CircleWidget*> CircleWidget::circleList;
|
|
|
|
|
2015-06-17 04:26:20 +08:00
|
|
|
CircleWidget::CircleWidget(FriendListWidget* parent, int id)
|
2015-06-12 03:27:54 +08:00
|
|
|
: CategoryWidget(parent)
|
2015-06-17 04:26:20 +08:00
|
|
|
, id(id)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-06-17 20:40:14 +08:00
|
|
|
setName(Settings::getInstance().getCircleName(id), false);
|
2015-06-17 04:26:20 +08:00
|
|
|
circleList[id] = this;
|
2015-06-04 04:18:40 +08:00
|
|
|
|
2015-06-08 02:25:05 +08:00
|
|
|
connect(nameLabel, &CroppingLabel::editFinished, [this](const QString &newName)
|
2015-06-02 02:39:40 +08:00
|
|
|
{
|
2015-06-08 02:25:05 +08:00
|
|
|
if (!newName.isEmpty())
|
2015-06-11 00:11:50 +08:00
|
|
|
emit renameRequested(this, newName);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(nameLabel, &CroppingLabel::editRemoved, [this]()
|
|
|
|
{
|
|
|
|
if (isCompact())
|
|
|
|
nameLabel->minimizeMaximumWidth();
|
2015-06-02 02:39:40 +08:00
|
|
|
});
|
|
|
|
|
2015-06-17 20:40:14 +08:00
|
|
|
setExpanded(Settings::getInstance().getCircleExpanded(id), false);
|
2015-06-10 01:40:45 +08:00
|
|
|
updateStatus();
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-13 02:52:05 +08:00
|
|
|
CircleWidget::~CircleWidget()
|
|
|
|
{
|
2015-06-19 02:55:46 +08:00
|
|
|
if (circleList[id] == this)
|
|
|
|
circleList.remove(id);
|
2015-06-13 02:52:05 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:26:20 +08:00
|
|
|
void CircleWidget::editName()
|
|
|
|
{
|
|
|
|
CategoryWidget::editName();
|
|
|
|
}
|
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
CircleWidget* CircleWidget::getFromID(int id)
|
2015-06-02 02:39:40 +08:00
|
|
|
{
|
2015-06-04 04:18:40 +08:00
|
|
|
auto circleIt = circleList.find(id);
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
if (circleIt != circleList.end())
|
|
|
|
return circleIt.value();
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
return nullptr;
|
2015-05-29 06:36:21 +08:00
|
|
|
}
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
void CircleWidget::contextMenuEvent(QContextMenuEvent* event)
|
2015-05-29 06:36:21 +08:00
|
|
|
{
|
|
|
|
QMenu menu;
|
2015-06-12 03:27:54 +08:00
|
|
|
QAction* renameAction = menu.addAction(tr("Rename circle", "Menu for renaming a circle"));
|
|
|
|
QAction* removeAction = menu.addAction(tr("Remove circle", "Menu for removing a circle"));
|
2015-07-01 00:29:09 +08:00
|
|
|
QAction* openAction = nullptr;
|
|
|
|
|
|
|
|
if (friendOfflineLayout()->count() + friendOnlineLayout()->count() > 0)
|
|
|
|
openAction = menu.addAction(tr("Open all in new window"));
|
2015-05-29 06:36:21 +08:00
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
QAction* selectedItem = menu.exec(mapToGlobal(event->pos()));
|
2015-07-01 00:29:09 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
if (selectedItem)
|
2015-06-18 03:45:03 +08:00
|
|
|
{
|
2015-07-20 20:13:51 +08:00
|
|
|
if (selectedItem == renameAction)
|
|
|
|
{
|
|
|
|
editName();
|
|
|
|
}
|
|
|
|
else if (selectedItem == removeAction)
|
|
|
|
{
|
|
|
|
FriendListWidget* friendList = static_cast<FriendListWidget*>(parentWidget());
|
|
|
|
moveFriendWidgets(friendList);
|
2015-06-04 04:18:40 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
friendList->removeCircleWidget(this);
|
2015-06-04 04:18:40 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
int replacedCircle = Settings::getInstance().removeCircle(id);
|
2015-06-19 02:55:46 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
auto circleReplace = circleList.find(replacedCircle);
|
|
|
|
if (circleReplace != circleList.end())
|
|
|
|
circleReplace.value()->updateID(id);
|
|
|
|
else
|
|
|
|
assert(true); // This should never happen.
|
2015-07-01 00:29:09 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
circleList.remove(replacedCircle);
|
|
|
|
}
|
|
|
|
else if (selectedItem == openAction)
|
2015-07-01 00:29:09 +08:00
|
|
|
{
|
2015-07-20 20:13:51 +08:00
|
|
|
ContentDialog* dialog = Widget::getInstance()->createContentDialog();
|
2015-07-01 00:29:09 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
for (int i = 0; i < friendOnlineLayout()->count(); ++i)
|
2015-07-01 00:29:09 +08:00
|
|
|
{
|
2016-09-23 10:06:20 +08:00
|
|
|
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendOnlineLayout()->itemAt(i)->widget());
|
2015-07-01 00:29:09 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
if (friendWidget != nullptr)
|
|
|
|
{
|
|
|
|
Friend* f = friendWidget->getFriend();
|
|
|
|
dialog->addFriend(friendWidget->friendId, f->getDisplayedName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < friendOfflineLayout()->count(); ++i)
|
2015-07-01 00:29:09 +08:00
|
|
|
{
|
2016-09-23 10:06:20 +08:00
|
|
|
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendOfflineLayout()->itemAt(i)->widget());
|
2015-07-20 20:13:51 +08:00
|
|
|
|
|
|
|
if (friendWidget != nullptr)
|
|
|
|
{
|
|
|
|
Friend* f = friendWidget->getFriend();
|
|
|
|
dialog->addFriend(friendWidget->friendId, f->getDisplayedName());
|
|
|
|
}
|
2015-07-01 00:29:09 +08:00
|
|
|
}
|
2015-07-01 19:49:36 +08:00
|
|
|
|
2015-07-20 20:13:51 +08:00
|
|
|
dialog->show();
|
|
|
|
dialog->ensureSplitterVisible();
|
|
|
|
}
|
2015-07-01 00:29:09 +08:00
|
|
|
}
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
setContainerAttribute(Qt::WA_UnderMouse, false);
|
2015-05-29 06:36:21 +08:00
|
|
|
}
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
void CircleWidget::dragEnterEvent(QDragEnterEvent* event)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2016-08-07 03:28:40 +08:00
|
|
|
ToxId toxId(event->mimeData()->text());
|
2017-01-06 19:02:54 +08:00
|
|
|
Friend* f = FriendList::findFriend(toxId.getPublicKey());
|
2016-08-07 03:28:40 +08:00
|
|
|
if (f != nullptr)
|
2015-05-28 01:17:12 +08:00
|
|
|
event->acceptProposedAction();
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
setContainerAttribute(Qt::WA_UnderMouse, true); // Simulate hover.
|
2015-05-28 22:28:11 +08:00
|
|
|
}
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
void CircleWidget::dragLeaveEvent(QDragLeaveEvent* )
|
2015-05-28 22:28:11 +08:00
|
|
|
{
|
2015-06-12 03:27:54 +08:00
|
|
|
setContainerAttribute(Qt::WA_UnderMouse, false);
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
void CircleWidget::dropEvent(QDropEvent* event)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2016-08-07 03:28:40 +08:00
|
|
|
setExpanded(true, false);
|
2015-05-28 01:17:12 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
// Check, that the element is dropped from qTox
|
|
|
|
QObject *o = event->source();
|
|
|
|
FriendWidget *widget = qobject_cast<FriendWidget*>(o);
|
|
|
|
if (!widget)
|
|
|
|
return;
|
2015-05-28 01:17:12 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
// Check, that the user has a friend with the same ToxId
|
|
|
|
ToxId toxId(event->mimeData()->text());
|
2017-01-06 19:02:54 +08:00
|
|
|
Friend* f = FriendList::findFriend(toxId.getPublicKey());
|
2016-08-07 03:28:40 +08:00
|
|
|
if (!f)
|
|
|
|
return;
|
2015-05-29 00:58:44 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
// Save CircleWidget before changing the Id
|
2016-12-30 06:14:48 +08:00
|
|
|
int circleId = Settings::getInstance().getFriendCircleID(toxId.getPublicKey());
|
2016-08-07 03:28:40 +08:00
|
|
|
CircleWidget* circleWidget = getFromID(circleId);
|
2015-05-29 00:58:44 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
addFriendWidget(widget, f->getStatus());
|
|
|
|
Settings::getInstance().savePersonal();
|
2015-05-28 22:28:11 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
if (circleWidget != nullptr)
|
|
|
|
{
|
|
|
|
circleWidget->updateStatus();
|
|
|
|
Widget::getInstance()->searchCircle(circleWidget);
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
2016-08-07 03:28:40 +08:00
|
|
|
|
|
|
|
setContainerAttribute(Qt::WA_UnderMouse, false);
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
2015-05-29 00:58:44 +08:00
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
void CircleWidget::onSetName()
|
|
|
|
{
|
|
|
|
Settings::getInstance().setCircleName(id, getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::onExpand()
|
|
|
|
{
|
|
|
|
Settings::getInstance().setCircleExpanded(id, isExpanded());
|
2015-06-17 20:40:14 +08:00
|
|
|
Settings::getInstance().savePersonal();
|
2015-06-12 03:27:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::onAddFriendWidget(FriendWidget* w)
|
2015-05-29 00:58:44 +08:00
|
|
|
{
|
2016-08-07 03:28:40 +08:00
|
|
|
Friend* f = FriendList::findFriend(w->friendId);
|
2016-12-30 06:14:48 +08:00
|
|
|
ToxPk toxId = f->getPublicKey();
|
2016-08-07 03:28:40 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(toxId, id);
|
2015-05-29 00:58:44 +08:00
|
|
|
}
|
2015-06-04 04:18:40 +08:00
|
|
|
|
|
|
|
void CircleWidget::updateID(int index)
|
|
|
|
{
|
|
|
|
// For when a circle gets destroyed, another takes its id.
|
2015-06-10 01:40:45 +08:00
|
|
|
// This function updates all friends widgets for this new id.
|
2015-06-19 02:55:46 +08:00
|
|
|
|
|
|
|
if (id == index)
|
|
|
|
return;
|
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
id = index;
|
|
|
|
circleList[id] = this;
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
for (int i = 0; i < friendOnlineLayout()->count(); ++i)
|
2015-06-04 04:18:40 +08:00
|
|
|
{
|
2016-09-23 10:06:20 +08:00
|
|
|
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendOnlineLayout()->itemAt(i)->widget());
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
if (friendWidget != nullptr)
|
2016-12-30 06:14:48 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(FriendList::findFriend(friendWidget->friendId)->getPublicKey(), id);
|
2015-06-04 04:18:40 +08:00
|
|
|
}
|
2015-06-12 03:27:54 +08:00
|
|
|
for (int i = 0; i < friendOfflineLayout()->count(); ++i)
|
2015-06-04 04:18:40 +08:00
|
|
|
{
|
2016-09-23 10:06:20 +08:00
|
|
|
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendOfflineLayout()->itemAt(i)->widget());
|
2015-06-18 03:45:03 +08:00
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
if (friendWidget != nullptr)
|
2016-12-30 06:14:48 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(FriendList::findFriend(friendWidget->friendId)->getPublicKey(), id);
|
2015-06-04 04:18:40 +08:00
|
|
|
}
|
|
|
|
}
|