Removed a few constants from panelcommon.h (that really weren't common at all) and...
[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 "coordinates/geocoordinate.h"
29 #include "listview.h"
30 #include "friendlistitem.h"
31 #include "friendlistitemdelegate.h"
32 #include "panelcommon.h"
33
34 #include "friendlistpanel.h"
35
36 FriendListPanel::FriendListPanel(QWidget *parent)
37     : QWidget(parent)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
42     const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
43
44     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
45     friendListPanelLayout->setMargin(0);
46     friendListPanelLayout->setSpacing(0);
47 ///< @todo magic
48     friendListPanelLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
49     setLayout(friendListPanelLayout);
50
51     QHBoxLayout *filterLayout = new QHBoxLayout();
52 ///< @todo magic
53     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
54                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
55
56     m_friendListHeaderWidget = new QWidget();
57     m_friendListHeaderWidget->setLayout(filterLayout);
58     m_friendListHeaderWidget->setAutoFillBackground(true);
59
60     QPalette labelPalette = m_friendListHeaderWidget->palette();
61     labelPalette.setColor(QPalette::Background, Qt::black);
62
63     m_friendListHeaderWidget->setPalette(labelPalette);
64     m_friendListHeaderWidget->hide();
65     m_friendListLabel = new QLabel(this);
66     m_clearFilterButton = new QPushButton(tr("Show all"));
67
68     filterLayout->addWidget(m_friendListLabel);
69     filterLayout->addWidget(m_clearFilterButton);
70
71     m_friendListView = new ListView(this);
72
73     m_friendListView->setAutoFillBackground(false);
74     m_friendListView->viewport()->setAutoFillBackground(false);
75     m_friendListItemDelegate = new FriendListItemDelegate();
76     m_friendListView->setItemDelegate(m_friendListItemDelegate);
77
78     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
79     friendListPanelLayout->addWidget(m_friendListView);
80
81     connect(m_friendListView, SIGNAL(listItemClicked(GeoCoordinate)),
82             this, SIGNAL(findFriend(GeoCoordinate)));
83
84     connect(m_clearFilterButton, SIGNAL(clicked()),
85             this, SLOT(clearFriendListFilter()));
86 }
87
88 void FriendListPanel::friendImageReady(User *user)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
93
94     if (item)
95         item->setAvatarImage(user->profileImage());
96 }
97
98 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     QStringList newUserIDs;
103
104     foreach (User *user, friendList) {
105         FriendListItem *item = 0;
106         if (!m_friendListView->contains(user->userId())) {
107             item = new FriendListItem();
108             item->setUserData(user);
109             m_friendListView->addListItem(user->userId(), item);
110         } else {
111             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
112                     user->userId()));
113
114             if (item) {
115                 item->setUserData(user);
116                 m_friendListView->addListItemToView(item);
117             }
118         }
119
120         newUserIDs.append(user->userId());
121     }
122
123     m_friendListView->clearUnused(newUserIDs);
124 }
125
126 void FriendListPanel::clearFriendListFilter()
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     m_friendListHeaderWidget->hide();
131     m_friendListView->clearFilter();
132 }
133
134 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
139
140     m_friendListHeaderWidget->show();
141     m_friendListView->filter(userIDs);
142
143     emit showPanelRequested(this);
144 }