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