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