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