1 #include "coordinates/geocoordinate.h"
2 #include "locationlistitem.h"
3 #include "locationlistview.h"
4 #include "extendedlistitemdelegate.h"
5 #include "routing/location.h"
6 #include "routing/route.h"
7 #include "routewaypointlistview.h"
8 #include "routewaypointlistitem.h"
9 #include "panelcommon.h"
11 #include "routingpanel.h"
13 RoutingPanel::RoutingPanel(QWidget *parent)
16 qDebug() << __PRETTY_FUNCTION__;
18 QVBoxLayout *routingLayout = new QVBoxLayout;
19 routingLayout->setMargin(0);
20 routingLayout->setSpacing(0);
21 setLayout(routingLayout);
23 QHBoxLayout *headerLayout = new QHBoxLayout();
24 headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
25 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
27 QVBoxLayout *listViewLayout = new QVBoxLayout;
28 listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
29 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
31 m_searchLocationButton = new QPushButton(tr("Search location"));
33 m_routeButton = new QPushButton(tr("Route to location"));
34 m_routeButton->setDisabled(true);
35 m_routeButton->hide();
37 m_locationListHeaderWidget = new QWidget();
38 m_locationListHeaderWidget->setLayout(headerLayout);
39 m_locationListHeaderWidget->setAutoFillBackground(true);
40 QPalette labelPalette = m_locationListHeaderWidget->palette();
41 labelPalette.setColor(QPalette::Background, Qt::black);
42 m_locationListHeaderWidget->setPalette(labelPalette);
43 m_locationListHeaderWidget->hide();
45 m_locationListLabel = new QLabel(this);
47 m_locationListView = new LocationListView(this);
48 m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
49 m_locationListView->hide();
51 m_routeWaypointListView = new RouteWaypointListView(this);
52 m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
53 m_routeWaypointListView->hide();
55 headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
57 listViewLayout->addWidget(m_locationListView);
58 listViewLayout->addWidget(m_routeWaypointListView);
60 routingLayout->addWidget(m_searchLocationButton);
61 routingLayout->addWidget(m_routeButton);
62 routingLayout->addWidget(m_locationListHeaderWidget);
63 routingLayout->addLayout(listViewLayout);
65 connect(m_locationListView,
66 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
68 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
70 connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
71 this, SLOT(setRouteButtonDisabled()));
73 connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
74 this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
76 connect(m_routeButton, SIGNAL(clicked()),
77 this, SLOT(routeToSelectedLocation()));
79 connect(m_searchLocationButton, SIGNAL(clicked()),
80 this, SIGNAL(requestSearchLocation()));
83 void RoutingPanel::clearListsSelections()
85 qDebug() << __PRETTY_FUNCTION__;
87 m_locationListView->clearItemSelection();
88 m_routeWaypointListView->clearItemSelection();
90 setRouteButtonDisabled();
93 void RoutingPanel::hideEvent(QHideEvent *event)
95 qDebug() << __PRETTY_FUNCTION__;
97 QWidget::hideEvent(event);
99 clearListsSelections();
102 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
104 qDebug() << __PRETTY_FUNCTION__;
106 m_routeButton->show();
108 m_locationListHeaderWidget->show();
109 m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
111 m_routeWaypointListView->hide();
112 m_locationListView->show();
113 m_locationListView->clearList();
115 for (int i = 0; i < locations.size(); ++i) {
116 LocationListItem *item = new LocationListItem();
117 item->setLocationData(locations.at(i));
118 m_locationListView->addListItem(QString::number(i), item);
121 const int FIRST_LOCATION_ITEM_INDEX = 0;
122 const int ONE_LOCATION_ITEM = 1;
124 if (locations.size() == ONE_LOCATION_ITEM) {
125 ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
128 m_locationListView->setSelectedItem(item);
131 m_locationListView->scrollToTop();
134 void RoutingPanel::routeToSelectedLocation()
136 qDebug() << __PRETTY_FUNCTION__;
138 LocationListItem *item = dynamic_cast<LocationListItem *>
139 (m_locationListView->selectedItem());
142 emit routeToLocation(item->coordinates());
145 void RoutingPanel::setRoute(Route &route)
147 qDebug() << __PRETTY_FUNCTION__;
149 m_routeButton->hide();
151 m_locationListHeaderWidget->hide();
152 m_locationListView->hide();
153 m_routeWaypointListView->show();
154 m_routeWaypointListView->clearList();
156 QList<RouteSegment> segments = route.segments();
157 QList<GeoCoordinate> geometryPoints = route.geometryPoints();
159 for (int i = 0; i < segments.size(); ++i) {
160 RouteWaypointListItem *item = new RouteWaypointListItem();
161 RouteSegment routeSegment = segments.at(i);
162 item->setRouteWaypointData(routeSegment,
163 geometryPoints.at(routeSegment.positionIndex()));
165 m_routeWaypointListView->addListItem(QString::number(i), item);
168 m_routeWaypointListView->scrollToTop();
170 emit showPanelRequested(this);
173 void RoutingPanel::setRouteButtonDisabled()
175 qDebug() << __PRETTY_FUNCTION__;
177 m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());