Added missing PanelTabBar to src.pro.
[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_searchLocationButton = new QPushButton(tr("Search location"));
31
32     m_routeButton = new QPushButton(tr("Route to location"));
33     m_routeButton->hide();
34
35     m_locationListHeaderWidget = new QWidget();
36     m_locationListHeaderWidget->setLayout(headerLayout);
37     m_locationListHeaderWidget->setAutoFillBackground(true);
38     QPalette labelPalette = m_locationListHeaderWidget->palette();
39     labelPalette.setColor(QPalette::Background, Qt::black);
40     m_locationListHeaderWidget->setPalette(labelPalette);
41     m_locationListHeaderWidget->hide();
42
43     m_locationListLabel = new QLabel(this);
44
45     m_locationListView = new LocationListView(this);
46     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
47     m_locationListView->hide();
48
49     m_routeWaypointListView = new RouteWaypointListView(this);
50     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
51     m_routeWaypointListView->hide();
52
53     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
54
55     listViewLayout->addWidget(m_locationListView);
56     listViewLayout->addWidget(m_routeWaypointListView);
57
58     routingLayout->addWidget(m_searchLocationButton);
59     routingLayout->addWidget(m_routeButton);
60     routingLayout->addWidget(m_locationListHeaderWidget);
61     routingLayout->addLayout(listViewLayout);
62
63     connect(m_locationListView,
64             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
65             this,
66             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
67
68     connect(m_routeButton, SIGNAL(clicked()),
69             this, SLOT(routeToSelectedLocation()));
70
71     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
72             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
73
74     connect(m_searchLocationButton, SIGNAL(clicked()),
75             this, SIGNAL(requestSearchLocation()));
76 }
77
78 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     m_routeButton->show();
83
84     m_locationListHeaderWidget->show();
85     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
86
87     m_routeWaypointListView->hide();
88     m_locationListView->show();
89     m_locationListView->clearList();
90
91     for (int i = 0; i < locations.size(); ++i) {
92         LocationListItem *item = new LocationListItem();
93         item->setLocationData(locations.at(i));
94         m_locationListView->addListItem(QString::number(i), item);
95     }
96
97     const int FIRST_LOCATION_ITEM_INDEX = 0;
98     const int ONE_LOCATION_ITEM = 1;
99
100     if (locations.size() == ONE_LOCATION_ITEM) {
101         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
102
103         if (item)
104             m_locationListView->setSelectedItem(item);
105     }
106 }
107
108 void RoutingPanel::routeToSelectedLocation()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     LocationListItem *item = dynamic_cast<LocationListItem *>
113                              (m_locationListView->selectedItem());
114
115     if (item)
116         emit routeToLocation(item->coordinates());
117 }
118
119 void RoutingPanel::setRoute(Route &route)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     m_locationListHeaderWidget->hide();
124     m_locationListView->hide();
125     m_routeWaypointListView->show();
126     m_routeWaypointListView->clearList();
127
128     QList<RouteSegment> segments = route.segments();
129     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
130
131     for (int i = 0; i < segments.size(); ++i) {
132         RouteWaypointListItem *item = new RouteWaypointListItem();
133         RouteSegment routeSegment = segments.at(i);
134         item->setRouteWaypointData(routeSegment,
135                                    geometryPoints.at(routeSegment.positionIndex()));
136
137         m_routeWaypointListView->addListItem(QString::number(i), item);
138     }
139 }