df452c400af9add3a8f6270ac5f06634fdb4e0bb
[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     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
66     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
67                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
68     resultsListViewLayout->addWidget(m_locationListView);
69
70     // --- MAIN LAYOUT ---
71     QVBoxLayout *panelLayout = new QVBoxLayout;
72     panelLayout->setSpacing(0);
73     setLayout(panelLayout);
74
75     const int MARGIN_LEFT = 0;
76     panelLayout->setContentsMargins(MARGIN_LEFT, PANEL_MARGIN_TOP,
77                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
78
79     panelLayout->addWidget(resultsHeaderWidget);
80     panelLayout->addLayout(resultsListViewLayout);
81
82     // --- CONTEXT BUTTONS ---
83     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
84                                                         ":/res/images/search_s.png", "", this);
85     connect(searchLocationButton, SIGNAL(clicked()),
86             this, SIGNAL(requestSearchLocation()));
87
88     m_routeButton = new ImageButton(":res/images/compass.png", "", "", this);
89     m_routeButton->setDisabled(true);
90     connect(m_routeButton, SIGNAL(clicked()),
91             this, SLOT(routeToSelectedLocation()));
92
93     m_contextButtonLayout->addWidget(searchLocationButton);
94     m_contextButtonLayout->addWidget(m_routeButton);
95 }
96
97 void LocationSearchPanel::clearListsSelections()
98 {
99     qDebug() << __PRETTY_FUNCTION__;
100
101     m_locationListView->clearItemSelection();
102
103     setRouteButtonDisabled();
104 }
105
106 void LocationSearchPanel::hideEvent(QHideEvent *event)
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109
110     QWidget::hideEvent(event);
111
112     clearListsSelections();
113 }
114
115 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     setHeaderText(locations.count());
120
121     m_locationListView->clearList();
122
123     for (int i = 0; i < locations.size(); ++i) {
124         LocationListItem *item = new LocationListItem();
125         item->setLocationData(locations.at(i));
126         m_locationListView->addListItem(QString::number(i), item);
127     }
128
129     const int FIRST_LOCATION_ITEM_INDEX = 0;
130     const int ONE_LOCATION_ITEM = 1;
131
132     if (locations.size() == ONE_LOCATION_ITEM) {
133         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
134
135         if (item)
136             m_locationListView->setSelectedItem(item);
137     }
138
139     m_locationListView->scrollToTop();
140 }
141
142 void LocationSearchPanel::routeToSelectedLocation()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     LocationListItem *item = dynamic_cast<LocationListItem *>
147                              (m_locationListView->selectedItem());
148
149     if (item)
150         emit routeToLocation(item->coordinates());
151 }
152
153 void LocationSearchPanel::setHeaderText(int count)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_resultsLabel->setText(tr("Search results: %1").arg(count));
158 }
159
160 void LocationSearchPanel::setRouteButtonDisabled()
161 {
162     qDebug() << __PRETTY_FUNCTION__;
163
164     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
165 }