Added return values to ListItem::itemClicked and added routing panel
[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(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
25                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
26
27     QVBoxLayout *listViewLayout = new QVBoxLayout;
28     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
29                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
30
31     m_searchLocationButton = new QPushButton(tr("Search location"));
32
33     m_routeButton = new QPushButton(tr("Route to location"));
34     m_routeButton->setDisabled(true);
35     m_routeButton->hide();
36
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();
44
45     m_locationListLabel = new QLabel(this);
46
47     m_locationListView = new LocationListView(this);
48     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
49     m_locationListView->hide();
50
51     m_routeWaypointListView = new RouteWaypointListView(this);
52     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
53     m_routeWaypointListView->hide();
54
55     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
56
57     listViewLayout->addWidget(m_locationListView);
58     listViewLayout->addWidget(m_routeWaypointListView);
59
60     routingLayout->addWidget(m_searchLocationButton);
61     routingLayout->addWidget(m_routeButton);
62     routingLayout->addWidget(m_locationListHeaderWidget);
63     routingLayout->addLayout(listViewLayout);
64
65     connect(m_locationListView,
66             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
67             this,
68             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
69
70     connect(m_locationListView, SIGNAL(clicked(QModelIndex)),
71             this, SLOT(setRouteButtonDisabled()));
72
73     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
74             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
75
76     connect(m_routeButton, SIGNAL(clicked()),
77             this, SLOT(routeToSelectedLocation()));
78
79     connect(m_searchLocationButton, SIGNAL(clicked()),
80             this, SIGNAL(requestSearchLocation()));
81 }
82
83 void RoutingPanel::anyPanelClosed()
84 {
85     qDebug() << __PRETTY_FUNCTION__;
86
87     m_locationListView->clearItemSelection();
88     m_routeWaypointListView->clearItemSelection();
89
90     setRouteButtonDisabled();
91 }
92
93 void RoutingPanel::hideEvent(QHideEvent *event)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     QWidget::hideEvent(event);
98
99     m_locationListView->clearItemSelection();
100     m_routeWaypointListView->clearItemSelection();
101     setRouteButtonDisabled();
102 }
103
104 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     m_routeButton->show();
109
110     m_locationListHeaderWidget->show();
111     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
112
113     m_routeWaypointListView->hide();
114     m_locationListView->show();
115     m_locationListView->clearList();
116
117     for (int i = 0; i < locations.size(); ++i) {
118         LocationListItem *item = new LocationListItem();
119         item->setLocationData(locations.at(i));
120         m_locationListView->addListItem(QString::number(i), item);
121     }
122
123     const int FIRST_LOCATION_ITEM_INDEX = 0;
124     const int ONE_LOCATION_ITEM = 1;
125
126     if (locations.size() == ONE_LOCATION_ITEM) {
127         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
128
129         if (item)
130             m_locationListView->setSelectedItem(item);
131     }
132
133     m_locationListView->scrollToTop();
134 }
135
136 void RoutingPanel::routeToSelectedLocation()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     LocationListItem *item = dynamic_cast<LocationListItem *>
141                              (m_locationListView->selectedItem());
142
143     if (item)
144         emit routeToLocation(item->coordinates());
145 }
146
147 void RoutingPanel::setRoute(Route &route)
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     m_routeButton->hide();
152
153     m_locationListHeaderWidget->hide();
154     m_locationListView->hide();
155     m_routeWaypointListView->show();
156     m_routeWaypointListView->clearList();
157
158     QList<RouteSegment> segments = route.segments();
159     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
160
161     for (int i = 0; i < segments.size(); ++i) {
162         RouteWaypointListItem *item = new RouteWaypointListItem();
163         RouteSegment routeSegment = segments.at(i);
164         item->setRouteWaypointData(routeSegment,
165                                    geometryPoints.at(routeSegment.positionIndex()));
166
167         m_routeWaypointListView->addListItem(QString::number(i), item);
168     }
169
170     m_routeWaypointListView->scrollToTop();
171
172     emit showPanelRequested(this);
173 }
174
175 void RoutingPanel::setRouteButtonDisabled()
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
180 }