Changed the button type, keyboard grab without error message
[situare] / src / ui / friendlistpanel.cpp
1  /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Henri Lampela - henri.lampela@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21  */
22
23 #include "coordinates/geocoordinate.h"
24 #include "listview.h"
25 #include "friendlistitem.h"
26 #include "friendlistitemdelegate.h"
27 #include "panelcommon.h"
28 #include "sidepanel.h"
29
30 #include "friendlistpanel.h"
31
32 FriendListPanel::FriendListPanel(QWidget *parent)
33     : SidePanel(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36     setType(SidePanel::FriendPanel);
37
38     QHBoxLayout *filterLayout = new QHBoxLayout;
39     filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
40                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
41     m_friendListHeaderWidget = new QWidget();
42     m_friendListHeaderWidget->setLayout(filterLayout);
43     m_friendListHeaderWidget->setAutoFillBackground(true);
44     QPalette labelPalette = m_friendListHeaderWidget->palette();
45     labelPalette.setColor(QPalette::Background, Qt::black);
46     m_friendListHeaderWidget->setPalette(labelPalette);
47     m_friendListHeaderWidget->hide();
48     m_friendListLabel = new QLabel(this);
49     m_clearFilterButton = new QPushButton(tr("Show all"));
50     filterLayout->addWidget(m_friendListLabel);
51     filterLayout->addWidget(m_clearFilterButton);
52     m_panelVBox->addWidget(m_friendListHeaderWidget);
53
54     QHBoxLayout *friendListLayout =  new QHBoxLayout;
55     friendListLayout->setContentsMargins(FRIENDPANEL_MARGIN_LEFT, FRIENDPANEL_MARGIN_TOP,
56                                          FRIENDPANEL_MARGIN_RIGHT, FRIENDPANEL_MARGIN_BOTTOM);
57
58     m_friendListView = new ListView(this);
59     m_friendListView->setAutoFillBackground(false);
60     m_friendListView->viewport()->setAutoFillBackground(false);
61     m_friendListItemDelegate = new FriendListItemDelegate();
62     m_friendListView->setItemDelegate(m_friendListItemDelegate);
63
64     friendListLayout->addWidget(m_friendListView);
65     m_panelVBox->addLayout(friendListLayout);
66
67     connect(m_friendListView, SIGNAL(listItemClicked(GeoCoordinate)),
68             this, SIGNAL(findFriend(GeoCoordinate)));
69
70     connect(m_clearFilterButton, SIGNAL(clicked()),
71             this, SLOT(clearFriendListFilter()));
72     connect(this, SIGNAL(panelOpened()),
73             this, SLOT(clearFriendListFilter()));
74
75     /// @todo remove old filterLayout when new panel are merged
76
77     //////////////////////////////////////////////////////////////////////////
78     // NOTE! Do not mix the new filtering layout below with the old one above
79     //////////////////////////////////////////////////////////////////////////
80
81     // filtering layout
82     QHBoxLayout *filteringLayout = new QHBoxLayout();
83     m_panelVBox->addLayout(filteringLayout);
84
85     // line edit for filtering
86     m_filterField = new QLineEdit;
87     filteringLayout->addWidget(m_filterField);
88
89     connect(m_filterField, SIGNAL(returnPressed()),
90             this, SLOT(clearFiltering()));
91
92     connect(m_filterField, SIGNAL(textChanged(QString)),
93             this, SLOT(filterTextChanged(QString)));
94
95     // button for clearing the filtering
96     m_filterClearButton = new QPushButton();
97     filteringLayout->addWidget(m_filterClearButton);
98     m_filterClearButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
99
100     connect(m_filterClearButton, SIGNAL(clicked()),
101             this, SLOT(clearFiltering()));
102 }
103
104 void FriendListPanel::clearFiltering()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     // clearing the filtering text field does cause also hiding the filtering layout
109     m_filterField->clear();
110 }
111
112 void FriendListPanel::clearFriendListFilter()
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     m_friendListHeaderWidget->hide();
117     m_friendListView->clearFilter();
118 }
119
120 void FriendListPanel::filterTextChanged(const QString &text)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     if (m_filterField->isHidden() && !text.isEmpty())
125         setFilteringLayoutVisible(true);
126     else if (m_filterField->isVisible() && text.isEmpty())
127         setFilteringLayoutVisible(false);
128 }
129
130 void FriendListPanel::friendImageReady(User *user)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     FriendListItem *item = static_cast<FriendListItem*>(m_friendListView->listItem(user->userId()));
135
136     if (item)
137         item->setAvatarImage(user->profileImage());
138 }
139
140 void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     QStringList newUserIDs;
145
146     foreach (User *user, friendList) {
147         FriendListItem *item = 0;
148         if (!m_friendListView->contains(user->userId())) {
149             item = new FriendListItem();
150             item->setUserData(user);
151             m_friendListView->addListItem(user->userId(), item);
152         }
153         else {
154             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
155                     user->userId()));
156
157             if (item) {
158                 item->setUserData(user);
159                 m_friendListView->addListItemToView(item);
160             }
161         }
162
163         newUserIDs.append(user->userId());
164     }
165
166     m_friendListView->clearUnused(newUserIDs);
167 }
168
169 void FriendListPanel::setFilteringLayoutVisible(bool visible)
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     m_filterField->setVisible(visible);
174     m_filterClearButton->setVisible(visible);
175 }
176
177 void FriendListPanel::showEvent(QShowEvent *event)
178 {
179     qWarning() << __PRETTY_FUNCTION__;
180
181     QWidget::showEvent(event);
182
183     if (QWidget::keyboardGrabber() != qobject_cast<QWidget *>(m_filterField)) {
184         qWarning() << __PRETTY_FUNCTION__ << "grabbing the keyboard for filter text field";
185         m_filterField->grabKeyboard();
186         setFilteringLayoutVisible(false);
187     }
188 }
189
190 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
191 {
192     qDebug() << __PRETTY_FUNCTION__;
193
194     m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
195
196     openPanel();
197     m_friendListHeaderWidget->show();
198     m_friendListView->filter(userIDs);
199 }