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