Merge branch 'master' into context_driven_buttons, review
[situare] / src / ui / friendlistpanel.cpp
index ab60076..99a2585 100644 (file)
@@ -1,9 +1,12 @@
- /*
+/*
     Situare - A location system for Facebook
     Copyright (C) 2010  Ixonos Plc. Authors:
 
         Kaj Wallin - kaj.wallin@ixonos.com
         Henri Lampela - henri.lampela@ixonos.com
+        Pekka Nissinen - pekka.nissinen@ixonos.com
+        Sami Rämö - sami.ramo@ixonos.com
+        Jussi Laitinen - jussi.laitinen@ixonos.com
 
     Situare is free software; you can redistribute it and/or
     modify it under the terms of the GNU General Public License
     along with Situare; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
     USA.
- */
+*/
+
+#include <QApplication>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
+#include <QPushButton>
 
 #include "coordinates/geocoordinate.h"
-#include "listview.h"
 #include "friendlistitem.h"
 #include "friendlistitemdelegate.h"
+#include "friendlistview.h"
+#include "imagebutton.h"
 #include "panelcommon.h"
-#include "sidepanel.h"
-#include "../routing/location.h"
-#include "locationlistitem.h"
-#include "extendedlistitemdelegate.h"
+#include "user/user.h"
 
 #include "friendlistpanel.h"
 
 FriendListPanel::FriendListPanel(QWidget *parent)
