192972e14db1f55d4f1dff3b820f86bf656e1632
[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(clearFiltering()));
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     clearFiltering();
133 }
134
135 void FriendListPanel::anyPanelOpened()
136 {
137     qWarning() << __PRETTY_FUNCTION__;
138
139     m_somePanelIsOpen = true;
140     updateKeyboardGrabbing();
141 }
142
143 void FriendListPanel::clearFiltering()
144 {
145     qWarning() << __PRETTY_FUNCTION__;
146
147     m_friendListHeaderWidget->hide();
148     m_friendListView->clearFilter();
149
150     // clearing the filtering text field does cause also hiding the filtering layout
151     m_filterField->clear();
152 }
153
154 void FriendListPanel::filterTextChanged(const QString &text)
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     if (m_filterField->isHidden() && !text.isEmpty())
159         setFilteringLayoutVisibility(true);
160     else if (m_filterField->isVisible() && text.isEmpty())
161         setFilteringLayoutVisibility(false);
162
163     m_friendListView->filter(text);
164 }
165
166 void FriendListPanel::friendImageReady(User *user)
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
171
172     if (item)
173         item->setAvatarImage(user->profileImage());
174 }
175
176 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
177 {
178     qDebug() << __PRETTY_FUNCTION__;
179
180     QStringList newUserIDs;
181
182     foreach (User *user, friendList) {
183         FriendListItem *item = 0;
184         if (!m_friendListView->contains(user->userId())) {
185             item = new FriendListItem();
186             item->setUserData(user);
187             m_friendListView->addListItem(user->userId(), item);
188         } else {
189             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
190                     user->userId()));
191
192             if (item) {
193                 item->setUserData(user);
194                 m_friendListView->addListItemToView(item);
195             }
196         }
197
198         newUserIDs.append(user->userId());
199     }
200
201     m_friendListView->clearUnused(newUserIDs);
202 }
203
204 void FriendListPanel::hideEvent(QHideEvent *event)
205 {
206     qDebug() << __PRETTY_FUNCTION__;
207
208     QWidget::hideEvent(event);
209     updateKeyboardGrabbing();
210     clearFiltering();
211 }
212
213 void FriendListPanel::routeToSelectedFriend()
214 {
215     qDebug() << __PRETTY_FUNCTION__;
216
217     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
218
219     if (item)
220         emit routeToFriend(item->coordinates());
221 }
222
223 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
224 {
225     qDebug() << __PRETTY_FUNCTION__;
226
227     m_filterField->setVisible(visible);
228     m_filterClearButton->setVisible(visible);
229 }
230
231 void FriendListPanel::updateKeyboardGrabbing()
232 {
233     qDebug() << __PRETTY_FUNCTION__;
234
235     if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
236         if (QWidget::keyboardGrabber() == m_filterField) {
237             m_filterField->releaseKeyboard();
238             qWarning() << __PRETTY_FUNCTION__ << "released";
239         }
240     } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
241         if (QWidget::keyboardGrabber() != m_filterField) {
242             m_filterField->grabKeyboard();
243             qWarning() << __PRETTY_FUNCTION__ << "grabbed";
244         }
245     }
246 }
247
248 void FriendListPanel::showEvent(QShowEvent *event)
249 {
250     qDebug() << __PRETTY_FUNCTION__;
251
252     QWidget::showEvent(event);
253     updateKeyboardGrabbing();
254     setFilteringLayoutVisibility(false);
255 }
256
257 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
258 {
259     qDebug() << __PRETTY_FUNCTION__;
260
261     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
262
263     m_friendListHeaderWidget->show();
264     m_friendListView->filter(userIDs);
265
266     // hide the text filtering fields
267     setFilteringLayoutVisibility(false);
268
269     emit showPanelRequested(this);
270 }
271
272 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
273 {
274     qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
275
276     m_mainWindowIsTopmost = mainWindowIsTopmost;
277     updateKeyboardGrabbing();
278 }