b765744e135235a054a0eb91a4f91f98d1377047
[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 "extendedlistitemdelegate.h"
24 #include "locationlistitem.h"
25 #include "locationlistview.h"
26 #include "imagebutton.h"
27 #include "panelcommon.h"
28 #include "routing/location.h"
29
30 #include "locationsearchpanel.h"
31
32 LocationSearchPanel::LocationSearchPanel(QWidget *parent)
33     : PanelBase(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     // --- HEADER WIDGET ---
38     QWidget *resultsHeaderWidget = new QWidget();
39     resultsHeaderWidget->setAutoFillBackground(true);
40     QPalette labelPalette = resultsHeaderWidget->palette();
41     labelPalette.setColor(QPalette::Background, Qt::black);
42     resultsHeaderWidget->setPalette(labelPalette);
43
44     QHBoxLayout *headerLayout = new QHBoxLayout();
45     resultsHeaderWidget->setLayout(headerLayout);
46     headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
47                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
48
49     m_resultsLabel = new QLabel(this);
50     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
51     setHeaderText(0);
52
53     // --- SEARCH RESULTS LIST VIEW ---
54     m_locationListView = new LocationListView(this);
55     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
56
57     connect(m_locationListView,
58             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
59             this,
60             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
61
62     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
63             this, SLOT(setRouteButtonDisabled()));
64
65     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
66             this, SLOT(onListItemSelectionChanged()));
67
68     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
69     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
70                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
71     resultsListViewLayout->addWidget(m_locationListView);
72
73     // --- MAIN LAYOUT ---
74     QVBoxLayout *panelLayout = new QVBoxLayout;
75     panelLayout->setSpacing(0);
76     setLayout(panelLayout);
77
78     const int MARGIN_LEFT = 0;
79     panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
80                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
81
82     panelLayout->addWidget(resultsHeaderWidget);
83     panelLayout->addLayout(resultsListViewLayout);
84
85     // --- CONTEXT BUTTONS ---
86     m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
87     m_routeButton->setDisabled(true);
88     connect(m_routeButton, SIGNAL(clicked()),
89             this, SLOT(routeToSelectedLocation()));
90
91     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
92                                                         ":/res/images/search_s.png", "", this);
93     connect(searchLocationButton, SIGNAL(clicked()),
94             this, SIGNAL(requestSearchLocation()));
95
96     m_itemButtonsLayout->addWidget(m_routeButton);
97     m_genericButtonsLayout->addWidget(searchLocationButton);
98 }
99
100 void LocationSearchPanel::clearListsSelections()
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     m_locationListView->clearItemSelection();
105
106     setRouteButtonDisabled();
107 }
108
109 void LocationSearchPanel::hideEvent(QHideEvent *event)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     QWidget::hideEvent(event);
114
115     clearListsSelections();
116 }
117
118 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     setHeaderText(locations.count());
123
124     m_locationListView->clearList();
125
126     for (int i = 0; i < locations.size(); ++i) {
127         LocationListItem *item = new LocationListItem();
128         item->setLocationData(locations.at(i));
129         m_locationListView->addListItem(QString::number(i), item);
130     }
131
132     const int FIRST_LOCATION_ITEM_INDEX = 0;
133     const int ONE_LOCATION_ITEM = 1;
134
135     if (locations.size() == ONE_LOCATION_ITEM) {
136         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
137
138         if (item)
139             m_locationListView->setSelectedItem(item);
140     }
141
142     m_locationListView->scrollToTop();
143 }
144
145 void LocationSearchPanel::routeToSelectedLocation()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     LocationListItem *item = dynamic_cast<LocationListItem *>
150                              (m_locationListView->selectedItem());
151
152     if (item)
153         emit routeToLocation(item->coordinates());
154 }
155
156 void LocationSearchPanel::setHeaderText(int count)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     m_resultsLabel->setText(tr("Search results: %1").arg(count));
161 }
162
163 void LocationSearchPanel::setRouteButtonDisabled()
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
168 }