Added back button, removed list selection from location list view when
[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(onListItemSelectionChanged()));
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/routing.png", "", "", this);
101     connect(m_routeButton, SIGNAL(clicked()),
102             this, SLOT(routeToSelectedLocation()));
103
104     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
105                                                         ":/res/images/search_s.png", "", this);
106
107     connect(searchLocationButton, SIGNAL(clicked()),
108             this, SIGNAL(requestSearchLocation()));
109
110     m_clearLocationListButton = new ImageButton(":/res/images/back_btn.png",
111                                                 ":/res/images/back_btn_s.png",
112                                                 ":/res/images/back_btn_d.png", this);
113     m_clearLocationListButton->setDisabled(true);
114
115     connect(m_clearLocationListButton, SIGNAL(clicked()),
116             this, SLOT(showSearchHistoryListView()));
117
118     m_itemButtonsLayout->addWidget(m_routeButton);
119     m_genericButtonsLayout->addWidget(searchLocationButton);
120     m_genericButtonsLayout->addWidget(m_clearLocationListButton);
121
122     readSettings();
123     showSearchHistoryListView();
124 }
125
126 LocationSearchPanel::~LocationSearchPanel()
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     QSettings settings(DIRECTORY_NAME, FILE_NAME);
131     QList<QVariant> searchHistories;
132
133     for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
134         SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
135                 m_searchHistoryListView->listItemAt(i));
136
137         if (item) {
138             QList<QString> searchHistory;
139             searchHistory.append(item->title());
140             searchHistory.append(item->dateTime().toString());
141             searchHistories.append(QVariant(searchHistory));
142         }
143     }
144
145     settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
146 }
147
148 void LocationSearchPanel::prependSearchHistory(QString searchString, QDateTime dateTime)
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     const int SEARCH_HISTORY_LIMIT = 10;
153     static int counter = 0;
154
155     if (m_searchHistoryListView->count() >= SEARCH_HISTORY_LIMIT)
156         m_searchHistoryListView->removeLastItem();
157
158     SearchHistoryListItem *item = new SearchHistoryListItem();
159     item->setSearchHistoryData(searchString, dateTime);
160     m_searchHistoryListView->prependListItem(QString::number(counter++), item);
161 }
162
163 void LocationSearchPanel::clearListsSelections()
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     m_locationListView->clearItemSelection();
168     m_searchHistoryListView->clearItemSelection();
169 }
170
171 void LocationSearchPanel::hideEvent(QHideEvent *event)
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     QWidget::hideEvent(event);
176
177     clearListsSelections();
178 }
179
180 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     m_locationListView->clearList();
185     showLocationListView(locations.count());
186
187     for (int i = 0; i < locations.size(); ++i) {
188         LocationListItem *item = new LocationListItem();
189         item->setLocationData(locations.at(i));
190         m_locationListView->addListItem(QString::number(i), item);
191     }
192
193     m_locationListView->scrollToTop();
194 }
195
196 void LocationSearchPanel::readSettings()
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     const int SEARCH_HISTORY_LIST_ITEM_COUNT = 2;
201
202     QSettings settings(DIRECTORY_NAME, FILE_NAME);
203     QList<QVariant> searchHistories = settings.value(SETTINGS_SEARCH_HISTORY).toList();
204
205     //Read from end to begin so items are prepended in correct order
206     for (int i = searchHistories.count() - 1; i >= 0; --i) {
207         QList<QVariant> searchHistory = searchHistories.at(i).toList();
208         if (searchHistory.count() == SEARCH_HISTORY_LIST_ITEM_COUNT) {
209             prependSearchHistory(searchHistory.at(0).toString(),
210                                  QDateTime::fromString(searchHistory.at(1).toString()));
211         }
212     }
213 }
214
215 void LocationSearchPanel::routeToSelectedLocation()
216 {
217     qDebug() << __PRETTY_FUNCTION__;
218
219     LocationListItem *item = dynamic_cast<LocationListItem *>
220                              (m_locationListView->selectedItem());
221
222     if (item)
223         emit routeToLocation(item->coordinates());
224 }
225
226 void LocationSearchPanel::setHeaderText(int count)
227 {
228     qDebug() << __PRETTY_FUNCTION__;
229
230     m_resultsLabel->setText(tr("Search results: %1").arg(count));
231 }
232
233 void LocationSearchPanel::showLocationListView(int locationItemsCount)
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     m_searchHistoryListView->clearItemSelection();
238     m_searchHistoryListView->hide();
239     setHeaderText(locationItemsCount);
240     m_clearLocationListButton->setEnabled(true);
241     m_locationListView->show();
242 }
243
244 void LocationSearchPanel::showSearchHistoryListView()
245 {
246     qDebug() << __PRETTY_FUNCTION__;
247
248     m_locationListView->clearList();
249     m_locationListView->hide();
250     m_resultsLabel->setText(tr("Search history:"));
251     m_clearLocationListButton->setDisabled(true);
252     m_searchHistoryListView->show();
253 }