Added new files.
[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     // --- SEARCH RESULTS LIST VIEW ---
68     m_locationListView = new LocationListView(this);
69     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
70
71     connect(m_locationListView,
72             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
73             this,
74             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
75
76     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
77             this, SLOT(setRouteButtonDisabled()));
78
79     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
80     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
81                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
82     resultsListViewLayout->addWidget(m_searchHistoryListView);
83     resultsListViewLayout->addWidget(m_locationListView);
84
85     // --- MAIN LAYOUT ---
86     QVBoxLayout *panelLayout = new QVBoxLayout;
87     panelLayout->setSpacing(0);
88     setLayout(panelLayout);
89
90     const int MARGIN_LEFT = 0;
91     panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
92                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
93
94     panelLayout->addWidget(resultsHeaderWidget);
95     panelLayout->addLayout(resultsListViewLayout);
96
97     // --- CONTEXT BUTTONS ---
98     m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
99     m_routeButton->setDisabled(true);
100     connect(m_routeButton, SIGNAL(clicked()),
101             this, SLOT(routeToSelectedLocation()));
102
103     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
104                                                         ":/res/images/search_s.png", "", this);
105
106     connect(searchLocationButton, SIGNAL(clicked()),
107             this, SIGNAL(requestSearchLocation()));
108
109     m_clearLocationListButton = new ImageButton(":/res/images/clear_btn.png",
110                                                 ":/res/images/clear_btn_s.png",
111                                                 ":/res/images/clear_btn_d.png", this);
112     m_clearLocationListButton->setDisabled(true);
113
114     connect(m_clearLocationListButton, SIGNAL(clicked()),
115             this, SLOT(showSearchHistoryList()));
116
117     m_contextButtonLayout->addWidget(m_routeButton);
118     m_contextButtonLayout->addWidget(searchLocationButton);
119     m_contextButtonLayout->addWidget(m_clearLocationListButton);
120
121     readSettings();
122     showSearchHistoryList();
123 }
124
125 LocationSearchPanel::~LocationSearchPanel()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     QSettings settings(DIRECTORY_NAME, FILE_NAME);
130     QList<QVariant> searchHistories;
131
132     for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
133         SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
134                 m_searchHistoryListView->listItemAt(i));
135
136         if (item) {
137             QList<QVariant> searchHistory;
138             searchHistory.append(item->title());
139             searchHistory.append(item->dateTime().toString());
140             searchHistories.append(searchHistory);
141         }
142     }
143
144     settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
145 }
146
147 void LocationSearchPanel::appendSearchHistory(QString searchString, QDateTime currentDateTime)
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     if (m_searchHistoryListView->count() >= 10)
152         m_searchHistoryListView->removeLastItem();
153
154     SearchHistoryListItem *item = new SearchHistoryListItem();
155     item->setSearchHistoryData(searchString, currentDateTime);
156     m_searchHistoryListView->prependListItem(searchString, item);
157 }
158
159 void LocationSearchPanel::clearListsSelections()
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162
163     m_locationListView->clearItemSelection();
164
165     setRouteButtonDisabled();
166 }
167
168 void LocationSearchPanel::hideEvent(QHideEvent *event)
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     QWidget::hideEvent(event);
173
174     clearListsSelections();
175 }
176
177 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180
181     m_searchHistoryListView->hide();
182     setHeaderText(locations.count());
183     m_locationListView->clearList();
184
185     for (int i = 0; i < locations.size(); ++i) {
186         LocationListItem *item = new LocationListItem();
187         item->setLocationData(locations.at(i));
188         m_locationListView->addListItem(QString::number(i), item);
189     }
190
191     const int FIRST_LOCATION_ITEM_INDEX = 0;
192     const int ONE_LOCATION_ITEM = 1;
193
194     if (locations.size() == ONE_LOCATION_ITEM) {
195         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
196
197         if (item)
198             m_locationListView->setSelectedItem(item);
199     }
200
201     m_locationListView->scrollToTop();
202     m_clearLocationListButton->setEnabled(true);
203     m_locationListView->show();
204 }
205
206 void LocationSearchPanel::readSettings()
207 {
208     QSettings settings(DIRECTORY_NAME, FILE_NAME);
209
210     QList<QVariant> searchHistories = settings.value(
211             SETTINGS_SEARCH_HISTORY).toList();
212
213     for (int i = 0; i < searchHistories.count(); ++i) {
214         QList<QVariant> searchHistory = searchHistories.at(i).toList();
215         appendSearchHistory(searchHistory.at(0).toString(),
216                             QDateTime::fromString(searchHistory.at(1).toString()));
217     }
218 }
219
220 void LocationSearchPanel::routeToSelectedLocation()
221 {
222     qDebug() << __PRETTY_FUNCTION__;
223
224     LocationListItem *item = dynamic_cast<LocationListItem *>
225                              (m_locationListView->selectedItem());
226
227     if (item)
228         emit routeToLocation(item->coordinates());
229 }
230
231 void LocationSearchPanel::setHeaderText(int count)
232 {
233     qDebug() << __PRETTY_FUNCTION__;
234
235     m_resultsLabel->setText(tr("Search results: %1").arg(count));
236 }
237
238 void LocationSearchPanel::setRouteButtonDisabled()
239 {
240     qDebug() << __PRETTY_FUNCTION__;
241
242     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
243 }
244
245 void LocationSearchPanel::showSearchHistoryList()
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_locationListView->clearList();
250     m_locationListView->hide();
251     m_resultsLabel->setText(tr("Search history:"));
252     m_clearLocationListButton->setDisabled(true);
253     m_searchHistoryListView->show();
254 }