Fixed search history settings read and write and added search history
[situare] / src / ui / locationsearchpanel.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Jussi Laitinen - jussi.laitinen@ixonos.com
6         Sami Rämö - sami.ramo@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21 */
22
23 #include <QPair>
24 #include <QSettings>
25
26 #include "avatarimage.h"
27 #include "../common.h"
28 #include "extendedlistitem.h"
29 #include "extendedlistitemdelegate.h"
30 #include "locationlistitem.h"
31 #include "locationlistview.h"
32 #include "imagebutton.h"
33 #include "panelcommon.h"
34 #include "routing/location.h"
35 #include "searchhistorylistitem.h"
36 #include "searchhistorylistview.h"
37
38 #include "locationsearchpanel.h"
39
40 const QString SETTINGS_SEARCH_HISTORY = "SEARCH_HISTORY";
41
42 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
43     : PanelBase(parent)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     // --- HEADER WIDGET ---
48     QWidget *resultsHeaderWidget = new QWidget();
49     resultsHeaderWidget->setAutoFillBackground(true);
50     QPalette labelPalette = resultsHeaderWidget->palette();
51     labelPalette.setColor(QPalette::Background, Qt::black);
52     resultsHeaderWidget->setPalette(labelPalette);
53
54     QHBoxLayout *headerLayout = new QHBoxLayout();
55     resultsHeaderWidget->setLayout(headerLayout);
56     headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
57                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
58
59     m_resultsLabel = new QLabel(this);
60     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
61     setHeaderText(0);
62
63     // --- SEARCH HISTORY LIST VIEW ---
64     m_searchHistoryListView = new SearchHistoryListView(this);
65     m_searchHistoryListView->setItemDelegate(new ExtendedListItemDelegate(this));
66
67     connect(m_searchHistoryListView, SIGNAL(searchHistoryItemClicked(QString)),
68             this, SIGNAL(searchHistoryItemClicked(QString)));
69
70     // --- SEARCH RESULTS LIST VIEW ---
71     m_locationListView = new LocationListView(this);
72     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
73
74     connect(m_locationListView,
75             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
76             this,
77             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
78
79     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
80             this, SLOT(setRouteButtonDisabled()));
81
82     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
83     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
84                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
85     resultsListViewLayout->addWidget(m_searchHistoryListView);
86     resultsListViewLayout->addWidget(m_locationListView);
87
88     // --- MAIN LAYOUT ---
89     QVBoxLayout *panelLayout = new QVBoxLayout;
90     panelLayout->setSpacing(0);
91     setLayout(panelLayout);
92
93     const int MARGIN_LEFT = 0;
94     panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
95                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
96
97     panelLayout->addWidget(resultsHeaderWidget);
98     panelLayout->addLayout(resultsListViewLayout);
99
100     // --- CONTEXT BUTTONS ---
101     m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
102     m_routeButton->setDisabled(true);
103     connect(m_routeButton, SIGNAL(clicked()),
104             this, SLOT(routeToSelectedLocation()));
105
106     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
107                                                         ":/res/images/search_s.png", "", this);
108
109     connect(searchLocationButton, SIGNAL(clicked()),
110             this, SIGNAL(requestSearchLocation()));
111
112     m_clearLocationListButton = new ImageButton(":/res/images/clear_btn.png",
113                                                 ":/res/images/clear_btn_s.png",
114                                                 ":/res/images/clear_btn_d.png", this);
115     m_clearLocationListButton->setDisabled(true);
116
117     connect(m_clearLocationListButton, SIGNAL(clicked()),
118             this, SLOT(showSearchHistoryList()));
119
120     m_contextButtonLayout->addWidget(m_routeButton);
121     m_contextButtonLayout->addWidget(searchLocationButton);
122     m_contextButtonLayout->addWidget(m_clearLocationListButton);
123
124     readSettings();
125     showSearchHistoryList();
126 }
127
128 LocationSearchPanel::~LocationSearchPanel()
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131
132     QSettings settings(DIRECTORY_NAME, FILE_NAME);
133     QList<QVariant> searchHistories;
134
135     for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
136         SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
137                 m_searchHistoryListView->listItemAt(i));
138
139         if (item) {
140             QList<QString> searchHistory;
141             searchHistory.append(item->title());
142             searchHistory.append(item->dateTime().toString());
143             searchHistories.append(QVariant(searchHistory));
144         }
145     }
146
147     settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
148 }
149
150 void LocationSearchPanel::prependSearchHistory(QString searchString, QDateTime dateTime)
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     const int SEARCH_HISTORY_LIMIT = 10;
155     static int counter = 0;
156
157     if (m_searchHistoryListView->count() >= SEARCH_HISTORY_LIMIT)
158         m_searchHistoryListView->removeLastItem();
159
160     SearchHistoryListItem *item = new SearchHistoryListItem();
161     item->setSearchHistoryData(searchString, dateTime);
162     m_searchHistoryListView->prependListItem(QString::number(counter++), item);
163 }
164
165 void LocationSearchPanel::clearListsSelections()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     m_locationListView->clearItemSelection();
170     m_searchHistoryListView->clearItemSelection();
171
172     setRouteButtonDisabled();
173 }
174
175 void LocationSearchPanel::hideEvent(QHideEvent *event)
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     QWidget::hideEvent(event);
180
181     clearListsSelections();
182 }
183
184 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     m_searchHistoryListView->clearItemSelection();
189     m_searchHistoryListView->hide();
190     setHeaderText(locations.count());
191     m_locationListView->clearList();
192     m_clearLocationListButton->setEnabled(true);
193     m_locationListView->show();
194
195     for (int i = 0; i < locations.size(); ++i) {
196         LocationListItem *item = new LocationListItem();
197         item->setLocationData(locations.at(i));
198         m_locationListView->addListItem(QString::number(i), item);
199     }
200
201     const int FIRST_LOCATION_ITEM_INDEX = 0;
202     const int ONE_LOCATION_ITEM = 1;
203
204     if (locations.size() == ONE_LOCATION_ITEM) {
205         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
206
207         if (item)
208             m_locationListView->setSelectedItem(item);
209     }
210
211     m_locationListView->scrollToTop();
212 }
213
214 void LocationSearchPanel::readSettings()
215 {
216     qDebug() << __PRETTY_FUNCTION__;
217
218     const int SEARCH_HISTORY_LIST_ITEM_COUNT = 2;
219
220     QSettings settings(DIRECTORY_NAME, FILE_NAME);
221     QList<QVariant> searchHistories = settings.value(SETTINGS_SEARCH_HISTORY).toList();
222
223     //Read from end to begin so items are prepended in correct order
224     for (int i = searchHistories.count() - 1; i >= 0; --i) {
225         QList<QVariant> searchHistory = searchHistories.at(i).toList();
226         if (searchHistory.count() == SEARCH_HISTORY_LIST_ITEM_COUNT) {
227             prependSearchHistory(searchHistory.at(0).toString(),
228                                  QDateTime::fromString(searchHistory.at(1).toString()));
229         }
230     }
231 }
232
233 void LocationSearchPanel::routeToSelectedLocation()
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     LocationListItem *item = dynamic_cast<LocationListItem *>
238                              (m_locationListView->selectedItem());
239
240     if (item)
241         emit routeToLocation(item->coordinates());
242 }
243
244 void LocationSearchPanel::setHeaderText(int count)
245 {
246     qDebug() << __PRETTY_FUNCTION__;
247
248     m_resultsLabel->setText(tr("Search results: %1").arg(count));
249 }
250
251 void LocationSearchPanel::setRouteButtonDisabled()
252 {
253     qDebug() << __PRETTY_FUNCTION__;
254
255     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
256 }
257
258 void LocationSearchPanel::showSearchHistoryList()
259 {
260     qDebug() << __PRETTY_FUNCTION__;
261
262     m_locationListView->clearList();
263     m_locationListView->hide();
264     m_resultsLabel->setText(tr("Search history:"));
265     m_clearLocationListButton->setDisabled(true);
266     m_searchHistoryListView->show();
267 }