Created SearchHistoryListView and -Item classes.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 2 Sep 2010 12:57:22 +0000 (15:57 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 2 Sep 2010 12:57:22 +0000 (15:57 +0300)
images.qrc
src/src.pro
src/ui/extendedlistitem.cpp
src/ui/listview.cpp
src/ui/listview.h
src/ui/locationsearchpanel.cpp
src/ui/locationsearchpanel.h
src/ui/mainwindow.cpp
src/ui/searchhistorylistitem.cpp [new file with mode: 0644]
src/ui/searchhistorylistitem.h [new file with mode: 0644]

index 400684d..a8fb10d 100644 (file)
@@ -60,5 +60,9 @@
         <file>res/images/zoom_in.png</file>
         <file>res/images/zoom_out.png</file>
         <file>res/images/location_search.png</file>
+        <file>res/images/search_history.png</file>
+        <file>res/images/clear_btn_d.png</file>
+        <file>res/images/clear_btn_s.png</file>
+        <file>res/images/clear_btn.png</file>
     </qresource>
 </RCC>
index 1cf0da4..bcf90c4 100644 (file)
@@ -82,7 +82,9 @@ SOURCES += main.cpp \
     ui/routewaypointlistview.cpp \
     user/user.cpp \
     ui/locationsearchpanel.cpp \
-    ui/textmodifier.cpp
+    ui/textmodifier.cpp \
+    ui/searchhistorylistview.cpp \
+    ui/searchhistorylistitem.cpp
 HEADERS += application.h \
     common.h \
     coordinates/geocoordinate.h \
@@ -166,7 +168,9 @@ HEADERS += application.h \
     ui/routewaypointlistview.h \
     user/user.h \
     ui/locationsearchpanel.h \
-    ui/textmodifier.h
+    ui/textmodifier.h \
+    ui/searchhistorylistview.h \
+    ui/searchhistorylistitem.h
 QT += network \
     webkit
 DEFINES += QT_NO_DEBUG_OUTPUT
index 1ecb362..79da1bd 100644 (file)
@@ -34,7 +34,7 @@ ExtendedListItem::ExtendedListItem()
     : m_selected(false),
       m_expandedHeight(ITEM_MIN_HEIGHT),
       m_normalHeight(ITEM_MIN_HEIGHT),
-      m_subItemTextWidth(0)
+      m_subItemTextWidth(SUBITEM_TEXT_MAX_WIDTH)
 
 {
     qDebug() << __PRETTY_FUNCTION__;
index fdcea75..41fe9b2 100644 (file)
@@ -77,7 +77,7 @@ void ListView::clearList()
 
 void ListView::clearUnused(const QStringList &itemIDs)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__ << itemIDs.count();
 
     foreach (QString key, m_listItems.keys()) {
         if (!itemIDs.contains(key)) {
@@ -206,20 +206,39 @@ ListItem *ListView::listItemAt(int index)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QHashIterator<QString, ListItem*> itemIterator(m_listItems);
-    ListItem *item = 0;
-    int counter = 0;
+    ListItem *listItem = 0;
 
-    while (itemIterator.hasNext()) {
-        itemIterator.next();
-        if (index == counter) {
-            item = itemIterator.value();
-            break;
-        }
-        counter++;
+    if (index < count())
+        listItem = dynamic_cast<ListItem*>(item(index));
+
+    return listItem;
+}
+
+void ListView::prependListItem(const QString &key, ListItem *item)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_listItems.contains(key)) {
+        insertItem(0, item);
+        m_listItems.insert(key, item);
     }
+}
 
-    return item;
+void ListView::removeLastItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    ListItem *item = listItemAt(count() - 1);
+
+    if (item) {
+        if (item) {
+            m_listItems.remove(item->title());
+            takeItem(row(item));
+            if (m_currentItem == item)
+                m_currentItem = 0;
+            delete item;
+        }
+    }
 }
 
 ListItem *ListView::selectedItem()
index 417c5d1..eaf32fb 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef LISTVIEW_H
 #define LISTVIEW_H
 
+#include <QHash>
 #include <QListWidget>
 
 class GeoCoordinate;
@@ -154,6 +155,19 @@ public:
     ListItem *listItemAt(int index);
 
     /**
+    * @brief Prepends item to view and item list.
+    *
+    * @param key user ID
+    * @param item item to prepend to view and list
+    */
+    void prependListItem(const QString &key, ListItem *item);
+
+    /**
+    * @brief Removes last ListItem in list.
+    */
+    void removeLastItem();
+
+    /**
     * @brief Returns selected ListItem.
     *
     * @return ListItem if there is selected, 0 otherwise
index d42353c..41aaf87 100644 (file)
     USA.
 */
 
+#include <QPair>
+#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 "routing/location.h"
+#include "searchhistorylistitem.h"
+#include "searchhistorylistview.h"
 
 #include "locationsearchpanel.h"
 
+const QString SETTINGS_SEARCH_HISTORY = "SEARCH_HISTORY";
+
 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
     : PanelBase(parent)
 {
@@ -50,6 +60,10 @@ LocationSearchPanel::LocationSearchPanel(QWidget *parent)
     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
     setHeaderText(0);
 
+    // --- SEARCH HISTORY LIST VIEW ---
+    m_searchHistoryListView = new SearchHistoryListView(this);
+    m_searchHistoryListView->setItemDelegate(new ExtendedListItemDelegate(this));
+
     // --- SEARCH RESULTS LIST VIEW ---
     m_locationListView = new LocationListView(this);
     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
@@ -65,6 +79,7 @@ LocationSearchPanel::LocationSearchPanel(QWidget *parent)
     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 LAYOUT ---
@@ -87,11 +102,58 @@ LocationSearchPanel::LocationSearchPanel(QWidget *parent)
 
     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
                                                         ":/res/images/search_s.png", "", this);
+
     connect(searchLocationButton, SIGNAL(clicked()),
             this, SIGNAL(requestSearchLocation()));
 
+    m_clearLocationListButton = new ImageButton(":/res/images/clear_btn.png",
+                                                ":/res/images/clear_btn_s.png",
+                                                ":/res/images/clear_btn_d.png", this);
+    m_clearLocationListButton->setDisabled(true);
+
+    connect(m_clearLocationListButton, SIGNAL(clicked()),
+            this, SLOT(showSearchHistoryList()));
+
     m_contextButtonLayout->addWidget(m_routeButton);
     m_contextButtonLayout->addWidget(searchLocationButton);
+    m_contextButtonLayout->addWidget(m_clearLocationListButton);
+
+    readSettings();
+    showSearchHistoryList();
+}
+
+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<QVariant> searchHistory;
+            searchHistory.append(item->title());
+            searchHistory.append(item->dateTime().toString());
+            searchHistories.append(searchHistory);
+        }
+    }
+
+    settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
+}
+
+void LocationSearchPanel::appendSearchHistory(QString searchString, QDateTime currentDateTime)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_searchHistoryListView->count() >= 10)
+        m_searchHistoryListView->removeLastItem();
+
+    SearchHistoryListItem *item = new SearchHistoryListItem();
+    item->setSearchHistoryData(searchString, currentDateTime);
+    m_searchHistoryListView->prependListItem(searchString, item);
 }
 
 void LocationSearchPanel::clearListsSelections()
