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