Added RouteWaypointList- and RouteWaypointListView classes.
[situare] / src / ui / routingpanel.cpp
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"
10
11 #include "routingpanel.h"
12
13 RoutingPanel::RoutingPanel(QWidget *parent)
14     : QWidget(parent)
15 {
16     qDebug() << __PRETTY_FUNCTION__;
17
18     QVBoxLayout *routingLayout = new QVBoxLayout;
19     routingLayout->setMargin(0);
20     routingLayout->setSpacing(0);
21     setLayout(routingLayout);
22
23     QHBoxLayout *headerLayout = new QHBoxLayout();
24     headerLayout->setContentsMargins(FRIENDPANEL_FILTER_MARGIN_LEFT, 0,
25                                      FRIENDPANEL_FILTER_MARGIN_RIGHT, 0);
26
27     QVBoxLayout *listViewLayout = new QVBoxLayout;
28     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, 0, PANEL_MARGIN_RIGHT, 0);
29
30     m_routeButton = new QPushButton("Route to location");
31
32     m_locationListHeaderWidget = new QWidget();
33     m_locationListHeaderWidget->setLayout(headerLayout);
34     m_locationListHeaderWidget->setAutoFillBackground(true);
35     QPalette labelPalette = m_locationListHeaderWidget->palette();
36     labelPalette.setColor(QPalette::Background, Qt::black);
37     m_locationListHeaderWidget->setPalette(labelPalette);
38     m_locationListHeaderWidget->hide();
39
40     m_locationListLabel = new QLabel(this);
41
42     m_locationListView = new LocationListView(this);
43     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
44     m_locationListView->hide();
45
46     m_routeWaypointListView = new RouteWaypointListView(this);
47     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
48     m_routeWaypointListView->hide();
49
50     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
51
52     listViewLayout->addWidget(m_locationListView);
53     listViewLayout->addWidget(m_routeWaypointListView);
54
55     routingLayout->addWidget(m_routeButton);
56     routingLayout->addWidget(m_locationListHeaderWidget);
57     routingLayout->addLayout(listViewLayout);
58
59     connect(m_locationListView,
60             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
61             this,
62             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
63
64     connect(m_routeButton, SIGNAL(clicked()),
65             this, SLOT(routeToSelectedLocation()));
66
67     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
68             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
69 }
70
71 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     m_locationListHeaderWidget->show();
76     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
77
78     m_routeWaypointListView->hide();
79     m_locationListView->show();
80     m_locationListView->clearList();
81
82     for (int i = 0; i < locations.size(); ++i) {
83         LocationListItem *item = new LocationListItem();
84         item->setLocationData(locations.at(i));
85         m_locationListView->addListItem(QString::number(i), item);
86     }
87
88     //openPanel();
89
90     const int FIRST_LOCATION_ITEM_INDEX = 0;
91     const int ONE_LOCATION_ITEM = 1;
92
93     if (locations.size() == ONE_LOCATION_ITEM) {
94         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
95
96         if (item)
97             m_locationListView->setSelectedItem(item);
98     }
99 }
100
101 void RoutingPanel::routeToSelectedLocation()
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     LocationListItem *item = dynamic_cast<LocationListItem *>
106                              (m_locationListView->selectedItem());
107
108     if (item)
109         emit routeToLocation(item->coordinates());
110 }
111
112 void RoutingPanel::setRoute(Route &route)
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     m_locationListView->hide();
117     m_routeWaypointListView->show();
118     m_routeWaypointListView->clear();
119
120     QList<RouteSegment> segments = route.segments();
121     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
122
123     for (int i = 0; i < segments.size(); ++i) {
124         RouteWaypointListItem *item = new RouteWaypointListItem();
125         RouteSegment routeSegment = segments.at(i);
126         item->setRouteWaypointData(routeSegment,
127                                    geometryPoints.at(routeSegment.positionIndex()));
128
129         m_routeWaypointListView->addListItem(QString::number(i), item);
130     }
131 }