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
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.
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.
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,
25 #include <QApplication>
26 #include <QHBoxLayout>
29 #include <QPushButton>
31 #include "coordinates/geocoordinate.h"
32 #include "friendlistitem.h"
33 #include "friendlistitemdelegate.h"
34 #include "friendlistview.h"
35 #include "imagebutton.h"
36 #include "panelcommon.h"
37 #include "user/user.h"
39 #include "friendlistpanel.h"
41 FriendListPanel::FriendListPanel(QWidget *parent)
43 m_mainWindowIsTopmost(false),
44 m_somePanelIsOpen(false)
46 qDebug() << __PRETTY_FUNCTION__;
48 const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
49 const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
50 const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
51 const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
53 // --- HEADER, HOW MANY FRIENDS ARE SELECTED ---
54 m_headerWidget = new QWidget();
56 m_headerWidget->hide();
57 m_headerWidget->setAutoFillBackground(true);
59 QPalette headerPalette = m_headerWidget->palette();
60 headerPalette.setColor(QPalette::Background, Qt::black);
61 m_headerWidget->setPalette(headerPalette);
63 QHBoxLayout *headerLayout = new QHBoxLayout();
64 m_headerWidget->setLayout(headerLayout);
65 headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT,
66 FRIENDPANEL_FILTER_MARGIN_TOP,
67 FRIENDPANEL_FILTER_MARGIN_RIGHT,
68 FRIENDPANEL_FILTER_MARGIN_BOTTOM);
70 m_headerLabel = new QLabel(this);
71 headerLayout->addWidget(m_headerLabel, 0, Qt::AlignCenter);
73 // --- FRIEND LIST ---
74 m_friendListView = new FriendListView(this);
75 m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
77 QVBoxLayout *listViewLayout = new QVBoxLayout;
78 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
79 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
80 listViewLayout->addWidget(m_friendListView);
82 connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
83 this, SIGNAL(findFriend(GeoCoordinate)));
85 connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
86 this, SLOT(setRouteButtonDisabled()));
88 // --- FOOTER, TEXT BASED FILTERING ---
89 QHBoxLayout *footerLayout = new QHBoxLayout();
91 m_filterField = new QLineEdit;
92 footerLayout->addWidget(m_filterField);
94 connect(m_filterField, SIGNAL(returnPressed()),
95 this, SLOT(clearTextFiltering()));
97 connect(m_filterField, SIGNAL(textChanged(QString)),
98 this, SLOT(filterTextChanged(QString)));
100 m_clearTextFilteringButton = new QPushButton();
101 footerLayout->addWidget(m_clearTextFilteringButton);
102 m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
104 connect(m_clearTextFilteringButton, SIGNAL(clicked()),
105 this, SLOT(clearTextFiltering()));
107 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
108 this, SLOT(topmostWindowChanged(bool)));
110 // --- MAIN LAYOUT ---
111 QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
112 friendListPanelLayout->setMargin(0);
113 friendListPanelLayout->setSpacing(0);
114 setLayout(friendListPanelLayout);
116 friendListPanelLayout->addWidget(m_headerWidget);
117 friendListPanelLayout->addLayout(listViewLayout);
118 friendListPanelLayout->addLayout(footerLayout);
120 // --- CONTEXT BUTTONS ---
121 m_routeButton = new ImageButton(":res/images/route_to_friend.png",
122 ":res/images/route_to_friend_s.png", "", this);
123 m_routeButton->setDisabled(true);
124 connect(m_routeButton, SIGNAL(clicked()),
125 this, SLOT(routeToSelectedFriend()));
127 m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
128 ":res/images/filtered_s.png", "", this);
129 m_clearGroupFilteringButton->setCheckable(true);
130 m_clearGroupFilteringButton->setDisabled(true);
131 connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
132 this, SLOT(clearFiltering()));
134 m_contextButtonLayout->addWidget(m_routeButton);
135 m_contextButtonLayout->addWidget(m_clearGroupFilteringButton);
138 void FriendListPanel::anyPanelClosed()
140 qDebug() << __PRETTY_FUNCTION__;
142 m_somePanelIsOpen = false;
143 updateKeyboardGrabbing();
147 m_friendListView->clearItemSelection();
148 setRouteButtonDisabled();
151 void FriendListPanel::anyPanelOpened()
153 qDebug() << __PRETTY_FUNCTION__;
155 m_somePanelIsOpen = true;
156 updateKeyboardGrabbing();
159 void FriendListPanel::clearFiltering()
161 qDebug() << __PRETTY_FUNCTION__;
163 m_headerWidget->hide();
164 m_clearGroupFilteringButton->setChecked(false);
165 m_clearGroupFilteringButton->setDisabled(true);
166 m_friendListView->clearFilter();
167 clearTextFiltering();
170 void FriendListPanel::clearTextFiltering()
172 qDebug() << __PRETTY_FUNCTION__;
174 // clearing the filtering text field does cause also hiding the filtering layout
175 m_filterField->clear();
178 void FriendListPanel::filterTextChanged(const QString &text)
180 qDebug() << __PRETTY_FUNCTION__;
182 if (m_filterField->isHidden() && !text.isEmpty())
183 setFilteringLayoutVisibility(true);
184 else if (m_filterField->isVisible() && text.isEmpty())
185 setFilteringLayoutVisibility(false);
187 m_friendListView->filter(text);
190 void FriendListPanel::friendImageReady(User *user)
192 qDebug() << __PRETTY_FUNCTION__;
194 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
197 item->setAvatarImage(user->profileImage());
200 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
202 qDebug() << __PRETTY_FUNCTION__;
204 QStringList newUserIDs;
206 foreach (User *user, friendList) {
207 FriendListItem *item = 0;
208 if (!m_friendListView->contains(user->userId())) {
209 item = new FriendListItem();
210 item->setUserData(user);
211 m_friendListView->addListItem(user->userId(), item);
213 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
217 item->setUserData(user);
218 m_friendListView->addListItemToView(item);
222 newUserIDs.append(user->userId());
225 m_friendListView->clearUnused(newUserIDs);
228 void FriendListPanel::hideEvent(QHideEvent *event)
230 qDebug() << __PRETTY_FUNCTION__;
232 QWidget::hideEvent(event);
233 updateKeyboardGrabbing();
236 m_friendListView->clearItemSelection();
237 setRouteButtonDisabled();
240 void FriendListPanel::routeToSelectedFriend()
242 qDebug() << __PRETTY_FUNCTION__;
244 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
247 emit routeToFriend(item->coordinates());
250 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
252 qDebug() << __PRETTY_FUNCTION__;
254 m_filterField->setVisible(visible);
255 m_clearTextFilteringButton->setVisible(visible);
258 void FriendListPanel::updateKeyboardGrabbing()
260 qDebug() << __PRETTY_FUNCTION__;
262 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
263 if (QWidget::keyboardGrabber() == m_filterField)
264 m_filterField->releaseKeyboard();
265 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
266 if (QWidget::keyboardGrabber() != m_filterField)
267 m_filterField->grabKeyboard();
271 void FriendListPanel::setRouteButtonDisabled()
273 qDebug() << __PRETTY_FUNCTION__;
275 m_routeButton->setDisabled(m_friendListView->selectedItems().isEmpty());
278 void FriendListPanel::showEvent(QShowEvent *event)
280 qDebug() << __PRETTY_FUNCTION__;
282 QWidget::showEvent(event);
283 updateKeyboardGrabbing();
284 setFilteringLayoutVisibility(false);
287 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
289 qDebug() << __PRETTY_FUNCTION__;
291 m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
293 m_headerWidget->show();
294 m_clearGroupFilteringButton->setDisabled(false);
295 m_clearGroupFilteringButton->setChecked(true);
296 m_friendListView->filter(userIDs);
298 clearTextFiltering();
300 emit openPanelRequested(this);
303 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
305 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
307 m_mainWindowIsTopmost = mainWindowIsTopmost;
308 updateKeyboardGrabbing();