2015-05-28 01:17:12 +08:00
|
|
|
/*
|
2015-06-12 03:27:54 +08:00
|
|
|
Copyright © 2015 by The qTox Project
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2015-05-28 08:45:23 +08:00
|
|
|
#include "circlewidget.h"
|
2015-06-12 03:27:54 +08:00
|
|
|
#include "friendwidget.h"
|
|
|
|
#include "friendlistwidget.h"
|
|
|
|
#include "tool/croppinglabel.h"
|
2015-06-10 07:42:51 +08:00
|
|
|
#include "src/widget/style.h"
|
2015-06-12 03:27:54 +08:00
|
|
|
|
2015-06-10 07:42:51 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-05-28 01:17:12 +08:00
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/friend.h"
|
2015-06-07 11:20:06 +08:00
|
|
|
#include "widget.h"
|
2015-05-28 01:17:12 +08:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QLabel>
|
|
|
|
#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>
|
2015-05-28 01:17:12 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
2015-06-04 04:18:40 +08:00
|
|
|
QHash<int, CircleWidget*> CircleWidget::circleList;
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
CircleWidget::CircleWidget(FriendListWidget* parent, int id_)
|
|
|
|
: CategoryWidget(parent)
|
2015-06-04 04:18:40 +08:00
|
|
|
, id(id_)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-06-04 04:18:40 +08:00
|
|
|
if (id != -1)
|
|
|
|
{
|
|
|
|
setName(Settings::getInstance().getCircleName(id));
|
|
|
|
}
|
|
|
|
|
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-04 04:18:40 +08:00
|
|
|
bool isNew = false;
|
|
|
|
auto circleIt = circleList.find(id);
|
|
|
|
if (circleIt == circleList.end())
|
|
|
|
{
|
|
|
|
if (id == -1)
|
|
|
|
{
|
|
|
|
isNew = true;
|
2015-06-07 11:20:06 +08:00
|
|
|
id = Settings::getInstance().addCircle();
|
|
|
|
nameLabel->setText(tr("Circle #%1").arg(id + 1));
|
|
|
|
Settings::getInstance().setCircleName(id, nameLabel->fullText());
|
2015-06-04 04:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-07 11:20:06 +08:00
|
|
|
circleList[id] = this;
|
2015-06-04 04:18:40 +08:00
|
|
|
|
|
|
|
if (isNew)
|
2015-06-12 03:27:54 +08:00
|
|
|
editName();
|
2015-06-04 04:18:40 +08:00
|
|
|
|
2015-06-06 03:34:32 +08:00
|
|
|
setExpanded(Settings::getInstance().getCircleExpanded(id));
|
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()
|
|
|
|
{
|
|
|
|
circleList.remove(id);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (circleIt != circleList.end())
|
|
|
|
return circleIt.value();
|
|
|
|
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-05-29 06:36:21 +08:00
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
QAction* selectedItem = menu.exec(mapToGlobal(event->pos()));
|
2015-05-29 06:36:21 +08:00
|
|
|
if (selectedItem == renameAction)
|
2015-06-12 03:27:54 +08:00
|
|
|
editName();
|
2015-05-29 06:36:21 +08:00
|
|
|
else if (selectedItem == removeAction)
|
|
|
|
{
|
2015-06-11 00:11:50 +08:00
|
|
|
FriendListWidget* friendList = dynamic_cast<FriendListWidget*>(parentWidget());
|
2015-06-12 03:27:54 +08:00
|
|
|
moveFriendWidgets(friendList);
|
2015-05-29 06:36:21 +08:00
|
|
|
|
|
|
|
friendList->removeCircleWidget(this);
|
2015-06-04 04:18:40 +08:00
|
|
|
|
|
|
|
circleList.remove(id);
|
|
|
|
int replacedCircle = Settings::getInstance().removeCircle(id);
|
|
|
|
|
|
|
|
auto circleReplace = circleList.find(replacedCircle);
|
|
|
|
if (circleReplace != circleList.end())
|
|
|
|
circleReplace.value()->updateID(id);
|
2015-05-29 06:36:21 +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
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("friend"))
|
|
|
|
event->acceptProposedAction();
|
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
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("friend"))
|
|
|
|
{
|
2015-06-06 03:34:32 +08:00
|
|
|
setExpanded(true);
|
2015-05-28 08:45:23 +08:00
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
int friendId = event->mimeData()->data("friend").toInt();
|
2015-06-12 03:27:54 +08:00
|
|
|
Friend* f = FriendList::findFriend(friendId);
|
2015-05-28 01:17:12 +08:00
|
|
|
assert(f != nullptr);
|
|
|
|
|
2015-06-12 03:27:54 +08:00
|
|
|
FriendWidget* widget = f->getFriendWidget();
|
2015-05-28 01:17:12 +08:00
|
|
|
assert(widget != nullptr);
|
|
|
|
|
2015-05-29 00:58:44 +08:00
|
|
|
// Update old circle after moved.
|
2015-06-12 03:27:54 +08:00
|
|
|
CircleWidget* circleWidget = getFromID(Settings::getInstance().getFriendCircleID(f->getToxId()));
|
2015-05-29 00:58:44 +08:00
|
|
|
|
|
|
|
addFriendWidget(widget, f->getStatus());
|
|
|
|
|
|
|
|
if (circleWidget != nullptr)
|
|
|
|
{
|
2015-05-31 03:10:43 +08:00
|
|
|
circleWidget->updateStatus();
|
2015-06-07 11:20:06 +08:00
|
|
|
Widget::getInstance()->searchCircle(circleWidget);
|
2015-05-29 00:58:44 +08:00
|
|
|
}
|
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-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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::onAddFriendWidget(FriendWidget* w)
|
2015-05-29 00:58:44 +08:00
|
|
|
{
|
2015-06-12 03:27:54 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(FriendList::findFriend(w->friendId)->getToxId(), 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-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
|
|
|
{
|
2015-06-12 03:27:54 +08:00
|
|
|
FriendWidget* friendWidget = dynamic_cast<FriendWidget*>(friendOnlineLayout()->itemAt(i));
|
2015-06-04 04:18:40 +08:00
|
|
|
if (friendWidget != nullptr)
|
|
|
|
{
|
2015-06-06 03:34:32 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(FriendList::findFriend(friendWidget->friendId)->getToxId(), 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
|
|
|
{
|
2015-06-12 03:27:54 +08:00
|
|
|
FriendWidget* friendWidget = dynamic_cast<FriendWidget*>(friendOfflineLayout()->itemAt(i));
|
2015-06-04 04:18:40 +08:00
|
|
|
if (friendWidget != nullptr)
|
|
|
|
{
|
2015-06-06 03:34:32 +08:00
|
|
|
Settings::getInstance().setFriendCircleID(FriendList::findFriend(friendWidget->friendId)->getToxId(), id);
|
2015-06-04 04:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|