Added new turn type images.
[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 <QHBoxLayout>
25 #include <QLabel>
26 #include <QPushButton>
27
28 #include "../user/user.h"
29 #include "coordinates/geocoordinate.h"
30 #include "friendlistitem.h"
31 #include "friendlistitemdelegate.h"
32 #include "friendlistview.h"
33 #include "panelcommon.h"
34
35 #include "friendlistpanel.h"
36
37 FriendListPanel::FriendListPanel(QWidget *parent)
38     : QWidget(parent)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
43     friendListPanelLayout->setMargin(0);
44     friendListPanelLayout->setSpacing(0);
45     setLayout(friendListPanelLayout);
46
47     QHBoxLayout *filterLayout = new QHBoxLayout();
48 ///< @todo magic
49     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
50                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
51
52     m_friendListHeaderWidget = new QWidget();
53     m_friendListHeaderWidget->setLayout(filterLayout);
54     m_friendListHeaderWidget->setAutoFillBackground(true);
55
56     m_routeButton = new QPushButton("Route to friend");
57
58     QPalette labelPalette = m_friendListHeaderWidget->palette();
59     labelPalette.setColor(QPalette::Background, Qt::black);
60
61     m_friendListHeaderWidget->setPalette(labelPalette);
62     m_friendListHeaderWidget->hide();
63     m_friendListLabel = new QLabel(this);
64     m_clearFilterButton = new QPushButton(tr("Show all"));
65
66     filterLayout->addWidget(m_friendListLabel);
67     filterLayout->addWidget(m_clearFilterButton);
68
69     m_friendListView = new FriendListView(this);
70     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
71
72     QVBoxLayout *listViewLayout = new QVBoxLayout;
73     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
74     listViewLayout->addWidget(m_friendListView);
75
76     friendListPanelLayout->addWidget(m_routeButton);
77     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
78     friendListPanelLayout->addLayout(listViewLayout);
79
80     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
81             this, SIGNAL(findFriend(GeoCoordinate)));
82
83     connect(m_clearFilterButton, SIGNAL(clicked()),
84             this, SLOT(clearFriendListFilter()));
85
86     connect(m_routeButton, SIGNAL(clicked()),
87             this, SLOT(routeToSelectedFriend()));
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::routeToSelectedFriend()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
141
142     if (item)
143         emit routeToFriend(item->coordinates());
144 }
145
146 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
147 {
148     qDebug() << __PRETTY_FUNCTION__;
149
150     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
151
152     m_friendListHeaderWidget->show();
153     m_friendListView->filter(userIDs);
154 }