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"
12 #include "routingpanel.h"
14 RoutingPanel::RoutingPanel(QWidget *parent)
17 qDebug() << __PRETTY_FUNCTION__;
19 // --- ROUTING INSTRUCTIONS WIDGET ---
20 m_routeWaypointListView = new RouteWaypointListView(this);
21 m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
23 connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
24 this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
26 QVBoxLayout *routingViewLayout = new QVBoxLayout;
27 routingViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
28 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
30 // main routing layout
31 m_routingView = new QWidget();
32 m_routingView->setLayout(routingViewLayout);
33 routingViewLayout->addWidget(m_routeWaypointListView);
35 // --- SEARCH RESULTS WIDGET ---
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);
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);
48 m_resultsLabel = new QLabel(this);
49 headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
52 m_locationListView = new LocationListView(this);
53 m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
55 connect(m_locationListView,
56 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
58 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
60 connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
61 this, SLOT(setRouteButtonDisabled()));
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);
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);
76 resultsViewLayout->addWidget(resultsHeaderWidget);
77 resultsViewLayout->addLayout(resultsListViewLayout);
79 // --- MAIN LAYOUT ---
80 QVBoxLayout *routingPanelLayout = new QVBoxLayout;
81 routingPanelLayout->setMargin(0);
82 routingPanelLayout->setSpacing(0);
83 setLayout(routingPanelLayout);
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);
90 m_views->addWidget(m_routingView);
91 m_views->addWidget(m_resultsView);
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)));
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)));
107 m_contextButtonLayout->addWidget(m_searchLocationButton);
108 m_contextButtonLayout->addWidget(m_routeButton);
111 void RoutingPanel::clearListsSelections()
113 qDebug() << __PRETTY_FUNCTION__;
115 m_locationListView->clearItemSelection();
116 m_routeWaypointListView->clearItemSelection();
118 setRouteButtonDisabled();
121 void RoutingPanel::hideEvent(QHideEvent *event)
123 qDebug() << __PRETTY_FUNCTION__;
125 QWidget::hideEvent(event);
127 clearListsSelections();
130 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
132 qDebug() << __PRETTY_FUNCTION__;
134 m_resultsLabel->setText(tr("Search results: %1").arg(locations.count()));
136 m_locationListView->clearList();
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);
144 const int FIRST_LOCATION_ITEM_INDEX = 0;
145 const int ONE_LOCATION_ITEM = 1;
147 if (locations.size() == ONE_LOCATION_ITEM) {
148 ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
151 m_locationListView->setSelectedItem(item);
154 m_locationListView->scrollToTop();
157 void RoutingPanel::routeButtonToggled(bool checked)
159 qDebug() << __PRETTY_FUNCTION__;
162 routeToSelectedLocation();
163 m_searchLocationButton->setChecked(false);
166 m_routeWaypointListView->clearList();
167 setRouteButtonDisabled();
171 void RoutingPanel::routeToSelectedLocation()
173 qDebug() << __PRETTY_FUNCTION__;
175 LocationListItem *item = dynamic_cast<LocationListItem *>
176 (m_locationListView->selectedItem());
179 emit routeToLocation(item->coordinates());
182 void RoutingPanel::searchLocationButtonToggled(bool checked)
184 qDebug() << __PRETTY_FUNCTION__;
188 emit requestSearchLocation();
194 void RoutingPanel::setRoute(Route &route)
196 qDebug() << __PRETTY_FUNCTION__;
198 m_routeWaypointListView->clearList();
200 QList<RouteSegment> segments = route.segments();
201 QList<GeoCoordinate> geometryPoints = route.geometryPoints();
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()));
209 m_routeWaypointListView->addListItem(QString::number(i), item);
212 m_routeWaypointListView->scrollToTop();
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
217 m_routeButton->setEnabled(true);
218 m_routeButton->setChecked(true);
220 m_searchLocationButton->setChecked(false);
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);
226 emit openPanelRequested(this);
229 void RoutingPanel::setRouteButtonDisabled()
231 qDebug() << __PRETTY_FUNCTION__;
233 m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty()
234 && !m_routeButton->isChecked());
237 void RoutingPanel::showResultsView()
239 qDebug() << __PRETTY_FUNCTION__;
241 m_views->setCurrentWidget(m_resultsView);
242 m_routeWaypointListView->clearItemSelection();
245 void RoutingPanel::showRoutingView()
247 qDebug() << __PRETTY_FUNCTION__;
249 m_views->setCurrentWidget(m_routingView);
250 m_locationListView->clearItemSelection();