Changed context buttons images
[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(0, ":/res/images/search.png",
33                                                              ":/res/images/search_s.png");
34
35     m_contextButtonList.append(searchLocationButton);
36
37     m_routeButton = new ImageButton(this, ":res/images/route_to_location.png",
38                                     ":res/images/route_to_location_s.png");
39     m_routeButton->setDisabled(true);
40     m_routeButton->hide();
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_routeButton);
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(clicked()),
81             this, SLOT(routeToSelectedLocation()));
82
83     connect(searchLocationButton, SIGNAL(clicked()),
84             this, SIGNAL(requestSearchLocation()));
85
86     // CONTEXT BUTTONS
87     m_contextButtonList.append(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_routeButton->show();
114
115     m_locationListHeaderWidget->show();
116     m_locationListLabel->setText(tr("Search results: %1").arg(locations.count()));
117
118     m_routeWaypointListView->hide();
119     m_locationListView->show();
120     m_locationListView->clearList();
121
122     for (int i = 0; i < locations.size(); ++i) {
123         LocationListItem *item = new LocationListItem();
124         item->setLocationData(locations.at(i));
125         m_locationListView->addListItem(QString::number(i), item);
126     }
127
128     const int FIRST_LOCATION_ITEM_INDEX = 0;
129     const int ONE_LOCATION_ITEM = 1;
130
131     if (locations.size() == ONE_LOCATION_ITEM) {
132         ListItem *item = m_locationListView->listItemAt(FIRST_LOCATION_ITEM_INDEX);
133
134         if (item)
135             m_locationListView->setSelectedItem(item);
136     }
137
138     m_locationListView->scrollToTop();
139 }
140
141 void RoutingPanel::routeToSelectedLocation()
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     LocationListItem *item = dynamic_cast<LocationListItem *>
146                              (m_locationListView->selectedItem());
147
148     if (item)
149         emit routeToLocation(item->coordinates());
150 }
151
152 void RoutingPanel::setRoute(Route &route)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     m_routeButton->hide();
157
158     m_locationListHeaderWidget->hide();
159     m_locationListView->hide();
160     m_routeWaypointListView->show();
161     m_routeWaypointListView->clearList();
162
163     QList<RouteSegment> segments = route.segments();
164     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
165
166     for (int i = 0; i < segments.size(); ++i) {
167         RouteWaypointListItem *item = new RouteWaypointListItem();
168         RouteSegment routeSegment = segments.at(i);
169         item->setRouteWaypointData(routeSegment,
170                                    geometryPoints.at(routeSegment.positionIndex()));
171
172         m_routeWaypointListView->addListItem(QString::number(i), item);
173     }
174
175     m_routeWaypointListView->scrollToTop();
176
177     emit showPanelRequested(this);
178 }
179
180 void RoutingPanel::setRouteButtonDisabled()
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     m_routeButton->setDisabled(m_locationListView->selectedItems().isEmpty());
185 }