Tuning the look of the location search panel with empty search history
[situare] / src / ui / locationsearchpanel.cpp
index 224b550..beb9b0c 100644 (file)
     USA.
 */
 
-#include "coordinates/geocoordinate.h"
+#include <QSettings>
+
+#include "avatarimage.h"
+#include "../common.h"
+#include "extendedlistitem.h"
 #include "extendedlistitemdelegate.h"
 #include "locationlistitem.h"
 #include "locationlistview.h"
 #include "imagebutton.h"
 #include "panelcommon.h"
-#include "routewaypointlistitem.h"
-#include "routewaypointlistview.h"
 #include "routing/location.h"
-#include "routing/route.h"
+#include "searchhistorylistitem.h"
+#include "searchhistorylistview.h"
 
 #include "locationsearchpanel.h"
 
+const QString SETTINGS_SEARCH_HISTORY = "SEARCH_HISTORY";
+
 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
     : PanelBase(parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    // --- ROUTING INSTRUCTIONS WIDGET ---
-    m_routeWaypointListView = new RouteWaypointListView(this);
-    m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
-
-    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
+    // --- HEADER WIDGET ---
     QWidget *resultsHeaderWidget = new QWidget();
     resultsHeaderWidget->setAutoFillBackground(true);
     QPalette labelPalette = resultsHeaderWidget->palette();
@@ -68,9 +56,17 @@ LocationSearchPanel::LocationSearchPanel(QWidget *parent)
                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
 
     m_resultsLabel = new QLabel(this);
+    m_resultsLabel->hide();
     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
 
-    // list view
+    // --- SEARCH HISTORY LIST VIEW ---
+    m_searchHistoryListView = new SearchHistoryListView(this);
+    m_searchHistoryListView->setItemDelegate(new ExtendedListItemDelegate(this));
+
+    connect(m_searchHistoryListView, SIGNAL(searchHistoryItemClicked(QString)),
+            this, SIGNAL(searchHistoryItemClicked(QString)));
+
+    // --- SEARCH RESULTS LIST VIEW ---
     m_locationListView = new LocationListView(this);
     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
 
@@ -80,54 +76,82 @@ LocationSearchPanel::LocationSearchPanel(QWidget *parent)
             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
 
     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
-            this, SLOT(setRouteButtonDisabled()));
+            this, SLOT(onListItemSelectionChanged()));
 
     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
+    resultsListViewLayout->addWidget(m_searchHistoryListView);
     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);
-
-    resultsViewLayout->addWidget(resultsHeaderWidget);
-    resultsViewLayout->addLayout(resultsListViewLayout);
-
     // --- MAIN LAYOUT ---
-    QVBoxLayout *LocationSearchPanelLayout = new QVBoxLayout;
-    LocationSearchPanelLayout->setMargin(0);
-    LocationSearchPanelLayout->setSpacing(0);
-    setLayout(LocationSearchPanelLayout);
+    QVBoxLayout *panelLayout = new QVBoxLayout;
+    panelLayout->setSpacing(0);
+    setLayout(panelLayout);
 
-    m_views = new QStackedLayout();
-    LocationSearchPanelLayout->addLayout(m_views);
-    m_views->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
-                                PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
+    const int MARGIN_LEFT = 0;
+    panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
+                                    PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
+    m_noSearchLabel = new QLabel();
+    m_noSearchLabel->setText("No search history");
+    m_noSearchLabel->setAlignment(Qt::AlignCenter);
 
-    m_views->addWidget(m_routingView);
-    m_views->addWidget(m_resultsView);
+    QPalette m_noSearchPalette = palette();
+    m_noSearchPalette.setColor(QPalette::Foreground, Qt::white);
+    m_noSearchLabel->setPalette(m_noSearchPalette);
 
-    // --- 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)));
+    panelLayout->addWidget(resultsHeaderWidget);
+    panelLayout->addWidget(m_noSearchLabel, Qt::AlignCenter);
+    panelLayout->addLayout(resultsListViewLayout);
 
+    // --- CONTEXT BUTTONS ---
     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)));
+    connect(m_routeButton, SIGNAL(clicked()),
+            this, SLOT(routeToSelectedLocation()));
+
+    ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
+                                                        ":/res/images/search_s.png", "", this);
+
+    connect(searchLocationButton, SIGNAL(clicked()),
+            this, SIGNAL(requestSearchLocation()));
 
-    m_contextButtonLayout->addWidget(m_searchLocationButton);
-    m_contextButtonLayout->addWidget(m_routeButton);
+    m_clearLocationListButton = new ImageButton(":/res/images/back.png",
+                                                ":/res/images/back_s.png",
+                                                ":/res/images/back_d.png", this);
+    m_clearLocationListButton->setDisabled(true);
+
+    connect(m_clearLocationListButton, SIGNAL(clicked()),
+            this, SLOT(showSearchHistoryListView()));
+
+    m_itemButtonsLayout->addWidget(m_routeButton);
+    m_genericButtonsLayout->addWidget(searchLocationButton);
+    m_genericButtonsLayout->addWidget(m_clearLocationListButton);
+
+    showEmptyPanel(true);
+    readSettings();
+}
+
+LocationSearchPanel::~LocationSearchPanel()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    QList<QVariant> searchHistories;
+
+    for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
+        SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
+                m_searchHistoryListView->listItemAt(i));
+
+        if (item) {
+            QList<QString> searchHistory;
+            searchHistory.append(item->title());
+            searchHistory.append(item->dateTime().toString());
+            searchHistories.append(QVariant(searchHistory));
+        }
+    }
+
+    settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
 }
 
 void LocationSearchPanel::clearListsSelections()
