2014-08-22 23:19:16 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project
|
|
|
|
|
2014-08-22 23:19:16 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-08-22 23:19:16 +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-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-08-22 23:19:16 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-08-22 23:19:16 +08:00
|
|
|
|
2015-06-06 09:40:08 +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/>.
|
2014-08-22 23:19:16 +08:00
|
|
|
*/
|
|
|
|
#include "friendlistwidget.h"
|
2014-08-30 21:15:23 +08:00
|
|
|
#include <QDebug>
|
2014-09-11 21:44:34 +08:00
|
|
|
#include <QGridLayout>
|
2015-05-29 00:58:44 +08:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDragLeaveEvent>
|
2015-03-12 08:23:22 +08:00
|
|
|
#include "src/friend.h"
|
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/widget/friendwidget.h"
|
2015-05-28 01:17:12 +08:00
|
|
|
#include "groupwidget.h"
|
2015-05-28 22:28:11 +08:00
|
|
|
#include "circlewidget.h"
|
|
|
|
#include "friendlistlayout.h"
|
2015-05-28 01:17:12 +08:00
|
|
|
#include <cassert>
|
2014-08-22 23:19:16 +08:00
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
FriendListWidget::FriendListWidget(QWidget *parent, bool groupsOnTop) :
|
2014-08-22 23:19:16 +08:00
|
|
|
QWidget(parent)
|
|
|
|
{
|
2015-05-29 00:58:44 +08:00
|
|
|
listLayout = new FriendListLayout(groupsOnTop);
|
|
|
|
setLayout(listLayout);
|
2014-08-22 23:19:16 +08:00
|
|
|
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
|
2015-05-28 01:17:12 +08:00
|
|
|
|
|
|
|
circleLayout = new QVBoxLayout();
|
|
|
|
circleLayout->setSpacing(0);
|
|
|
|
circleLayout->setMargin(0);
|
2014-08-22 23:19:16 +08:00
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->addLayout(circleLayout);
|
2015-05-29 00:58:44 +08:00
|
|
|
|
|
|
|
setAcceptDrops(true);
|
2014-08-22 23:19:16 +08:00
|
|
|
}
|
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
void FriendListWidget::addGroupWidget(GroupWidget *widget)
|
2014-08-22 23:19:16 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->groupLayout->addWidget(widget);
|
2014-08-22 23:19:16 +08:00
|
|
|
}
|
|
|
|
|
2015-05-31 03:10:43 +08:00
|
|
|
void FriendListWidget::addFriendWidget(FriendWidget *w, Status s, int circleIndex)
|
|
|
|
{
|
|
|
|
CircleWidget *circleWidget = nullptr;
|
|
|
|
qDebug() << circleIndex;
|
|
|
|
if (circleIndex >= 0 && circleIndex < circleLayout->count())
|
|
|
|
circleWidget = dynamic_cast<CircleWidget*>(circleLayout->itemAt(circleIndex)->widget());
|
|
|
|
|
|
|
|
if (circleWidget == nullptr)
|
|
|
|
circleIndex = -1;
|
|
|
|
|
|
|
|
if (circleIndex == -1)
|
|
|
|
moveWidget(w, s, true);
|
|
|
|
else
|
|
|
|
circleWidget->addFriendWidget(w, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FriendListWidget::addCircleWidget(const QString &name)
|
|
|
|
{
|
|
|
|
CircleWidget *circleWidget = new CircleWidget(this);
|
|
|
|
circleWidget->setName(name);
|
|
|
|
circleLayout->addWidget(circleWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
CircleWidget* FriendListWidget::addCircleWidget(FriendWidget *friendWidget)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-05-29 06:36:21 +08:00
|
|
|
CircleWidget *circleWidget = new CircleWidget(this);
|
|
|
|
circleLayout->addWidget(circleWidget);
|
2015-05-29 21:26:43 +08:00
|
|
|
if (friendWidget != nullptr)
|
|
|
|
{
|
|
|
|
circleWidget->addFriendWidget(friendWidget, FriendList::findFriend(friendWidget->friendId)->getStatus());
|
|
|
|
circleWidget->toggle();
|
|
|
|
}
|
2015-05-29 06:36:21 +08:00
|
|
|
circleWidget->show(); // Avoid flickering.
|
2015-05-31 03:10:43 +08:00
|
|
|
return circleWidget;
|
2015-05-29 06:36:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FriendListWidget::removeCircleWidget(CircleWidget *widget)
|
|
|
|
{
|
|
|
|
//setUpdatesEnabled(false);
|
|
|
|
//widget->setVisible(false);
|
2015-05-29 21:26:43 +08:00
|
|
|
circleLayout->removeWidget(widget);
|
2015-05-29 06:36:21 +08:00
|
|
|
widget->deleteLater();
|
|
|
|
//setUpdatesEnabled(true);
|
|
|
|
//widget->deleteLater();
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
void FriendListWidget::searchChatrooms(const QString &searchString, bool hideOnline, bool hideOffline, bool hideGroups)
|
|
|
|
{
|
|
|
|
listLayout->searchChatrooms(searchString, hideOnline, hideOffline, hideGroups);
|
|
|
|
for (int i = 0; i != circleLayout->count(); ++i)
|
|
|
|
{
|
|
|
|
CircleWidget *circleWidget = static_cast<CircleWidget*>(circleLayout->itemAt(i)->widget());
|
|
|
|
circleWidget->searchChatrooms(searchString, hideOnline, hideOffline, hideGroups);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
QVBoxLayout* FriendListWidget::getFriendLayout(Status s)
|
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
return s == Status::Offline ? listLayout->friendLayouts[Offline] : listLayout->friendLayouts[Online];
|
2014-08-22 23:19:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 08:41:18 +08:00
|
|
|
void FriendListWidget::onGroupchatPositionChanged(bool top)
|
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->removeItem(circleLayout);
|
|
|
|
listLayout->removeItem(listLayout->groupLayout);
|
|
|
|
listLayout->removeItem(listLayout->friendLayouts[Online]);
|
2015-03-26 00:27:33 +08:00
|
|
|
if (top)
|
2015-03-23 17:38:24 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->addLayout(listLayout->groupLayout);
|
|
|
|
listLayout->addLayout(listLayout->friendLayouts[Online]);
|
2015-03-12 08:41:18 +08:00
|
|
|
}
|
2015-03-23 17:38:24 +08:00
|
|
|
else
|
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->addLayout(listLayout->friendLayouts[Online]);
|
|
|
|
listLayout->addLayout(listLayout->groupLayout);
|
2015-03-12 08:41:18 +08:00
|
|
|
}
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->addLayout(circleLayout);
|
2015-03-12 08:41:18 +08:00
|
|
|
}
|
|
|
|
|
2015-03-22 16:16:32 +08:00
|
|
|
QList<GenericChatroomWidget*> FriendListWidget::getAllFriends()
|
|
|
|
{
|
|
|
|
QList<GenericChatroomWidget*> friends;
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
for (int i = 0; i < listLayout->count(); ++i)
|
2015-03-22 16:16:32 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
QLayout* subLayout = listLayout->itemAt(i)->layout();
|
2015-03-22 16:16:32 +08:00
|
|
|
|
|
|
|
if(!subLayout)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < subLayout->count(); ++j)
|
|
|
|
{
|
|
|
|
GenericChatroomWidget* widget =
|
2015-05-28 08:45:23 +08:00
|
|
|
dynamic_cast<GenericChatroomWidget*>(subLayout->itemAt(j)->widget());
|
2015-03-22 16:16:32 +08:00
|
|
|
|
|
|
|
if(!widget)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
friends.append(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return friends;
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:26:43 +08:00
|
|
|
QVector<CircleWidget*> FriendListWidget::getAllCircles()
|
|
|
|
{
|
|
|
|
QVector<CircleWidget*> vec;
|
|
|
|
vec.reserve(circleLayout->count());
|
|
|
|
for (int i = 0; i < circleLayout->count(); ++i)
|
|
|
|
{
|
|
|
|
vec.push_back(dynamic_cast<CircleWidget*>(circleLayout->itemAt(i)->widget()));
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:58:44 +08:00
|
|
|
void FriendListWidget::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("friend"))
|
|
|
|
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);
|
|
|
|
|
|
|
|
FriendWidget *widget = f->getFriendWidget();
|
|
|
|
assert(widget != nullptr);
|
|
|
|
|
|
|
|
// Update old circle after moved.
|
|
|
|
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(widget->parent());
|
|
|
|
|
|
|
|
listLayout->addFriendWidget(widget, f->getStatus());
|
|
|
|
|
|
|
|
if (circleWidget != nullptr)
|
|
|
|
{
|
|
|
|
// In case the status was changed while moving, update both.
|
2015-05-31 03:10:43 +08:00
|
|
|
circleWidget->updateStatus();
|
2015-05-29 00:58:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
void FriendListWidget::moveWidget(FriendWidget *w, Status s, bool add)
|
2014-08-22 23:19:16 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(w->parent());
|
2015-05-28 01:17:12 +08:00
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
if (circleWidget == nullptr || add)
|
2015-03-23 17:38:24 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->addFriendWidget(w, s);
|
|
|
|
return;
|
2015-03-12 08:23:22 +08:00
|
|
|
}
|
2015-05-28 08:45:23 +08:00
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
circleWidget->addFriendWidget(w, s);
|
2014-08-22 23:19:16 +08:00
|
|
|
}
|
2015-06-03 18:26:48 +08:00
|
|
|
|
|
|
|
// update widget after add/delete/hide/show
|
|
|
|
void FriendListWidget::reDraw()
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
show();
|
|
|
|
resize(QSize()); //lifehack
|
|
|
|
}
|