-    : SidePanel(parent)
+    : PanelBase(parent),
+      m_mainWindowIsTopmost(false),
+      m_somePanelIsOpen(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    setType(SidePanel::FriendPanel);
-
-    QHBoxLayout *filterLayout = new QHBoxLayout;
-    filterLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
-                                     FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
-    m_friendListHeaderWidget = new QWidget();
-    m_friendListHeaderWidget->setLayout(filterLayout);
-    m_friendListHeaderWidget->setAutoFillBackground(true);
-    QPalette labelPalette = m_friendListHeaderWidget->palette();
-    labelPalette.setColor(QPalette::Background, Qt::black);
-    m_friendListHeaderWidget->setPalette(labelPalette);
-    m_friendListHeaderWidget->hide();
-    m_friendListLabel = new QLabel(this);
-    m_clearFilterButton = new QPushButton(tr("Show all"));
-    filterLayout->addWidget(m_friendListLabel);
-    filterLayout->addWidget(m_clearFilterButton);
-    m_panelVBox->addWidget(m_friendListHeaderWidget);
-
-    QHBoxLayout *friendListLayout =  new QHBoxLayout;
-    friendListLayout->setContentsMargins(FRIENDPANEL_MARGIN_LEFT, FRIENDPANEL_MARGIN_TOP,
-                                         FRIENDPANEL_MARGIN_RIGHT, FRIENDPANEL_MARGIN_BOTTOM);
-
-    m_friendListView = new ListView(this);
-    m_friendListView->setAutoFillBackground(false);
-    m_friendListView->viewport()->setAutoFillBackground(false);
-    m_friendListItemDelegate = new FriendListItemDelegate();
-    m_friendListView->setItemDelegate(m_friendListItemDelegate);
-    //REMOVE
-    m_friendListView->hide();
-
-    m_locationListView = new ListView(this);
-    m_locationListView->setAutoFillBackground(false);
-    m_locationListView->viewport()->setAutoFillBackground(false);
-    m_locationListView->setItemDelegate(new ExtendedListItemDelegate());
-
-    //friendListLayout->addWidget(m_friendListView);
-    friendListLayout->addWidget(m_locationListView);
-    m_panelVBox->addLayout(friendListLayout);
-
-    connect(m_friendListView, SIGNAL(listItemClicked(GeoCoordinate)),
+
+    const int FRIENDPANEL_FILTER_MARGIN_LEFT = PANEL_MARGIN_LEFT + 4;
+    const int FRIENDPANEL_FILTER_MARGIN_TOP = 0;
+    const int FRIENDPANEL_FILTER_MARGIN_RIGHT = PANEL_MARGIN_RIGHT + MAEMO5_SCROLLBAR_WIDTH + 4;
+    const int FRIENDPANEL_FILTER_MARGIN_BOTTOM = 0;
+
+    // --- HEADER, HOW MANY FRIENDS ARE SELECTED ---
+    m_headerWidget = new QWidget();
+
+    m_headerWidget->hide();
+    m_headerWidget->setAutoFillBackground(true);
+
+    QPalette headerPalette = m_headerWidget->palette();
+    headerPalette.setColor(QPalette::Background, Qt::black);
+    m_headerWidget->setPalette(headerPalette);
+
+    QHBoxLayout *headerLayout = new QHBoxLayout();
+    m_headerWidget->setLayout(headerLayout);
+    headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT,
+                                     FRIENDPANEL_FILTER_MARGIN_TOP,
+                                     FRIENDPANEL_FILTER_MARGIN_RIGHT,
+                                     FRIENDPANEL_FILTER_MARGIN_BOTTOM);
+
+    m_headerLabel = new QLabel(this);
+    headerLayout->addWidget(m_headerLabel, 0, Qt::AlignCenter);
+
+    // --- FRIEND LIST ---
+    m_friendListView = new FriendListView(this);
+    m_friendListView->setItemDelegate(new FriendListItemDelegate(this));
+
+    QVBoxLayout *listViewLayout = new QVBoxLayout;
+    listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
+                                       PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
+    listViewLayout->addWidget(m_friendListView);
+
+    connect(m_friendListView, SIGNAL(friendItemClicked(GeoCoordinate)),
             this, SIGNAL(findFriend(GeoCoordinate)));
 
-    connect(m_clearFilterButton, SIGNAL(clicked()),
-            this, SLOT(clearFriendListFilter()));
-    connect(this, SIGNAL(panelOpened()),
-            this, SLOT(clearFriendListFilter()));
+    connect(m_friendListView, SIGNAL(listItemSelectionChanged()),
+            this, SLOT(onListItemSelectionChanged()));
+
+    // --- FOOTER, TEXT BASED FILTERING ---
+    QHBoxLayout *footerLayout = new QHBoxLayout();
+
+    m_filterField = new QLineEdit;
+    footerLayout->addWidget(m_filterField);
+
+    connect(m_filterField, SIGNAL(returnPressed()),
+            this, SLOT(clearTextFiltering()));
+
+    connect(m_filterField, SIGNAL(textChanged(QString)),
+            this, SLOT(filterTextChanged(QString)));
+
+    m_clearTextFilteringButton = new QPushButton();
+    footerLayout->addWidget(m_clearTextFilteringButton);
+    m_clearTextFilteringButton->setIcon(QIcon::fromTheme(QLatin1String("general_close")));
+
+    connect(m_clearTextFilteringButton, SIGNAL(clicked()),
+            this, SLOT(clearTextFiltering()));
+
+    connect(qApp, SIGNAL(topmostWindowChanged(bool)),
+            this, SLOT(topmostWindowChanged(bool)));
+
+    // --- MAIN LAYOUT ---
+    QVBoxLayout *friendListPanelLayout = new QVBoxLayout();
+    friendListPanelLayout->setMargin(0);
+    friendListPanelLayout->setSpacing(0);
+    setLayout(friendListPanelLayout);
+
+    friendListPanelLayout->addWidget(m_headerWidget);
+    friendListPanelLayout->addLayout(listViewLayout);
+    friendListPanelLayout->addLayout(footerLayout);
+
+    // --- CONTEXT BUTTONS ---
+    m_routeButton = new ImageButton(":res/images/route_to_friend.png",
+                                    ":res/images/route_to_friend_s.png",
+                                    ":res/images/route_to_friend_d.png", this);
+    connect(m_routeButton, SIGNAL(clicked()),
+            this, SLOT(routeToSelectedFriend()));
+
+    m_showContactButton = new ImageButton(":res/images/contact_btn.png",
+                                          ":res/images/contact_btn_s.png",
+                                          ":res/images/contact_btn_d.png", this);
+    connect(m_showContactButton, SIGNAL(clicked()),
+            this, SLOT(requestSelectedFriendContactDialog()));
+
+    m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
+                                                  ":res/images/filtered_s.png",
+                                                  ":res/images/filtered_d.png", this);
+    m_clearGroupFilteringButton->setCheckable(true);
+    m_clearGroupFilteringButton->setDisabled(true);
+    connect(m_clearGroupFilteringButton, SIGNAL(clicked()),
+            this, SLOT(clearFiltering()));
+
+    m_itemButtonsLayout->addWidget(m_routeButton);
+    m_itemButtonsLayout->addWidget(m_showContactButton);
+    m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
+}
+
+void FriendListPanel::anyPanelClosed()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_somePanelIsOpen = false;
+    updateKeyboardGrabbing();
+
+    clearFiltering();
+
+    m_friendListView->clearItemSelection();
+}
+
+void FriendListPanel::anyPanelOpened()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_somePanelIsOpen = true;
+    updateKeyboardGrabbing();
+}
+
+void FriendListPanel::clearFiltering()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_headerWidget->hide();
+    m_clearGroupFilteringButton->setChecked(false);
+    m_clearGroupFilteringButton->setDisabled(true);
+    m_friendListView->clearFilter();
+    clearTextFiltering();
+}
+
+void FriendListPanel::clearTextFiltering()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    // clearing the filtering text field does cause also hiding the filtering layout
+    m_filterField->clear();
+}
+
+void FriendListPanel::filterTextChanged(const QString &text)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_filterField->isHidden() && !text.isEmpty())
+        setFilteringLayoutVisibility(true);
+    else if (m_filterField->isVisible() && text.isEmpty())
+        setFilteringLayoutVisibility(false);
+
+    m_friendListView->filter(text);
 }
 
 void FriendListPanel::friendImageReady(User *user)
