Renamed ListView::m_previousItem and RoutingPanel::anyPanelClosed().
[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(listItemSelectionChanged()),
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::clearListsSelections()
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     clearListsSelections();
100 }
101
102 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     m_routeButton->show();
107
108     m_locationListHeaderWidget->show();
109     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
110
111     m_routeWaypointListView->hide();
112     m_locationListView->show();
113     m_locationListView->clearList();
114
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);
119     }
120
121     const int FIRST_LOCATION_ITEM_INDEX = 0;
122     const int ONE_LOCATION_ITEM = 1;
123
124     if (locations.size() == ONE_LOCATION_ITEM) {
125         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
126
127         if (item)
128             m_locationListView->setSelectedItem(item);
129     }
130
131     m_locationListView->scrollToTop();
132 }
133
134 void RoutingPanel::routeToSelectedLocation()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     LocationListItem *item = dynamic_cast<LocationListItem *>
139                              (m_locationListView->selectedItem());
140
141     if (item)
142         emit routeToLocation(item->coordinates());
143 }
144
145 void RoutingPanel::setRoute(Route &route)
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     m_routeButton->hide();
150
151     m_locationListHeaderWidget->hide();
152     m_locationListView->hide();
153     m_routeWaypointListView->show();
154     m_routeWaypointListView->clearList();
155
156     QList<RouteSegment> segments = route.segments();
157     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
158
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()));
164
165         m_routeWaypointListView->addListItem(QString::number(i), item);
166     }
167
168     m_routeWaypointListView->scrollToTop();
169
170     emit showPanelRequested(this);
171 }
172
173 void RoutingPanel::setRouteButtonDisabled()
174 {
175     qDebug() << __PRETTY_FUNCTION__;
176
177     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
178 }