1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qdeclarativegeoroutesegment_p.h"
38 
39 #include <QtQml/QQmlEngine>
40 #include <QtQml/private/qqmlengine_p.h>
41 #include <QtQml/private/qv4scopedvalue_p.h>
42 #include <QtQml/private/qv4arrayobject_p.h>
43 
44 QT_BEGIN_NAMESPACE
45 
46 /*!
47     \qmltype RouteSegment
48     \instantiates QDeclarativeGeoRouteSegment
49     \inqmlmodule QtLocation
50     \ingroup qml-QtLocation5-routing
51     \since QtLocation 5.5
52 
53     \brief The RouteSegment type represents a segment of a Route.
54 
55     A RouteSegment instance has information about the physical layout
56     of the route segment, the length of the route and estimated time required
57     to traverse the route segment and optional RouteManeuvers associated with
58     the end of the route segment.
59 
60     RouteSegment instances can be thought of as edges on a routing
61     graph, with RouteManeuver instances as optional labels attached to the
62     vertices of the graph.
63 
64     The primary means of acquiring Route objects is via Routes via \l RouteModel.
65 
66     \section1 Example
67 
68     The following QML snippet demonstrates how to print information about a
69     route segment:
70 
71     \snippet declarative/routing.qml QtQuick import
72     \snippet declarative/maps.qml QtLocation import
73     \codeline
74     \snippet declarative/routing.qml RouteSegment
75 */
76 
QDeclarativeGeoRouteSegment(QObject * parent)77 QDeclarativeGeoRouteSegment::QDeclarativeGeoRouteSegment(QObject *parent)
78     : QObject(parent)
79 {
80     maneuver_ = new QDeclarativeGeoManeuver(this);
81 }
82 
QDeclarativeGeoRouteSegment(const QGeoRouteSegment & segment,QObject * parent)83 QDeclarativeGeoRouteSegment::QDeclarativeGeoRouteSegment(const QGeoRouteSegment &segment,
84                                                          QObject *parent)
85     : QObject(parent),
86       segment_(segment)
87 {
88     maneuver_ = new QDeclarativeGeoManeuver(segment_.maneuver(), this);
89 }
90 
~QDeclarativeGeoRouteSegment()91 QDeclarativeGeoRouteSegment::~QDeclarativeGeoRouteSegment() {}
92 
93 /*!
94     \qmlproperty int QtLocation::RouteSegment::travelTime
95 
96     Read-only property which holds the estimated amount of time it will take to
97     traverse this segment, in seconds.
98 
99 */
100 
travelTime() const101 int QDeclarativeGeoRouteSegment::travelTime() const
102 {
103     return segment_.travelTime();
104 }
105 
106 /*!
107     \qmlproperty real QtLocation::RouteSegment::distance
108 
109     Read-only property which holds the distance covered by this segment of the route, in meters.
110 
111 */
112 
distance() const113 qreal QDeclarativeGeoRouteSegment::distance() const
114 {
115     return segment_.distance();
116 }
117 
118 /*!
119     \qmlproperty RouteManeuver QtLocation::RouteSegment::maneuver
120 
121     Read-only property which holds the maneuver for this route segment.
122 
123     Will return invalid maneuver if no information has been attached to the endpoint
124     of this route segment.
125 */
126 
maneuver() const127 QDeclarativeGeoManeuver *QDeclarativeGeoRouteSegment::maneuver() const
128 {
129     return maneuver_;
130 }
131 
132 /*!
133     \qmlproperty list<coordinate> QtLocation::RouteSegment::path
134 
135     Read-only property which holds the geographical coordinates of this segment.
136     Coordinates are listed in the order in which they would be traversed by someone
137     traveling along this segment of the route.
138 
139     To access individual segments you can use standard list accessors: 'path.length'
140     indicates the number of objects and 'path[index starting from zero]' gives
141     the actual object.
142 
143     \sa QtPositioning::coordinate
144 */
145 
path() const146 QJSValue QDeclarativeGeoRouteSegment::path() const
147 {
148     QQmlContext *context = QQmlEngine::contextForObject(parent());
149     QQmlEngine *engine = context->engine();
150     QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine);
151 
152     QV4::Scope scope(v4);
153     QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(segment_.path().length()));
154     for (int i = 0; i < segment_.path().length(); ++i) {
155         const QGeoCoordinate &c = segment_.path().at(i);
156 
157         QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(c)));
158         pathArray->put(i, cv);
159     }
160 
161     return QJSValue(v4, pathArray.asReturnedValue());
162 }
163 
164 QT_END_NAMESPACE
165