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