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
qdeclarativepositionsource.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
7
8#include <QtCore/QCoreApplication>
9#include <QtQml/qqmlinfo.h>
10#include <QtQml/qqml.h>
11#include <qdeclarativepluginparameter_p.h>
12#include <QFile>
13#include <QtNetwork/QTcpSocket>
14#include <QTimer>
15
16QT_BEGIN_NAMESPACE
17
18/*!
19 \qmltype PositionSource
20 //! \nativetype QDeclarativePositionSource
21 \inqmlmodule QtPositioning
22 \since 5.2
23
24 \brief The PositionSource type provides the device's current position.
25
26 The PositionSource type provides information about the user device's
27 current position. The position is available as a \l{Position} type, which
28 contains all the standard parameters typically available from GPS and other
29 similar systems, including longitude, latitude, speed and accuracy details.
30
31 As different position sources are available on different platforms and
32 devices, these are categorized by their basic type (Satellite, NonSatellite,
33 and AllPositioningMethods). The available methods for the current platform
34 can be enumerated in the \l{supportedPositioningMethods} property.
35
36 To indicate which methods are suitable for your application, set the
37 \l{preferredPositioningMethods} property. If the preferred methods are not
38 available, the default source of location data for the platform will be
39 chosen instead. If no default source is available (because none are installed
40 for the runtime platform, or because it is disabled), the \l{valid} property
41 will be set to false.
42
43 The \l updateInterval property can then be used to indicate how often your
44 application wishes to receive position updates. The \l{start}(),
45 \l{stop}() and \l{update}() methods can be used to control the operation
46 of the PositionSource, as well as the \l{active} property, which when set
47 is equivalent to calling \l{start}() or \l{stop}().
48
49 When the PositionSource is active, position updates can be retrieved
50 either by simply using the \l{position} property in a binding (as the
51 value of another item's property), or by providing an implementation of
52 the \c {onPositionChanged} signal-handler.
53
54 \section2 Example Usage
55
56 The following example shows a simple PositionSource used to receive
57 updates every second and print the longitude and latitude out to
58 the console.
59
60 \code
61 PositionSource {
62 id: src
63 updateInterval: 1000
64 active: true
65
66 onPositionChanged: {
67 var coord = src.position.coordinate;
68 console.log("Coordinate:", coord.longitude, coord.latitude);
69 }
70 }
71 \endcode
72
73 \section2 Controlling Operation State
74
75 As it's mentioned above, PositionSource provides two ways to control its
76 operation state:
77
78 \list
79 \li By using the \l active \l {Qt Bindable Properties}{bindable} property.
80 \li By using \l start() and \l stop() methods.
81 \endlist
82
83 \note It's very important not to mix these approaches. If a bindable
84 \l active property is used to control the PositionSource object, but later
85 \l start() or \l stop() is called from the other part of the code, the
86 binding is broken, which may result in, for example, a UI element that is
87 not connected to any underlying object anymore.
88
89 Consider the following example of \b {bad code} where the \c active property
90 is bound to the CheckBox state, and calling \l stop() in the \c onClicked
91 signal handler breaks that binding.
92
93 \qml
94 Window {
95 width: 640
96 height: 480
97 visible: true
98
99 PositionSource {
100 id: posSource
101 name: "geoclue2"
102 active: cb.checked
103 }
104
105 Column {
106 anchors.centerIn: parent
107 spacing: 20
108 CheckBox {
109 id: cb
110 }
111 Button {
112 id: btn
113 text: "Stop"
114 onClicked: {
115 posSource.stop()
116 }
117 }
118 }
119 }
120 \endqml
121
122 Once the \e Stop button is clicked, \l stop() is executed, and the binding
123 for the \l active property is broken. At this point the CheckBox UI element
124 is no longer controlling the PositionSource object.
125
126 A straightforward fix in this case is to update the CheckBox state from
127 the \c onClicked handler. As soon as the CheckBox is unchecked, the
128 \l active property will be notified, and the PositionSource object's state
129 will update accordingly. The UI will also be in a consistent state.
130
131 \qml
132 Button {
133 id: btn
134 text: "Stop"
135 onClicked: {
136 cb.checked = false
137 }
138 }
139 \endqml
140
141 \note Using \l update() to request a single position update \e {does not}
142 have any effect on the \l active property's bindings, so they can be used
143 together without any problems.
144
145 \sa {QtPositioning::Position}, {QGeoPositionInfoSource}, {PluginParameter},
146 {Qt Bindable Properties}
147
148*/
149
150QDeclarativePositionSource::QDeclarativePositionSource()
151 : m_singleUpdate(0), m_regularUpdates(0), m_componentComplete(0),
152 m_parametersInitialized(0), m_startRequested(0), m_defaultSourceUsed(0)
153{
154 m_position.setValueBypassingBindings(new QDeclarativePosition(this));
155}
156
157QDeclarativePositionSource::~QDeclarativePositionSource()
158{
159 delete m_positionSource;
160}
161
162
163/*!
164 \qmlproperty string PositionSource::name
165
166 This property holds the unique internal name for the plugin currently
167 providing position information.
168
169 Setting the property causes the PositionSource to use a particular positioning provider. If
170 the PositionSource is active at the time that the name property is changed, it will become
171 inactive. If the specified positioning provider cannot be loaded the position source will
172 become invalid.
173
174 Changing the name property may cause the \l {updateInterval}, \l {supportedPositioningMethods}
175 and \l {preferredPositioningMethods} properties to change as well.
176*/
177
178
179QString QDeclarativePositionSource::name() const
180{
181 return m_sourceName;
182}
183
184void QDeclarativePositionSource::setName(const QString &newName)
185{
186 m_sourceName.removeBindingUnlessInWrapper();
187 if (m_positionSource && m_positionSource->sourceName() == newName)
188 return;
189
190 if (newName.isEmpty() && m_defaultSourceUsed)
191 return; // previously attached to a default source, now requesting the same.
192
193 const QString previousName = m_sourceName.valueBypassingBindings();
194
195 if (!m_componentComplete || !m_parametersInitialized) {
196 if (previousName != newName) {
197 m_sourceName.setValueBypassingBindings(newName);
198 m_sourceName.notify();
199 }
200 return;
201 }
202
203 // tryAttach() will update the m_sourceName correctly
204 tryAttach(newName, false);
205}
206
207QBindable<QString> QDeclarativePositionSource::bindableName()
208{
209 return QBindable<QString>(&m_sourceName);
210}
211
212QBindable<QDeclarativePosition *> QDeclarativePositionSource::bindablePosition() const
213{
214 return QBindable<QDeclarativePosition *>(&m_position);
215}
216
217/*!
218 \internal
219*/
220void QDeclarativePositionSource::tryAttach(const QString &newName, bool useFallback)
221{
222 const QString previousName = name();
223 const bool sourceExisted = (m_positionSource != nullptr);
224
225 int previousUpdateInterval = updateInterval();
226 PositioningMethods previousPositioningMethods = supportedPositioningMethods();
227 PositioningMethods previousPreferredPositioningMethods = preferredPositioningMethods();
228
229 m_defaultSourceUsed = false;
230
231 if (newName.isEmpty()) {
232 setSource(QGeoPositionInfoSource::createDefaultSource(parameterMap(), this));
233 m_defaultSourceUsed = true;
234 } else {
235 setSource(QGeoPositionInfoSource::createSource(newName, parameterMap(), this));
236 if (!m_positionSource && useFallback) {
237 setSource(QGeoPositionInfoSource::createDefaultSource(parameterMap(), this));
238 m_defaultSourceUsed = true;
239 }
240 }
241
242 if (m_positionSource) {
243 m_sourceName.setValueBypassingBindings(m_positionSource->sourceName());
244
245 connect(m_positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
246 this, SLOT(positionUpdateReceived(QGeoPositionInfo)));
247 connect(m_positionSource, SIGNAL(errorOccurred(QGeoPositionInfoSource::Error)),
248 this, SLOT(sourceErrorReceived(QGeoPositionInfoSource::Error)));
249
250 m_positionSource->setUpdateInterval(m_updateInterval);
251 m_positionSource->setPreferredPositioningMethods(
252 static_cast<QGeoPositionInfoSource::PositioningMethods>(int(m_preferredPositioningMethods)));
253 m_positionSource->setPreferredAltitudeType(m_preferredAltitudeType);
254
255 if (m_startRequested) {
256 const QGeoPositionInfo &lastKnown = m_positionSource->lastKnownPosition();
257 if (lastKnown.isValid())
258 setPosition(lastKnown);
259 }
260 } else {
261 m_sourceName.setValueBypassingBindings(newName);
262 m_defaultSourceUsed = false;
263 if (m_active) {
264 // We do not want to break the binding here, because we just want to
265 // give the user an opportunity to select another plugin and keep
266 // working.
267 m_active.setValueBypassingBindings(false);
268 m_active.notify();
269 }
270 }
271
272 if (previousUpdateInterval != updateInterval())
273 emit updateIntervalChanged();
274
275 if (previousPreferredPositioningMethods != preferredPositioningMethods())
276 emit preferredPositioningMethodsChanged();
277
278 if (previousPositioningMethods != supportedPositioningMethods())
279 notifySupportedPositioningMethodsChanged();
280
281 const bool sourceCurrentlyExists = (m_positionSource != nullptr);
282 if (sourceExisted != sourceCurrentlyExists) {
283 m_isValid.notify();
284 emit validityChanged();
285 }
286
287 if (m_active) { // implies m_positionSource
288 // If m_active is true, then the previous position source existed!
289 Q_ASSERT(sourceExisted);
290 // New source is set. It should be inactive by default.
291 // But we do not want to break the binding.
292 m_active.setValueBypassingBindings(false);
293 m_active.notify();
294 } else if (m_startRequested) {
295 m_startRequested = false;
296 executeStart();
297 }
298
299 if (previousName != m_sourceName)
300 m_sourceName.notify();
301}
302
303/*!
304 \qmlproperty bool PositionSource::valid
305
306 This property is true if the PositionSource object has acquired a valid
307 backend plugin to provide data. If false, other methods on the PositionSource
308 will have no effect.
309
310 Applications should check this property to determine whether positioning is
311 available and enabled on the runtime platform, and react accordingly.
312*/
313bool QDeclarativePositionSource::isValid() const
314{
315 return m_isValid.value();
316}
317
318QBindable<bool> QDeclarativePositionSource::bindableIsValid() const
319{
320 return QBindable<bool>(&m_isValid);
321}
322
323bool QDeclarativePositionSource::isValidActualComputation() const
324{
325 return m_positionSource != nullptr;
326}
327
328/*!
329 \internal
330*/
331void QDeclarativePositionSource::onParameterInitialized()
332{
333 m_parametersInitialized = true;
334 for (QDeclarativePluginParameter *p: std::as_const(m_parameters)) {
335 if (!p->isInitialized()) {
336 m_parametersInitialized = false;
337 break;
338 }
339 }
340
341 // If here, componentComplete has been called.
342 if (m_parametersInitialized)
343 tryAttach(m_sourceName.value());
344}
345
346void QDeclarativePositionSource::notifySupportedPositioningMethodsChanged()
347{
348 m_supportedPositioningMethods.notify();
349 emit supportedPositioningMethodsChanged();
350}
351
352void QDeclarativePositionSource::setPosition(const QGeoPositionInfo &pi)
353{
354 m_position.value()->setPosition(pi);
355 m_position.notify();
356 emit positionChanged();
357}
358
359void QDeclarativePositionSource::setSource(QGeoPositionInfoSource *source)
360{
361 if (m_positionSource)
362 delete m_positionSource;
363
364 if (!source) {
365 m_positionSource = nullptr;
366 } else {
367 m_positionSource = source;
368 connect(m_positionSource, &QGeoPositionInfoSource::supportedPositioningMethodsChanged,
369 this, &QDeclarativePositionSource::notifySupportedPositioningMethodsChanged);
370 }
371}
372
373bool QDeclarativePositionSource::parametersReady()
374{
375 for (const QDeclarativePluginParameter *p: std::as_const(m_parameters)) {
376 if (!p->isInitialized())
377 return false;
378 }
379 return true;
380}
381
382/*!
383 \internal
384*/
385QVariantMap QDeclarativePositionSource::parameterMap() const
386{
387 QVariantMap map;
388
389 for (const auto *parameter : m_parameters)
390 map.insert(parameter->name(), parameter->value());
391
392 return map;
393}
394
395/*!
396 \internal
397*/
398void QDeclarativePositionSource::setUpdateInterval(int updateInterval)
399{
400 if (m_positionSource) {
401 int previousUpdateInterval = m_positionSource->updateInterval();
402
403 m_updateInterval = updateInterval;
404
405 if (previousUpdateInterval != updateInterval) {
406 m_positionSource->setUpdateInterval(updateInterval);
407 if (previousUpdateInterval != m_positionSource->updateInterval())
408 emit updateIntervalChanged();
409 }
410 } else {
411 if (m_updateInterval != updateInterval) {
412 m_updateInterval = updateInterval;
413 emit updateIntervalChanged();
414 }
415 }
416}
417
418/*!
419 \qmlproperty int PositionSource::updateInterval
420
421 This property holds the desired interval between updates (milliseconds).
422
423 \sa {QGeoPositionInfoSource::updateInterval()}
424*/
425
426int QDeclarativePositionSource::updateInterval() const
427{
428 if (!m_positionSource)
429 return m_updateInterval;
430
431 return m_positionSource->updateInterval();
432}
433
434/*!
435 \qmlproperty enumeration PositionSource::supportedPositioningMethods
436
437 This property holds the supported positioning methods of the
438 current source.
439
440 \value PositionSource.NoPositioningMethods
441 No positioning methods supported (no source).
442 \value PositionSource.SatellitePositioningMethods
443 Satellite-based positioning methods such as GPS are supported.
444 \value PositionSource.NonSatellitePositioningMethods
445 Non-satellite-based methods are supported.
446 \value PositionSource.AllPositioningMethods
447 Both satellite-based and non-satellite positioning methods are supported.
448*/
449
450QDeclarativePositionSource::PositioningMethods
451QDeclarativePositionSource::supportedPositioningMethods() const
452{
453 return m_supportedPositioningMethods.value();
454}
455
456QDeclarativePositionSource::PositioningMethods
457QDeclarativePositionSource::supportedMethodsActualComputation() const
458{
459 if (m_positionSource) {
460 return static_cast<QDeclarativePositionSource::PositioningMethods>(
461 int(m_positionSource->supportedPositioningMethods()));
462 }
463 return QDeclarativePositionSource::NoPositioningMethods;
464}
465
466QBindable<QDeclarativePositionSource::PositioningMethods>
467QDeclarativePositionSource::bindableSupportedPositioningMethods() const
468{
469 return QBindable<PositioningMethods>(&m_supportedPositioningMethods);
470}
471
472/*!
473 \qmlproperty enumeration PositionSource::preferredPositioningMethods
474
475 This property holds the preferred positioning methods of the
476 current source.
477
478 \value PositionSource.NoPositioningMethods
479 No positioning method is preferred.
480 \value PositionSource.SatellitePositioningMethods
481 Satellite-based positioning methods such as GPS should be preferred.
482 \value PositionSource.NonSatellitePositioningMethods
483 Non-satellite-based methods should be preferred.
484 \value PositionSource.AllPositioningMethods
485 Any positioning methods are acceptable.
486*/
487
488void QDeclarativePositionSource::setPreferredPositioningMethods(PositioningMethods methods)
489{
490 if (m_positionSource) {
491 PositioningMethods previousPreferredPositioningMethods = preferredPositioningMethods();
492
493 m_preferredPositioningMethods = methods;
494
495 if (previousPreferredPositioningMethods != methods) {
496 m_positionSource->setPreferredPositioningMethods(
497 static_cast<QGeoPositionInfoSource::PositioningMethods>(int(methods)));
498 if (previousPreferredPositioningMethods != m_positionSource->preferredPositioningMethods())
499 emit preferredPositioningMethodsChanged();
500 }
501 } else {
502 if (m_preferredPositioningMethods != methods) {
503 m_preferredPositioningMethods = methods;
504 emit preferredPositioningMethodsChanged();
505 }
506 }
507}
508
509QDeclarativePositionSource::PositioningMethods QDeclarativePositionSource::preferredPositioningMethods() const
510{
511 if (m_positionSource) {
512 return static_cast<QDeclarativePositionSource::PositioningMethods>(
513 int(m_positionSource->preferredPositioningMethods()));
514 }
515 return m_preferredPositioningMethods;
516}
517
518/*!
519 \qmlproperty enumeration PositionSource::preferredAltitudeType
520 \since 6.13
521
522 This property holds the preferred reference surface for the altitude of
523 the reported positions.
524
525 \value GeoCoordinate.Unknown
526 No altitude type is preferred. The source reports the altitude in the
527 same way as in the previous Qt versions, and the
528 \l {geoCoordinate::altitudeType}{altitudeType} of the reported
529 coordinates is \c {GeoCoordinate.Unknown}. This is the default value.
530 \value GeoCoordinate.MSL
531 The altitude above the mean sea level (MSL) should be preferred.
532 \value GeoCoordinate.WGS84
533 The altitude above the WGS84 reference ellipsoid should be preferred.
534
535 If the source can't provide the preferred altitude, it reports the
536 altitude that it can provide. The
537 \l {geoCoordinate::altitudeType}{altitudeType} of the reported
538 coordinates always specifies the reference surface of the actually
539 reported altitude.
540
541 The sources that do not support this property ignore it.
542
543 For more details see the \l {QGeoPositionInfoSource::preferredAltitudeType}
544 property.
545*/
546
547void QDeclarativePositionSource::setPreferredAltitudeType(QGeoCoordinate::AltitudeType type)
548{
549 if (m_preferredAltitudeType == type)
550 return;
551
552 m_preferredAltitudeType = type;
553 if (m_positionSource)
554 m_positionSource->setPreferredAltitudeType(type);
555 emit preferredAltitudeTypeChanged();
556}
557
558QGeoCoordinate::AltitudeType QDeclarativePositionSource::preferredAltitudeType() const
559{
560 return m_preferredAltitudeType;
561}
562
563/*!
564 \qmlmethod void PositionSource::start()
565
566 Requests updates from the location source.
567 Uses \l updateInterval if set, default interval otherwise.
568 If there is no source available, this method has no effect.
569
570 \note Calling this method breaks the bindings of
571 \l {PositionSource::}{active} property.
572
573 \sa stop(), update(), active
574*/
575
576void QDeclarativePositionSource::start()
577{
578 if (m_positionSource) {
579 m_active.removeBindingUnlessInWrapper();
580 executeStart();
581 }
582}
583
584/*!
585 \qmlmethod void PositionSource::update(int timeout)
586
587 A convenience method to request single update from the location source.
588 If there is no source available, this method has no effect.
589
590 If the position source is not active, it will be activated for as
591 long as it takes to receive an update, or until the request times
592 out. The request timeout period is source-specific.
593
594 The \a timeout is specified in milliseconds. If the \a timeout is zero
595 (the default value), it defaults to a reasonable timeout period as
596 appropriate for the source.
597
598 \sa start(), stop(), active
599*/
600
601void QDeclarativePositionSource::update(int timeout)
602{
603 if (m_positionSource) {
604 m_singleUpdate = true;
605 if (!m_active) {
606 // Questionable: we do not want this method to break the binding.
607 // Mostly because it can be called while the updates are already
608 // running.
609 m_active.setValueBypassingBindings(true);
610 m_active.notify();
611 }
612 // Use default timeout value. Set active before calling the
613 // update request because on some platforms there may
614 // be results immediately.
615 m_positionSource->requestUpdate(timeout);
616 }
617}
618
619/*!
620 \qmlmethod void PositionSource::stop()
621
622 Stops updates from the location source.
623 If there is no source available or it is not active,
624 this method has no effect.
625
626 \note Calling this method breaks the bindings of
627 \l {PositionSource::}{active} property.
628
629 \sa start(), update(), active
630*/
631
632void QDeclarativePositionSource::stop()
633{
634 if (m_positionSource) {
635 m_positionSource->stopUpdates();
636 m_regularUpdates = false;
637 // Try to break the binding even if we do not actually need to update
638 // the active state. The m_active can be updated later, when the
639 // single update request finishes.
640 m_active.removeBindingUnlessInWrapper();
641 if (m_active && !m_singleUpdate) {
642 m_active.setValueBypassingBindings(false);
643 m_active.notify();
644 }
645 }
646}
647
648/*!
649 \qmlproperty bool PositionSource::active
650
651 This property indicates whether the position source is active.
652 Setting this property to false equals calling \l stop, and
653 setting this property true equals calling \l start.
654
655 \sa start, stop, update
656*/
657void QDeclarativePositionSource::setActive(bool active)
658{
659 // We need to remove binding, if this method is called explicitly.
660 // Other changes to m_active are done inside start() and stop() methods.
661 m_active.removeBindingUnlessInWrapper();
662 if (active == m_active)
663 return;
664
665 if (active) {
666 // If the component is not completed yet, we need to defer the
667 // actual executeStart() call until everything is set up.
668 if (m_componentComplete && m_parametersInitialized)
669 executeStart();
670 else
671 m_startRequested = true;
672 } else {
673 stop();
674 }
675}
676
677bool QDeclarativePositionSource::isActive() const
678{
679 return m_active;
680}
681
682/*!
683 \qmlproperty Position PositionSource::position
684
685 This property holds the last known positional data.
686 It is a read-only property.
687
688 The Position type has different positional member variables,
689 whose validity can be checked with appropriate validity functions
690 (for example sometimes an update does not have speed or altitude data).
691
692 However, whenever a \c {positionChanged} signal has been received, at least
693 position::coordinate::latitude, position::coordinate::longitude, and position::timestamp can
694 be assumed to be valid.
695
696 \sa start, stop, update
697*/
698
699QDeclarativePosition *QDeclarativePositionSource::position()
700{
701 return m_position.value();
702}
703
704void QDeclarativePositionSource::positionUpdateReceived(const QGeoPositionInfo &update)
705{
706 setPosition(update);
707
708 if (m_singleUpdate && m_active) {
709 // we need to reset m_singleUpdate because we got one
710 m_singleUpdate = false;
711 if (!m_regularUpdates) {
712 // but we change the active state only if the regular updates are
713 // also stopped
714 m_active.setValueBypassingBindings(false);
715 m_active.notify();
716 }
717 }
718}
719
720
721/*!
722 \qmlproperty enumeration PositionSource::sourceError
723
724 This property holds the error which last occurred with the PositionSource.
725
726 \value PositionSource.AccessError
727 The connection setup to the remote positioning backend failed because the
728 application lacked the required privileges.
729 \value PositionSource.ClosedError
730 The positioning backend closed the connection, which happens for example in case
731 the user is switching location services to off. As soon as the location service is re-enabled
732 regular updates will resume.
733 \value PositionSource.NoError No error has occurred.
734 \value PositionSource.UnknownSourceError An unidentified error occurred.
735 \value PositionSource.UpdateTimeoutError
736 The current position could not be
737 retrieved within the specified timeout, or this PositionSource determined
738 that it will not be able to provide further regular updates.
739*/
740
741QDeclarativePositionSource::SourceError QDeclarativePositionSource::sourceError() const
742{
743 return m_sourceError;
744}
745
746QBindable<QDeclarativePositionSource::SourceError>
747QDeclarativePositionSource::bindableSourceError() const
748{
749 return QBindable<QDeclarativePositionSource::SourceError>(&m_sourceError);
750}
751
752QGeoPositionInfoSource *QDeclarativePositionSource::positionSource() const
753{
754 return m_positionSource;
755}
756
757/*!
758 \qmlproperty list<PluginParameter> PositionSource::parameters
759 \qmldefault
760
761 This property holds the list of plugin parameters.
762
763 \since QtPositioning 5.14
764*/
765QQmlListProperty<QDeclarativePluginParameter> QDeclarativePositionSource::parameters()
766{
767 return QQmlListProperty<QDeclarativePluginParameter>(this,
768 0,
769 parameter_append,
770 parameter_count,
771 parameter_at,
772 parameter_clear);
773}
774
775/*!
776 \internal
777*/
778void QDeclarativePositionSource::parameter_append(QQmlListProperty<QDeclarativePluginParameter> *prop, QDeclarativePluginParameter *parameter)
779{
780 QDeclarativePositionSource *p = static_cast<QDeclarativePositionSource *>(prop->object);
781 p->m_parameters.append(parameter);
782}
783
784/*!
785 \internal
786*/
787qsizetype QDeclarativePositionSource::parameter_count(QQmlListProperty<QDeclarativePluginParameter> *prop)
788{
789 return static_cast<QDeclarativePositionSource *>(prop->object)->m_parameters.size();
790}
791
792/*!
793 \internal
794*/
795QDeclarativePluginParameter *QDeclarativePositionSource::parameter_at(QQmlListProperty<QDeclarativePluginParameter> *prop, qsizetype index)
796{
797 return static_cast<QDeclarativePositionSource *>(prop->object)->m_parameters[index];
798}
799
800/*!
801 \internal
802*/
803void QDeclarativePositionSource::parameter_clear(QQmlListProperty<QDeclarativePluginParameter> *prop)
804{
805 QDeclarativePositionSource *p = static_cast<QDeclarativePositionSource *>(prop->object);
806 p->m_parameters.clear();
807}
808
809void QDeclarativePositionSource::executeStart()
810{
811 if (m_positionSource) {
812 m_positionSource->startUpdates();
813
814 // If this method is called directly from start(), the binding is
815 // already broken there (for the consistency with stop()).
816 // If this method is called by a timer, started in setActive(), we do
817 // not need to break the binding, because it was already done (if
818 // needed).
819
820 m_regularUpdates = true;
821 if (!m_active) {
822 m_active.setValueBypassingBindings(true);
823 m_active.notify();
824 }
825 }
826}
827
828void QDeclarativePositionSource::componentComplete()
829{
830 m_componentComplete = true;
831 m_parametersInitialized = true;
832 for (QDeclarativePluginParameter *p: std::as_const(m_parameters)) {
833 if (!p->isInitialized()) {
834 m_parametersInitialized = false;
835 connect(p, &QDeclarativePluginParameter::initialized,
836 this, &QDeclarativePositionSource::onParameterInitialized,
837 Qt::SingleShotConnection);
838 }
839 }
840
841 if (m_parametersInitialized)
842 tryAttach(m_sourceName.value());
843}
844
845/*!
846 \qmlmethod bool PositionSource::setBackendProperty(string name, var value)
847
848 Sets the backend-specific property named \a name to \a value.
849 Returns true on success, false otherwise, including if called on an uninitialized PositionSource.
850 Supported backend-specific properties are listed and described in
851 \l {Qt Positioning plugins#Default plugins}.
852
853 \since Qt Positioning 5.14
854
855 \sa backendProperty, QGeoPositionInfoSource::setBackendProperty
856*/
857bool QDeclarativePositionSource::setBackendProperty(const QString &name, const QVariant &value)
858{
859 if (m_positionSource)
860 return m_positionSource->setBackendProperty(name, value);
861 return false;
862}
863
864/*!
865 \qmlmethod var PositionSource::backendProperty(string name)
866
867 Returns the value of the backend-specific property named \a name, if present.
868 Otherwise, including if called on an uninitialized PositionSource, the return value will be invalid.
869 Supported backend-specific properties are listed and described in
870 \l {Qt Positioning plugins#Default plugins}.
871
872 \since Qt Positioning 5.14
873
874 \sa setBackendProperty, QGeoPositionInfoSource::setBackendProperty
875*/
876QVariant QDeclarativePositionSource::backendProperty(const QString &name) const
877{
878 if (m_positionSource)
879 return m_positionSource->backendProperty(name);
880 return QVariant();
881}
882
883QBindable<bool> QDeclarativePositionSource::bindableActive()
884{
885 return QBindable<bool>(&m_active);
886}
887
888/*!
889 \internal
890*/
891void QDeclarativePositionSource::sourceErrorReceived(const QGeoPositionInfoSource::Error error)
892{
893 if (error == QGeoPositionInfoSource::AccessError)
894 m_sourceError.setValueBypassingBindings(QDeclarativePositionSource::AccessError);
895 else if (error == QGeoPositionInfoSource::ClosedError)
896 m_sourceError.setValueBypassingBindings(QDeclarativePositionSource::ClosedError);
897 else if (error == QGeoPositionInfoSource::UpdateTimeoutError)
898 m_sourceError.setValueBypassingBindings(QDeclarativePositionSource::UpdateTimeoutError);
899 else if (error == QGeoPositionInfoSource::NoError)
900 return; //nothing to do
901 else
902 m_sourceError.setValueBypassingBindings(QDeclarativePositionSource::UnknownSourceError);
903
904 m_sourceError.notify();
905 emit sourceErrorChanged();
906
907 // if an error occurred during single update, the update is stopped, so we
908 // need to change the active state.
909 if (m_active && m_singleUpdate) {
910 m_singleUpdate = false;
911 if (!m_regularUpdates) {
912 m_active.setValueBypassingBindings(false);
913 m_active.notify();
914 }
915 }
916}
917
918QT_END_NAMESPACE
919
920#include "moc_qdeclarativepositionsource_p.cpp"