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"
12 #include "routingpanel.h"
14 RoutingPanel::RoutingPanel(QWidget *parent)
17 qDebug() << __PRETTY_FUNCTION__;
19 // --- ROUTING INSTRUCTIONS WIDGET ---
20 m_routeWaypointListView = new RouteWaypointListView(this);
21 m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
22 // m_routeWaypointListView->hide();
24 connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
25 this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
27 QVBoxLayout *routingViewLayout = new QVBoxLayout;
28 routingViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
29 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
31 // main routing layout
32 m_routingView = new QWidget();
33 m_routingView->setLayout(routingViewLayout);
34 routingViewLayout->addWidget(m_routeWaypointListView);
36 // --- SEARCH RESULTS WIDGET ---
38 QWidget *resultsHeaderWidget = new QWidget();
39 resultsHeaderWidget->setAutoFillBackground(true);
40 QPalette labelPalette = resultsHeaderWidget->palette();
41 labelPalette.setColor(QPalette::Background, Qt::black);
42 resultsHeaderWidget->setPalette(labelPalette);
43 // m_resultsHeaderWidget->hide();
45 QHBoxLayout *headerLayout = new QHBoxLayout();
46 resultsHeaderWidget->setLayout(headerLayout);
47 headerLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
48 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
50 m_resultsLabel = new QLabel(this);
51 headerLayout->addWidget(m_resultsLabel, 0, Qt::AlignCenter);
54 m_locationListView = new LocationListView(this);
55 m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
56 // m_locationListView->hide();
58 connect(m_locationListView,
59 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
61 SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
63 connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
64 this, SLOT(setRouteButtonDisabled()));
66 QVBoxLayout *resultsListViewLayout = new QVBoxLayout;
67 resultsListViewLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
68 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
69 resultsListViewLayout->addWidget(m_locationListView);
71 // main results layout
72 m_resultsView = new QWidget();
73 QVBoxLayout *resultsViewLayout = new QVBoxLayout;
74 const int CONTENTS_MARGIN_LEFT = 0;
75 resultsViewLayout->setContentsMargins(CONTENTS_MARGIN_LEFT, PANEL_MARGIN_TOP,
76 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
77 m_resultsView->setLayout(resultsViewLayout);
79 resultsViewLayout->addWidget(resultsHeaderWidget);
80 resultsViewLayout->addLayout(resultsListViewLayout);
82 // --- MAIN LAYOUT ---
83 QVBoxLayout *routingPanelLayout = new QVBoxLayout;
84 routingPanelLayout->setMargin(0);
85 routingPanelLayout->setSpacing(0);
86 setLayout(routingPanelLayout);
88 m_views = new QStackedLayout();
89 routingPanelLayout->addLayout(m_views);
90 m_views->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
91 PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
93 m_views->addWidget(m_resultsView);
94 m_views->addWidget(m_routingView);
96 // --- CONTEXT BUTTONS ---
97 m_searchLocationButton = new ImageButton(":/res/images/search.png",
98 ":/res/images/search_s.png", "", this);
99 m_searchLocationButton->setCheckable(true);
100 connect(m_searchLocationButton, SIGNAL(toggled(bool)),
101 this, SLOT(searchLocationButtonToggled(bool)));
103 m_routeButton = new ImageButton(":res/images/route_to_location.png",
104 ":res/images/route_to_location_s.png", "", this);
105 m_routeButton->setCheckable(true);
106 m_routeButton->setDisabled(true);
107 connect(m_routeButton, SIGNAL(toggled(bool)),
108 this, SLOT(routeButtonToggled(bool)));
110 m_contextButtonLayout->addWidget(m_searchLocationButton);
111 m_contextButtonLayout->addWidget(m_routeButton);
114 void RoutingPanel::clearListsSelections()
116 qDebug() << __PRETTY_FUNCTION__;
118 m_locationListView->clearItemSelection();
119 m_routeWaypointListView->clearItemSelection();
121 setRouteButtonDisabled();
124 void RoutingPanel::hideEvent(QHideEvent *event)
126 qDebug() << __PRETTY_FUNCTION__;
128 QWidget::hideEvent(event);
130 clearListsSelections();
133 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
135 qDebug() << __PRETTY_FUNCTION__;
137 // m_resultsHeaderWidget->show();
138 m_resultsLabel->setText(tr("Search results: %1").arg(locations.count()));
140 // m_routeWaypointListView->hide();
141 // m_locationListView->show();
142 m_locationListView->clearList();
144 for (int i = 0; i < locations.size(); ++i) {
145 LocationListItem *item = new LocationListItem();
146 item->setLocationData(locations.at(i));
147 m_locationListView->addListItem(QString::number(i), item);
150 const int FIRST_LOCATION_ITEM_INDEX = 0;
151 const int ONE_LOCATION_ITEM = 1;
153 if (locations.size() == ONE_LOCATION_ITEM) {
154 ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
157 m_locationListView->setSelectedItem(item);
160 m_locationListView->scrollToTop();
163 void RoutingPanel::routeButtonToggled(bool checked)
166 routeToSelectedLocation();
167 m_searchLocationButton->setChecked(false);
170 m_routeWaypointListView->clearList();
171 setRouteButtonDisabled();
175 void RoutingPanel::routeToSelectedLocation()
177 qDebug() << __PRETTY_FUNCTION__;
179 LocationListItem *item = dynamic_cast<LocationListItem *>
180 (m_locationListView->selectedItem());
183 emit routeToLocation(item->coordinates());
186 void RoutingPanel::searchLocationButtonToggled(bool checked)
190 emit requestSearchLocation();
193 // m_resultsHeaderWidget->hide();
194 // m_locationListView->hide();
198 void RoutingPanel::setRoute(Route &route)
200 qDebug() << __PRETTY_FUNCTION__;
202 // m_resultsHeaderWidget->hide();
203 // m_locationListView->hide();
204 // m_routeWaypointListView->show();
205 // m_routeWaypointListView->clearList();
207 QList<RouteSegment> segments = route.segments();
208 QList<GeoCoordinate> geometryPoints = route.geometryPoints();
210 for (int i = 0; i < segments.size(); ++i) {
211 RouteWaypointListItem *item = new RouteWaypointListItem();
212 RouteSegment routeSegment = segments.at(i);
213 item->setRouteWaypointData(routeSegment,
214 geometryPoints.at(routeSegment.positionIndex()));
216 m_routeWaypointListView->addListItem(QString::number(i), item);
219 m_routeWaypointListView->scrollToTop();
221 // route might come from FriendListPanel's route to friend button, so we have to
222 // check the routing button without emitting new routing request
224 m_routeButton->setEnabled(true);
225 m_routeButton->setChecked(true);
227 m_searchLocationButton->setChecked(false);
228 // search location button might be already false, so we have to make sure the
229 // toggle action handler is caller every time
230 searchLocationButtonToggled(false);
232 emit showPanelRequested(this);
235 void RoutingPanel::setRouteButtonDisabled()
237 qDebug() << __PRETTY_FUNCTION__;
239 m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty()
240 && !m_routeButton->isChecked());
243 void RoutingPanel::showResultsView()
245 qDebug() << __PRETTY_FUNCTION__;
247 m_views->setCurrentWidget(m_resultsView);
248 m_routeWaypointListView->clearItemSelection();
251 void RoutingPanel::showRoutingView()
253 qDebug() << __PRETTY_FUNCTION__;
255 m_views->setCurrentWidget(m_routingView);
256 m_locationListView->clearItemSelection();