Fixed a bug that prevented the routing tab to be selected when a route to friend...
[situare] / src / ui / routingpanel.cpp
1 #include "coordinates/geocoordinate.h"
2 #include "extendedlistitemdelegate.h"
3 #include "locationlistitem.h"
4 #include "locationlistview.h"
5 #include "imagebutton.h"
6 #include "panelcommon.h"
7 #include "routewaypointlistitem.h"
8 #include "routewaypointlistview.h"
9 #include "routing/location.h"
10 #include "routing/route.h"
11
12 #include "routingpanel.h"
13
14 RoutingPanel::RoutingPanel(QWidget *parent)
15     : PanelBase(parent)
16 {
17     qDebug() << __PRETTY_FUNCTION__;
18
19     // --- ROUTING INSTRUCTIONS WIDGET ---
20     m_routeWaypointListView = new RouteWaypointListView(this);
21     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
22
23     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
24             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
25
26     QVBoxLayout *routingViewLayout = new QVBoxLayout;
27     routingViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
28                                           PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
29
30     // main routing layout
31     m_routingView = new QWidget();
32     m_routingView->setLayout(routingViewLayout);
33     routingViewLayout->addWidget(m_routeWaypointListView);
34
35     // --- SEARCH RESULTS WIDGET ---
36     // header
37     QWidget *resultsHeaderWidget = new QWidget();
38     resultsHeaderWidget->setAutoFillBackground(true);
39     QPalette labelPalette = resultsHeaderWidget->palette();
40     labelPalette.setColor(QPalette::Background, Qt::black);
41     resultsHeaderWidget->setPalette(labelPalette);
42
43     QHBoxLayout *headerLayout = new QHBoxLayout();
44     resultsHeaderWidget->setLayout(headerLayout);
45     headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
46                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
47
48     m_resultsLabel = new QLabel(this);
49     headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
50
51     // list view
52     m_locationListView = new LocationListView(this);
53     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
54
55     connect(m_locationListView,
56             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
57             this,
58             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
59
60     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
61             this, SLOT(setRouteButtonDisabled()));
62
63     QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
64     resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
65                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
66     resultsListViewLayout->addWidget(m_locationListView);
67
68     // main results layout
69     m_resultsView = new QWidget();
70     QVBoxLayout *resultsViewLayout = new QVBoxLayout;
71     const int CONTENTS_MARGIN_LEFT = 0;
72     resultsViewLayout->setContentsMargins(CONTENTS_MARGIN_LEFT, PANEL_MARGIN_TOP,
73                                           PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
74     m_resultsView->setLayout(resultsViewLayout);
75
76     resultsViewLayout->addWidget(resultsHeaderWidget);
77     resultsViewLayout->addLayout(resultsListViewLayout);
78
79     // --- MAIN LAYOUT ---
80     QVBoxLayout *routingPanelLayout = new QVBoxLayout;
81     routingPanelLayout->setMargin(0);
82     routingPanelLayout->setSpacing(0);
83     setLayout(routingPanelLayout);
84
85     m_views = new QStackedLayout();
86     routingPanelLayout->addLayout(m_views);
87     m_views->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
88                                 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
89
90     m_views->addWidget(m_routingView);
91     m_views->addWidget(m_resultsView);
92
93     // --- CONTEXT BUTTONS ---
94     m_searchLocationButton = new ImageButton(":/res/images/search.png",
95                                              ":/res/images/search_s.png", "", this);
96     m_searchLocationButton->setCheckable(true);
97     connect(m_searchLocationButton, SIGNAL(toggled(bool)),
98             this, SLOT(searchLocationButtonToggled(bool)));
99
100     m_routeButton = new ImageButton(":res/images/route_to_location.png",
101                                     ":res/images/route_to_location_s.png", "", this);
102     m_routeButton->setCheckable(true);
103     m_routeButton->setDisabled(true);
104     connect(m_routeButton, SIGNAL(toggled(bool)),
105             this, SLOT(routeButtonToggled(bool)));
106
107     m_contextButtonLayout->addWidget(m_searchLocationButton);
108     m_contextButtonLayout->addWidget(m_routeButton);
109 }
110
111 void RoutingPanel::clearListsSelections()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     m_locationListView->clearItemSelection();
116     m_routeWaypointListView->clearItemSelection();
117
118     setRouteButtonDisabled();
119 }
120
121 void RoutingPanel::hideEvent(QHideEvent *event)
122 {
123     qDebug() << __PRETTY_FUNCTION__;
124
125     QWidget::hideEvent(event);
126
127     clearListsSelections();
128 }
129
130 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     m_resultsLabel->setText(tr("Search results: %1").arg(locations.count()));
135
136     m_locationListView->clearList();
137
138     for (int i = 0; i < locations.size(); ++i) {
139         LocationListItem *item = new LocationListItem();
140         item->setLocationData(locations.at(i));
141         m_locationListView->addListItem(QString::number(i), item);
142     }
143
144     const int FIRST_LOCATION_ITEM_INDEX = 0;
145     const int ONE_LOCATION_ITEM = 1;
146
147     if (locations.size() == ONE_LOCATION_ITEM) {
148         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
149
150         if (item)
151             m_locationListView->setSelectedItem(item);
152     }
153
154     m_locationListView->scrollToTop();
155 }
156
157 void RoutingPanel::routeButtonToggled(bool checked)
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     if (checked) {
162         routeToSelectedLocation();
163         m_searchLocationButton->setChecked(false);
164     } else {
165         emit clearRoute();
166         m_routeWaypointListView->clearList();
167         setRouteButtonDisabled();
168     }
169 }
170
171 void RoutingPanel::routeToSelectedLocation()
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     LocationListItem *item = dynamic_cast<LocationListItem *>
176                              (m_locationListView->selectedItem());
177
178     if (item)
179         emit routeToLocation(item->coordinates());
180 }
181
182 void RoutingPanel::searchLocationButtonToggled(bool checked)
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     if (checked) {
187         showResultsView();
188         emit requestSearchLocation();
189     } else {
190         showRoutingView();
191     }
192 }
193
194 void RoutingPanel::setRoute(Route &route)
195 {
196     qDebug() << __PRETTY_FUNCTION__;
197
198     m_routeWaypointListView->clearList();
199
200     QList<RouteSegment> segments = route.segments();
201     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
202
203     for (int i = 0; i < segments.size(); ++i) {
204         RouteWaypointListItem *item = new RouteWaypointListItem();
205         RouteSegment routeSegment = segments.at(i);
206         item->setRouteWaypointData(routeSegment,
207                                    geometryPoints.at(routeSegment.positionIndex()));
208
209         m_routeWaypointListView->addListItem(QString::number(i), item);
210     }
211
212     m_routeWaypointListView->scrollToTop();
213
214     // route might come from FriendListPanel's route to friend button, so we have to
215     // check the routing button without emitting new routing request
216     blockSignals(true);
217     m_routeButton->setEnabled(true);
218     m_routeButton->setChecked(true);
219     blockSignals(false);
220     m_searchLocationButton->setChecked(false);
221
222     // search location button might be already false, so we have to make sure the
223     // toggle action handler is called every time
224     searchLocationButtonToggled(false);
225
226     emit openPanelRequested(this);
227 }
228
229 void RoutingPanel::setRouteButtonDisabled()
230 {
231     qDebug() << __PRETTY_FUNCTION__;
232
233     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty()
234                                && !m_routeButton->isChecked());
235 }
236
237 void RoutingPanel::showResultsView()
238 {
239     qDebug() << __PRETTY_FUNCTION__;
240
241     m_views->setCurrentWidget(m_resultsView);
242     m_routeWaypointListView->clearItemSelection();
243 }
244
245 void RoutingPanel::showRoutingView()
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_views->setCurrentWidget(m_routingView);
250     m_locationListView->clearItemSelection();
251 }