e473cfc210082bd7bbbec6fac6f5fa16dd93891f
[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(onListItemSelectionChanged()));
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     m_noFriendsLabel = new QLabel();
118     m_noFriendsLabel->setText("No friends");
119     m_noFriendsLabel->setAlignment(Qt::AlignCenter);
120
121     QPalette noFriendsPalette = palette();
122     noFriendsPalette.setColor(QPalette::Foreground, Qt::white);
123     m_noFriendsLabel->setPalette(noFriendsPalette);
124
125     friendListPanelLayout->addWidget(m_noFriendsLabel, Qt::AlignCenter);
126     friendListPanelLayout->addWidget(m_headerWidget);
127     friendListPanelLayout->addLayout(listViewLayout);
128     friendListPanelLayout->addLayout(footerLayout);
129
130     // --- CONTEXT BUTTONS ---
131     m_routeButton = new ImageButton(":res/images/route_to_friend.png",
132                                     ":res/images/route_to_friend_s.png",
133                                     ":res/images/route_to_friend_d.png", this);
134     connect(m_routeButton, SIGNAL(clicked()),
135             this, SLOT(routeToSelectedFriend()));
136
137     m_showContactButton = new ImageButton(":res/images/contact.png",
138                                           ":res/images/contact_s.png",
139                                           ":res/images/contact_d.png", this);
140     connect(m_showContactButton, SIGNAL(clicked()),
141             this, SLOT(requestSelectedFriendContactDialog()));
142
143     m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
144                                                   ":res/images/filtered_s.png",
145                                                   ":res/images/filtered_d.png", this);
146     m_clearGroupFilteringButton->setCheckable(true);
147     m_clearGroupFilteringButton->setDisabled(true);
148     connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
149             this, SLOT(clearFiltering()));
150
151     m_itemButtonsLayout->addWidget(m_routeButton);
152     m_itemButtonsLayout->addWidget(m_showContactButton);
153     m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
154
155     showEmptyPanel(true);
156 }
157
158 void FriendListPanel::anyPanelClosed()
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     m_somePanelIsOpen = false;
163     updateKeyboardGrabbing();
164
165     clearFiltering();
166
167     m_friendListView->clearItemSelection();
168 }
169
170 void FriendListPanel::anyPanelOpened()
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     m_somePanelIsOpen = true;
175     updateKeyboardGrabbing();
176 }
177
178 void FriendListPanel::clearFiltering()
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182     m_headerWidget->hide();
183     m_clearGroupFilteringButton->setChecked(false);
184     m_clearGroupFilteringButton->setDisabled(true);
185     m_friendListView->clearFilter();
186     clearTextFiltering();
187 }
188
189 void FriendListPanel::clearTextFiltering()
190 {
191     qDebug() << __PRETTY_FUNCTION__;
192
193     // clearing the filtering text field does cause also hiding the filtering layout
194     m_filterField->clear();
195 }
196
197 void FriendListPanel::filterTextChanged(const QString &text)
198 {
199     qDebug() << __PRETTY_FUNCTION__;
200
201     if (m_filterField->isHidden() && !text.isEmpty())
202         setFilteringLayoutVisibility(true);
203     else if (m_filterField->isVisible() && text.isEmpty())
204         setFilteringLayoutVisibility(false);
205
206     m_friendListView->filter(text);
207 }
208
209 void FriendListPanel::friendImageReady(const QString &id, const QPixmap &image)
210 {
211     qDebug() << __PRETTY_FUNCTION__;
212
213     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(id));
214
215     if (item)
216         item->setAvatarImage(image);
217 }
218
219 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
220 {
221     qDebug() << __PRETTY_FUNCTION__;
222
223     if (!friendList.isEmpty())
224            showEmptyPanel(false);
225
226     QStringList newUserIDs;
227
228     foreach (User *user, friendList) {
229         FriendListItem *item = 0;
230         if (!m_friendListView->contains(user->userId())) {
231             item = new FriendListItem();
232             item->setUserData(user);
233             m_friendListView->addListItem(user->userId(), item);
234         } else {
235             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
236                     user->userId()));
237
238             if (item) {
239                 item->setUserData(user);
240                 m_friendListView->addListItemToView(item);
241             }
242         }
243
244         newUserIDs.append(user->userId());
245     }
246
247     m_friendListView->clearUnused(newUserIDs);
248     m_friendListView->show();
249 }
250
251 void FriendListPanel::hideEvent(QHideEvent *event)
252 {
253     qDebug() << __PRETTY_FUNCTION__;
254
255     QWidget::hideEvent(event);
256     updateKeyboardGrabbing();
257     clearFiltering();
258
259     m_friendListView->clearItemSelection();
260 }
261
262 void FriendListPanel::requestSelectedFriendContactDialog()
263 {
264      qDebug() << __PRETTY_FUNCTION__;
265
266      FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
267
268      if (item) {
269          QString facebookId = item->facebookId();
270          if (!facebookId.isEmpty())
271              emit requestContactDialog(facebookId);
272      }
273 }
274
275 void FriendListPanel::routeToSelectedFriend()
276 {
277     qDebug() << __PRETTY_FUNCTION__;
278
279     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
280
281     if (item)
282         emit routeToFriend(item->coordinates());
283 }
284
285 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
286 {
287     qDebug() << __PRETTY_FUNCTION__;
288
289     m_filterField->setVisible(visible);
290     m_clearTextFilteringButton->setVisible(visible);
291 }
292
293 void FriendListPanel::updateKeyboardGrabbing()
294 {
295     qDebug() << __PRETTY_FUNCTION__;
296
297     if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
298         if (QWidget::keyboardGrabber() == m_filterField)
299             m_filterField->releaseKeyboard();
300     } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
301         if (QWidget::keyboardGrabber() != m_filterField)
302             m_filterField->grabKeyboard();
303     }
304 }
305
306 void FriendListPanel::showEvent(QShowEvent *event)
307 {
308     qDebug() << __PRETTY_FUNCTION__;
309
310     QWidget::showEvent(event);
311     updateKeyboardGrabbing();
312     setFilteringLayoutVisibility(false);
313 }
314
315 void FriendListPanel::showEmptyPanel(bool show)
316 {
317     if (show) {
318         m_noFriendsLabel->show();
319         m_friendListView->hide();
320     }
321     else {
322         m_noFriendsLabel->hide();
323     }
324 }
325
326 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
327 {
328     qDebug() << __PRETTY_FUNCTION__;
329
330     m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
331
332     m_headerWidget->show();
333     m_clearGroupFilteringButton->setDisabled(false);
334     m_clearGroupFilteringButton->setChecked(true);
335     m_friendListView->filter(userIDs);
336
337     clearTextFiltering();
338
339     emit openPanelRequested(this);
340 }
341
342 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
343 {
344     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
345
346     m_mainWindowIsTopmost = mainWindowIsTopmost;
347     updateKeyboardGrabbing();
348 }