Fix: Routing instructions list was not cleared when route was deleted.
[situare] / src / ui / routingpanel.cpp
index eca995f..9ead7be 100644 (file)
+/*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        Jussi Laitinen - jussi.laitinen@ixonos.com
+        Sami Rämö - sami.ramo@ixonos.com
+
+    Situare is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    version 2 as published by the Free Software Foundation.
+
+    Situare is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy 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 "coordinates/geocoordinate.h"
-#include "locationlistitem.h"
-#include "locationlistview.h"
 #include "extendedlistitemdelegate.h"
+#include "imagebutton.h"
+#include "panelcommon.h"
+#include "routewaypointlistitem.h"
+#include "routewaypointlistview.h"
 #include "routing/location.h"
 #include "routing/route.h"
-#include "routewaypointlistview.h"
-#include "routewaypointlistitem.h"
-#include "panelcommon.h"
 
 #include "routingpanel.h"
 
 RoutingPanel::RoutingPanel(QWidget *parent)
-    : QWidget(parent)
+    : PanelBase(parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QVBoxLayout *routingLayout = new QVBoxLayout;
-    routingLayout->setMargin(0);
-    routingLayout->setSpacing(0);
-    setLayout(routingLayout);
-
-    QHBoxLayout *headerLayout = new QHBoxLayout();
-    headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
-                                     FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
-
-    QVBoxLayout *listViewLayout = new QVBoxLayout;
-    listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
-
-    m_searchLocationButton = new QPushButton(tr("Search location"));
-
-    m_routeButton = new QPushButton(tr("Route to location"));
-    m_routeButton->hide();
-
-    m_locationListHeaderWidget = new QWidget();
-    m_locationListHeaderWidget->setLayout(headerLayout);
-    m_locationListHeaderWidget->setAutoFillBackground(true);
-    QPalette labelPalette = m_locationListHeaderWidget->palette();
-    labelPalette.setColor(QPalette::Background, Qt::black);
-    m_locationListHeaderWidget->setPalette(labelPalette);
-    m_locationListHeaderWidget->hide();
-
-    m_locationListLabel = new QLabel(this);
-
-    m_locationListView = new LocationListView(this);
-    m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
-    m_locationListView->hide();
-
+    // --- LAYOUT & ROUTING INSTRUCTIONS VIEW ---
     m_routeWaypointListView = new RouteWaypointListView(this);
     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
-    m_routeWaypointListView->hide();
 
-    headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
-
-    listViewLayout->addWidget(m_locationListView);
-    listViewLayout->addWidget(m_routeWaypointListView);
+    connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
+            this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
 
-    routingLayout->addWidget(m_searchLocationButton);
-    routingLayout->addWidget(m_routeButton);
-    routingLayout->addWidget(m_locationListHeaderWidget);
-    routingLayout->addLayout(listViewLayout);
+    QVBoxLayout *panelLayout = new QVBoxLayout;
+    panelLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
+                                    PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
 
-    connect(m_locationListView,
-            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
-            this,
-            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
+    panelLayout->addWidget(m_routeWaypointListView);
+    setLayout(panelLayout);
 
-    connect(m_routeButton, SIGNAL(clicked()),
-            this, SLOT(routeToSelectedLocation()));
+    // --- CONTEXT BUTTONS ---
+    m_clearRouteButton = new ImageButton(":/res/images/gps_position.png",
+                                         "", "", this);
+    connect(m_clearRouteButton, SIGNAL(clicked()),
+            this, SLOT(clearRouteButtonClicked()));
+    m_clearRouteButton->setDisabled(true);
 
-    connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
-            this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
+    ImageButton *routeToCursorButton = new ImageButton(":res/images/sight.png",
+                                                       "", "", this);
+    connect(routeToCursorButton, SIGNAL(clicked()),
+            this, SIGNAL(routeToCursor()));
 
-    connect(m_searchLocationButton, SIGNAL(clicked()),
-            this, SIGNAL(requestSearchLocation()));
+    m_contextButtonLayout->addWidget(m_clearRouteButton);
+    m_contextButtonLayout->addWidget(routeToCursorButton);
 }
 
-void RoutingPanel::populateLocationListView(const QList<Location> &locations)
+void RoutingPanel::clearListsSelections()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_routeButton->show();
-
-    m_locationListHeaderWidget->show();
-    m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
-
-    m_routeWaypointListView->hide();
-    m_locationListView->show();
-    m_locationListView->clearList();
-
-    for (int i = 0; i < locations.size(); ++i) {
-        LocationListItem *item = new LocationListItem();
-        item->setLocationData(locations.at(i));
-        m_locationListView->addListItem(QString::number(i), item);
-    }
-
-    const int FIRST_LOCATION_ITEM_INDEX = 0;
-    const int ONE_LOCATION_ITEM = 1;
+    m_routeWaypointListView->clearItemSelection();
+}
 
-    if (locations.size() == ONE_LOCATION_ITEM) {
-        ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
+void RoutingPanel::clearRouteButtonClicked()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        if (item)
-            m_locationListView->setSelectedItem(item);
-    }
+    m_clearRouteButton->setDisabled(true);
+    m_routeWaypointListView->clearList();
+    emit clearRoute();
 }
 
-void RoutingPanel::routeToSelectedLocation()
+void RoutingPanel::hideEvent(QHideEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    LocationListItem *item = dynamic_cast<LocationListItem *>
-                             (m_locationListView->selectedItem());
+    QWidget::hideEvent(event);
 
-    if (item)
-        emit routeToLocation(item->coordinates());
+    clearListsSelections();
 }
 
 void RoutingPanel::setRoute(Route &route)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_routeButton->hide();
-
-    m_locationListHeaderWidget->hide();
-    m_locationListView->hide();
-    m_routeWaypointListView->show();
     m_routeWaypointListView->clearList();
 
     QList<RouteSegment> segments = route.segments();
@@ -138,4 +108,10 @@ void RoutingPanel::setRoute(Route &route)
 
         m_routeWaypointListView->addListItem(QString::number(i), item);
     }
+
+    m_routeWaypointListView->scrollToTop();
+
+    m_clearRouteButton->setDisabled(false);
+
+    emit openPanelRequested(this);
 }