ExtendedListItem code cleanup, removed panel FriendListPanel
[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
83     connect(m_locationListView, SIGNAL(locationItemClicked(GeoCoordinate&,GeoCoordinate&)),
84             this, SIGNAL(locationItemClicked(GeoCoordinate&,GeoCoordinate&)));
85
86     connect(this, SIGNAL(panelOpened()),
87             this, SLOT(clearFriendListFilter()));
88 }
89
90 void FriendListPanel::friendImageReady(User *user)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
95
96     if (item)
97         item->setAvatarImage(user->profileImage());
98 }
99
100 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     QStringList newUserIDs;
105
106     foreach (User *user, friendList) {
107         FriendListItem *item = 0;
108         if (!m_friendListView->contains(user->userId())) {
109             item = new FriendListItem();
110             item->setUserData(user);
111             m_friendListView->addListItem(user->userId(), item);
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
136 void FriendListPanel::locationDataReady(QList<Location> &result)
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     if (result.size() == 0)
141         return;
142
143     m_locationListView->clearList();
144
145     for (int i = 0; i < result.size(); ++i) {
146         LocationListItem *item = new LocationListItem();
147         item->setLocationData(result.at(i));
148         m_locationListView->addListItem(QString::number(i), item);
149     }
150
151     openPanel();
152
153     if (result.size() == 1) {
154         ListItem *item = m_locationListView->listItem(QString("0"));
155
156         if (item)
157             m_locationListView->listItemClicked(item);
158     }
159 }
160
161 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
166
167     openPanel();
168     m_friendListHeaderWidget->show();
169     m_friendListView->filter(userIDs);
170 }