Fix: Routing instructions list was not cleared when route was deleted.
[situare] / src / ui / routingpanel.cpp
index daed223..9ead7be 100644 (file)
@@ -1,13 +1,33 @@
+/*
+    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 "extendedlistitemdelegate.h"
-#include "locationlistitem.h"
-#include "locationlistview.h"
 #include "imagebutton.h"
 #include "panelcommon.h"
-#include "routing/location.h"
-#include "routing/route.h"
 #include "routewaypointlistitem.h"
 #include "routewaypointlistview.h"
+#include "routing/location.h"
+#include "routing/route.h"
 
 #include "routingpanel.h"
 
@@ -16,193 +36,66 @@ RoutingPanel::RoutingPanel(QWidget *parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    // --- ROUTING INSTRUCTIONS WIDGET ---
+    // --- LAYOUT & ROUTING INSTRUCTIONS VIEW ---
     m_routeWaypointListView = new RouteWaypointListView(this);
     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
-//    m_routeWaypointListView->hide();
 
     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
 
-    QVBoxLayout *routingViewLayout = new QVBoxLayout;
-    routingViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                          PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
-
-    // main routing layout
-    m_routingView = new QWidget();
-    m_routingView->setLayout(routingViewLayout);
-    routingViewLayout->addWidget(m_routeWaypointListView);
-
-    // --- SEARCH RESULTS WIDGET ---
-    // header
-    QWidget *resultsHeaderWidget = new QWidget();
-    resultsHeaderWidget->setAutoFillBackground(true);
-    QPalette labelPalette = resultsHeaderWidget->palette();
-    labelPalette.setColor(QPalette::Background, Qt::black);
-    resultsHeaderWidget->setPalette(labelPalette);
-//    m_resultsHeaderWidget->hide();
-
-    QHBoxLayout *headerLayout = new QHBoxLayout();
-    resultsHeaderWidget->setLayout(headerLayout);
-    headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
-
-    m_resultsLabel = new QLabel(this);
-    headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
-
-    // list view
-    m_locationListView = new LocationListView(this);
-    m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
-//    m_locationListView->hide();
-
-    connect(m_locationListView,
-            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
-            this,
-            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
-
-    connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
-            this, SLOT(setRouteButtonDisabled()));
-
-    QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
-    resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                       PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
-    resultsListViewLayout->addWidget(m_locationListView);
-
-    // main results layout
-    m_resultsView = new QWidget();
-    QVBoxLayout *resultsViewLayout = new QVBoxLayout;
-    const int CONTENTS_MARGIN_LEFT = 0;
-    resultsViewLayout->setContentsMargins(CONTENTS_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                          PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
-    m_resultsView->setLayout(resultsViewLayout);
+    QVBoxLayout *panelLayout = new QVBoxLayout;
+    panelLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
+                                    PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
 
-    resultsViewLayout->addWidget(resultsHeaderWidget);
-    resultsViewLayout->addLayout(resultsListViewLayout);
-
-    // --- MAIN LAYOUT ---
-    QVBoxLayout *routingPanelLayout = new QVBoxLayout;
-    routingPanelLayout->setMargin(0);
-    routingPanelLayout->setSpacing(0);
-    setLayout(routingPanelLayout);
-
-    m_views = new QStackedLayout();
-    routingPanelLayout->addLayout(m_views);
-    m_views->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
-
-    m_views->addWidget(m_resultsView);
-    m_views->addWidget(m_routingView);
+    panelLayout->addWidget(m_routeWaypointListView);
+    setLayout(panelLayout);
 
     // --- CONTEXT BUTTONS ---
-    m_searchLocationButton = new ImageButton(":/res/images/search.png",
-                                             ":/res/images/search_s.png", "", this);
-    m_searchLocationButton->setCheckable(true);
-    connect(m_searchLocationButton, SIGNAL(toggled(bool)),
-            this, SLOT(searchLocationButtonToggled(bool)));
-
-    m_routeButton = new ImageButton(":res/images/route_to_location.png",
-                                    ":res/images/route_to_location_s.png", "", this);
-    m_routeButton->setCheckable(true);
-    m_routeButton->setDisabled(true);
-    connect(m_routeButton, SIGNAL(toggled(bool)),
-            this, SLOT(routeButtonToggled(bool)));
-
-    m_contextButtonLayout->addWidget(m_searchLocationButton);
-    m_contextButtonLayout->addWidget(m_routeButton);
+    m_clearRouteButton = new ImageButton(":/res/images/gps_position.png",
+                                         "", "", this);
+    connect(m_clearRouteButton, SIGNAL(clicked()),
+            this, SLOT(clearRouteButtonClicked()));
+    m_clearRouteButton->setDisabled(true);
+
+    ImageButton *routeToCursorButton = new ImageButton(":res/images/sight.png",
+                                                       "", "", this);
+    connect(routeToCursorButton, SIGNAL(clicked()),
+            this, SIGNAL(routeToCursor()));
+
+    m_contextButtonLayout->addWidget(m_clearRouteButton);
+    m_contextButtonLayout->addWidget(routeToCursorButton);
 }
 
 void RoutingPanel::clearListsSelections()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_locationListView->clearItemSelection();
     m_routeWaypointListView->clearItemSelection();
-
-    setRouteButtonDisabled();
 }
 
-void RoutingPanel::hideEvent(QHideEvent *event)
+void RoutingPanel::clearRouteButtonClicked()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QWidget::hideEvent(event);
-
-    clearListsSelections();
+    m_clearRouteButton->setDisabled(true);
+    m_routeWaypointListView->clearList();
+    emit clearRoute();
 }
 
-void RoutingPanel::populateLocationListView(const QList<Location> &locations)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-//    m_resultsHeaderWidget->show();
-    m_resultsLabel->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;
-
-    if (locations.size() == ONE_LOCATION_ITEM) {
-        ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
-
-        if (item)
-            m_locationListView->setSelectedItem(item);
-    }
-
-    m_locationListView->scrollToTop();
-}
-
-void RoutingPanel::routeButtonToggled(bool checked)
-{
-    if (checked) {
-        routeToSelectedLocation();
-        m_searchLocationButton->setChecked(false);
-    } else {
-        emit clearRoute();
-        m_routeWaypointListView->clearList();
-        setRouteButtonDisabled();
-    }
-}
-
-void RoutingPanel::routeToSelectedLocation()
+void RoutingPanel::hideEvent(QHideEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    LocationListItem *item = dynamic_cast<LocationListItem *>
-                             (m_locationListView->selectedItem());
-
-    if (item)
-        emit routeToLocation(item->coordinates());
-}
+    QWidget::hideEvent(event);
 
-void RoutingPanel::searchLocationButtonToggled(bool checked)
-{
-    if (checked) {
-        showResultsView();
-        emit requestSearchLocation();
-    } else {
-        showRoutingView();
-//        m_resultsHeaderWidget->hide();
-//        m_locationListView->hide();
-    }
+    clearListsSelections();
 }
 
 void RoutingPanel::setRoute(Route &route)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-//    m_resultsHeaderWidget->hide();
-//    m_locationListView->hide();
-//    m_routeWaypointListView->show();
-//    m_routeWaypointListView->clearList();
+    m_routeWaypointListView->clearList();
 
     QList<RouteSegment> segments = route.segments();
     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
@@ -218,41 +111,7 @@ void RoutingPanel::setRoute(Route &route)
 
     m_routeWaypointListView->scrollToTop();
 
-    // route might come from FriendListPanel's route to friend button, so we have to
-    // check the routing button without emitting new routing request
-    blockSignals(true);
-    m_routeButton->setEnabled(true);
-    m_routeButton->setChecked(true);
-    blockSignals(false);
-    m_searchLocationButton->setChecked(false);
-    // search location button might be already false, so we have to make sure the
-    // toggle action handler is caller every time
-    searchLocationButtonToggled(false);
-
-    emit showPanelRequested(this);
-}
-
-void RoutingPanel::setRouteButtonDisabled()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty()
-                               && !m_routeButton->isChecked());
-}
-
-void RoutingPanel::showResultsView()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_views->setCurrentWidget(m_resultsView);
-    m_routeWaypointListView->clearItemSelection();
-}
-
-void RoutingPanel::showRoutingView()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_views->setCurrentWidget(m_routingView);
-    m_locationListView->clearItemSelection();
+    m_clearRouteButton->setDisabled(false);
 
+    emit openPanelRequested(this);
 }