Search history feature reviewed.
[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 <QSettings>
24
25 #include "avatarimage.h"
26 #include "../common.h"
27 #include "extendedlistitem.h"
28 #include "extendedlistitemdelegate.h"
29 #include "locationlistitem.h"
30 #include "locationlistview.h"
31 #include "imagebutton.h"
32 #include "panelcommon.h"
33 #include "routing/location.h"
34 #include "searchhistorylistitem.h"
35 #include "searchhistorylistview.h"
36
37 #include "locationsearchpanel.h"
38
39 const QString SETTINGS_SEARCH_HISTORY = "SEARCH_HISTORY";
40
41 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
42     : PanelBase(parent)
43 {
44     qDebug() << __PRETTY_FUNCTION__;
45
46     // --- HEADER WIDGET ---
47     QWidget *resultsHeaderWidget = new QWidget();
48     resultsHeaderWidget->setAutoFillBackground(true);
49     QPalette labelPalette = resultsHeaderWidget->palette();
50     labelPalette.setColor(QPalette::Background, Qt::black);
51     resultsHeaderWidget->setPalette(labelPalette);
52
53     QHBoxLayout *headerLayout = new QHBoxLayout();
54     resultsHeaderWidget->setLayout(headerLayout);
55     headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
56                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
57
58     m_resultsLabel = new QLabel(this);
59     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
60     setHeaderText(0);
61
62     // --- SEARCH HISTORY LIST VIEW ---
63     m_searchHistoryListView = new SearchHistoryListView(this);
64     m_searchHistoryListView->setItemDelegate(new ExtendedListItemDelegate(this));
65
66     connect(m_searchHistoryListView, SIGNAL(searchHistoryItemClicked(QString)),
67             this, SIGNAL(searchHistoryItemClicked(QString)));
68
69     // --- SEARCH RESULTS LIST VIEW ---
70     m_locationListView = new LocationListView(this);
71     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
72
73     connect(m_locationListView,
74             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
75             this,
76             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
77
78     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
79             this, SLOT(setRouteButtonDisabled()));
80
81     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
82     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
83                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
84     resultsListViewLayout->addWidget(m_searchHistoryListView);
85     resultsListViewLayout->addWidget(m_locationListView);
86
87     // --- MAIN LAYOUT ---
88     QVBoxLayout *panelLayout = new QVBoxLayout;
89     panelLayout->setSpacing(0);
90     setLayout(panelLayout);
91
92     const int MARGIN_LEFT = 0;
93     panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
94                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
95
96     panelLayout->addWidget(resultsHeaderWidget);
97     panelLayout->addLayout(resultsListViewLayout);
98
99     // --- CONTEXT BUTTONS ---
100     m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
101     m_routeButton->setDisabled(true);
102     connect(m_routeButton, SIGNAL(clicked()),
103             this, SLOT(routeToSelectedLocation()));
104
105     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
106                                                         ":/res/images/search_s.png", "", this);
107
108     connect(searchLocationButton, SIGNAL(clicked()),
109             this, SIGNAL(requestSearchLocation()));
110
111     m_clearLocationListButton = new ImageButton(":/res/images/clear_btn.png",
112                                                 ":/res/images/clear_btn_s.png",
113                                                 ":/res/images/clear_btn_d.png", this);
114     m_clearLocationListButton->setDisabled(true);
115
116     connect(m_clearLocationListButton, SIGNAL(clicked()),
117             this, SLOT(showSearchHistoryListView()));
118
119     m_contextButtonLayout->addWidget(m_routeButton);
120     m_contextButtonLayout->addWidget(searchLocationButton);
121     m_contextButtonLayout->addWidget(m_clearLocationListButton);
122
123     readSettings();
124     showSearchHistoryListView();
125 }
126
127 LocationSearchPanel::~LocationSearchPanel()
128 {
129     qDebug() << __PRETTY_FUNCTION__;
130
131     QSettings settings(DIRECTORY_NAME, FILE_NAME);
132     QList<QVariant> searchHistories;
133
134     for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
135         SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
136                 m_searchHistoryListView->listItemAt(i));
137
138         if (item) {
139             QList<QString> searchHistory;
140             searchHistory.append(item->title());
141             searchHistory.append(item->dateTime().toString());
142             searchHistories.append(QVariant(searchHistory));
143         }
144     }
145
146     settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
147 }
148
149 void LocationSearchPanel::prependSearchHistory(QString searchString, QDateTime dateTime)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     const int SEARCH_HISTORY_LIMIT = 10;
154     static int counter = 0;
155
156     if (m_searchHistoryListView->count() >= SEARCH_HISTORY_LIMIT)
157         m_searchHistoryListView->removeLastItem();
158
159     SearchHistoryListItem *item = new SearchHistoryListItem();
160     item->setSearchHistoryData(searchString, dateTime);
161     m_searchHistoryListView->prependListItem(QString::number(counter++), item);
162 }
163
164 void LocationSearchPanel::clearListsSelections()
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     m_locationListView->clearItemSelection();
169     m_searchHistoryListView->clearItemSelection();
170
171     setRouteButtonDisabled();
172 }
173
174 void LocationSearchPanel::hideEvent(QHideEvent *event)
175 {
176     qDebug() << __PRETTY_FUNCTION__;
177
178     QWidget::hideEvent(event);
179
180     clearListsSelections();
181 }
182
183 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
184 {
185     qDebug() << __PRETTY_FUNCTION__;
186
187     m_locationListView->clearList();
188     showLocationListView(locations.count());
189
190     for (int i = 0; i < locations.size(); ++i) {
191         LocationListItem *item = new LocationListItem();
192         item->setLocationData(locations.at(i));
193         m_locationListView->addListItem(QString::number(i), item);
194     }
195
196     const int FIRST_LOCATION_ITEM_INDEX = 0;
197     const int ONE_LOCATION_ITEM = 1;
198
199     if (locations.size() == ONE_LOCATION_ITEM) {
200         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
201
202         if (item)
203             m_locationListView->setSelectedItem(item);
204     }
205
206     m_locationListView->scrollToTop();
207 }
208
209 void LocationSearchPanel::readSettings()
210 {
211     qDebug() << __PRETTY_FUNCTION__;
212
213     const int SEARCH_HISTORY_LIST_ITEM_COUNT = 2;
214
215     QSettings settings(DIRECTORY_NAME, FILE_NAME);
216     QList<QVariant> searchHistories = settings.value(SETTINGS_SEARCH_HISTORY).toList();
217
218     //Read from end to begin so items are prepended in correct order
219     for (int i = searchHistories.count() - 1; i >= 0; --i) {
220         QList<QVariant> searchHistory = searchHistories.at(i).toList();
221         if (searchHistory.count() == SEARCH_HISTORY_LIST_ITEM_COUNT) {
222             prependSearchHistory(searchHistory.at(0).toString(),
223                                  QDateTime::fromString(searchHistory.at(1).toString()));
224         }
225     }
226 }
227
228 void LocationSearchPanel::routeToSelectedLocation()
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     LocationListItem *item = dynamic_cast<LocationListItem *>
233                              (m_locationListView->selectedItem());
234
235     if (item)
236         emit routeToLocation(item->coordinates());
237 }
238
239 void LocationSearchPanel::setHeaderText(int count)
240 {
241     qDebug() << __PRETTY_FUNCTION__;
242
243     m_resultsLabel->setText(tr("Search results: %1").arg(count));
244 }
245
246 void LocationSearchPanel::setRouteButtonDisabled()
247 {
248     qDebug() << __PRETTY_FUNCTION__;
249
250     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
251 }
252
253 void LocationSearchPanel::showLocationListView(int locationItemsCount)
254 {
255     qDebug() << __PRETTY_FUNCTION__;
256
257     m_searchHistoryListView->clearItemSelection();
258     m_searchHistoryListView->hide();
259     setHeaderText(locationItemsCount);
260     m_clearLocationListButton->setEnabled(true);
261     m_locationListView->show();
262 }
263
264 void LocationSearchPanel::showSearchHistoryListView()
265 {
266     qDebug() << __PRETTY_FUNCTION__;
267
268     m_locationListView->clearList();
269     m_locationListView->hide();
270     m_resultsLabel->setText(tr("Search history:"));
271     m_clearLocationListButton->setDisabled(true);
272     m_searchHistoryListView->show();
273 }