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