Added RouteWaypointList- and RouteWaypointListView 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         Pekka Nissinen - pekka.nissinen@ixonos.com
8
9     Situare is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License
11     version 2 as published by the Free Software Foundation.
12
13     Situare is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with Situare; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21     USA.
22  */
23
24 #include "../user/user.h"
25 #include "coordinates/geocoordinate.h"
26 #include "friendlistitem.h"
27 #include "friendlistitemdelegate.h"
28 #include "friendlistview.h"
29 #include "panelcommon.h"
30
31 #include "friendlistpanel.h"
32
33 FriendListPanel::FriendListPanel(QWidget *parent)
34     : QWidget(parent)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37
38     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
39     friendListPanelLayout->setMargin(0);
40     friendListPanelLayout->setSpacing(0);
41     setLayout(friendListPanelLayout);
42
43     QHBoxLayout *filterLayout = new QHBoxLayout();
44     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
45                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
46
47     m_friendListHeaderWidget = new QWidget();
48     m_friendListHeaderWidget->setLayout(filterLayout);
49     m_friendListHeaderWidget->setAutoFillBackground(true);
50
51     m_routeButton = new QPushButton("Route to friend");
52
53     QPalette labelPalette = m_friendListHeaderWidget->palette();
54     labelPalette.setColor(QPalette::Background, Qt::black);
55
56     m_friendListHeaderWidget->setPalette(labelPalette);
57     m_friendListHeaderWidget->hide();
58     m_friendListLabel = new QLabel(this);
59     m_clearFilterButton = new QPushButton(tr("Show all"));
60
61     filterLayout->addWidget(m_friendListLabel);
62     filterLayout->addWidget(m_clearFilterButton);
63
64     m_friendListView = new FriendListView(this);
65     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
66
67     QVBoxLayout *listViewLayout = new QVBoxLayout;
68     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
69     listViewLayout->addWidget(m_friendListView);
70
71     friendListPanelLayout->addWidget(m_routeButton);
72     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
73     friendListPanelLayout->addLayout(listViewLayout);
74
75     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
76             this, SIGNAL(findFriend(GeoCoordinate)));
77
78     connect(m_clearFilterButton, SIGNAL(clicked()),
79             this, SLOT(clearFriendListFilter()));
80
81     connect(m_routeButton, SIGNAL(clicked()),
82             this, SLOT(routeToSelectedFriend()));
83 }
84
85 void FriendListPanel::friendImageReady(User *user)
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
90
91     if (item)
92         item->setAvatarImage(user->profileImage());
93 }
94
95 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     QStringList newUserIDs;
100
101     foreach (User *user, friendList) {
102         FriendListItem *item = 0;
103         if (!m_friendListView->contains(user->userId())) {
104             item = new FriendListItem();
105             item->setUserData(user);
106             m_friendListView->addListItem(user->userId(), item);
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::routeToSelectedFriend()
132 {
133     qDebug() << __PRETTY_FUNCTION__;
134
135     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
136
137     if (item)
138         emit routeToFriend(item->coordinates());
139 }
140
141 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
146
147 //    openPanel();
148     m_friendListHeaderWidget->show();
149     m_friendListView->filter(userIDs);
150 }