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"));
66 m_routeButton->setDisabled(true);
68 m_showContactButton = new QPushButton(tr("Show contact"));
69 m_showContactButton->setDisabled(true);
71 QPalette labelPalette = m_friendListHeaderWidget->palette();
72 labelPalette.setColor(QPalette::Background, Qt::black);
74 m_friendListHeaderWidget->setPalette(labelPalette);
75 m_friendListHeaderWidget->hide();
76 m_friendListLabel = new QLabel(this);
77 m_clearFilterButton = new QPushButton(tr("Show all"));
79 filterLayout->addWidget(m_friendListLabel);
80 filterLayout->addWidget(m_clearFilterButton);
82 m_friendListView = new FriendListView(this);
83 m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
85 QVBoxLayout *listViewLayout = new QVBoxLayout;
86 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
87 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
88 listViewLayout->addWidget(m_friendListView);
90 friendListPanelLayout->addWidget(m_showContactButton);
91 friendListPanelLayout->addWidget(m_routeButton);
92 friendListPanelLayout->addWidget(m_friendListHeaderWidget);
93 friendListPanelLayout->addLayout(listViewLayout);
95 connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
96 this, SIGNAL(findFriend(GeoCoordinate)));
98 connect(m_clearFilterButton, SIGNAL(clicked()),
99 this, SLOT(clearFiltering()));
101 connect(m_routeButton, SIGNAL(clicked()),
102 this, SLOT(routeToSelectedFriend()));
104 connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
105 this, SLOT(setSelectionButtonsDisabled()));
107 /// @todo remove old filterLayout when new panel are merged
109 //////////////////////////////////////////////////////////////////////////
110 // NOTE! Do not mix the new filtering layout below with the old one above
111 //////////////////////////////////////////////////////////////////////////
114 QHBoxLayout *filteringLayout = new QHBoxLayout();
115 friendListPanelLayout->addLayout(filteringLayout);
117 // line edit for filtering
118 m_filterField = new QLineEdit;
119 filteringLayout->addWidget(m_filterField);
121 connect(m_filterField, SIGNAL(returnPressed()),
122 this, SLOT(clearTextFiltering()));
124 connect(m_filterField, SIGNAL(textChanged(QString)),
125 this, SLOT(filterTextChanged(QString)));
127 // button for clearing the filtering
128 m_filterClearButton = new QPushButton();
129 filteringLayout->addWidget(m_filterClearButton);
130 m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
132 connect(m_filterClearButton, SIGNAL(clicked()),
133 this, SLOT(clearTextFiltering()));
135 connect(qApp, SIGNAL(topmostWindowChanged(bool)),
136 this, SLOT(topmostWindowChanged(bool)));
138 connect(m_showContactButton, SIGNAL(clicked()),
139 this, SLOT(requestSelectedFriendContactDialog()));
142 void FriendListPanel::anyPanelClosed()
144 qDebug() << __PRETTY_FUNCTION__;
146 m_somePanelIsOpen = false;
147 updateKeyboardGrabbing();
151 m_friendListView->clearItemSelection();
152 setSelectionButtonsDisabled();
155 void FriendListPanel::anyPanelOpened()
157 qDebug() << __PRETTY_FUNCTION__;
159 m_somePanelIsOpen = true;
160 updateKeyboardGrabbing();
163 void FriendListPanel::clearFiltering()
165 qDebug() << __PRETTY_FUNCTION__;
167 m_friendListHeaderWidget->hide();
168 m_friendListView->clearFilter();
169 clearTextFiltering();
172 void FriendListPanel::clearTextFiltering()
174 qDebug() << __PRETTY_FUNCTION__;
176 // clearing the filtering text field does cause also hiding the filtering layout
177 m_filterField->clear();
180 void FriendListPanel::filterTextChanged(const QString &text)
182 qDebug() << __PRETTY_FUNCTION__;
184 if (m_filterField->isHidden() && !text.isEmpty())
185 setFilteringLayoutVisibility(true);
186 else if (m_filterField->isVisible() && text.isEmpty())
187 setFilteringLayoutVisibility(false);
189 m_friendListView->filter(text);
192 void FriendListPanel::friendImageReady(User *user)
194 qDebug() << __PRETTY_FUNCTION__;
196 FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
199 item->setAvatarImage(user->profileImage());
202 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
204 qDebug() << __PRETTY_FUNCTION__;
206 QStringList newUserIDs;
208 foreach (User *user, friendList) {
209 FriendListItem *item = 0;
210 if (!m_friendListView->contains(user->userId())) {
211 item = new FriendListItem();
212 item->setUserData(user);
213 m_friendListView->addListItem(user->userId(), item);
215 item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
219 item->setUserData(user);
220 m_friendListView->addListItemToView(item);
224 newUserIDs.append(user->userId());
227 m_friendListView->clearUnused(newUserIDs);
230 void FriendListPanel::hideEvent(QHideEvent *event)
232 qDebug() << __PRETTY_FUNCTION__;
234 QWidget::hideEvent(event);
235 updateKeyboardGrabbing();
238 m_friendListView->clearItemSelection();
239 setSelectionButtonsDisabled();
242 void FriendListPanel::requestSelectedFriendContactDialog()
244 qDebug() << __PRETTY_FUNCTION__;
246 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
249 QString facebookId = item->facebookId();
250 if (!facebookId.isEmpty())
251 emit requestContactDialog(facebookId);
255 void FriendListPanel::routeToSelectedFriend()
257 qDebug() << __PRETTY_FUNCTION__;
259 FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
262 emit routeToFriend(item->coordinates());
265 void FriendListPanel::setFilteringLayoutVisibility(bool visible)
267 qDebug() << __PRETTY_FUNCTION__;
269 m_filterField->setVisible(visible);
270 m_filterClearButton->setVisible(visible);
273 void FriendListPanel::setSelectionButtonsDisabled()
275 qDebug() << __PRETTY_FUNCTION__;
277 bool isAnyItemSelected = m_friendListView->selectedItems().isEmpty();
279 m_routeButton->setDisabled(isAnyItemSelected);
280 m_showContactButton->setDisabled(isAnyItemSelected);
283 void FriendListPanel::updateKeyboardGrabbing()
285 qDebug() << __PRETTY_FUNCTION__;
287 if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
288 if (QWidget::keyboardGrabber() == m_filterField)
289 m_filterField->releaseKeyboard();
290 } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
291 if (QWidget::keyboardGrabber() != m_filterField)
292 m_filterField->grabKeyboard();
296 void FriendListPanel::showEvent(QShowEvent *event)
298 qDebug() << __PRETTY_FUNCTION__;
300 QWidget::showEvent(event);
301 updateKeyboardGrabbing();
302 setFilteringLayoutVisibility(false);
305 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
307 qDebug() << __PRETTY_FUNCTION__;
309 m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
311 m_friendListHeaderWidget->show();
312 m_friendListView->filter(userIDs);
314 clearTextFiltering();
316 emit showPanelRequested(this);
319 void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
321 qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
323 m_mainWindowIsTopmost = mainWindowIsTopmost;
324 updateKeyboardGrabbing();