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