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(onListItemSelectionChanged()));
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 m_noFriendsLabel = new QLabel();
118 m_noFriendsLabel->setText("No friends");
119 m_noFriendsLabel->setAlignment(Qt::AlignCenter);
121 QPalette noFriendsPalette = palette();
122 noFriendsPalette.setColor(QPalette::Foreground, Qt::white);
123 m_noFriendsLabel->setPalette(noFriendsPalette);
125 friendListPanelLayout->addWidget(m_noFriendsLabel, Qt::AlignCenter);
126 friendListPanelLayout->addWidget(m_headerWidget);
127 friendListPanelLayout->addLayout(listViewLayout);
128 friendListPanelLayout->addLayout(footerLayout);
130 // --- CONTEXT BUTTONS ---
131 m_routeButton = new ImageButton(":res/images/route_to_friend.png",
132 ":res/images/route_to_friend_s.png",
133 ":res/images/route_to_friend_d.png", this);
134 connect(m_routeButton, SIGNAL(clicked()),
135 this, SLOT(routeToSelectedFriend()));
137 m_showContactButton = new ImageButton(":res/images/contact.png",
138 ":res/images/contact_s.png",
139 ":res/images/contact_d.png", this);
140 connect(m_showContactButton, SIGNAL(clicked()),
141 this, SLOT(requestSelectedFriendContactDialog()));
143 m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
144 ":res/images/filtered_s.png",
145 ":res/images/filtered_d.png", this);
146 m_clearGroupFilteringButton->setCheckable(true);
147 m_clearGroupFilteringButton->setDisabled(true);
148 connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
149 this, SLOT(clearFiltering()));
151 m_itemButtonsLayout->addWidget(m_routeButton);
152 m_itemButtonsLayout->addWidget(m_showContactButton);
153 m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
155 showEmptyPanel(true);
158 void FriendListPanel::anyPanelClosed()
160 qDebug() << __PRETTY_FUNCTION__;
162 m_somePanelIsOpen = false;
163 updateKeyboardGrabbing();
167 m_friendListView->clearItemSelection();
170 void FriendListPanel::anyPanelOpened()
172 qDebug() << __PRETTY_FUNCTION__;
174 m_somePanelIsOpen = true;
175 updateKeyboardGrabbing();
178 void FriendListPanel::clearFiltering()
180 qDebug() << __PRETTY_FUNCTION__;
182 m_headerWidget->hide();
183 m_clearGroupFilteringButton->setChecked(false);
184 m_clearGroupFilteringButton->setDisabled(true);
185 m_friendListView->clearFilter();
186 clearTextFiltering();
189 void FriendListPanel::clearTextFiltering()
191 qDebug() << __PRETTY_FUNCTION__;
193 // clearing the filtering text field does cause also hiding the filtering layout
194 m_filterField->clear();
197 void FriendListPanel::filterTextChanged(const QString &text)
199 qDebug() << __PRETTY_FUNCTION__;
201 if (m_filterField->isHidden() && !text.isEmpty())
202 setFilteringLayoutVisibility(true);
203 else if (m_filterField->isVisible() && text.isEmpty())
204 setFilteringLayoutVisibility(false);
206 m_friendListView->filter(text);
209 void FriendListPanel::friendImageReady(const QString &id, const QPixmap &image)
211 qDebug() << __PRETTY_FUNCTION__;
213 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(id));
216 item->setAvatarImage(image);
219 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
221 qDebug() << __PRETTY_FUNCTION__;
223 if (!friendList.isEmpty())
224 showEmptyPanel(false);
226 QStringList newUserIDs;
228 foreach (User *user, friendList) {
229 FriendListItem *item = 0;
230 if (!m_friendListView->contains(user->userId())) {
231 item = new FriendListItem();
232 item->setUserData(user);
233 m_friendListView->addListItem(user->userId(), item);
235 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
239 item->setUserData(user);
240 m_friendListView->addListItemToView(item);
244 newUserIDs.append(user->userId());
247 m_friendListView->clearUnused(newUserIDs);
248 m_friendListView->show();
251 void FriendListPanel::hideEvent(QHideEvent *event)
253 qDebug() << __PRETTY_FUNCTION__;
255 QWidget::hideEvent(event);
256 updateKeyboardGrabbing();
259 m_friendListView->clearItemSelection();
262 void FriendListPanel::requestSelectedFriendContactDialog()
264 qDebug() << __PRETTY_FUNCTION__;
266 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
269 QString facebookId = item->facebookId();
270 if (!facebookId.isEmpty())
271 emit requestContactDialog(facebookId);
275 void FriendListPanel::routeToSelectedFriend()
277 qDebug() << __PRETTY_FUNCTION__;
279 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
282 emit routeToFriend(item->coordinates());
285 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
287 qDebug() << __PRETTY_FUNCTION__;
289 m_filterField->setVisible(visible);
290 m_clearTextFilteringButton->setVisible(visible);
293 void FriendListPanel::updateKeyboardGrabbing()
295 qDebug() << __PRETTY_FUNCTION__;
297 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
298 if (QWidget::keyboardGrabber() == m_filterField)
299 m_filterField->releaseKeyboard();
300 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
301 if (QWidget::keyboardGrabber() != m_filterField)
302 m_filterField->grabKeyboard();
306 void FriendListPanel::showEvent(QShowEvent *event)
308 qDebug() << __PRETTY_FUNCTION__;
310 QWidget::showEvent(event);
311 updateKeyboardGrabbing();
312 setFilteringLayoutVisibility(false);
315 void FriendListPanel::showEmptyPanel(bool show)
318 m_noFriendsLabel->show();
319 m_friendListView->hide();
322 m_noFriendsLabel->hide();
326 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
328 qDebug() << __PRETTY_FUNCTION__;
330 m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
332 m_headerWidget->show();
333 m_clearGroupFilteringButton->setDisabled(false);
334 m_clearGroupFilteringButton->setChecked(true);
335 m_friendListView->filter(userIDs);
337 clearTextFiltering();
339 emit openPanelRequested(this);
342 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
344 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
346 m_mainWindowIsTopmost = mainWindowIsTopmost;
347 updateKeyboardGrabbing();