2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
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 Jussi Laitinen - jussi.laitinen@ixonos.com
11 Situare is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 version 2 as published by the Free Software Foundation.
15 Situare is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Situare; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include <QApplication>
27 #include <QHBoxLayout>
30 #include <QPushButton>
32 #include "coordinates/geocoordinate.h"
33 #include "friendlistitem.h"
34 #include "friendlistitemdelegate.h"
35 #include "friendlistview.h"
36 #include "imagebutton.h"
37 #include "panelcommon.h"
38 #include "user/user.h"
40 #include "friendlistpanel.h"
42 FriendListPanel::FriendListPanel(QWidget *parent)
44 m_mainWindowIsTopmost(false),
45 m_somePanelIsOpen(false)
47 qDebug() << __PRETTY_FUNCTION__;
49 const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
50 const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
51 const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
52 const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
54 // --- HEADER, HOW MANY FRIENDS ARE SELECTED ---
55 m_headerWidget = new QWidget();
57 m_headerWidget->hide();
58 m_headerWidget->setAutoFillBackground(true);
60 QPalette headerPalette = m_headerWidget->palette();
61 headerPalette.setColor(QPalette::Background, Qt::black);
62 m_headerWidget->setPalette(headerPalette);
64 QHBoxLayout *headerLayout = new QHBoxLayout();
65 m_headerWidget->setLayout(headerLayout);
66 headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT,
67 FRIENDPANEL_FILTER_MARGIN_TOP,
68 FRIENDPANEL_FILTER_MARGIN_RIGHT,
69 FRIENDPANEL_FILTER_MARGIN_BOTTOM);
71 m_headerLabel = new QLabel(this);
72 headerLayout->addWidget(m_headerLabel, 0, Qt::AlignCenter);
74 // --- FRIEND LIST ---
75 m_friendListView = new FriendListView(this);
76 m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
78 QVBoxLayout *listViewLayout = new QVBoxLayout;
79 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
80 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
81 listViewLayout->addWidget(m_friendListView);
83 connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
84 this, SIGNAL(findFriend(GeoCoordinate)));
86 connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
87 this, SLOT(setRouteButtonDisabled()));
89 // --- FOOTER, TEXT BASED FILTERING ---
90 QHBoxLayout *footerLayout = new QHBoxLayout();
92 m_filterField = new QLineEdit;
93 footerLayout->addWidget(m_filterField);
95 connect(m_filterField, SIGNAL(returnPressed()),
96 this, SLOT(clearTextFiltering()));
98 connect(m_filterField, SIGNAL(textChanged(QString)),
99 this, SLOT(filterTextChanged(QString)));
101 m_clearTextFilteringButton = new QPushButton();
102 footerLayout->addWidget(m_clearTextFilteringButton);
103 m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
105 connect(m_clearTextFilteringButton, SIGNAL(clicked()),
106 this, SLOT(clearTextFiltering()));
108 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
109 this, SLOT(topmostWindowChanged(bool)));
111 // --- MAIN LAYOUT ---
112 QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
113 friendListPanelLayout->setMargin(0);
114 friendListPanelLayout->setSpacing(0);
115 setLayout(friendListPanelLayout);
117 friendListPanelLayout->addWidget(m_headerWidget);
118 friendListPanelLayout->addLayout(listViewLayout);
119 friendListPanelLayout->addLayout(footerLayout);
121 // --- CONTEXT BUTTONS ---
122 m_routeButton = new ImageButton(":res/images/route_to_friend.png",
123 ":res/images/route_to_friend_s.png",
124 ":res/images/route_to_friend_d.png", this);
125 m_routeButton->setDisabled(true);
126 connect(m_routeButton, SIGNAL(clicked()),
127 this, SLOT(routeToSelectedFriend()));
129 m_showContactButton = new ImageButton(":res/images/contact_btn.png",
130 ":res/images/contact_btn_s.png",
131 ":res/images/contact_btn_d.png", this);
132 connect(m_showContactButton, SIGNAL(clicked()),
133 this, SLOT(requestSelectedFriendContactDialog()));
135 m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
136 ":res/images/filtered_s.png",
137 ":res/images/filtered_d.png", this);
138 m_clearGroupFilteringButton->setCheckable(true);
139 m_clearGroupFilteringButton->setDisabled(true);
140 connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
141 this, SLOT(clearFiltering()));
143 m_contextButtonLayout->addWidget(m_routeButton);
144 m_contextButtonLayout->addWidget(m_showContactButton);
145 m_contextButtonLayout->addWidget(m_clearGroupFilteringButton);
148 void FriendListPanel::anyPanelClosed()
150 qDebug() << __PRETTY_FUNCTION__;
152 m_somePanelIsOpen = false;
153 updateKeyboardGrabbing();
157 m_friendListView->clearItemSelection();
158 setRouteButtonDisabled();
161 void FriendListPanel::anyPanelOpened()
163 qDebug() << __PRETTY_FUNCTION__;
165 m_somePanelIsOpen = true;
166 updateKeyboardGrabbing();
169 void FriendListPanel::clearFiltering()
171 qDebug() << __PRETTY_FUNCTION__;
173 m_headerWidget->hide();
174 m_clearGroupFilteringButton->setChecked(false);
175 m_clearGroupFilteringButton->setDisabled(true);
176 m_friendListView->clearFilter();
177 clearTextFiltering();
180 void FriendListPanel::clearTextFiltering()
182 qDebug() << __PRETTY_FUNCTION__;
184 // clearing the filtering text field does cause also hiding the filtering layout
185 m_filterField->clear();
188 void FriendListPanel::filterTextChanged(const QString &text)
190 qDebug() << __PRETTY_FUNCTION__;
192 if (m_filterField->isHidden() && !text.isEmpty())
193 setFilteringLayoutVisibility(true);
194 else if (m_filterField->isVisible() && text.isEmpty())
195 setFilteringLayoutVisibility(false);
197 m_friendListView->filter(text);
200 void FriendListPanel::friendImageReady(User *user)
202 qDebug() << __PRETTY_FUNCTION__;
204 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
207 item->setAvatarImage(user->profileImage());
210 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
212 qDebug() << __PRETTY_FUNCTION__;
214 QStringList newUserIDs;
216 foreach (User *user, friendList) {
217 FriendListItem *item = 0;
218 if (!m_friendListView->contains(user->userId())) {
219 item = new FriendListItem();
220 item->setUserData(user);
221 m_friendListView->addListItem(user->userId(), item);
223 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
227 item->setUserData(user);
228 m_friendListView->addListItemToView(item);
232 newUserIDs.append(user->userId());
235 m_friendListView->clearUnused(newUserIDs);
238 void FriendListPanel::hideEvent(QHideEvent *event)
240 qDebug() << __PRETTY_FUNCTION__;
242 QWidget::hideEvent(event);
243 updateKeyboardGrabbing();
246 m_friendListView->clearItemSelection();
247 setRouteButtonDisabled();
250 void FriendListPanel::requestSelectedFriendContactDialog()
252 qDebug() << __PRETTY_FUNCTION__;
254 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
257 QString facebookId = item->facebookId();
258 if (!facebookId.isEmpty())
259 emit requestContactDialog(facebookId);
263 void FriendListPanel::routeToSelectedFriend()
265 qDebug() << __PRETTY_FUNCTION__;
267 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
270 emit routeToFriend(item->coordinates());
273 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
275 qDebug() << __PRETTY_FUNCTION__;
277 m_filterField->setVisible(visible);
278 m_clearTextFilteringButton->setVisible(visible);
281 void FriendListPanel::updateKeyboardGrabbing()
283 qDebug() << __PRETTY_FUNCTION__;
285 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
286 if (QWidget::keyboardGrabber() == m_filterField)
287 m_filterField->releaseKeyboard();
288 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
289 if (QWidget::keyboardGrabber() != m_filterField)
290 m_filterField->grabKeyboard();
294 void FriendListPanel::setRouteButtonDisabled()
296 qDebug() << __PRETTY_FUNCTION__;
298 m_routeButton->setDisabled(m_friendListView->selectedItems().isEmpty());
301 void FriendListPanel::showEvent(QShowEvent *event)
303 qDebug() << __PRETTY_FUNCTION__;
305 QWidget::showEvent(event);
306 updateKeyboardGrabbing();
307 setFilteringLayoutVisibility(false);
310 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
312 qDebug() << __PRETTY_FUNCTION__;
314 m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
316 m_headerWidget->show();
317 m_clearGroupFilteringButton->setDisabled(false);
318 m_clearGroupFilteringButton->setChecked(true);
319 m_friendListView->filter(userIDs);
321 clearTextFiltering();
323 emit openPanelRequested(this);
326 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
328 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
330 m_mainWindowIsTopmost = mainWindowIsTopmost;
331 updateKeyboardGrabbing();