@@ -135,9 +159,7 @@ void LocationSearchPanel::clearListsSelections()
     qDebug() << __PRETTY_FUNCTION__;
 
     m_locationListView->clearItemSelection();
-    m_routeWaypointListView->clearItemSelection();
-
-    setRouteButtonDisabled();
+    m_searchHistoryListView->clearItemSelection();
 }
 
 void LocationSearchPanel::hideEvent(QHideEvent *event)
@@ -153,9 +175,8 @@ void LocationSearchPanel::populateLocationListView(const QList<Location> &locati
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_resultsLabel->setText(tr("Search results: %1").arg(locations.count()));
-
     m_locationListView->clearList();
+    showLocationListView(locations.count());
 
     for (int i = 0; i < locations.size(); ++i) {
         LocationListItem *item = new LocationListItem();
@@ -163,30 +184,43 @@ void LocationSearchPanel::populateLocationListView(const QList<Location> &locati
         m_locationListView->addListItem(QString::number(i), item);
     }
 
-    const int FIRST_LOCATION_ITEM_INDEX = 0;
-    const int ONE_LOCATION_ITEM = 1;
+    m_locationListView->scrollToTop();
+}
 
-    if (locations.size() == ONE_LOCATION_ITEM) {
-        ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
+void LocationSearchPanel::prependSearchHistory(QString searchString, QDateTime dateTime)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        if (item)
-            m_locationListView->setSelectedItem(item);
-    }
+    const int SEARCH_HISTORY_LIMIT = 10;
+    static int counter = 0;
 
-    m_locationListView->scrollToTop();
+    if (m_searchHistoryListView->count() >= SEARCH_HISTORY_LIMIT)
+        m_searchHistoryListView->removeLastItem();
+
+    SearchHistoryListItem *item = new SearchHistoryListItem();
+    item->setSearchHistoryData(searchString, dateTime);
+    m_searchHistoryListView->prependListItem(QString::number(counter++), item);
 }
 
-void LocationSearchPanel::routeButtonToggled(bool checked)
+void LocationSearchPanel::readSettings()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (checked) {
-        routeToSelectedLocation();
-        m_searchLocationButton->setChecked(false);
-    } else {
-        emit clearRoute();
-        m_routeWaypointListView->clearList();
-        setRouteButtonDisabled();
+    const int SEARCH_HISTORY_LIST_ITEM_COUNT = 2;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    QList<QVariant> searchHistories = settings.value(SETTINGS_SEARCH_HISTORY).toList();
+
+    if (searchHistories.count() > 0)
+            showSearchHistoryListView();
+
+    //Read from end to begin so items are prepended in correct order
+    for (int i = searchHistories.count() - 1; i >= 0; --i) {
+        QList<QVariant> searchHistory = searchHistories.at(i).toList();
+        if (searchHistory.count() == SEARCH_HISTORY_LIST_ITEM_COUNT) {
+            prependSearchHistory(searchHistory.at(0).toString(),
+                                 QDateTime::fromString(searchHistory.at(1).toString()));
+        }
     }
 }
 
@@ -201,73 +235,45 @@ void LocationSearchPanel::routeToSelectedLocation()
         emit routeToLocation(item->coordinates());
 }
 
-void LocationSearchPanel::searchLocationButtonToggled(bool checked)
+void LocationSearchPanel::setHeaderText(int count)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (checked) {
-        showResultsView();
-        emit requestSearchLocation();
-    } else {
-        showRoutingView();
-    }
+    m_resultsLabel->setText(tr("Search results: %1").arg(count));
 }
 
-void LocationSearchPanel::setRoute(Route &route)
+void LocationSearchPanel::showEmptyPanel(bool show)
 {
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_routeWaypointListView->clearList();
-
-    QList<RouteSegment> segments = route.segments();
-    QList<GeoCoordinate> geometryPoints = route.geometryPoints();
-
-    for (int i = 0; i < segments.size(); ++i) {
-        RouteWaypointListItem *item = new RouteWaypointListItem();
-        RouteSegment routeSegment = segments.at(i);
-        item->setRouteWaypointData(routeSegment,
-                                   geometryPoints.at(routeSegment.positionIndex()));
-
-        m_routeWaypointListView->addListItem(QString::number(i), item);
+    if (show) {
+        m_noSearchLabel->show();
+        m_searchHistoryListView->hide();
+        m_locationListView->hide();
+    }
+    else {
+        m_noSearchLabel->hide();
+        m_resultsLabel->show();
     }
-
-    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 called every time
-    searchLocationButtonToggled(false);
-
-    emit openPanelRequested(this);
-}
-
-void LocationSearchPanel::setRouteButtonDisabled()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty()
-                               && !m_routeButton->isChecked());
 }
 
-void LocationSearchPanel::showResultsView()
+void LocationSearchPanel::showLocationListView(int locationItemsCount)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_views->setCurrentWidget(m_resultsView);
-    m_routeWaypointListView->clearItemSelection();
+    m_searchHistoryListView->clearItemSelection();
+    m_searchHistoryListView->hide();
+    setHeaderText(locationItemsCount);
+    m_clearLocationListButton->setEnabled(true);
+    m_locationListView->show();
+    showEmptyPanel(false);
 }
 
-void LocationSearchPanel::showRoutingView()
+void LocationSearchPanel::showSearchHistoryListView()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_views->setCurrentWidget(m_routingView);
-    m_locationListView->clearItemSelection();
+    m_locationListView->clearList();
+    m_locationListView->hide();
+    m_resultsLabel->setText(tr("Search history:"));
+    m_clearLocationListButton->setDisabled(true);
+    m_searchHistoryListView->show();
 }