Modifying RoutingPanel buttons functionality
[situare] / src / ui / routingpanel.cpp
1 #include "coordinates/geocoordinate.h"
2 #include "extendedlistitemdelegate.h"
3 #include "locationlistitem.h"
4 #include "locationlistview.h"
5 #include "imagebutton.h"
6 #include "panelcommon.h"
7 #include "routing/location.h"
8 #include "routing/route.h"
9 #include "routewaypointlistitem.h"
10 #include "routewaypointlistview.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, PANEL_MARGIN_TOP,
26                                      PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
27
28     QVBoxLayout *listViewLayout = new QVBoxLayout;
29     listViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
30                                        PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
31
32     ImageButton *searchLocationButton = new ImageButton(":/res/images/search.png",
33                                                         ":/res/images/search_s.png",
34                                                         "", this);
35     searchLocationButton->setCheckable(true);
36     m_contextButtonLayout->addWidget(searchLocationButton);
37
38     m_routeButton = new ImageButton(":res/images/route_to_location.png",
39                                     ":res/images/route_to_location_s.png", "", this);
40     m_routeButton->setCheckable(true);
41     m_routeButton->setDisabled(true);
42
43     m_locationListHeaderWidget = new QWidget();
44     m_locationListHeaderWidget->setLayout(headerLayout);
45     m_locationListHeaderWidget->setAutoFillBackground(true);
46     QPalette labelPalette = m_locationListHeaderWidget->palette();
47     labelPalette.setColor(QPalette::Background, Qt::black);
48     m_locationListHeaderWidget->setPalette(labelPalette);
49     m_locationListHeaderWidget->hide();
50
51     m_locationListLabel = new QLabel(this);
52
53     m_locationListView = new LocationListView(this);
54     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
55     m_locationListView->hide();
56
57     m_routeWaypointListView = new RouteWaypointListView(this);
58     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
59     m_routeWaypointListView->hide();
60
61     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
62
63     listViewLayout->addWidget(m_locationListView);
64     listViewLayout->addWidget(m_routeWaypointListView);
65
66     routingLayout->addWidget(m_locationListHeaderWidget);
67     routingLayout->addLayout(listViewLayout);
68
69     connect(m_locationListView,
70             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
71             this,
72             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
73
74     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
75             this, SLOT(setRouteButtonDisabled()));
76
77     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
78             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
79
80     connect(m_routeButton, SIGNAL(toggled(bool)),
81             this, SLOT(routeButtonToggled(bool)));
82
83     connect(searchLocationButton, SIGNAL(toggled(bool)),
84             this, SLOT(searchLocationButtonToggled(bool)));
85
86     // CONTEXT BUTTONS
87     m_contextButtonLayout->addWidget(m_routeButton);
88 }
89
90 void RoutingPanel::clearListsSelections()
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     m_locationListView->clearItemSelection();
95     m_routeWaypointListView->clearItemSelection();
96
97     setRouteButtonDisabled();
98 }
99
100 void RoutingPanel::hideEvent(QHideEvent *event)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     QWidget::hideEvent(event);
105
106     clearListsSelections();
107 }
108
109 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     m_locationListHeaderWidget->show();
114     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
115
116     m_routeWaypointListView->hide();
117     m_locationListView->show();
118     m_locationListView->clearList();
119
120     for (int i = 0; i < locations.size(); ++i) {
121         LocationListItem *item = new LocationListItem();
122         item->setLocationData(locations.at(i));
123         m_locationListView->addListItem(QString::number(i), item);
124     }
125
126     const int FIRST_LOCATION_ITEM_INDEX = 0;
127     const int ONE_LOCATION_ITEM = 1;
128
129     if (locations.size() == ONE_LOCATION_ITEM) {
130         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
131
132         if (item)
133             m_locationListView->setSelectedItem(item);
134     }
135
136     m_locationListView->scrollToTop();
137 }
138
139 void RoutingPanel::routeButtonToggled(bool checked)
140 {
141     if (checked) {
142         routeToSelectedLocation();
143     } else {
144         emit clearRoute();
145         /// @todo clear route panel
146     }
147 }
148
149 void RoutingPanel::routeToSelectedLocation()
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     LocationListItem *item = dynamic_cast<LocationListItem *>
154                              (m_locationListView->selectedItem());
155
156     if (item)
157         emit routeToLocation(item->coordinates());
158 }
159
160 void RoutingPanel::searchLocationButtonToggled(bool checked)
161 {
162     if (checked) {
163         emit requestSearchLocation();
164     } else {
165         m_locationListHeaderWidget->hide();
166         m_locationListView->hide();
167     }
168 }
169
170 void RoutingPanel::setRoute(Route &route)
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     m_locationListHeaderWidget->hide();
175     m_locationListView->hide();
176     m_routeWaypointListView->show();
177     m_routeWaypointListView->clearList();
178
179     QList<RouteSegment> segments = route.segments();
180     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
181
182     for (int i = 0; i < segments.size(); ++i) {
183         RouteWaypointListItem *item = new RouteWaypointListItem();
184         RouteSegment routeSegment = segments.at(i);
185         item->setRouteWaypointData(routeSegment,
186                                    geometryPoints.at(routeSegment.positionIndex()));
187
188         m_routeWaypointListView->addListItem(QString::number(i), item);
189     }
190
191     m_routeWaypointListView->scrollToTop();
192
193     emit showPanelRequested(this);
194 }
195
196 void RoutingPanel::setRouteButtonDisabled()
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
201 }