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)
44 qDebug() << __PRETTY_FUNCTION__;
46 QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
47 friendListPanelLayout->setMargin(0);
48 friendListPanelLayout->setSpacing(0);
49 setLayout(friendListPanelLayout);
51 QHBoxLayout *filterLayout = new QHBoxLayout();
52 filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
53 FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
55 m_friendListHeaderWidget = new QWidget();
56 m_friendListHeaderWidget->setLayout(filterLayout);
57 m_friendListHeaderWidget->setAutoFillBackground(true);
59 m_routeButton = new QPushButton(tr("Route to friend"));
61 QPalette labelPalette = m_friendListHeaderWidget->palette();
62 labelPalette.setColor(QPalette::Background, Qt::black);
64 m_friendListHeaderWidget->setPalette(labelPalette);
65 m_friendListHeaderWidget->hide();
66 m_friendListLabel = new QLabel(this);
67 m_clearFilterButton = new QPushButton(tr("Show all"));
69 filterLayout->addWidget(m_friendListLabel);
70 filterLayout->addWidget(m_clearFilterButton);
72 m_friendListView = new FriendListView(this);
73 m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
75 QVBoxLayout *listViewLayout = new QVBoxLayout;
76 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
77 listViewLayout->addWidget(m_friendListView);
79 friendListPanelLayout->addWidget(m_routeButton);
80 friendListPanelLayout->addWidget(m_friendListHeaderWidget);
81 friendListPanelLayout->addLayout(listViewLayout);
83 connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
84 this, SIGNAL(findFriend(GeoCoordinate)));
86 connect(m_clearFilterButton, SIGNAL(clicked()),
87 this, SLOT(clearFriendListFilter()));
89 connect(m_routeButton, SIGNAL(clicked()),
90 this, SLOT(routeToSelectedFriend()));
92 /// @todo remove old filterLayout when new panel are merged
94 //////////////////////////////////////////////////////////////////////////
95 // NOTE! Do not mix the new filtering layout below with the old one above
96 //////////////////////////////////////////////////////////////////////////
99 QHBoxLayout *filteringLayout = new QHBoxLayout();
100 friendListPanelLayout->addLayout(filteringLayout);
102 // line edit for filtering
103 m_filterField = new QLineEdit;
104 filteringLayout->addWidget(m_filterField);
106 connect(m_filterField, SIGNAL(returnPressed()),
107 this, SLOT(clearFiltering()));
109 connect(m_filterField, SIGNAL(textChanged(QString)),
110 this, SLOT(filterTextChanged(QString)));
112 // button for clearing the filtering
113 m_filterClearButton = new QPushButton();
114 filteringLayout->addWidget(m_filterClearButton);
115 m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
117 connect(m_filterClearButton, SIGNAL(clicked()),
118 this, SLOT(clearFiltering()));
120 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
121 this, SLOT(topmostWindowChanged(bool)));
124 void FriendListPanel::clearFiltering()
126 qDebug() << __PRETTY_FUNCTION__;
128 ///< @todo Clear the filtering when fried list panel is closed (no hideEvent dispatched)
130 // clearing the filtering text field does cause also hiding the filtering layout
131 m_filterField->clear();
134 void FriendListPanel::clearFriendListFilter()
136 qDebug() << __PRETTY_FUNCTION__;
138 m_friendListHeaderWidget->hide();
139 m_friendListView->clearFilter();
142 void FriendListPanel::filterTextChanged(const QString &text)
144 qDebug() << __PRETTY_FUNCTION__;
146 if (m_filterField->isHidden() && !text.isEmpty())
147 setFilteringLayoutVisibility(true);
148 else if (m_filterField->isVisible() && text.isEmpty())
149 setFilteringLayoutVisibility(false);
151 m_friendListView->filter(text);
154 void FriendListPanel::friendImageReady(User *user)
156 qDebug() << __PRETTY_FUNCTION__;
158 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
161 item->setAvatarImage(user->profileImage());
164 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
166 qDebug() << __PRETTY_FUNCTION__;
168 QStringList newUserIDs;
170 foreach (User *user, friendList) {
171 FriendListItem *item = 0;
172 if (!m_friendListView->contains(user->userId())) {
173 item = new FriendListItem();
174 item->setUserData(user);
175 m_friendListView->addListItem(user->userId(), item);
177 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
181 item->setUserData(user);
182 m_friendListView->addListItemToView(item);
186 newUserIDs.append(user->userId());
189 m_friendListView->clearUnused(newUserIDs);
192 void FriendListPanel::hideEvent(QHideEvent *event)
194 qWarning() << __PRETTY_FUNCTION__;
196 QWidget::hideEvent(event);
197 updateKeyboardGrabbing();
201 void FriendListPanel::routeToSelectedFriend()
203 qDebug() << __PRETTY_FUNCTION__;
205 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
208 emit routeToFriend(item->coordinates());
211 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
213 qDebug() << __PRETTY_FUNCTION__;
215 m_filterField->setVisible(visible);
216 m_filterClearButton->setVisible(visible);
219 void FriendListPanel::updateKeyboardGrabbing()
221 qWarning() << __PRETTY_FUNCTION__;
223 if (!m_mainWindowIsTopmost || !isVisible()) {
224 if (QWidget::keyboardGrabber() == m_filterField) {
225 m_filterField->releaseKeyboard();
226 qWarning() << __PRETTY_FUNCTION__ << "released";
228 } else if (m_mainWindowIsTopmost && isVisible()) {
229 if (QWidget::keyboardGrabber() != m_filterField) {
230 m_filterField->grabKeyboard();
231 qWarning() << __PRETTY_FUNCTION__ << "grabbed";
236 void FriendListPanel::showEvent(QShowEvent *event)
238 qWarning() << __PRETTY_FUNCTION__;
240 QWidget::showEvent(event);
241 updateKeyboardGrabbing();
242 setFilteringLayoutVisibility(false);
245 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
247 qDebug() << __PRETTY_FUNCTION__;
249 m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
251 m_friendListHeaderWidget->show();
252 m_friendListView->filter(userIDs);
254 emit showPanelRequested(this);
257 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
259 qWarning() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
261 m_mainWindowIsTopmost = mainWindowIsTopmost;
262 updateKeyboardGrabbing();