@@ -116,8 +178,8 @@ void LocationSearchPanel::populateLocationListView(const QList<Location> &locati
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    m_searchHistoryListView->hide();
     setHeaderText(locations.count());
-
     m_locationListView->clearList();
 
     for (int i = 0; i < locations.size(); ++i) {
@@ -137,6 +199,22 @@ void LocationSearchPanel::populateLocationListView(const QList<Location> &locati
     }
 
     m_locationListView->scrollToTop();
+    m_clearLocationListButton->setEnabled(true);
+    m_locationListView->show();
+}
+
+void LocationSearchPanel::readSettings()
+{
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+
+    QList<QVariant> searchHistories = settings.value(
+            SETTINGS_SEARCH_HISTORY).toList();
+
+    for (int i = 0; i < searchHistories.count(); ++i) {
+        QList<QVariant> searchHistory = searchHistories.at(i).toList();
+        appendSearchHistory(searchHistory.at(0).toString(),
+                            QDateTime::fromString(searchHistory.at(1).toString()));
+    }
 }
 
 void LocationSearchPanel::routeToSelectedLocation()
@@ -163,3 +241,14 @@ void LocationSearchPanel::setRouteButtonDisabled()
 
     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
 }
+
+void LocationSearchPanel::showSearchHistoryList()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_locationListView->clearList();
+    m_locationListView->hide();
+    m_resultsLabel->setText(tr("Search history:"));
+    m_clearLocationListButton->setDisabled(true);
+    m_searchHistoryListView->show();
+}
index fd87bf2..101f55b 100644 (file)
@@ -32,6 +32,7 @@ class GeoCoordinate;
 class ImageButton;
 class Location;
 class LocationListView;
+class SearchHistoryListView;
 
 /**
  * @brief Location search panel
@@ -51,6 +52,13 @@ public:
      */
     LocationSearchPanel(QWidget *parent = 0);
 
+    /**
+     * @brief Destructor
+     *
+     * Writes search history to settings.
+     */
+    ~LocationSearchPanel();
+
 /*******************************************************************************
  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
  ******************************************************************************/
@@ -68,6 +76,8 @@ protected:
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
 private:
