Review & fixes
[situare] / src / ui / routingpanel.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Jussi Laitinen - jussi.laitinen@ixonos.com
6         Sami Rämö - sami.ramo@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21 */
22
23 #include "coordinates/geocoordinate.h"
24 #include "extendedlistitemdelegate.h"
25 #include "imagebutton.h"
26 #include "panelcommon.h"
27 #include "routewaypointlistitem.h"
28 #include "routewaypointlistview.h"
29 #include "routing/location.h"
30 #include "routing/route.h"
31
32 #include "routingpanel.h"
33
34 RoutingPanel::RoutingPanel(QWidget *parent)
35     : PanelBase(parent)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     // --- LAYOUT & ROUTING INSTRUCTIONS VIEW ---
40     m_routeWaypointListView = new RouteWaypointListView(this);
41     m_routeWaypointListView->setItemDelegate(new ExtendedListItemDelegate(this));
42
43     connect(m_routeWaypointListView, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
44             this, SIGNAL(routeWaypointItemClicked(GeoCoordinate)));
45
46     QVBoxLayout *panelLayout = new QVBoxLayout;
47     panelLayout->setContentsMargins(PANEL_MARGIN_LEFT, PANEL_MARGIN_TOP,
48                                     PANEL_MARGIN_RIGHT, PANEL_MARGIN_BOTTOM);
49
50     panelLayout->addWidget(m_routeWaypointListView);
51     setLayout(panelLayout);
52
53     // --- CONTEXT BUTTONS ---
54     m_clearRouteButton = new ImageButton(":/res/images/gps_position.png",
55                                          "", "", this);
56     connect(m_clearRouteButton, SIGNAL(clicked()),
57             this, SLOT(clearRouteButtonClicked()));
58     m_clearRouteButton->setDisabled(true);
59
60     ImageButton *routeToCursorButton = new ImageButton(":res/images/sight.png",
61                                                        "", "", this);
62     connect(routeToCursorButton, SIGNAL(clicked()),
63             this, SIGNAL(routeToCursor()));
64
65     m_contextButtonLayout->addWidget(m_clearRouteButton);
66     m_contextButtonLayout->addWidget(routeToCursorButton);
67 }
68
69 void RoutingPanel::clearListsSelections()
70 {
71     qDebug() << __PRETTY_FUNCTION__;
72
73     m_routeWaypointListView->clearItemSelection();
74 }
75
76 void RoutingPanel::clearRouteButtonClicked()
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     m_clearRouteButton->setDisabled(true);
81     emit clearRoute();
82 }
83
84 void RoutingPanel::hideEvent(QHideEvent *event)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     QWidget::hideEvent(event);
89
90     clearListsSelections();
91 }
92
93 void RoutingPanel::setRoute(Route &route)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     m_routeWaypointListView->clearList();
98
99     QList<RouteSegment> segments = route.segments();
100     QList<GeoCoordinate> geometryPoints = route.geometryPoints();
101
102     for (int i = 0; i < segments.size(); ++i) {
103         RouteWaypointListItem *item = new RouteWaypointListItem();
104         RouteSegment routeSegment = segments.at(i);
105         item->setRouteWaypointData(routeSegment,
106                                    geometryPoints.at(routeSegment.positionIndex()));
107
108         m_routeWaypointListView->addListItem(QString::number(i), item);
109     }
110
111     m_routeWaypointListView->scrollToTop();
112
113     m_clearRouteButton->setDisabled(false);
114
115     emit openPanelRequested(this);
116 }