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