2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
6 Sami Rämö - sami.ramo@ixonos.com
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.
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.
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,
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"
38 #include "locationsearchpanel.h"
40 const QString SETTINGS_SEARCH_HISTORY = "SEARCH_HISTORY";
42 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
45 qDebug() << __PRETTY_FUNCTION__;
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);
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);
59 m_resultsLabel = new QLabel(this);
60 headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
63 // --- SEARCH HISTORY LIST VIEW ---
64 m_searchHistoryListView = new SearchHistoryListView(this);
65 m_searchHistoryListView->setItemDelegate(new ExtendedListItemDelegate(this));
67 connect(m_searchHistoryListView, SIGNAL(searchHistoryItemClicked(QString)),
68 this, SIGNAL(searchHistoryItemClicked(QString)));
70 // --- SEARCH RESULTS LIST VIEW ---
71 m_locationListView = new LocationListView(this);
72 m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
74 connect(m_locationListView,
75 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
77 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
79 connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
80 this, SLOT(setRouteButtonDisabled()));
82 QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
83 resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
84 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
85 resultsListViewLayout->addWidget(m_searchHistoryListView);
86 resultsListViewLayout->addWidget(m_locationListView);
88 // --- MAIN LAYOUT ---
89 QVBoxLayout *panelLayout = new QVBoxLayout;
90 panelLayout->setSpacing(0);
91 setLayout(panelLayout);
93 const int MARGIN_LEFT = 0;
94 panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
95 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
97 panelLayout->addWidget(resultsHeaderWidget);
98 panelLayout->addLayout(resultsListViewLayout);
100 // --- CONTEXT BUTTONS ---
101 m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
102 m_routeButton->setDisabled(true);
103 connect(m_routeButton, SIGNAL(clicked()),
104 this, SLOT(routeToSelectedLocation()));
106 ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
107 ":/res/images/search_s.png", "", this);
109 connect(searchLocationButton, SIGNAL(clicked()),
110 this, SIGNAL(requestSearchLocation()));
112 m_clearLocationListButton = new ImageButton(":/res/images/clear_btn.png",
113 ":/res/images/clear_btn_s.png",
114 ":/res/images/clear_btn_d.png", this);
115 m_clearLocationListButton->setDisabled(true);
117 connect(m_clearLocationListButton, SIGNAL(clicked()),
118 this, SLOT(showSearchHistoryList()));
120 m_contextButtonLayout->addWidget(m_routeButton);
121 m_contextButtonLayout->addWidget(searchLocationButton);
122 m_contextButtonLayout->addWidget(m_clearLocationListButton);
125 showSearchHistoryList();
128 LocationSearchPanel::~LocationSearchPanel()
130 qDebug() << __PRETTY_FUNCTION__;
132 QSettings settings(DIRECTORY_NAME, FILE_NAME);
133 QList<QVariant> searchHistories;
135 for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
136 SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
137 m_searchHistoryListView->listItemAt(i));
140 QList<QString> searchHistory;
141 searchHistory.append(item->title());
142 searchHistory.append(item->dateTime().toString());
143 searchHistories.append(QVariant(searchHistory));
147 settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
150 void LocationSearchPanel::prependSearchHistory(QString searchString, QDateTime dateTime)
152 qDebug() << __PRETTY_FUNCTION__;
154 const int SEARCH_HISTORY_LIMIT = 10;
155 static int counter = 0;
157 if (m_searchHistoryListView->count() >= SEARCH_HISTORY_LIMIT)
158 m_searchHistoryListView->removeLastItem();
160 SearchHistoryListItem *item = new SearchHistoryListItem();
161 item->setSearchHistoryData(searchString, dateTime);
162 m_searchHistoryListView->prependListItem(QString::number(counter++), item);
165 void LocationSearchPanel::clearListsSelections()
167 qDebug() << __PRETTY_FUNCTION__;
169 m_locationListView->clearItemSelection();
170 m_searchHistoryListView->clearItemSelection();
172 setRouteButtonDisabled();
175 void LocationSearchPanel::hideEvent(QHideEvent *event)
177 qDebug() << __PRETTY_FUNCTION__;
179 QWidget::hideEvent(event);
181 clearListsSelections();
184 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
186 qDebug() << __PRETTY_FUNCTION__;
188 m_searchHistoryListView->clearItemSelection();
189 m_searchHistoryListView->hide();
190 setHeaderText(locations.count());
191 m_locationListView->clearList();
192 m_clearLocationListButton->setEnabled(true);
193 m_locationListView->show();
195 for (int i = 0; i < locations.size(); ++i) {
196 LocationListItem *item = new LocationListItem();
197 item->setLocationData(locations.at(i));
198 m_locationListView->addListItem(QString::number(i), item);
201 const int FIRST_LOCATION_ITEM_INDEX = 0;
202 const int ONE_LOCATION_ITEM = 1;
204 if (locations.size() == ONE_LOCATION_ITEM) {
205 ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
208 m_locationListView->setSelectedItem(item);
211 m_locationListView->scrollToTop();
214 void LocationSearchPanel::readSettings()
216 qDebug() << __PRETTY_FUNCTION__;
218 const int SEARCH_HISTORY_LIST_ITEM_COUNT = 2;
220 QSettings settings(DIRECTORY_NAME, FILE_NAME);
221 QList<QVariant> searchHistories = settings.value(SETTINGS_SEARCH_HISTORY).toList();
223 //Read from end to begin so items are prepended in correct order
224 for (int i = searchHistories.count() - 1; i >= 0; --i) {
225 QList<QVariant> searchHistory = searchHistories.at(i).toList();
226 if (searchHistory.count() == SEARCH_HISTORY_LIST_ITEM_COUNT) {
227 prependSearchHistory(searchHistory.at(0).toString(),
228 QDateTime::fromString(searchHistory.at(1).toString()));
233 void LocationSearchPanel::routeToSelectedLocation()
235 qDebug() << __PRETTY_FUNCTION__;
237 LocationListItem *item = dynamic_cast<LocationListItem *>
238 (m_locationListView->selectedItem());
241 emit routeToLocation(item->coordinates());
244 void LocationSearchPanel::setHeaderText(int count)
246 qDebug() << __PRETTY_FUNCTION__;
248 m_resultsLabel->setText(tr("Search results: %1").arg(count));
251 void LocationSearchPanel::setRouteButtonDisabled()
253 qDebug() << __PRETTY_FUNCTION__;
255 m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
258 void LocationSearchPanel::showSearchHistoryList()
260 qDebug() << __PRETTY_FUNCTION__;
262 m_locationListView->clearList();
263 m_locationListView->hide();
264 m_resultsLabel->setText(tr("Search history:"));
265 m_clearLocationListButton->setDisabled(true);
266 m_searchHistoryListView->show();