Changed routing panel to use PanelBase as a base class, removed old search location...
[situare] / src / ui / routingpanel.cpp
1 #include "coordinates/geocoordinate.h"
2 #include "locationlistitem.h"
3 #include "locationlistview.h"
4 #include "extendedlistitemdelegate.h"
5 #include "imagebutton.h"
6 #include "routing/location.h"
7 #include "routing/route.h"
8 #include "routewaypointlistview.h"
9 #include "routewaypointlistitem.h"
10 #include "panelcommon.h"
11
12 #include "routingpanel.h"
13
14 RoutingPanel::RoutingPanel(QWidget *parent)
15     : PanelBase(parent)
16 {
17     qDebug() << __PRETTY_FUNCTION__;
18
19     QVBoxLayout *routingLayout = new QVBoxLayout;
20     routingLayout->setMargin(0);
21     routingLayout->setSpacing(0);
22     setLayout(routingLayout);
23
24     QHBoxLayout *headerLayout = new QHBoxLayout();
25     headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0,
26                                      PANEL_MARGIN_RIGHT, 0);
27
28     QVBoxLayout *listViewLayout = new QVBoxLayout;
29     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
30
31     ImageButton *searchLocationButton = new ImageButton(0, ":/res/images/search.png",
32                                                              ":/res/images/search_s.png");
33
34     m_contextButtonList.append(searchLocationButton);
35
36     m_routeButton = new QPushButton(tr("Route to location"));
37     m_routeButton->hide();
38
39     m_locationListHeaderWidget = new QWidget();
40     m_locationListHeaderWidget->setLayout(headerLayout);
41     m_locationListHeaderWidget->setAutoFillBackground(true);
42     QPalette labelPalette = m_locationListHeaderWidget->palette();
43     labelPalette.setColor(QPalette::Background, Qt::black);
44     m_locationListHeaderWidget->setPalette(labelPalette);
45     m_locationListHeaderWidget->hide();
46
47     m_locationListLabel = new QLabel(this);
48
49     m_locationListView = new LocationListView(this);
50     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
51     m_locationListView->hide();
52
53     m_routeWaypointListView = new RouteWaypointListView(this);
54     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
55     m_routeWaypointListView->hide();
56
57     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
58
59     listViewLayout->addWidget(m_locationListView);
60     listViewLayout->addWidget(m_routeWaypointListView);
61
62     routingLayout->addWidget(m_routeButton);
63     routingLayout->addWidget(m_locationListHeaderWidget);
64     routingLayout->addLayout(listViewLayout);
65
66     connect(m_locationListView,
67             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
68             this,
69             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
70
71     connect(m_routeButton, SIGNAL(clicked()),
72             this, SLOT(routeToSelectedLocation()));
73
74     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
75             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
76
77     connect(searchLocationButton, SIGNAL(clicked()),
78             this, SIGNAL(requestSearchLocation()));
79 }
80
81 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     m_routeButton->show();
86
87     m_locationListHeaderWidget->show();
88     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
89
90     m_routeWaypointListView->hide();
91     m_locationListView->show();
92     m_locationListView->clearList();
93
94     for (int i = 0; i < locations.size(); ++i) {
95         LocationListItem *item = new LocationListItem();
96         item->setLocationData(locations.at(i));
97         m_locationListView->addListItem(QString::number(i), item);
98     }
99
100     const int FIRST_LOCATION_ITEM_INDEX = 0;
101     const int ONE_LOCATION_ITEM = 1;
102
103     if (locations.size() == ONE_LOCATION_ITEM) {
104         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
105
106         if (item)
107             m_locationListView->setSelectedItem(item);
108     }
109
110     m_locationListView->scrollToTop();
111 }
112
113 void RoutingPanel::routeToSelectedLocation()
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     LocationListItem *item = dynamic_cast<LocationListItem *>
118                              (m_locationListView->selectedItem());
119
120     if (item)
121         emit routeToLocation(item->coordinates());
122 }
123
124 void RoutingPanel::setRoute(Route &route)
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     m_routeButton->hide();
129
130     m_locationListHeaderWidget->hide();
131     m_locationListView->hide();
132     m_routeWaypointListView->show();
133     m_routeWaypointListView->clearList();
134
135     QList<RouteSegment> segments = route.segments();
136     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
137
138     for (int i = 0; i < segments.size(); ++i) {
139         RouteWaypointListItem *item = new RouteWaypointListItem();
140         RouteSegment routeSegment = segments.at(i);
141         item->setRouteWaypointData(routeSegment,
142                                    geometryPoints.at(routeSegment.positionIndex()));
143
144         m_routeWaypointListView->addListItem(QString::number(i), item);
145     }
146
147     m_routeWaypointListView->scrollToTop();
148
149     emit showPanelRequested(this);
150 }