Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgeoroute.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qgeoroute.h"
6#include "qgeoroute_p.h"
7
10
11#include <QDateTime>
12#include <QVariantMap>
13
15
16QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QGeoRoutePrivate)
17
18/*!
19 \class QGeoRoute
20 \inmodule QtLocation
21 \ingroup QtLocation-routing
22 \since 5.6
23
24 \brief The QGeoRoute class represents a route between two points.
25
26 A QGeoRoute object contains high level information about a route, such
27 as the length the route, the estimated travel time for the route,
28 and enough information to render a basic image of the route on a map.
29
30 The QGeoRoute object also contains a list of QGeoRouteSegment objecs which
31 describe subsections of the route in greater detail.
32
33 Routing information is normally requested using
34 QGeoRoutingManager::calculateRoute(), which returns a QGeoRouteReply
35 instance. If the operation is completed successfully the routing
36 information can be accessed with QGeoRouteReply::routes()
37
38 \sa QGeoRoutingManager
39*/
40
41/*!
42 \qmlvaluetype route
43 \inqmlmodule QtLocation
44 \ingroup qml-QtLocation5-routing
45 \since QtLocation 5.5
46
47 \brief The route type represents one geographical route.
48
49 A route type contains high level information about a route, such
50 as the length the route, the estimated travel time for the route,
51 and enough information to render a basic image of the route on a map.
52
53 The QGeoRoute object also contains a list of \l routeSegment objects which
54 describe subsections of the route in greater detail.
55
56 The primary means of acquiring route objects is \l RouteModel.
57
58 \section1 Example
59
60 This example shows how to display a route's maneuvers in a ListView:
61
62 \snippet declarative/routing.qml QtQuick import
63 \snippet declarative/maps.qml QtLocation import
64 \codeline
65 \snippet declarative/routing.qml Route Maneuver List1
66 \snippet declarative/routing.qml Route Maneuver List2
67 \snippet declarative/routing.qml Route Maneuver List3
68
69*/
70
71/*!
72 Constructs a route object.
73*/
74QGeoRoute::QGeoRoute()
75 : d_ptr(new QGeoRoutePrivate())
76{}
77
78/*!
79 Returns the private implementation.
80*/
81QExplicitlySharedDataPointer<QGeoRoutePrivate> &QGeoRoute::d()
82{
83 return d_ptr;
84}
85
86const QExplicitlySharedDataPointer<QGeoRoutePrivate> &QGeoRoute::const_d() const
87{
88 return d_ptr;
89}
90
91/*!
92 Constructs a route object from the contents of \a other.
93*/
94QGeoRoute::QGeoRoute(const QGeoRoute &other) noexcept = default;
95
96/*!
97 Destroys this route object.
98*/
99QGeoRoute::~QGeoRoute() = default;
100
101/*!
102 Assigns the contents of \a other to this route and returns a reference to
103 this route.
104*/
105QGeoRoute &QGeoRoute::operator=(const QGeoRoute & other) noexcept
106{
107 if (this == &other)
108 return *this;
109
110 d_ptr = other.d_ptr;
111 return *this;
112}
113
114/*!
115 \fn bool QGeoRoute::operator==(const QGeoRoute &lhs, const QGeoRoute &rhs) noexcept
116
117 Returns whether the routes \a lhs and \a rhs are equal.
118*/
119
120/*!
121 \fn bool QGeoRoute::operator!=(const QGeoRoute &lhs, const QGeoRoute &rhs) noexcept
122
123 Returns whether the routes \a lhs and \a rhs are not equal.
124*/
125
126bool QGeoRoute::isEqual(const QGeoRoute &other) const noexcept
127{
128 return ( (d_ptr.constData() == other.d_ptr.constData())
129 || (*d_ptr) == (*other.d_ptr));
130}
131
132
133/*!
134 \property QGeoRoute::routeId
135 \brief the identifier of this route
136
137 Service providers which support the updating of routes commonly assign
138 identifiers to routes. If this route came from such a service provider,
139 then changing the identifier will probably cause route updates to stop
140 working.
141*/
142void QGeoRoute::setRouteId(const QString &id)
143{
144 d_ptr->setId(id);
145}
146
147QString QGeoRoute::routeId() const
148{
149 return d_ptr->id();
150}
151
152/*!
153 \internal
154 \property QGeoRoute::request
155 \brief the route request which describes the criteria used in the
156 calculation of this route
157*/
158void QGeoRoute::setRequest(const QGeoRouteRequest &request)
159{
160 d_ptr->setRequest(request);
161}
162
163QGeoRouteRequest QGeoRoute::request() const
164{
165 return d_ptr->request();
166}
167
168/*!
169 \qmlproperty geoRectangle QtLocation::route::bounds
170
171 Read-only property which holds a bounding box which encompasses the entire route.
172*/
173
174/*!
175 \property QGeoRoute::bounds
176 \brief the bounding box which encompasses the entire route
177*/
178void QGeoRoute::setBounds(const QGeoRectangle &bounds)
179{
180 d_ptr->setBounds(bounds);
181}
182
183QGeoRectangle QGeoRoute::bounds() const
184{
185 return d_ptr->bounds();
186}
187
188/*!
189 Sets the first route segment in the route to \a routeSegment.
190*/
191void QGeoRoute::setFirstRouteSegment(const QGeoRouteSegment &routeSegment)
192{
193 d_ptr->setFirstSegment(routeSegment);
194}
195
196/*!
197 Returns the first route segment in the route.
198
199 Will return an invalid route segment if there are no route segments
200 associated with the route.
201
202 The remaining route segments can be accessed sequentially with
203 QGeoRouteSegment::nextRouteSegment.
204*/
205QGeoRouteSegment QGeoRoute::firstRouteSegment() const
206{
207 return d_ptr->firstSegment();
208}
209
210/*!
211 \qmlmethod int QtLocation::route::segmentsCount()
212
213 Returns the number of segments in the route
214
215 \sa routeSegment
216
217 \since 5.11
218*/
219
220/*!
221 \property QGeoRoute::segmentsCount
222 \brief the number of segments in the route
223*/
224qsizetype QGeoRoute::segmentsCount() const
225{
226 return d_ptr->segmentsCount();
227}
228
229/*!
230 \qmlproperty list<routeSegment> QtLocation::route::segments
231
232 Read-only property which holds the list of \l routeSegment objects of this route.
233
234 To access individual segments you can use standard list accessors: 'segments.length'
235 indicates the number of objects and 'segments[index starting from zero]' gives
236 the actual objects.
237
238 \sa routeSegment
239*/
240
241/*!
242 \property QGeoRoute::segments
243 \brief the list of QGeoRouteSegment objects of this route
244*/
245QList<QGeoRouteSegment> QGeoRoute::segments() const
246{
247 return d_ptr->segments();
248}
249
250/*!
251 \qmlproperty int QtLocation::route::travelTime
252
253 Read-only property which holds the estimated amount of time it will take to
254 traverse this route, in seconds.
255*/
256
257/*!
258 \property QGeoRoute::travelTime
259 \brief the estimated amount of time it will take to traverse this route,
260 in seconds
261*/
262void QGeoRoute::setTravelTime(int secs)
263{
264 d_ptr->setTravelTime(secs);
265}
266
267int QGeoRoute::travelTime() const
268{
269 return d_ptr->travelTime();
270}
271
272/*!
273 \qmlproperty real QtLocation::route::distance
274
275 Read-only property which holds distance covered by this route, in meters.
276*/
277
278/*!
279 \property QGeoRoute::distance
280 \brief the distance covered by this route, in meters
281*/
282void QGeoRoute::setDistance(qreal distance)
283{
284 d_ptr->setDistance(distance);
285}
286
287qreal QGeoRoute::distance() const
288{
289 return d_ptr->distance();
290}
291
292/*!
293 Sets the travel mode for this route to \a mode.
294
295 This should be one of the travel modes returned by request().travelModes().
296*/
297void QGeoRoute::setTravelMode(QGeoRouteRequest::TravelMode mode)
298{
299 d_ptr->setTravelMode(mode);
300}
301
302/*!
303 Returns the travel mode for the this route.
304
305 This should be one of the travel modes returned by request().travelModes().
306*/
307QGeoRouteRequest::TravelMode QGeoRoute::travelMode() const
308{
309 return d_ptr->travelMode();
310}
311
312/*!
313 \qmlproperty list<coordinate> QtLocation::route::path
314
315 Read-only property which holds the geographical coordinates of this route.
316 Coordinates are listed in the order in which they would be traversed by someone
317 traveling along this segment of the route.
318
319 To access individual segments you can use standard list accessors: 'path.length'
320 indicates the number of objects and 'path[index starting from zero]' gives
321 the actual object.
322
323 \sa QtPositioning::coordinate
324*/
325
326/*!
327 \property QGeoRoute::path
328 \brief the geometric shape of the route
329
330 The coordinates should be listed in the order in which they
331 would be traversed by someone traveling along this segment of the route.
332*/
333void QGeoRoute::setPath(const QList<QGeoCoordinate> &path)
334{
335 d_ptr->setPath(path);
336}
337
338
339QList<QGeoCoordinate> QGeoRoute::path() const
340{
341 return d_ptr->path();
342}
343
344/*!
345 \qmlproperty list<route> QtLocation::route::legs
346
347 Returns the route legs associated with this route.
348 Route legs are the sub-routes between each two adjacent waypoints.
349 The result may be empty, if this level of detail is not supported by the
350 backend.
351
352 \since QtLocation 5.12
353*/
354
355/*!
356 \property QGeoRoute::routeLegs
357 \brief the route \a legs for a multi-waypoint route
358
359 \since 5.12
360*/
361void QGeoRoute::setRouteLegs(const QList<QGeoRoute> &legs)
362{
363 d_ptr->setRouteLegs(legs);
364}
365
366QList<QGeoRoute> QGeoRoute::routeLegs() const
367{
368 return d_ptr->routeLegs();
369}
370
371
372/*!
373 \qmlproperty Object route::extendedAttributes
374
375 This property holds the extended attributes of the route and is a map.
376 These attributes are plugin specific, and can be empty.
377
378 Consult the \l {Qt Location#Plugin References and Parameters}{plugin documentation}
379 for what attributes are supported and how they should be used.
380
381 Note, due to limitations of the QQmlPropertyMap, it is not possible
382 to declaratively specify the attributes in QML, assignment of attributes keys
383 and values can only be accomplished by JavaScript.
384
385 \since QtLocation 5.13
386*/
387
388/*!
389 \property QGeoRoute::extendedAttributes
390 \brief the extended attributes associated with this route
391
392 \since 5.13
393*/
394void QGeoRoute::setExtendedAttributes(const QVariantMap &extendedAttributes)
395{
396 d_ptr->setExtendedAttributes(extendedAttributes);
397}
398
399QVariantMap QGeoRoute::extendedAttributes() const
400{
401 return d_ptr->extendedAttributes();
402}
403
404/*!
405 \qmlproperty int QtLocation::route::legIndex
406
407 Read-only property which holds the index of the leg within the containing route's
408 list of QtLocation::route::legs. The index is -1 if this route is not a leg within
409 an overall route.
410
411 \sa overallRoute
412*/
413
414/*!
415 \property QGeoRoute::legIndex
416 \brief the leg index of this route
417
418 The index of the leg inside the containing QGeoRoute::routeLegs list
419 can be used to find the next legs.
420*/
421void QGeoRoute::setLegIndex(int idx)
422{
423 d()->setLegIndex(idx);
424}
425
426int QGeoRoute::legIndex() const
427{
428 return const_d()->legIndex();
429}
430
431/*!
432 \qmlproperty Route QtLocation::route::overallRoute
433
434 Read-only property which holds the route that contains this leg.
435*/
436
437/*!
438 \property QGeoRoute::overallRoute
439 \brief the route that contains this route leg
440
441 This this route is not a leg within an overall route, then this property
442 holds an empty route.
443*/
444void QGeoRoute::setOverallRoute(const QGeoRoute &route)
445{
446 d()->setContainingRoute(route);
447}
448
449QGeoRoute QGeoRoute::overallRoute() const
450{
451 return const_d()->containingRoute();
452}
453
454/*******************************************************************************
455*******************************************************************************/
456
457bool QGeoRoutePrivate::operator ==(const QGeoRoutePrivate &other) const
458{
459 return equals(other);
460}
461
462bool QGeoRoutePrivate::equals(const QGeoRoutePrivate &other) const
463{
464 // here both routes are of type QGeoRoutePrivate
465 QGeoRouteSegment s1 = firstSegment();
466 QGeoRouteSegment s2 = other.firstSegment();
467
468 while (true) {
469 if (s1.isValid() != s2.isValid())
470 return false;
471 if (!s1.isValid())
472 break;
473 if (s1 != s2)
474 return false;
475 s1 = s1.nextRouteSegment();
476 s2 = s2.nextRouteSegment();
477 }
478
479 return id() == other.id()
480 && request() == other.request()
481 && bounds() == other.bounds()
482 && travelTime() == other.travelTime()
483 && distance() == other.distance()
484 && travelMode() == other.travelMode()
485 && path() == other.path()
486 && routeLegs() == other.routeLegs()
487 && extendedAttributes() == other.extendedAttributes();
488}
489
490void QGeoRoutePrivate::setId(const QString &id)
491{
492 m_id = id;
493}
494
495QString QGeoRoutePrivate::id() const
496{
497 return m_id;
498}
499
500void QGeoRoutePrivate::setRequest(const QGeoRouteRequest &request)
501{
502 m_request = request;
503}
504
505QGeoRouteRequest QGeoRoutePrivate::request() const
506{
507 return m_request;
508}
509
510void QGeoRoutePrivate::setBounds(const QGeoRectangle &bounds)
511{
512 m_bounds = bounds;
513}
514
515QGeoRectangle QGeoRoutePrivate::bounds() const
516{
517 return m_bounds;
518}
519
520void QGeoRoutePrivate::setTravelTime(int travelTime)
521{
522 m_travelTime = travelTime;
523}
524
525int QGeoRoutePrivate::travelTime() const
526{
527 return m_travelTime;
528}
529
530void QGeoRoutePrivate::setDistance(qreal distance)
531{
532 m_distance = distance;
533}
534
535qreal QGeoRoutePrivate::distance() const
536{
537 return m_distance;
538}
539
540void QGeoRoutePrivate::setTravelMode(QGeoRouteRequest::TravelMode mode)
541{
542 m_travelMode = mode;
543}
544
545QGeoRouteRequest::TravelMode QGeoRoutePrivate::travelMode() const
546{
547 return m_travelMode;
548}
549
550void QGeoRoutePrivate::setPath(const QList<QGeoCoordinate> &path)
551{
552 m_path = path;
553}
554
555QList<QGeoCoordinate> QGeoRoutePrivate::path() const
556{
557 return m_path;
558}
559
560void QGeoRoutePrivate::setFirstSegment(const QGeoRouteSegment &firstSegment)
561{
562 m_firstSegment = firstSegment;
563 m_numSegments = -1;
564}
565
566QGeoRouteSegment QGeoRoutePrivate::firstSegment() const
567{
568 return m_firstSegment;
569}
570
571int QGeoRoutePrivate::segmentsCount() const
572{
573 if (m_numSegments >= 0)
574 return m_numSegments;
575
576 int count = 0;
577 forEachSegment([&count](const QGeoRouteSegment &){
578 ++count;
579 });
580 m_numSegments = count;
581 return count;
582}
583
584QList<QGeoRouteSegment> QGeoRoutePrivate::segments() const
585{
586 QList<QGeoRouteSegment> segments;
587 forEachSegment([&segments](const QGeoRouteSegment &segment){
588 segments.append(segment);
589 });
590 return segments;
591}
592
593void QGeoRoutePrivate::setRouteLegs(const QList<QGeoRoute> &legs)
594{
595 m_legs = legs;
596}
597
598QList<QGeoRoute> QGeoRoutePrivate::routeLegs() const
599{
600 return m_legs;
601}
602
603void QGeoRoutePrivate::setExtendedAttributes(const QVariantMap &extendedAttributes)
604{
605 m_extendedAttributes = extendedAttributes;
606}
607
608QVariantMap QGeoRoutePrivate::extendedAttributes() const
609{
610 return m_extendedAttributes;
611}
612
613void QGeoRoutePrivate::setLegIndex(int idx)
614{
615 if (idx >= 0)
616 m_legIndex = idx;
617}
618
619int QGeoRoutePrivate::legIndex() const
620{
621 return m_legIndex;
622}
623
624void QGeoRoutePrivate::setContainingRoute(const QGeoRoute &route)
625{
626 m_containingRoute.reset(new QGeoRoute(route));
627}
628
629QGeoRoute QGeoRoutePrivate::containingRoute() const
630{
631 if (m_containingRoute)
632 return *m_containingRoute;
633 return QGeoRoute();
634}
635
636QT_END_NAMESPACE
637
638#include "moc_qgeoroute.cpp"
Combined button and popup list for selecting options.