Clean-up and re-factoring for LocationSearchPanel
[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/route_to_location.png",
89                                     ":res/images/route_to_location_s.png", "", this);
90     m_routeButton->setDisabled(true);
91     connect(m_routeButton, SIGNAL(clicked()),
92             this, SLOT(routeToSelectedLocation()));
93
94     m_contextButtonLayout->addWidget(searchLocationButton);
95     m_contextButtonLayout->addWidget(m_routeButton);
96 }
97
98 void LocationSearchPanel::clearListsSelections()
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     m_locationListView->clearItemSelection();
103
104     setRouteButtonDisabled();
105 }
106
107 void LocationSearchPanel::hideEvent(QHideEvent *event)
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     QWidget::hideEvent(event);
112
113     clearListsSelections();
114 }
115
116 void LocationSearchPanel::populateLocationListView(const QList<Location> &locations)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     setHeaderText(locations.count());
121
122     m_locationListView->clearList();
123
124     for (int i = 0; i < locations.size(); ++i) {
125         LocationListItem *item = new LocationListItem();
126         item->setLocationData(locations.at(i));
127         m_locationListView->addListItem(QString::number(i), item);
128     }
129
130     const int FIRST_LOCATION_ITEM_INDEX = 0;
131     const int ONE_LOCATION_ITEM = 1;
132
133     if (locations.size() == ONE_LOCATION_ITEM) {
134         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
135
136         if (item)
137             m_locationListView->setSelectedItem(item);
138     }
139
140     m_locationListView->scrollToTop();
141 }
142
143 void LocationSearchPanel::routeToSelectedLocation()
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     LocationListItem *item = dynamic_cast<LocationListItem *>
148                              (m_locationListView->selectedItem());
149
150     if (item)
151         emit routeToLocation(item->coordinates());
152 }
153
154 void LocationSearchPanel::setHeaderText(int count)
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     m_resultsLabel->setText(tr("Search results: %1").arg(count));
159 }
160
161 void LocationSearchPanel::setRouteButtonDisabled()
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
166 }