Merge branch 'new_panels_with_context_buttons' of https://vcs.maemo.org/git/situare...
[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
36     m_contextButtonLayout->addWidget(searchLocationButton, 0, 0);
37
38     m_routeButton = new ImageButton(":res/images/route_to_location.png",
39                                     ":res/images/route_to_location_s.png", "", this);
40     m_routeButton->setDisabled(true);
41
42     m_locationListHeaderWidget = new QWidget();
43     m_locationListHeaderWidget->setLayout(headerLayout);
44     m_locationListHeaderWidget->setAutoFillBackground(true);
45     QPalette labelPalette = m_locationListHeaderWidget->palette();
46     labelPalette.setColor(QPalette::Background, Qt::black);
47     m_locationListHeaderWidget->setPalette(labelPalette);
48     m_locationListHeaderWidget->hide();
49
50     m_locationListLabel = new QLabel(this);
51
52     m_locationListView = new LocationListView(this);
53     m_locationListView->setItemDelegate(new ExtendedListItemDelegate(this));
54     m_locationListView->hide();
55
56     m_routeWaypointListView = new RouteWaypointListView(this);
57     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
58     m_routeWaypointListView->hide();
59
60     headerLayout->addWidget(m_locationListLabel, 0, Qt::AlignCenter);
61
62     listViewLayout->addWidget(m_locationListView);
63     listViewLayout->addWidget(m_routeWaypointListView);
64
65     routingLayout->addWidget(m_locationListHeaderWidget);
66     routingLayout->addLayout(listViewLayout);
67
68     connect(m_locationListView,
69             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
70             this,
71             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
72
73     connect(m_locationListView, SIGNAL(listItemSelectionChanged()),
74             this, SLOT(setRouteButtonDisabled()));
75
76     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
77             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
78
79     connect(m_routeButton, SIGNAL(clicked()),
80             this, SLOT(routeToSelectedLocation()));
81
82     connect(searchLocationButton, SIGNAL(clicked()),
83             this, SIGNAL(requestSearchLocation()));
84
85     // CONTEXT BUTTONS
86     m_contextButtonLayout->addWidget(m_routeButton, 1, 0);
87 }
88
89 void RoutingPanel::clearListsSelections()
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     m_locationListView->clearItemSelection();
94     m_routeWaypointListView->clearItemSelection();
95
96     setRouteButtonDisabled();
97 }
98
99 void RoutingPanel::hideEvent(QHideEvent *event)
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     QWidget::hideEvent(event);
104
105     clearListsSelections();
106 }
107
108 void RoutingPanel::populateLocationListView(const QList<Location> &locations)
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     m_locationListHeaderWidget->show();
113     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
114
115     m_routeWaypointListView->hide();
116     m_locationListView->show();
117     m_locationListView->clearList();
118
119     for (int i = 0; i < locations.size(); ++i) {
120         LocationListItem *item = new LocationListItem();
121         item->setLocationData(locations.at(i));
122         m_locationListView->addListItem(QString::number(i), item);
123     }
124
125     const int FIRST_LOCATION_ITEM_INDEX = 0;
126     const int ONE_LOCATION_ITEM = 1;
127
128     if (locations.size() == ONE_LOCATION_ITEM) {
129         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
130
131         if (item)
132             m_locationListView->setSelectedItem(item);
133     }
134
135     m_locationListView->scrollToTop();
136 }
137
138 void RoutingPanel::routeToSelectedLocation()
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     LocationListItem *item = dynamic_cast<LocationListItem *>
143                              (m_locationListView->selectedItem());
144
145     if (item)
146         emit routeToLocation(item->coordinates());
147 }
148
149 void RoutingPanel::setRoute(Route &route)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
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 }