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

133 lines
3.7 KiB
C++
Raw Normal View History

2014-08-22 23:19:16 +08:00
/*
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#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"
2014-08-22 23:19:16 +08:00
FriendListWidget::FriendListWidget(QWidget *parent, bool groupchatPosition) :
2014-08-22 23:19:16 +08:00
QWidget(parent)
{
mainLayout = new QGridLayout();
setLayout(mainLayout);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
layout()->setSpacing(0);
layout()->setMargin(0);
groupLayout = new QVBoxLayout();
groupLayout->setSpacing(0);
groupLayout->setMargin(0);
for (Status s : {Status::Online, Status::Offline})
2014-08-22 23:19:16 +08:00
{
QVBoxLayout *l = new QVBoxLayout();
2014-08-22 23:19:16 +08:00
l->setSpacing(0);
l->setMargin(0);
layouts[static_cast<int>(s)] = l;
}
2015-03-26 00:27:33 +08:00
if (groupchatPosition)
2015-03-23 17:38:24 +08:00
{
mainLayout->addLayout(groupLayout, 0, 0);
mainLayout->addLayout(layouts[static_cast<int>(Status::Online)], 1, 0);
mainLayout->addLayout(layouts[static_cast<int>(Status::Offline)], 2, 0);
}
2015-03-23 17:38:24 +08:00
else
{
mainLayout->addLayout(layouts[static_cast<int>(Status::Online)], 0, 0);
mainLayout->addLayout(groupLayout, 1, 0);
mainLayout->addLayout(layouts[static_cast<int>(Status::Offline)], 2, 0);
}
2014-08-22 23:19:16 +08:00
}
QVBoxLayout* FriendListWidget::getGroupLayout()
2014-08-22 23:19:16 +08:00
{
return groupLayout;
}
QVBoxLayout* FriendListWidget::getFriendLayout(Status s)
2014-08-22 23:19:16 +08:00
{
auto res = layouts.find(static_cast<int>(s));
if (res != layouts.end())
return res.value();
return layouts[static_cast<int>(Status::Online)];
}
void FriendListWidget::onGroupchatPositionChanged(bool top)
{
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
{
mainLayout->addLayout(groupLayout, 0, 0);
mainLayout->addLayout(layouts[static_cast<int>(Status::Online)], 1, 0);
}
2015-03-23 17:38:24 +08:00
else
{
mainLayout->addLayout(layouts[static_cast<int>(Status::Online)], 0, 0);
mainLayout->addLayout(groupLayout, 1, 0);
}
}
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 =
reinterpret_cast<GenericChatroomWidget*>(subLayout->itemAt(j)->widget());
if(!widget)
continue;
friends.append(widget);
}
}
return friends;
}
void FriendListWidget::moveWidget(QWidget *w, Status s)
2014-08-22 23:19:16 +08:00
{
QVBoxLayout* l = getFriendLayout(s);
2015-03-23 17:38:24 +08:00
l->removeWidget(w);
Friend* g = FriendList::findFriend(dynamic_cast<FriendWidget*>(w)->friendId);
2015-03-26 00:27:33 +08:00
for (int i = 0; i < l->count(); i++)
2015-03-23 17:38:24 +08:00
{
FriendWidget* w1 = dynamic_cast<FriendWidget*>(l->itemAt(i)->widget());
2015-03-26 00:27:33 +08:00
if (w1 != NULL)
2015-03-23 17:38:24 +08:00
{
Friend* f = FriendList::findFriend(w1->friendId);
2015-03-26 00:27:33 +08:00
if (f->getDisplayedName().localeAwareCompare(g->getDisplayedName()) > 0)
2015-03-23 17:38:24 +08:00
{
l->insertWidget(i,w);
return;
}
}
}
l->addWidget(w);
2014-08-22 23:19:16 +08:00
}