b94329e7827eb0cd06be631c810262b73a14b97b
[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         Sami Rämö - sami.ramo@ixonos.com
9
10     Situare is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     version 2 as published by the Free Software Foundation.
13
14     Situare is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with Situare; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22     USA.
23 */
24
25 #include <QApplication>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QPushButton>
30
31 #include "coordinates/geocoordinate.h"
32 #include "friendlistitem.h"
33 #include "friendlistitemdelegate.h"
34 #include "friendlistview.h"
35 #include "panelcommon.h"
36 #include "user/user.h"
37
38 #include "friendlistpanel.h"
39
40 FriendListPanel::FriendListPanel(QWidget *parent)
41     : QWidget(parent),
42       m_mainWindowIsTopmost(false),
43       m_somePanelIsOpen(false)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
48     const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
49     const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
50     const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
51
52     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
53     friendListPanelLayout->setMargin(0);
54     friendListPanelLayout->setSpacing(0);
55     setLayout(friendListPanelLayout);
56
57     QHBoxLayout *filterLayout = new QHBoxLayout();
58     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, FRIENDPANEL_FILTER_MARGIN_TOP,
59                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, FRIENDPANEL_FILTER_MARGIN_BOTTOM);
60
61     m_friendListHeaderWidget = new QWidget();
62     m_friendListHeaderWidget->setLayout(filterLayout);
63     m_friendListHeaderWidget->setAutoFillBackground(true);
64
65     m_routeButton = new QPushButton(tr("Route to friend"));
66     m_routeButton->setDisabled(true);
67
68     QPalette labelPalette = m_friendListHeaderWidget->palette();
69     labelPalette.setColor(QPalette::Background, Qt::black);
70
71     m_friendListHeaderWidget->setPalette(labelPalette);
72     m_friendListHeaderWidget->hide();
73     m_friendListLabel = new QLabel(this);
74     m_clearFilterButton = new QPushButton(tr("Show all"));
75
76     filterLayout->addWidget(m_friendListLabel);
77     filterLayout->addWidget(m_clearFilterButton);
78
79     m_friendListView = new FriendListView(this);
80     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
81
82     QVBoxLayout *listViewLayout = new QVBoxLayout;
83     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
84                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
85     listViewLayout->addWidget(m_friendListView);
86
87     friendListPanelLayout->addWidget(m_routeButton);
88     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
89     friendListPanelLayout->addLayout(listViewLayout);
90
91     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
92             this, SIGNAL(findFriend(GeoCoordinate)));
93
94     connect(m_clearFilterButton, SIGNAL(clicked()),
95             this, SLOT(clearFiltering()));
96
97     connect(m_routeButton, SIGNAL(clicked()),
98             this, SLOT(routeToSelectedFriend()));
99
100     connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
101             this, SLOT(setRouteButtonDisabled()));
102
103     /// @todo remove old filterLayout when new panel are merged
104
105     //////////////////////////////////////////////////////////////////////////
106     // NOTE! Do not mix the new filtering layout below with the old one above
107     //////////////////////////////////////////////////////////////////////////
108
109     // filtering layout
110     QHBoxLayout *filteringLayout = new QHBoxLayout();
111     friendListPanelLayout->addLayout(filteringLayout);
112
113     // line edit for filtering
114     m_filterField = new QLineEdit;
115     filteringLayout->addWidget(m_filterField);
116
117     connect(m_filterField, SIGNAL(returnPressed()),
118             this, SLOT(clearTextFiltering()));
119
120     connect(m_filterField, SIGNAL(textChanged(QString)),
121             this, SLOT(filterTextChanged(QString)));
122
123     // button for clearing the filtering
124     m_filterClearButton = new QPushButton();
125     filteringLayout->addWidget(m_filterClearButton);
126     m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
127
128     connect(m_filterClearButton, SIGNAL(clicked()),
129             this, SLOT(clearTextFiltering()));
130
131     connect(qApp, SIGNAL(topmostWindowChanged(bool)),
132             this, SLOT(topmostWindowChanged(bool)));
133 }
134
135 void FriendListPanel::anyPanelClosed()
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     m_somePanelIsOpen = false;
140     updateKeyboardGrabbing();
141
142     clearFiltering();
143
144     m_friendListView->clearItemSelection();
145     setRouteButtonDisabled();
146 }
147
148 void FriendListPanel::anyPanelOpened()
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     m_somePanelIsOpen = true;
153     updateKeyboardGrabbing();
154 }
155
156 void FriendListPanel::clearFiltering()
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     m_friendListHeaderWidget->hide();
161     m_friendListView->clearFilter();
162     clearTextFiltering();
163 }
164
165 void FriendListPanel::clearTextFiltering()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     // clearing the filtering text field does cause also hiding the filtering layout
170     m_filterField->clear();
171 }
172
173 void FriendListPanel::filterTextChanged(const QString &text)
174 {
175     qDebug() << __PRETTY_FUNCTION__;
176
177     if (m_filterField->isHidden() && !text.isEmpty())
178         setFilteringLayoutVisibility(true);
179     else if (m_filterField->isVisible() && text.isEmpty())
180         setFilteringLayoutVisibility(false);
181
182     m_friendListView->filter(text);
183 }
184
185 void FriendListPanel::friendImageReady(User *user)
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
190
191     if (item)
192         item->setAvatarImage(user->profileImage());
193 }
194
195 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
196 {
197     qDebug() << __PRETTY_FUNCTION__;
198
199     QStringList newUserIDs;
200
201     foreach (User *user, friendList) {
202         FriendListItem *item = 0;
203         if (!m_friendListView->contains(user->userId())) {
204             item = new FriendListItem();
205             item->setUserData(user);
206             m_friendListView->addListItem(user->userId(), item);
207         } else {
208             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
209                     user->userId()));
210
211             if (item) {
212                 item->setUserData(user);
213                 m_friendListView->addListItemToView(item);
214             }
215         }
216
217         newUserIDs.append(user->userId());
218     }
219
220     m_friendListView->clearUnused(newUserIDs);
221 }
222
223 void FriendListPanel::hideEvent(QHideEvent *event)
224 {
225     qDebug() << __PRETTY_FUNCTION__;
226
227     QWidget::hideEvent(event);
228     updateKeyboardGrabbing();
229     clearFiltering();
230
231     m_friendListView->clearItemSelection();
232     setRouteButtonDisabled();
233 }
234
235 void FriendListPanel::routeToSelectedFriend()
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
240
241     if (item)
242         emit routeToFriend(item->coordinates());
243 }
244
245 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_filterField->setVisible(visible);
250     m_filterClearButton->setVisible(visible);
251 }
252
253 void FriendListPanel::updateKeyboardGrabbing()
254 {
255     qDebug() << __PRETTY_FUNCTION__;
256
257     if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
258         if (QWidget::keyboardGrabber() == m_filterField)
259             m_filterField->releaseKeyboard();
260     } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
261         if (QWidget::keyboardGrabber() != m_filterField)
262             m_filterField->grabKeyboard();
263     }
264 }
265
266 void FriendListPanel::setRouteButtonDisabled()
267 {
268     qDebug() << __PRETTY_FUNCTION__;
269
270     m_routeButton->setDisabled(m_friendListView->selectedItems().isEmpty());
271 }
272
273 void FriendListPanel::showEvent(QShowEvent *event)
274 {
275     qDebug() << __PRETTY_FUNCTION__;
276
277     QWidget::showEvent(event);
278     updateKeyboardGrabbing();
279     setFilteringLayoutVisibility(false);
280 }
281
282 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
283 {
284     qDebug() << __PRETTY_FUNCTION__;
285
286     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
287
288     m_friendListHeaderWidget->show();
289     m_friendListView->filter(userIDs);
290
291     clearTextFiltering();
292
293     emit showPanelRequested(this);
294 }
295
296 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
297 {
298     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
299
300     m_mainWindowIsTopmost = mainWindowIsTopmost;
301     updateKeyboardGrabbing();
302 }