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 "panelcommon.h"
36 #include "user/user.h"
38 #include "friendlistpanel.h"
40 FriendListPanel::FriendListPanel(QWidget *parent)
42 m_mainWindowIsTopmost(false),
43 m_somePanelIsOpen(false)
45 qDebug() << __PRETTY_FUNCTION__;
47 const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
48 const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
49 const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
50 const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
52 QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
53 friendListPanelLayout->setMargin(0);
54 friendListPanelLayout->setSpacing(0);
55 setLayout(friendListPanelLayout);
57 QHBoxLayout *filterLayout = new QHBoxLayout();
58 filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, FRIENDPANEL_FILTER_MARGIN_TOP,
59 FRIENDPANEL_FILTER_MARGIN_RIGHT, FRIENDPANEL_FILTER_MARGIN_BOTTOM);
61 m_friendListHeaderWidget = new QWidget();
62 m_friendListHeaderWidget->setLayout(filterLayout);
63 m_friendListHeaderWidget->setAutoFillBackground(true);
65 m_routeButton = new QPushButton(tr("Route to friend"));
67 QPalette labelPalette = m_friendListHeaderWidget->palette();
68 labelPalette.setColor(QPalette::Background, Qt::black);
70 m_friendListHeaderWidget->setPalette(labelPalette);
71 m_friendListHeaderWidget->hide();
72 m_friendListLabel = new QLabel(this);
73 m_clearFilterButton = new QPushButton(tr("Show all"));
75 filterLayout->addWidget(m_friendListLabel);
76 filterLayout->addWidget(m_clearFilterButton);
78 m_friendListView = new FriendListView(this);
79 m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
81 QVBoxLayout *listViewLayout = new QVBoxLayout;
82 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
83 listViewLayout->addWidget(m_friendListView);
85 friendListPanelLayout->addWidget(m_routeButton);
86 friendListPanelLayout->addWidget(m_friendListHeaderWidget);
87 friendListPanelLayout->addLayout(listViewLayout);
89 connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
90 this, SIGNAL(findFriend(GeoCoordinate)));
92 connect(m_clearFilterButton, SIGNAL(clicked()),
93 this, SLOT(clearFiltering()));
95 connect(m_routeButton, SIGNAL(clicked()),
96 this, SLOT(routeToSelectedFriend()));
98 /// @todo remove old filterLayout when new panel are merged
100 //////////////////////////////////////////////////////////////////////////
101 // NOTE! Do not mix the new filtering layout below with the old one above
102 //////////////////////////////////////////////////////////////////////////
105 QHBoxLayout *filteringLayout = new QHBoxLayout();
106 friendListPanelLayout->addLayout(filteringLayout);
108 // line edit for filtering
109 m_filterField = new QLineEdit;
110 filteringLayout->addWidget(m_filterField);
112 connect(m_filterField, SIGNAL(returnPressed()),
113 this, SLOT(clearTextFiltering()));
115 connect(m_filterField, SIGNAL(textChanged(QString)),
116 this, SLOT(filterTextChanged(QString)));
118 // button for clearing the filtering
119 m_filterClearButton = new QPushButton();
120 filteringLayout->addWidget(m_filterClearButton);
121 m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
123 connect(m_filterClearButton, SIGNAL(clicked()),
124 this, SLOT(clearTextFiltering()));
126 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
127 this, SLOT(topmostWindowChanged(bool)));
130 void FriendListPanel::anyPanelClosed()
132 qDebug() << __PRETTY_FUNCTION__;
134 m_somePanelIsOpen = false;
135 updateKeyboardGrabbing();
140 void FriendListPanel::anyPanelOpened()
142 qDebug() << __PRETTY_FUNCTION__;
144 m_somePanelIsOpen = true;
145 updateKeyboardGrabbing();
148 void FriendListPanel::clearFiltering()
150 qDebug() << __PRETTY_FUNCTION__;
152 m_friendListHeaderWidget->hide();
153 m_friendListView->clearFilter();
154 clearTextFiltering();
157 void FriendListPanel::clearTextFiltering()
159 qDebug() << __PRETTY_FUNCTION__;
161 // clearing the filtering text field does cause also hiding the filtering layout
162 m_filterField->clear();
165 void FriendListPanel::filterTextChanged(const QString &text)
167 qDebug() << __PRETTY_FUNCTION__;
169 if (m_filterField->isHidden() && !text.isEmpty())
170 setFilteringLayoutVisibility(true);
171 else if (m_filterField->isVisible() && text.isEmpty())
172 setFilteringLayoutVisibility(false);
174 m_friendListView->filter(text);
177 void FriendListPanel::friendImageReady(User *user)
179 qDebug() << __PRETTY_FUNCTION__;
181 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
184 item->setAvatarImage(user->profileImage());
187 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
189 qDebug() << __PRETTY_FUNCTION__;
191 QStringList newUserIDs;
193 foreach (User *user, friendList) {
194 FriendListItem *item = 0;
195 if (!m_friendListView->contains(user->userId())) {
196 item = new FriendListItem();
197 item->setUserData(user);
198 m_friendListView->addListItem(user->userId(), item);
200 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
204 item->setUserData(user);
205 m_friendListView->addListItemToView(item);
209 newUserIDs.append(user->userId());
212 m_friendListView->clearUnused(newUserIDs);
215 void FriendListPanel::hideEvent(QHideEvent *event)
217 qDebug() << __PRETTY_FUNCTION__;
219 QWidget::hideEvent(event);
220 updateKeyboardGrabbing();
224 void FriendListPanel::routeToSelectedFriend()
226 qDebug() << __PRETTY_FUNCTION__;
228 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
231 emit routeToFriend(item->coordinates());
234 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
236 qDebug() << __PRETTY_FUNCTION__;
238 m_filterField->setVisible(visible);
239 m_filterClearButton->setVisible(visible);
242 void FriendListPanel::updateKeyboardGrabbing()
244 qDebug() << __PRETTY_FUNCTION__;
246 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
247 if (QWidget::keyboardGrabber() == m_filterField)
248 m_filterField->releaseKeyboard();
249 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
250 if (QWidget::keyboardGrabber() != m_filterField)
251 m_filterField->grabKeyboard();
255 void FriendListPanel::showEvent(QShowEvent *event)
257 qDebug() << __PRETTY_FUNCTION__;
259 QWidget::showEvent(event);
260 updateKeyboardGrabbing();
261 setFilteringLayoutVisibility(false);
264 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
266 qDebug() << __PRETTY_FUNCTION__;
268 m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
270 m_friendListHeaderWidget->show();
271 m_friendListView->filter(userIDs);
273 clearTextFiltering();
275 emit showPanelRequested(this);
278 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
280 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
282 m_mainWindowIsTopmost = mainWindowIsTopmost;
283 updateKeyboardGrabbing();