Re-factored the source to use the new coordinate classes
[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 "coordinates/geocoordinate.h"
24 #include "friendlistview.h"
25 #include "friendlistitem.h"
26 #include "panelcommon.h"
27 #include "sidepanel.h"
28
29 #include "friendlistpanel.h"
30
31 FriendListPanel::FriendListPanel(QWidget *parent)
32     : SidePanel(parent)
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35     setType(SidePanel::FriendPanel);
36
37     QHBoxLayout *filterLayout = new QHBoxLayout;
38     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
39                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
40     m_friendListHeaderWidget = new QWidget();
41     m_friendListHeaderWidget->setLayout(filterLayout);
42     m_friendListHeaderWidget->setAutoFillBackground(true);
43     QPalette labelPalette = m_friendListHeaderWidget->palette();
44     labelPalette.setColor(QPalette::Background, Qt::black);
45     m_friendListHeaderWidget->setPalette(labelPalette);
46     m_friendListHeaderWidget->hide();
47     m_friendListLabel = new QLabel(this);
48     m_clearFilterButton = new QPushButton(tr("Show all"));
49     filterLayout->addWidget(m_friendListLabel);
50     filterLayout->addWidget(m_clearFilterButton);
51     m_panelVBox->addWidget(m_friendListHeaderWidget);
52
53     QHBoxLayout *friendListLayout =  new QHBoxLayout;
54     friendListLayout->setContentsMargins(FRIENDPANEL_MARGIN_LEFT, FRIENDPANEL_MARGIN_TOP,
55                                          FRIENDPANEL_MARGIN_RIGHT, FRIENDPANEL_MARGIN_BOTTOM);
56     m_friendListView = new FriendListView(this);
57     QScrollArea *friendListScroll = new QScrollArea(this);
58     friendListScroll->setWidgetResizable(false);
59     friendListScroll->setWidget(m_friendListView);
60     friendListScroll->viewport()->setAutoFillBackground(false);
61     friendListScroll->widget()->setAutoFillBackground(false);
62     friendListLayout->addWidget(friendListScroll);
63     m_panelVBox->addLayout(friendListLayout);
64
65     connect(m_clearFilterButton, SIGNAL(clicked()),
66             this, SLOT(clearFriendListFilter()));
67     connect(this, SIGNAL(panelOpened()),
68             this, SLOT(clearFriendListFilter()));
69 }
70
71 void FriendListPanel::friendImageReady(User *user)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     FriendListItem *item = m_friendListView->widget(user->userId());
76     item->setAvatarImage(user->profileImage());
77 }
78
79 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     QStringList newUserIDs;
84
85     foreach (User *user, friendList) {
86         FriendListItem *item = 0;
87         if (!m_friendListView->contains(user->userId())) {
88             item = new FriendListItem(m_friendListView);
89             item->setData(user);
90             m_friendListView->addWidget(user->userId(), item);
91
92             connect(item, SIGNAL(findFriend(GeoCoordinate)),
93                 this, SIGNAL(findFriend(GeoCoordinate)));
94         }
95         else {
96             item = m_friendListView->takeWidgetFromView(user->userId());
97             if (item) {
98                 item->setData(user);
99                 m_friendListView->addWidgetToView(item);
100             }
101         }
102
103         newUserIDs.append(user->userId());
104     }
105
106     m_friendListView->clearUnused(newUserIDs);
107 }
108
109 void FriendListPanel::clearFriendListFilter()
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     m_friendListHeaderWidget->hide();
114     m_friendListView->clearFilter();
115 }
116
117 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
118 {
119     qDebug() << __PRETTY_FUNCTION__;
120
121     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
122
123     openPanel();
124     m_friendListHeaderWidget->show();
125     m_friendListView->filter(userIDs);
126 }