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 // --- SEARCH RESULTS LIST VIEW ---
68 m_locationListView = new LocationListView(this);
69 m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
71 connect(m_locationListView,
72 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
74 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
76 connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
77 this, SLOT(setRouteButtonDisabled()));
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);
85 // --- MAIN LAYOUT ---
86 QVBoxLayout *panelLayout = new QVBoxLayout;
87 panelLayout->setSpacing(0);
88 setLayout(panelLayout);
90 const int MARGIN_LEFT = 0;
91 panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
92 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
94 panelLayout->addWidget(resultsHeaderWidget);
95 panelLayout->addLayout(resultsListViewLayout);
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()));
103 ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
104 ":/res/images/search_s.png", "", this);
106 connect(searchLocationButton, SIGNAL(clicked()),
107 this, SIGNAL(requestSearchLocation()));
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);
114 connect(m_clearLocationListButton, SIGNAL(clicked()),
115 this, SLOT(showSearchHistoryList()));
117 m_contextButtonLayout->addWidget(m_routeButton);
118 m_contextButtonLayout->addWidget(searchLocationButton);
119 m_contextButtonLayout->addWidget(m_clearLocationListButton);
122 showSearchHistoryList();
125 LocationSearchPanel::~LocationSearchPanel()
127 qDebug() << __PRETTY_FUNCTION__;
129 QSettings settings(DIRECTORY_NAME, FILE_NAME);
130 QList<QVariant> searchHistories;
132 for (int i = 0; i < m_searchHistoryListView->count(); ++i) {
133 SearchHistoryListItem *item = dynamic_cast<SearchHistoryListItem*>(
134 m_searchHistoryListView->listItemAt(i));
137 QList<QVariant> searchHistory;
138 searchHistory.append(item->title());
139 searchHistory.append(item->dateTime().toString());
140 searchHistories.append(searchHistory);
144 settings.setValue(SETTINGS_SEARCH_HISTORY, searchHistories);
147 void LocationSearchPanel::appendSearchHistory(QString searchString, QDateTime currentDateTime)
149 qDebug() << __PRETTY_FUNCTION__;
151 if (m_searchHistoryListView->count() >= 10)
152 m_searchHistoryListView->removeLastItem();
154 SearchHistoryListItem *item = new SearchHistoryListItem();
155 item->setSearchHistoryData(searchString, currentDateTime);
156 m_searchHistoryListView->prependListItem(searchString, item);
159 void LocationSearchPanel::clearListsSelections()
161 qDebug() << __PRETTY_FUNCTION__;
163 m_locationListView->clearItemSelection();
165 setRouteButtonDisabled();
168 void LocationSearchPanel::hideEvent(QHideEvent *event)
170 qDebug() << __PRETTY_FUNCTION__;
172 QWidget::hideEvent(event);
174 clearListsSelections();
177 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
179 qDebug() << __PRETTY_FUNCTION__;
181 m_searchHistoryListView->hide();
182 setHeaderText(locations.count());
183 m_locationListView->clearList();
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);
191 const int FIRST_LOCATION_ITEM_INDEX = 0;
192 const int ONE_LOCATION_ITEM = 1;
194 if (locations.size() == ONE_LOCATION_ITEM) {
195 ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
198 m_locationListView->setSelectedItem(item);
201 m_locationListView->scrollToTop();
202 m_clearLocationListButton->setEnabled(true);
203 m_locationListView->show();
206 void LocationSearchPanel::readSettings()
208 QSettings settings(DIRECTORY_NAME, FILE_NAME);
210 QList<QVariant> searchHistories = settings.value(
211 SETTINGS_SEARCH_HISTORY).toList();
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()));
220 void LocationSearchPanel::routeToSelectedLocation()
222 qDebug() << __PRETTY_FUNCTION__;
224 LocationListItem *item = dynamic_cast<LocationListItem *>
225 (m_locationListView->selectedItem());
228 emit routeToLocation(item->coordinates());
231 void LocationSearchPanel::setHeaderText(int count)
233 qDebug() << __PRETTY_FUNCTION__;
235 m_resultsLabel->setText(tr("Search results: %1").arg(count));
238 void LocationSearchPanel::setRouteButtonDisabled()
240 qDebug() << __PRETTY_FUNCTION__;
242 m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
245 void LocationSearchPanel::showSearchHistoryList()
247 qDebug() << __PRETTY_FUNCTION__;
249 m_locationListView->clearList();
250 m_locationListView->hide();
251 m_resultsLabel->setText(tr("Search history:"));
252 m_clearLocationListButton->setDisabled(true);
253 m_searchHistoryListView->show();