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