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 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 connect(m_routeButton, SIGNAL(clicked()),
126 this, SLOT(routeToSelectedFriend()));
128 m_showContactButton = new ImageButton(":res/images/contact_btn.png",
129 ":res/images/contact_btn_s.png",
130 ":res/images/contact_btn_d.png", this);
131 connect(m_showContactButton, SIGNAL(clicked()),
132 this, SLOT(requestSelectedFriendContactDialog()));
134 m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
135 ":res/images/filtered_s.png",
136 ":res/images/filtered_d.png", this);
137 m_clearGroupFilteringButton->setCheckable(true);
138 m_clearGroupFilteringButton->setDisabled(true);
139 connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
140 this, SLOT(clearFiltering()));
142 m_itemButtonsLayout->addWidget(m_routeButton);
143 m_itemButtonsLayout->addWidget(m_showContactButton);
144 m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
147 void FriendListPanel::anyPanelClosed()
149 qDebug() << __PRETTY_FUNCTION__;
151 m_somePanelIsOpen = false;
152 updateKeyboardGrabbing();
156 m_friendListView->clearItemSelection();
159 void FriendListPanel::anyPanelOpened()
161 qDebug() << __PRETTY_FUNCTION__;
163 m_somePanelIsOpen = true;
164 updateKeyboardGrabbing();
167 void FriendListPanel::clearFiltering()
169 qDebug() << __PRETTY_FUNCTION__;
171 m_headerWidget->hide();
172 m_clearGroupFilteringButton->setChecked(false);
173 m_clearGroupFilteringButton->setDisabled(true);
174 m_friendListView->clearFilter();
175 clearTextFiltering();
178 void FriendListPanel::clearTextFiltering()
180 qDebug() << __PRETTY_FUNCTION__;
182 // clearing the filtering text field does cause also hiding the filtering layout
183 m_filterField->clear();
186 void FriendListPanel::filterTextChanged(const QString &text)
188 qDebug() << __PRETTY_FUNCTION__;
190 if (m_filterField->isHidden() && !text.isEmpty())
191 setFilteringLayoutVisibility(true);
192 else if (m_filterField->isVisible() && text.isEmpty())
193 setFilteringLayoutVisibility(false);
195 m_friendListView->filter(text);
198 void FriendListPanel::friendImageReady(User *user)
200 qDebug() << __PRETTY_FUNCTION__;
202 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
205 item->setAvatarImage(user->profileImage());
208 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
210 qDebug() << __PRETTY_FUNCTION__;
212 QStringList newUserIDs;
214 foreach (User *user, friendList) {
215 FriendListItem *item = 0;
216 if (!m_friendListView->contains(user->userId())) {
217 item = new FriendListItem();
218 item->setUserData(user);
219 m_friendListView->addListItem(user->userId(), item);
221 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
225 item->setUserData(user);
226 m_friendListView->addListItemToView(item);
230 newUserIDs.append(user->userId());
233 m_friendListView->clearUnused(newUserIDs);
236 void FriendListPanel::hideEvent(QHideEvent *event)
238 qDebug() << __PRETTY_FUNCTION__;
240 QWidget::hideEvent(event);
241 updateKeyboardGrabbing();
244 m_friendListView->clearItemSelection();
247 void FriendListPanel::requestSelectedFriendContactDialog()
249 qDebug() << __PRETTY_FUNCTION__;
251 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
254 QString facebookId = item->facebookId();
255 if (!facebookId.isEmpty())
256 emit requestContactDialog(facebookId);
260 void FriendListPanel::routeToSelectedFriend()
262 qDebug() << __PRETTY_FUNCTION__;
264 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
267 emit routeToFriend(item->coordinates());
270 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
272 qDebug() << __PRETTY_FUNCTION__;
274 m_filterField->setVisible(visible);
275 m_clearTextFilteringButton->setVisible(visible);
278 void FriendListPanel::updateKeyboardGrabbing()
280 qDebug() << __PRETTY_FUNCTION__;
282 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
283 if (QWidget::keyboardGrabber() == m_filterField)
284 m_filterField->releaseKeyboard();
285 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
286 if (QWidget::keyboardGrabber() != m_filterField)
287 m_filterField->grabKeyboard();
291 void FriendListPanel::showEvent(QShowEvent *event)
293 qDebug() << __PRETTY_FUNCTION__;
295 QWidget::showEvent(event);
296 updateKeyboardGrabbing();
297 setFilteringLayoutVisibility(false);
300 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
302 qDebug() << __PRETTY_FUNCTION__;
304 m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
306 m_headerWidget->show();
307 m_clearGroupFilteringButton->setDisabled(false);
308 m_clearGroupFilteringButton->setChecked(true);
309 m_friendListView->filter(userIDs);
311 clearTextFiltering();
313 emit openPanelRequested(this);
316 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
318 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
320 m_mainWindowIsTopmost = mainWindowIsTopmost;
321 updateKeyboardGrabbing();