Merge branch 'master' into listview
[situare] / src / ui / friendlistpanel.cpp
1  /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Henri Lampela - henri.lampela@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21  */
22
23 #include "friendlistpanel.h"
24 #include "listview.h"
25 #include "friendlistitem.h"
26 #include "friendlistitemdelegate.h"
27 #include "panelcommon.h"
28 #include "sidepanel.h"
29
30 FriendListPanel::FriendListPanel(QWidget *parent)
31     : SidePanel(parent)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34     setType(SidePanel::FriendPanel);
35
36     QHBoxLayout *filterLayout = new QHBoxLayout;
37     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
38                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
39     m_friendListHeaderWidget = new QWidget();
40     m_friendListHeaderWidget->setLayout(filterLayout);
41     m_friendListHeaderWidget->setAutoFillBackground(true);
42     QPalette labelPalette = m_friendListHeaderWidget->palette();
43     labelPalette.setColor(QPalette::Background, Qt::black);
44     m_friendListHeaderWidget->setPalette(labelPalette);
45     m_friendListHeaderWidget->hide();
46     m_friendListLabel = new QLabel(this);
47     m_clearFilterButton = new QPushButton(tr("Show all"));
48     filterLayout->addWidget(m_friendListLabel);
49     filterLayout->addWidget(m_clearFilterButton);
50     m_panelVBox->addWidget(m_friendListHeaderWidget);
51
52     QHBoxLayout *friendListLayout =  new QHBoxLayout;
53     friendListLayout->setContentsMargins(FRIENDPANEL_MARGIN_LEFT, FRIENDPANEL_MARGIN_TOP,
54                                          FRIENDPANEL_MARGIN_RIGHT, FRIENDPANEL_MARGIN_BOTTOM);
55
56     m_friendListView = new ListView(this);
57     m_friendListView->setAutoFillBackground(false);
58     m_friendListView->viewport()->setAutoFillBackground(false);
59     m_friendListItemDelegate = new FriendListItemDelegate();
60     m_friendListView->setItemDelegate(m_friendListItemDelegate);
61
62     friendListLayout->addWidget(m_friendListView);
63     m_panelVBox->addLayout(friendListLayout);
64
65     connect(m_friendListView, SIGNAL(listItemClicked(QPointF)),
66             this, SIGNAL(findFriend(QPointF)));
67
68     connect(m_clearFilterButton, SIGNAL(clicked()),
69             this, SLOT(clearFriendListFilter()));
70     connect(this, SIGNAL(panelOpened()),
71             this, SLOT(clearFriendListFilter()));
72 }
73
74 void FriendListPanel::friendImageReady(User *user)
75 {
76     qDebug() << __PRETTY_FUNCTION__;
77
78     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
79
80     if (item)
81         item->setAvatarImage(user->profileImage());
82 }
83
84 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     QStringList newUserIDs;
89
90     foreach (User *user, friendList) {
91         FriendListItem *item = 0;
92         if (!m_friendListView->contains(user->userId())) {
93             item = new FriendListItem();
94             item->setUserData(user);
95             m_friendListView->addListItem(user->userId(), item);
96         }
97         else {
98             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
99                     user->userId()));
100
101             if (item) {
102                 item->setUserData(user);
103                 m_friendListView->addListItemToView(item);
104             }
105         }
106
107         newUserIDs.append(user->userId());
108     }
109
110     m_friendListView->clearUnused(newUserIDs);
111 }
112
113 void FriendListPanel::clearFriendListFilter()
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     m_friendListHeaderWidget->hide();
118     m_friendListView->clearFilter();
119 }
120
121 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
122 {
123     qDebug() << __PRETTY_FUNCTION__;
124
125     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
126
127     openPanel();
128     m_friendListHeaderWidget->show();
129     m_friendListView->filter(userIDs);
130 }