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