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

202 lines
5.6 KiB
C++
Raw Normal View History

2014-08-22 23:19:16 +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.
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.
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-08-22 23:19:16 +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"
#include <QDebug>
2014-09-11 21:44:34 +08:00
#include <QGridLayout>
#include "src/friend.h"
#include "src/friendlist.h"
#include "src/widget/friendwidget.h"
2015-05-28 01:17:12 +08:00
#include "groupwidget.h"
#include "circlewidget.hpp"
#include <cassert>
2014-08-22 23:19:16 +08:00
FriendListWidget::FriendListWidget(QWidget *parent, bool groupchatPosition) :
2014-08-22 23:19:16 +08:00
QWidget(parent)
{
2015-05-28 01:17:12 +08:00
mainLayout = new QVBoxLayout();
2014-08-22 23:19:16 +08:00
setLayout(mainLayout);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
layout()->setSpacing(0);
layout()->setMargin(0);
groupLayout = new QVBoxLayout();
groupLayout->setSpacing(0);
groupLayout->setMargin(0);
2015-05-28 01:17:12 +08:00
friendLayouts[Online] = new QVBoxLayout();
friendLayouts[Online]->setSpacing(0);
friendLayouts[Online]->setMargin(0);
2014-08-22 23:19:16 +08:00
2015-05-28 01:17:12 +08:00
friendLayouts[Offline] = new QVBoxLayout();
friendLayouts[Offline]->setSpacing(0);
friendLayouts[Offline]->setMargin(0);
circleLayout = new QVBoxLayout();
circleLayout->setSpacing(0);
circleLayout->setMargin(0);
2014-08-22 23:19:16 +08:00
2015-03-26 00:27:33 +08:00
if (groupchatPosition)
2015-03-23 17:38:24 +08:00
{
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(groupLayout);
mainLayout->addLayout(friendLayouts[Online]);
mainLayout->addLayout(friendLayouts[Offline]);
}
2015-03-23 17:38:24 +08:00
else
{
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(friendLayouts[Online]);
mainLayout->addLayout(groupLayout);
mainLayout->addLayout(friendLayouts[Offline]);
}
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(circleLayout);
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 01:17:12 +08:00
groupLayout->addWidget(widget);
2014-08-22 23:19:16 +08:00
}
2015-05-28 01:17:12 +08:00
void FriendListWidget::hideGroups(QString searchString, bool hideAll)
2014-08-22 23:19:16 +08:00
{
2015-05-28 01:17:12 +08:00
QVBoxLayout* groups = groupLayout;
int groupCount = groups->count(), index;
for (index = 0; index<groupCount; index++)
{
GroupWidget* groupWidget = static_cast<GroupWidget*>(groups->itemAt(index)->widget());
QString groupName = groupWidget->getName();
if (!groupName.contains(searchString, Qt::CaseInsensitive) | hideAll)
groupWidget->setVisible(false);
else
groupWidget->setVisible(true);
}
}
2014-08-22 23:19:16 +08:00
2015-05-28 01:17:12 +08:00
void FriendListWidget::addCircleWidget(CircleWidget *widget)
{
circleLayout->addWidget(widget);
}
void FriendListWidget::hideFriends(QString searchString, Status status, bool hideAll)
{
QVBoxLayout* friends = getFriendLayout(status);
int friendCount = friends->count(), index;
for (index = 0; index<friendCount; index++)
{
FriendWidget* friendWidget = static_cast<FriendWidget*>(friends->itemAt(index)->widget());
QString friendName = friendWidget->getName();
if (!friendName.contains(searchString, Qt::CaseInsensitive) | hideAll)
friendWidget->setVisible(false);
else
friendWidget->setVisible(true);
}
}
QVBoxLayout* FriendListWidget::getFriendLayout(Status s)
{
if (s == Status::Offline)
{
return friendLayouts[Offline];
}
return friendLayouts[Online];
2014-08-22 23:19:16 +08:00
}
void FriendListWidget::onGroupchatPositionChanged(bool top)
{
2015-05-28 01:17:12 +08:00
mainLayout->removeItem(circleLayout);
mainLayout->removeItem(groupLayout);
mainLayout->removeItem(getFriendLayout(Status::Online));
2015-03-26 00:27:33 +08:00
if (top)
2015-03-23 17:38:24 +08:00
{
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(groupLayout);
mainLayout->addLayout(friendLayouts[Online]);
}
2015-03-23 17:38:24 +08:00
else
{
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(friendLayouts[Online]);
mainLayout->addLayout(groupLayout);
}
2015-05-28 01:17:12 +08:00
mainLayout->addLayout(circleLayout);
}
QList<GenericChatroomWidget*> FriendListWidget::getAllFriends()
{
QList<GenericChatroomWidget*> friends;
for (int i = 0; i < mainLayout->count(); ++i)
{
QLayout* subLayout = mainLayout->itemAt(i)->layout();
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());
if(!widget)
continue;
friends.append(widget);
}
}
return friends;
}
2015-05-28 08:45:23 +08:00
void FriendListWidget::moveWidget(QWidget *w, Status s)
2014-08-22 23:19:16 +08:00
{
QVBoxLayout* l = getFriendLayout(s);
2015-05-28 01:17:12 +08:00
l->removeWidget(w); // In case the widget is already in this layout.
2015-05-28 08:45:23 +08:00
Friend* g = FriendList::findFriend(dynamic_cast<FriendWidget*>(w)->friendId);
2015-05-28 01:17:12 +08:00
// Binary search.
int min = 0, max = l->count(), mid;
while (min < max)
2015-03-23 17:38:24 +08:00
{
2015-05-28 01:17:12 +08:00
mid = (max - min) / 2 + min;
2015-05-28 08:45:23 +08:00
FriendWidget* w1 = dynamic_cast<FriendWidget*>(l->itemAt(mid)->widget());
2015-05-28 01:17:12 +08:00
assert(w1 != nullptr);
2015-06-08 02:24:55 +08:00
Friend* f = FriendList::findFriend(w1->friendId);
2015-05-28 01:17:12 +08:00
int compareValue = f->getDisplayedName().localeAwareCompare(g->getDisplayedName());
if (compareValue > 0)
2015-05-28 08:45:23 +08:00
{
2015-05-28 01:17:12 +08:00
max = mid;
2015-05-28 08:45:23 +08:00
}
2015-05-28 01:17:12 +08:00
else
2015-05-28 08:45:23 +08:00
{
2015-05-28 01:17:12 +08:00
min = mid + 1;
2015-05-28 08:45:23 +08:00
}
}
2015-05-28 08:45:23 +08:00
2015-05-28 01:17:12 +08:00
l->insertWidget(min, w);
2014-08-22 23:19:16 +08:00
}
// update widget after add/delete/hide/show
void FriendListWidget::reDraw()
{
hide();
show();
resize(QSize()); //lifehack
}