+    void readSettings();
+
     /**
     * @brief Set text for header
     *
@@ -76,6 +86,9 @@ private:
     void setHeaderText(int count);
 
 private slots:
+
+    void appendSearchHistory(QString searchString, QDateTime currentTime = QDateTime());
+
     /**
     * @brief Clears lists' selections.
     *
@@ -84,6 +97,11 @@ private slots:
     void clearListsSelections();
 
     /**
+    * @brief Shows search history list.
+    */
+    void showSearchHistoryList();
+
+    /**
     * @brief Populates location list view.
     *
     * @param locations list of Location objects
@@ -139,10 +157,12 @@ signals:
  * DATA MEMBERS
  ******************************************************************************/
 private:
-    QLabel *m_resultsLabel;                 ///< Location list label
+    QLabel *m_resultsLabel;                         ///< Location list label
 
-    ImageButton *m_routeButton;             ///< Route to location button
-    LocationListView *m_locationListView;   ///< Search results list view
+    ImageButton *m_clearLocationListButton;         ///< Clear location list button
+    ImageButton *m_routeButton;                     ///< Route to location button
+    SearchHistoryListView *m_searchHistoryListView; ///< Search history list view
+    LocationListView *m_locationListView;           ///< Search results list view
 };
 
 #endif // LOCATIONSEARCHPANEL_H
index e99cf6b..4ba4c65 100644 (file)
@@ -269,6 +269,9 @@ void MainWindow::buildLocationSearchPanel()
 
     connect(m_locationSearchPanel, SIGNAL(requestSearchLocation()),
             this, SLOT(startLocationSearch()));
+
+    connect(this, SIGNAL(searchForLocation(QString)),
+            m_locationSearchPanel, SLOT(appendSearchHistory(QString)));
 }
 
 void MainWindow::buildMap()
diff --git a/src/ui/searchhistorylistitem.cpp b/src/ui/searchhistorylistitem.cpp
new file mode 100644 (file)
index 0000000..bd0aaff
--- /dev/null
@@ -0,0 +1,52 @@
+#include <QDebug>
+
+#include "avatarimage.h"
+#include "../common.h"
+#include "listcommon.h"
+
+#include "searchhistorylistitem.h"
+
+const int SEARCH_HISTORY_SUBITEM_TEXT_MAX_WIDTH = SUBITEM_TEXT_MAX_WIDTH + MARGIN + IMAGE_WIDTH;
+
+SearchHistoryListItem::SearchHistoryListItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setSubitemTextWidth(SEARCH_HISTORY_SUBITEM_TEXT_MAX_WIDTH);
+}
+
+SearchHistoryListItem::~SearchHistoryListItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+QDateTime SearchHistoryListItem::dateTime() const
+{
+    return m_dateTime;
+}
+
+void SearchHistoryListItem::setDateTime(const QDateTime &dateTime)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (dateTime.isNull())
+        m_dateTime = QDateTime::currentDateTime();
+    else
+        m_dateTime = dateTime;
+}
+
+void SearchHistoryListItem::setSearchHistoryData(const QString &searchString,
+                                                 const QDateTime &dateTime)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setDateTime(dateTime);
+
+    setTitle(shortenText(searchString, ITEM_WIDTH - 3 * MARGIN,
+                            ListItem::TEXT_SIZE_NORMAL));
+
+    setImage(AvatarImage::create(QPixmap(":/res/images/search_history.png"), AvatarImage::Small));
+
+    clearSubItems();
+    addSubItem(m_dateTime.toString(), QPixmap(":/res/images/clock.png"));
+}
diff --git a/src/ui/searchhistorylistitem.h b/src/ui/searchhistorylistitem.h
new file mode 100644 (file)
index 0000000..faa14a1
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       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
+   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.
+*/
+
+#ifndef SEARCHHISTORYLISTITEM_H
+#define SEARCHHISTORYLISTITEM_H
+
+#include <QDateTime>
+
+#include "extendedlistitem.h"
+
+/**
+* @brief List item stores information about search history.
+*
+* @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
+*/
+class SearchHistoryListItem : public ExtendedListItem
+{
+public:
+    /**
+    * @brief Constructor.
+    *
+    * Sets sub items' text width.
+    */
+    SearchHistoryListItem();
+
+    /**
+    * @brief Destructor.
+    */
+    ~SearchHistoryListItem();
+
+/******************************************************************************
+* MEMBER FUNCTIONS AND SLOTS
+******************************************************************************/
+public:
+    /**
+    * @brief Returns date and time.
+    *
+    * @return QDateTime
+    */
+    QDateTime dateTime() const;
+
+    /**
+    * @brief Set location data for this item.
+    *
+    * @param location Location data
+    */
+    void setSearchHistoryData(const QString &searchString, const QDateTime &dateTime);
+
+private:
+    /**
+    * @brief Sets date and time.
+    *
+    * @param dateTime QDateTime date and time
+    */
+    void setDateTime(const QDateTime &dateTime);
+
+/******************************************************************************
+* DATA MEMBERS
+******************************************************************************/
+private:
+    QDateTime m_dateTime;   ///< Date time
+};
+
+#endif // LOCATIONLISTITEM_H