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