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 connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
89 this, SLOT(onListItemSelectionChanged()));
91 // --- FOOTER, TEXT BASED FILTERING ---
92 QHBoxLayout *footerLayout = new QHBoxLayout();
94 m_filterField = new QLineEdit;
95 footerLayout->addWidget(m_filterField);
97 connect(m_filterField, SIGNAL(returnPressed()),
98 this, SLOT(clearTextFiltering()));
100 connect(m_filterField, SIGNAL(textChanged(QString)),
101 this, SLOT(filterTextChanged(QString)));
103 m_clearTextFilteringButton = new QPushButton();
104 footerLayout->addWidget(m_clearTextFilteringButton);
105 m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
107 connect(m_clearTextFilteringButton, SIGNAL(clicked()),
108 this, SLOT(clearTextFiltering()));
110 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
111 this, SLOT(topmostWindowChanged(bool)));
113 // --- MAIN LAYOUT ---
114 QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
115 friendListPanelLayout->setMargin(0);
116 friendListPanelLayout->setSpacing(0);
117 setLayout(friendListPanelLayout);
119 friendListPanelLayout->addWidget(m_headerWidget);
120 friendListPanelLayout->addLayout(listViewLayout);
121 friendListPanelLayout->addLayout(footerLayout);
123 // --- CONTEXT BUTTONS ---
124 m_routeButton = new ImageButton(":res/images/route_to_friend.png",
125 ":res/images/route_to_friend_s.png",
126 ":res/images/route_to_friend_d.png", this);
127 m_routeButton->setDisabled(true);
128 connect(m_routeButton, SIGNAL(clicked()),
129 this, SLOT(routeToSelectedFriend()));
131 m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
132 ":res/images/filtered_s.png",
133 ":res/images/filtered_d.png", this);
134 m_clearGroupFilteringButton->setCheckable(true);
135 m_clearGroupFilteringButton->setDisabled(true);
136 connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
137 this, SLOT(clearFiltering()));
139 m_itemButtonsLayout->addWidget(m_routeButton);
140 m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
143 void FriendListPanel::anyPanelClosed()
145 qDebug() << __PRETTY_FUNCTION__;
147 m_somePanelIsOpen = false;
148 updateKeyboardGrabbing();
152 m_friendListView->clearItemSelection();
153 setRouteButtonDisabled();
156 void FriendListPanel::anyPanelOpened()
158 qDebug() << __PRETTY_FUNCTION__;
160 m_somePanelIsOpen = true;
161 updateKeyboardGrabbing();
164 void FriendListPanel::clearFiltering()
166 qDebug() << __PRETTY_FUNCTION__;
168 m_headerWidget->hide();
169 m_clearGroupFilteringButton->setChecked(false);
170 m_clearGroupFilteringButton->setDisabled(true);
171 m_friendListView->clearFilter();
172 clearTextFiltering();
175 void FriendListPanel::clearTextFiltering()
177 qDebug() << __PRETTY_FUNCTION__;
179 // clearing the filtering text field does cause also hiding the filtering layout
180 m_filterField->clear();
183 void FriendListPanel::filterTextChanged(const QString &text)
185 qDebug() << __PRETTY_FUNCTION__;
187 if (m_filterField->isHidden() && !text.isEmpty())
188 setFilteringLayoutVisibility(true);
189 else if (m_filterField->isVisible() && text.isEmpty())
190 setFilteringLayoutVisibility(false);
192 m_friendListView->filter(text);
195 void FriendListPanel::friendImageReady(User *user)
197 qDebug() << __PRETTY_FUNCTION__;
199 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
202 item->setAvatarImage(user->profileImage());
205 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
207 qDebug() << __PRETTY_FUNCTION__;
209 QStringList newUserIDs;
211 foreach (User *user, friendList) {
212 FriendListItem *item = 0;
213 if (!m_friendListView->contains(user->userId())) {
214 item = new FriendListItem();
215 item->setUserData(user);
216 m_friendListView->addListItem(user->userId(), item);
218 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
222 item->setUserData(user);
223 m_friendListView->addListItemToView(item);
227 newUserIDs.append(user->userId());
230 m_friendListView->clearUnused(newUserIDs);
233 void FriendListPanel::hideEvent(QHideEvent *event)
235 qDebug() << __PRETTY_FUNCTION__;
237 QWidget::hideEvent(event);
238 updateKeyboardGrabbing();
241 m_friendListView->clearItemSelection();
242 setRouteButtonDisabled();
245 void FriendListPanel::routeToSelectedFriend()
247 qDebug() << __PRETTY_FUNCTION__;
249 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
252 emit routeToFriend(item->coordinates());
255 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
257 qDebug() << __PRETTY_FUNCTION__;
259 m_filterField->setVisible(visible);
260 m_clearTextFilteringButton->setVisible(visible);
263 void FriendListPanel::updateKeyboardGrabbing()
265 qDebug() << __PRETTY_FUNCTION__;
267 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
268 if (QWidget::keyboardGrabber() == m_filterField)
269 m_filterField->releaseKeyboard();
270 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
271 if (QWidget::keyboardGrabber() != m_filterField)
272 m_filterField->grabKeyboard();
276 void FriendListPanel::setRouteButtonDisabled()
278 qDebug() << __PRETTY_FUNCTION__;
280 m_routeButton->setDisabled(m_friendListView->selectedItems().isEmpty());
283 void FriendListPanel::showEvent(QShowEvent *event)
285 qDebug() << __PRETTY_FUNCTION__;
287 QWidget::showEvent(event);
288 updateKeyboardGrabbing();
289 setFilteringLayoutVisibility(false);
292 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
294 qDebug() << __PRETTY_FUNCTION__;
296 m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
298 m_headerWidget->show();
299 m_clearGroupFilteringButton->setDisabled(false);
300 m_clearGroupFilteringButton->setChecked(true);
301 m_friendListView->filter(userIDs);
303 clearTextFiltering();
305 emit openPanelRequested(this);
308 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
310 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
312 m_mainWindowIsTopmost = mainWindowIsTopmost;
313 updateKeyboardGrabbing();