Added new class ExtendedListitem.
[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 "listview.h"
25 #include "friendlistitem.h"
26 #include "friendlistitemdelegate.h"
27 #include "panelcommon.h"
28 #include "sidepanel.h"
29 #include "../routing/location.h"
30 #include "locationlistitem.h"
31
32 #include "friendlistpanel.h"
33
34 FriendListPanel::FriendListPanel(QWidget *parent)
35     : SidePanel(parent)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38     setType(SidePanel::FriendPanel);
39
40     QHBoxLayout *filterLayout = new QHBoxLayout;
41     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
42                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
43     m_friendListHeaderWidget = new QWidget();
44     m_friendListHeaderWidget->setLayout(filterLayout);
45     m_friendListHeaderWidget->setAutoFillBackground(true);
46     QPalette labelPalette = m_friendListHeaderWidget->palette();
47     labelPalette.setColor(QPalette::Background, Qt::black);
48     m_friendListHeaderWidget->setPalette(labelPalette);
49     m_friendListHeaderWidget->hide();
50     m_friendListLabel = new QLabel(this);
51     m_clearFilterButton = new QPushButton(tr("Show all"));
52     filterLayout->addWidget(m_friendListLabel);
53     filterLayout->addWidget(m_clearFilterButton);
54     m_panelVBox->addWidget(m_friendListHeaderWidget);
55
56     QHBoxLayout *friendListLayout =  new QHBoxLayout;
57     friendListLayout->setContentsMargins(FRIENDPANEL_MARGIN_LEFT, FRIENDPANEL_MARGIN_TOP,
58                                          FRIENDPANEL_MARGIN_RIGHT, FRIENDPANEL_MARGIN_BOTTOM);
59
60     m_friendListView = new ListView(this);
61     m_friendListView->setAutoFillBackground(false);
62     m_friendListView->viewport()->setAutoFillBackground(false);
63     m_friendListItemDelegate = new FriendListItemDelegate();
64     m_friendListView->setItemDelegate(m_friendListItemDelegate);
65
66     m_locationListView = new ListView(this);
67     m_locationListView->setAutoFillBackground(false);
68     m_locationListView->viewport()->setAutoFillBackground(false);
69     m_locationListView->setItemDelegate(m_friendListItemDelegate);
70
71     friendListLayout->addWidget(m_friendListView);
72     m_panelVBox->addLayout(friendListLayout);
73     m_panelVBox->addWidget(m_locationListView);
74
75     connect(m_friendListView, SIGNAL(listItemClicked(GeoCoordinate)),
76             this, SIGNAL(findFriend(GeoCoordinate)));
77
78     connect(m_clearFilterButton, SIGNAL(clicked()),
79             this, SLOT(clearFriendListFilter()));
80     connect(this, SIGNAL(panelOpened()),
81             this, SLOT(clearFriendListFilter()));
82 }
83
84 void FriendListPanel::friendImageReady(User *user)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
89
90     if (item)
91         item->setAvatarImage(user->profileImage());
92 }
93
94 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     QStringList newUserIDs;
99
100     foreach (User *user, friendList) {
101         FriendListItem *item = 0;
102         if (!m_friendListView->contains(user->userId())) {
103             item = new FriendListItem();
104             item->setUserData(user);
105             m_friendListView->addListItem(user->userId(), item);
106         }
107         else {
108             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
109                     user->userId()));
110
111             if (item) {
112                 item->setUserData(user);
113                 m_friendListView->addListItemToView(item);
114             }
115         }
116
117         newUserIDs.append(user->userId());
118     }
119
120     m_friendListView->clearUnused(newUserIDs);
121 }
122
123 void FriendListPanel::clearFriendListFilter()
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     m_friendListHeaderWidget->hide();
128     m_friendListView->clearFilter();
129 }
130
131 void FriendListPanel::locationDataReady(QList<Location> &result)
132 {
133     qWarning() << __PRETTY_FUNCTION__;
134
135     for (int i = 0; i < result.size(); ++i) {
136         LocationListItem *item = new LocationListItem();
137         item->setLocationData(result.at(i));
138         m_locationListView->addListItem(QString::number(i), item);
139     }
140
141
142 }
143
144 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
149
150     openPanel();
151     m_friendListHeaderWidget->show();
152     m_friendListView->filter(userIDs);
153 }