Merge branch 'new_panels_with_context_buttons' of https://vcs.maemo.org/git/situare...
[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 "imagebutton.h"
36 #include "panelcommon.h"
37 #include "user/user.h"
38
39 #include "friendlistpanel.h"
40
41 FriendListPanel::FriendListPanel(QWidget *parent)
42     : PanelBase(parent),
43       m_mainWindowIsTopmost(false),
44       m_somePanelIsOpen(false)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
49     const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
50     const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
51     const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
52
53     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
54     friendListPanelLayout->setMargin(0);
55     friendListPanelLayout->setSpacing(0);
56     setLayout(friendListPanelLayout);
57
58     QHBoxLayout *filterLayout = new QHBoxLayout();
59     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, FRIENDPANEL_FILTER_MARGIN_TOP,
60                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, FRIENDPANEL_FILTER_MARGIN_BOTTOM);
61
62     m_friendListHeaderWidget = new QWidget();
63     m_friendListHeaderWidget->setLayout(filterLayout);
64     m_friendListHeaderWidget->setAutoFillBackground(true);
65
66     m_routeButton = new ImageButton(":res/images/route_to_friend.png",
67                                     ":res/images/route_to_friend_s.png", "", this);
68     m_routeButton->setDisabled(true);
69
70     QPalette labelPalette = m_friendListHeaderWidget->palette();
71     labelPalette.setColor(QPalette::Background, Qt::black);
72
73     m_friendListHeaderWidget->setPalette(labelPalette);
74     m_friendListHeaderWidget->hide();
75     m_friendListLabel = new QLabel(this);
76     m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
77                                                   ":res/images/filtered_s.png", "", this);
78
79     m_clearGroupFilteringButton->setCheckable(true);
80     m_clearGroupFilteringButton->setDisabled(true);
81
82     filterLayout->addWidget(m_friendListLabel);
83     filterLayout->addWidget(m_clearGroupFilteringButton);
84
85     m_friendListView = new FriendListView(this);
86     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
87
88     QVBoxLayout *listViewLayout = new QVBoxLayout;
89     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
90                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
91     listViewLayout->addWidget(m_friendListView);
92
93     friendListPanelLayout->addWidget(m_routeButton);
94     friendListPanelLayout->addWidget(m_friendListHeaderWidget);
95     friendListPanelLayout->addLayout(listViewLayout);
96
97     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
98             this, SIGNAL(findFriend(GeoCoordinate)));
99
100     connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
101             this, SLOT(clearFiltering()));
102
103     connect(m_routeButton, SIGNAL(clicked()),
104             this, SLOT(routeToSelectedFriend()));
105
106     connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
107             this, SLOT(setRouteButtonDisabled()));
108
109     /// @todo remove old filterLayout when new panel are merged
110
111     //////////////////////////////////////////////////////////////////////////
112     // NOTE! Do not mix the new filtering layout below with the old one above
113     //////////////////////////////////////////////////////////////////////////
114
115     // filtering layout
116     QHBoxLayout *filteringLayout = new QHBoxLayout();
117     friendListPanelLayout->addLayout(filteringLayout);
118
119     // line edit for filtering
120     m_filterField = new QLineEdit;
121     filteringLayout->addWidget(m_filterField);
122
123     connect(m_filterField, SIGNAL(returnPressed()),
124             this, SLOT(clearTextFiltering()));
125
126     connect(m_filterField, SIGNAL(textChanged(QString)),
127             this, SLOT(filterTextChanged(QString)));
128
129     // button for clearing the filtering
130     m_clearTextFilteringButton = new QPushButton();
131     filteringLayout->addWidget(m_clearTextFilteringButton);
132     m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
133
134     connect(m_clearTextFilteringButton, SIGNAL(clicked()),
135             this, SLOT(clearTextFiltering()));
136
137     connect(qApp, SIGNAL(topmostWindowChanged(bool)),
138             this, SLOT(topmostWindowChanged(bool)));
139
140     // CONTEXT BUTTONS
141     m_contextButtonLayout->addWidget(m_routeButton);
142     m_contextButtonLayout->addWidget(m_clearGroupFilteringButton);
143 }
144
145 void FriendListPanel::anyPanelClosed()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     m_somePanelIsOpen = false;
150     updateKeyboardGrabbing();
151
152     clearFiltering();
153
154     m_friendListView->clearItemSelection();
155     setRouteButtonDisabled();
156 }
157
158 void FriendListPanel::anyPanelOpened()
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     m_somePanelIsOpen = true;
163     updateKeyboardGrabbing();
164 }
165
166 void FriendListPanel::clearFiltering()
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     m_friendListHeaderWidget->hide();
171     m_clearGroupFilteringButton->setChecked(false);
172     m_clearGroupFilteringButton->setDisabled(true);
173     m_friendListView->clearFilter();
174     clearTextFiltering();
175 }
176
177 void FriendListPanel::clearTextFiltering()
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180
181     // clearing the filtering text field does cause also hiding the filtering layout
182     m_filterField->clear();
183 }
184
185 void FriendListPanel::filterTextChanged(const QString &text)
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     if (m_filterField->isHidden() && !text.isEmpty())
190         setFilteringLayoutVisibility(true);
191     else if (m_filterField->isVisible() && text.isEmpty())
192         setFilteringLayoutVisibility(false);
193
194     m_friendListView->filter(text);
195 }
196
197 void FriendListPanel::friendImageReady(User *user)
198 {
199     qDebug() << __PRETTY_FUNCTION__;
200
201     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
202
203     if (item)
204         item->setAvatarImage(user->profileImage());
205 }
206
207 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
208 {
209     qDebug() << __PRETTY_FUNCTION__;
210
211     QStringList newUserIDs;
212
213     foreach (User *user, friendList) {
214         FriendListItem *item = 0;
215         if (!m_friendListView->contains(user->userId())) {
216             item = new FriendListItem();
217             item->setUserData(user);
218             m_friendListView->addListItem(user->userId(), item);
219         } else {
220             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
221                     user->userId()));
222
223             if (item) {
224                 item->setUserData(user);
225                 m_friendListView->addListItemToView(item);
226             }
227         }
228
229         newUserIDs.append(user->userId());
230     }
231
232     m_friendListView->clearUnused(newUserIDs);
233 }
234
235 void FriendListPanel::hideEvent(QHideEvent *event)
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239     QWidget::hideEvent(event);
240     updateKeyboardGrabbing();
241     clearFiltering();
242
243     m_friendListView->clearItemSelection();
244     setRouteButtonDisabled();
245 }
246
247 void FriendListPanel::routeToSelectedFriend()
248 {
249     qDebug() << __PRETTY_FUNCTION__;
250
251     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
252
253     if (item)
254         emit routeToFriend(item->coordinates());
255 }
256
257 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
258 {
259     qDebug() << __PRETTY_FUNCTION__;
260
261     m_filterField->setVisible(visible);
262     m_clearTextFilteringButton->setVisible(visible);
263 }
264
265 void FriendListPanel::updateKeyboardGrabbing()
266 {
267     qDebug() << __PRETTY_FUNCTION__;
268
269     if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
270         if (QWidget::keyboardGrabber() == m_filterField)
271             m_filterField->releaseKeyboard();
272     } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
273         if (QWidget::keyboardGrabber() != m_filterField)
274             m_filterField->grabKeyboard();
275     }
276 }
277
278 void FriendListPanel::setRouteButtonDisabled()
279 {
280     qDebug() << __PRETTY_FUNCTION__;
281
282     m_routeButton->setDisabled(m_friendListView->selectedItems().isEmpty());
283 }
284
285 void FriendListPanel::showEvent(QShowEvent *event)
286 {
287     qDebug() << __PRETTY_FUNCTION__;
288
289     QWidget::showEvent(event);
290     updateKeyboardGrabbing();
291     setFilteringLayoutVisibility(false);
292 }
293
294 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
295 {
296     qDebug() << __PRETTY_FUNCTION__;
297
298     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
299
300     m_friendListHeaderWidget->show();
301     m_clearGroupFilteringButton->setDisabled(false);
302     m_clearGroupFilteringButton->setChecked(true);
303     m_friendListView->filter(userIDs);
304
305     clearTextFiltering();
306
307     emit showPanelRequested(this);
308 }
309
310 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
311 {
312     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
313
314     m_mainWindowIsTopmost = mainWindowIsTopmost;
315     updateKeyboardGrabbing();
316 }