@@ -106,8 +217,7 @@ void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
             item = new FriendListItem();
             item->setUserData(user);
             m_friendListView->addListItem(user->userId(), item);
-        }
-        else {
+        } else {
             item = static_cast<FriendListItem *>(m_friendListView->takeListItemFromView(
                     user->userId()));
 
@@ -123,34 +233,90 @@ void FriendListPanel::friendInfoReceived(QList<User *> &friendList)
     m_friendListView->clearUnused(newUserIDs);
 }
 
-void FriendListPanel::clearFriendListFilter()
+void FriendListPanel::hideEvent(QHideEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_friendListHeaderWidget->hide();
-    m_friendListView->clearFilter();
+    QWidget::hideEvent(event);
+    updateKeyboardGrabbing();
+    clearFiltering();
+
+    m_friendListView->clearItemSelection();
+}
+
+void FriendListPanel::requestSelectedFriendContactDialog()
+{
+     qDebug() << __PRETTY_FUNCTION__;
+
+     FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
+
+     if (item) {
+         QString facebookId = item->facebookId();
+         if (!facebookId.isEmpty())
+             emit requestContactDialog(facebookId);
+     }
+}
+
+void FriendListPanel::routeToSelectedFriend()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
+
+    if (item)
+        emit routeToFriend(item->coordinates());
 }
 
-void FriendListPanel::locationDataReady(QList<Location> &result)
+void FriendListPanel::setFilteringLayoutVisibility(bool visible)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_filterField->setVisible(visible);
+    m_clearTextFilteringButton->setVisible(visible);
+}
 
-    for (int i = 0; i < result.size(); ++i) {
-        LocationListItem *item = new LocationListItem();
-        item->setLocationData(result.at(i));
-        m_locationListView->addListItem(QString::number(i), item);
+void FriendListPanel::updateKeyboardGrabbing()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_mainWindowIsTopmost || !m_somePanelIsOpen || !isVisible()) {
+        if (QWidget::keyboardGrabber() == m_filterField)
+            m_filterField->releaseKeyboard();
+    } else if (m_mainWindowIsTopmost && m_somePanelIsOpen && isVisible()) {
+        if (QWidget::keyboardGrabber() != m_filterField)
+            m_filterField->grabKeyboard();
     }
+}
 
+void FriendListPanel::showEvent(QShowEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
+    QWidget::showEvent(event);
+    updateKeyboardGrabbing();
+    setFilteringLayoutVisibility(false);
 }
 
 void FriendListPanel::showFriendsInList(const QList<QString> &userIDs)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_friendListLabel->setText(tr("Selected: %1").arg(userIDs.count()));
+    m_headerLabel->setText(tr("Selected: %1").arg(userIDs.count()));
 
-    openPanel();
-    m_friendListHeaderWidget->show();
+    m_headerWidget->show();
+    m_clearGroupFilteringButton->setDisabled(false);
+    m_clearGroupFilteringButton->setChecked(true);
     m_friendListView->filter(userIDs);
+
+    clearTextFiltering();
+
+    emit openPanelRequested(this);
+}
+
+void FriendListPanel::topmostWindowChanged(bool mainWindowIsTopmost)
+{
+    qDebug() << __PRETTY_FUNCTION__ << mainWindowIsTopmost;
+
+    m_mainWindowIsTopmost = mainWindowIsTopmost;
+    updateKeyboardGrabbing();
 }