Merge branch 'master' into text_cutting_bug
[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     // --- HEADER, HOW MANY FRIENDS ARE SELECTED ---
54     m_headerWidget = new QWidget();
55
56     m_headerWidget->hide();
57     m_headerWidget->setAutoFillBackground(true);
58
59     QPalette headerPalette = m_headerWidget->palette();
60     headerPalette.setColor(QPalette::Background, Qt::black);
61     m_headerWidget->setPalette(headerPalette);
62
63     QHBoxLayout *headerLayout = new QHBoxLayout();
64     m_headerWidget->setLayout(headerLayout);
65     headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT,
66                                      FRIENDPANEL_FILTER_MARGIN_TOP,
67                                      FRIENDPANEL_FILTER_MARGIN_RIGHT,
68                                      FRIENDPANEL_FILTER_MARGIN_BOTTOM);
69
70     m_headerLabel = new QLabel(this);
71     headerLayout->addWidget(m_headerLabel, 0, Qt::AlignCenter);
72
73     // --- FRIEND LIST ---
74     m_friendListView = new FriendListView(this);
75     m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
76
77     QVBoxLayout *listViewLayout = new QVBoxLayout;
78     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
79                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
80     listViewLayout->addWidget(m_friendListView);
81
82     connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
83             this, SIGNAL(findFriend(GeoCoordinate)));
84
85     connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
86             this, SLOT(setRouteButtonDisabled()));
87
88     // --- FOOTER, TEXT BASED FILTERING ---
89     QHBoxLayout *footerLayout = new QHBoxLayout();
90
91     m_filterField = new QLineEdit;
92     footerLayout->addWidget(m_filterField);
93
94     connect(m_filterField, SIGNAL(returnPressed()),
95             this, SLOT(clearTextFiltering()));
96
97     connect(m_filterField, SIGNAL(textChanged(QString)),
98             this, SLOT(filterTextChanged(QString)));
99
100     m_clearTextFilteringButton = new QPushButton();
101     footerLayout->addWidget(m_clearTextFilteringButton);
102     m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
103
104     connect(m_clearTextFilteringButton, SIGNAL(clicked()),
105             this, SLOT(clearTextFiltering()));
106
107     connect(qApp, SIGNAL(topmostWindowChanged(bool)),
108             this, SLOT(topmostWindowChanged(bool)));
109
110     // --- MAIN LAYOUT ---
111     QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
112     friendListPanelLayout->setMargin(0);
113     friendListPanelLayout->setSpacing(0);
114     setLayout(friendListPanelLayout);
115
116     friendListPanelLayout->addWidget(m_headerWidget);
117     friendListPanelLayout->addLayout(listViewLayout);
118     friendListPanelLayout->addLayout(footerLayout);
119
120     // --- CONTEXT BUTTONS ---
121     m_routeButton = new ImageButton(":res/images/route_to_friend.png",
122                                     ":res/images/route_to_friend_s.png",
123                                     ":res/images/route_to_friend_d.png", this);
124     m_routeButton->setDisabled(true);
125     connect(m_routeButton, SIGNAL(clicked()),
126             this, SLOT(routeToSelectedFriend()));
127
128     m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
129                                                   ":res/images/filtered_s.png",
130                                                   ":res/images/filtered_d.png", this);
131     m_clearGroupFilteringButton->setCheckable(true);
132     m_clearGroupFilteringButton->setDisabled(true);
133     connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
134             this, SLOT(clearFiltering()));
135
136     m_contextButtonLayout->addWidget(m_routeButton);
137     m_contextButtonLayout->addWidget(m_clearGroupFilteringButton);
138 }
139
140 void FriendListPanel::anyPanelClosed()
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     m_somePanelIsOpen = false;
145     updateKeyboardGrabbing();
146
147     clearFiltering();
148
149     m_friendListView->clearItemSelection();
150     setRouteButtonDisabled();
151 }
152
153 void FriendListPanel::anyPanelOpened()
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_somePanelIsOpen = true;
158     updateKeyboardGrabbing();
159 }
160
161 void FriendListPanel::clearFiltering()
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_headerWidget->hide();
166     m_clearGroupFilteringButton->setChecked(false);
167     m_clearGroupFilteringButton->setDisabled(true);
168     m_friendListView->clearFilter();
169     clearTextFiltering();
170 }
171
172 void FriendListPanel::clearTextFiltering()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     // clearing the filtering text field does cause also hiding the filtering layout
177     m_filterField->clear();
178 }
179
180 void FriendListPanel::filterTextChanged(const QString &text)
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     if (m_filterField->isHidden() && !text.isEmpty())
185         setFilteringLayoutVisibility(true);
186     else if (m_filterField->isVisible() && text.isEmpty())
187         setFilteringLayoutVisibility(false);
188
189     m_friendListView->filter(text);
190 }
191
192 void FriendListPanel::friendImageReady(User *user)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
197
198     if (item)
199         item->setAvatarImage(user->profileImage());
200 }
201
202 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
203 {
204     qDebug() << __PRETTY_FUNCTION__;
205
206     QStringList newUserIDs;
207     static int count = 0;
208
209     foreach (User *user, friendList) {
210         FriendListItem *item = 0;
211         if (!m_friendListView->contains(user->userId())) {
212             item = new FriendListItem();
213             item->setUserData(user);
214             m_friendListView->addListItem(user->userId(), item);
215         } else {
216             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
217                     user->userId()));
218
219             if (item) {
220                 item->setUserData(user);
221                 m_friendListView->addListItemToView(item);
222             }
223         }
224
225         newUserIDs.append(user->userId());
226
227 //        count++;
228 //        if (count >= 1)
229 //            break;
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_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
299
300     m_headerWidget->show();
301     m_clearGroupFilteringButton->setDisabled(false);
302     m_clearGroupFilteringButton->setChecked(true);
303     m_friendListView->filter(userIDs);
304
305     clearTextFiltering();
306
307     emit openPanelRequested(this);
308 }
309
310 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
311 {
312     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
313
314     m_mainWindowIsTopmost = mainWindowIsTopmost;
315     updateKeyboardGrabbing();
316 }