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
qgeoareamonitorsource.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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 <QtCore/private/qobject_p.h>
6#include <QGeoAreaMonitorSource>
9
10/*!
11 \class QGeoAreaMonitorSource
12 \inmodule QtPositioning
13 \ingroup QtPositioning-positioning
14 \since 5.2
15
16 \brief The QGeoAreaMonitorSource class enables the detection of proximity
17 changes for a specified set of coordinates.
18
19 A QGeoAreaMonitorSource emits signals when the current position is in
20 range, or has moved out of range, of a specified area.
21 Each area is specified by a \l QGeoAreaMonitorInfo object.
22 For example:
23
24 \snippet cpp/cppqml.cpp BigBen
25
26 \c QGeoAreaMonitorSource follows a singleton pattern. Each instance of
27 the class with the same \l sourceName() shares the same area monitoring backend.
28 If a new \l QGeoAreaMonitorInfo object is added via \l startMonitoring()
29 or \l requestUpdate() it can be retrieved by another instance of this class
30 (provided that they are sourced from the same area monitor provider plug-in).
31 The same singleton pattern applies to the \l QGeoPositionInfoSource instance
32 used by this class. The following code snippet emphasizes the behavior:
33
34 \code
35 QGeoAreaMonitorSource *s1 = QGeoAreaMonitorSource::createSource("blah", this);
36 QGeoAreaMonitorSource *s2 = QGeoAreaMonitorSource::createSource("blah", this);
37 QVERIFY(s1->positionInfoSource() == s2->positionInfoSource);
38 \endcode
39*/
40
42
43
44
51
52/*!
53 \enum QGeoAreaMonitorSource::Error
54 Defines the types of positioning methods.
55
56 The Error enumeration represents the errors which can occur.
57
58 \value AccessError The connection setup to the remote area monitoring backend failed because the
59 application lacked the required privileges.
60 \value InsufficientPositionInfo The area monitoring source could not retrieve a location fix or
61 the accuracy of the fix is not high enough to provide an effective area monitoring.
62 \value NoError No error has occurred.
63 \value UnknownSourceError An unidentified error occurred.
64*/
65
66/*!
67 \enum QGeoAreaMonitorSource::AreaMonitorFeature
68 Defines the types of area monitoring capabilities.
69
70 \value PersistentAreaMonitorFeature QGeoAreaMonitorInfo instances can be made persistent.
71 A persistent monitor continues to be active even when the application managing the monitor is
72 not running.
73 \value AnyAreaMonitorFeature Matches all possible area monitoring features.
74*/
75
76/*!
77 \fn virtual AreaMonitoringFeatures QGeoAreaMonitorSource::supportedAreaMonitorFeatures() const = 0;
78
79 Returns the area monitoring features available to this source.
80*/
81
82/*!
83 \fn virtual QGeoAreaMonitorSource::Error QGeoAreaMonitorSource::error() const
84
85 Returns the type of error that last occurred.
86
87 \note Since Qt6 the last error is always reset when calling
88 startMonitoring() or requestUpdate().
89*/
90
91/*!
92 Creates a monitor source with the given \a parent.
93*/
94QGeoAreaMonitorSource::QGeoAreaMonitorSource(QObject *parent)
95 : QObject(*new QGeoAreaMonitorSourcePrivate, parent)
96{
97 Q_D(QGeoAreaMonitorSource);
98 d->source = nullptr;
99}
100
101/*!
102 Destroys the monitor source.
103*/
104QGeoAreaMonitorSource::~QGeoAreaMonitorSource()
105{
106}
107
108/*!
109 Creates and returns a monitor source with the given \a parent that
110 monitors areas using resources on the underlying system.
111
112 Returns \c nullptr if the system has no support for position monitoring.
113*/
114QGeoAreaMonitorSource *QGeoAreaMonitorSource::createDefaultSource(QObject *parent)
115{
116 const QList<QCborMap> plugins = QGeoPositionInfoSourcePrivate::pluginsSorted();
117 for (const QCborMap &obj : plugins) {
118 if (obj.value(QStringLiteral("Monitor")).isBool()
119 && obj.value(QStringLiteral("Monitor")).toBool())
120 {
121 QGeoAreaMonitorSource *s = nullptr;
122 auto factory = QGeoPositionInfoSourcePrivate::loadFactory(obj);
123 if (factory)
124 s = factory->areaMonitor(parent, QVariantMap());
125 if (s)
126 s->d_func()->providerName = obj.value(QStringLiteral("Provider")).toString();
127 return s;
128 }
129 }
130 return nullptr;
131}
132
133/*!
134 Creates and returns a monitor source with the given \a parent,
135 by loading the plugin named \a sourceName.
136
137 Returns \c nullptr if the plugin cannot be found.
138*/
139QGeoAreaMonitorSource *QGeoAreaMonitorSource::createSource(const QString &sourceName, QObject *parent)
140{
141 auto plugins = QGeoPositionInfoSourcePrivate::plugins();
142 if (plugins.contains(sourceName)) {
143 const auto metaData = plugins.value(sourceName);
144 QGeoAreaMonitorSource *s = nullptr;
145 auto factory = QGeoPositionInfoSourcePrivate::loadFactory(metaData);
146 if (factory)
147 s = factory->areaMonitor(parent, QVariantMap());
148 if (s)
149 s->d_func()->providerName = metaData.value(QStringLiteral("Provider")).toString();
150 return s;
151 }
152
153 return nullptr;
154}
155
156/*!
157 Returns a list of available monitor plugins, including the default system
158 backend if one is available.
159*/
160QStringList QGeoAreaMonitorSource::availableSources()
161{
162 QStringList plugins;
163 const auto meta = QGeoPositionInfoSourcePrivate::plugins();
164 for (auto it = meta.cbegin(), end = meta.cend(); it != end; ++it) {
165 if (it.value().value(QStringLiteral("Monitor")).isBool()
166 && it.value().value(QStringLiteral("Monitor")).toBool()) {
167 plugins << it.key();
168 }
169 }
170
171 return plugins;
172}
173
174/*!
175 Returns the unique name of the area monitor source implementation in use.
176
177 This is the same name that can be passed to createSource() in order to
178 create a new instance of a particular area monitor source implementation.
179*/
180QString QGeoAreaMonitorSource::sourceName() const
181{
182 Q_D(const QGeoAreaMonitorSource);
183 return d->providerName;
184}
185
186/*!
187 \since 6.2
188
189 Sets the backend-specific property named \a name to \a value.
190 Returns \c true on success, otherwise returns \c false.
191 Backend-specific properties can be used to configure the area monitoring
192 subsystem behavior at runtime.
193
194 \sa backendProperty()
195*/
196bool QGeoAreaMonitorSource::setBackendProperty(const QString &name, const QVariant &value)
197{
198 Q_UNUSED(name);
199 Q_UNUSED(value);
200 return false;
201}
202
203/*!
204 \since 6.2
205
206 Returns the value of the backend-specific property named \a name,
207 if present. Otherwise the returned value will be invalid.
208
209 \sa setBackendProperty()
210*/
211QVariant QGeoAreaMonitorSource::backendProperty(const QString &name) const
212{
213 Q_UNUSED(name);
214 return QVariant();
215}
216
217/*!
218 Returns the current QGeoPositionInfoSource used by this QGeoAreaMonitorSource
219 object. The function will return \l QGeoPositionInfoSource::createDefaultSource()
220 if no other object has been set.
221
222 The function returns \c nullptr if not even a default QGeoPositionInfoSource
223 exists.
224
225 Any usage of the returned \l QGeoPositionInfoSource instance should account
226 for the fact that it may reside in a different thread.
227
228 \sa QGeoPositionInfoSource, setPositionInfoSource()
229*/
230QGeoPositionInfoSource* QGeoAreaMonitorSource::positionInfoSource() const
231{
232 Q_D(const QGeoAreaMonitorSource);
233 return d->source;
234}
235
236/*!
237 Sets the new \l QGeoPositionInfoSource to be used by this QGeoAreaMonitorSource object.
238 The area monitoring backend becomes the new QObject parent for \a newSource.
239 The previous \l QGeoPositionInfoSource object will be deleted. All QGeoAreaMonitorSource
240 instances based on the same \l sourceName() share the same QGeoPositionInfoSource
241 instance.
242
243 This may be useful when it is desirable to manipulate the positioning system
244 used by the area monitoring engine.
245
246 Note that ownership must be taken care of by subclasses of QGeoAreaMonitorSource.
247 Due to the singleton pattern behind this class \a newSource may be moved to a
248 new thread.
249
250 \sa positionInfoSource()
251 */
252void QGeoAreaMonitorSource::setPositionInfoSource(QGeoPositionInfoSource *newSource)
253{
254 Q_D(QGeoAreaMonitorSource);
255 d->source = newSource;
256}
257
258
259/*!
260 \fn virtual bool QGeoAreaMonitorSource::startMonitoring(const QGeoAreaMonitorInfo &monitor)
261
262 Returns \c true if the monitoring of \a monitor could be successfully started; otherwise
263 returns \c false. A reason for not being able to start monitoring could be the unavailability
264 of an appropriate default position info source while no alternative QGeoPositionInfoSource
265 has been set via \l setPositionInfoSource().
266
267 If \a monitor is already active, the existing monitor object will be replaced by the new \a monitor reference.
268 The identification of QGeoAreaMonitorInfo instances happens via \l QGeoAreaMonitorInfo::identifier().
269 Therefore this function can also be used to update active monitors.
270
271 If \a monitor has an expiry date that has been passed this function returns false. Calling
272 this function for an already via \l requestUpdate() registered single shot monitor
273 switches the monitor to a permanent monitoring mode.
274
275 Requesting persistent monitoring on a QGeoAreaMonitorSource instance fails if the area monitoring
276 backend doesn't support \l QGeoAreaMonitorSource::PersistentAreaMonitorFeature.
277
278 \note Since Qt6 this method always resets the last error to
279 \l {QGeoAreaMonitorSource::}{NoError} before starting monitoring.
280
281 \sa stopMonitoring()
282*/
283
284/*!
285 \fn virtual bool QGeoAreaMonitorSource::requestUpdate(const QGeoAreaMonitorInfo &monitor, const char *signal)
286
287 Enables single shot area monitoring. Area monitoring for \a monitor will be performed
288 until this QGeoAreaMonitorSource instance emits \a signal for the first time. Once
289 the signal was emitted, \a monitor is automatically removed from the list of
290 \l activeMonitors(). If \a monitor is invalid or has an expiry date that has
291 been passed, this function returns \c false.
292
293 \code
294 QGeoAreaMonitor singleShotMonitor;
295 QGeoAreaMonitorSource * source = QGeoAreaMonitorSource::createDefaultSource(this);
296 //...
297 bool ret = source->requestUpdate(singleShotMonitor,
298 SIGNAL(areaExited(QGeoAreaMonitor,QGeoPositionInfo)));
299 \endcode
300
301 The above \c singleShotMonitor object will cease to send updates once the \l areaExited() signal
302 was emitted for the first time. Until this point in time any other signal may be emitted
303 zero or more times depending on the area context.
304
305 It is not possible to simultanously request updates for more than one signal of the same monitor object.
306 The last call to this function determines the signal upon which the updates cease to continue.
307 At this stage only the \l areaEntered() and \l areaExited() signals can be used to
308 terminate the monitoring process.
309
310 Requesting persistent monitoring on a QGeoAreaMonitorSource instance fails if the area monitoring
311 backend doesn't support \l QGeoAreaMonitorSource::PersistentAreaMonitorFeature.
312
313 If \a monitor was already registered via \l startMonitoring() it is converted to a single
314 shot behavior.
315
316 \note Since Qt6 this method always resets the last error to
317 \l {QGeoAreaMonitorSource::}{NoError} before starting monitoring.
318
319 \sa startMonitoring(), stopMonitoring()
320 */
321
322/*!
323 \fn virtual bool QGeoAreaMonitorSource::stopMonitoring(const QGeoAreaMonitorInfo &monitor)
324
325 Returns true if \a monitor was successfully removed from the list of \l activeMonitors();
326 otherwise returns false. This behavior is independent on whether \a monitor was registered
327 via \l startMonitoring() or \l requestUpdate().
328*/
329
330/*!
331 \fn virtual QList<QGeoAreaMonitorInfo> QGeoAreaMonitorSource::activeMonitors() const
332
333 Returns the list of all active monitors known to the QGeoAreaMonitorSource object.
334
335 An active monitor was started via startMonitoring(). For every active
336 monitor the source object will emit the required signals, such as
337 areaEntered() or areaExited(). Multiple \l QGeoAreaMonitorSource instances
338 within the same application share the same active monitor objects.
339
340 Unless an active QGeoAreaMonitorInfo \l {QGeoAreaMonitorInfo::isPersistent()}{isPersistent()} an active QGeoAreaMonitorInfo
341 will be stopped once the current application terminates.
342*/
343
344/*!
345 \fn virtual QList<QGeoAreaMonitorInfo> QGeoAreaMonitorSource::activeMonitors(const QGeoShape &lookupArea) const
346
347 Returns the list of all active monitors known to the QGeoAreaMonitorSource object whose
348 center lies within \a lookupArea. If \a lookupArea is empty the returned list will be empty.
349
350 An active monitor was started via startMonitoring(). For every active
351 monitor the source object will emit the required signals, such as
352 areaEntered() or areaExited(). Multiple \l QGeoAreaMonitorSource instances
353 within the same application share the same active monitor objects.
354
355 Unless an active QGeoAreaMonitorInfo \l {QGeoAreaMonitorInfo::isPersistent()}{isPersistent()} an active QGeoAreaMonitorInfo
356 will be stopped once the current application terminates.
357
358 \sa QGeoShape
359*/
360
361
362/*!
363 \fn void QGeoAreaMonitorSource::monitorExpired(const QGeoAreaMonitorInfo &monitor)
364
365 Emitted when \a monitor has expired. An expired area monitor is automatically
366 removed from the list of \l activeMonitors().
367
368 \sa activeMonitors()
369*/
370
371/*!
372 \fn void QGeoAreaMonitorSource::areaEntered(const QGeoAreaMonitorInfo &monitor, const QGeoPositionInfo &update)
373
374 Emitted when the current position has moved from a position outside of the active \a monitor
375 to a position within the monitored area.
376
377 The \a update holds the new position.
378*/
379
380/*!
381 \fn void QGeoAreaMonitorSource::areaExited(const QGeoAreaMonitorInfo &monitor, const QGeoPositionInfo &update)
382
383 Emitted when the current position has moved from a position within the active \a monitor
384 to a position outside the monitored area.
385
386 The \a update holds the new position.
387*/
388
389/*!
390 \fn void QGeoAreaMonitorSource::errorOccurred(QGeoAreaMonitorSource::Error areaMonitoringError)
391
392 This signal is emitted after an error occurred. The \a areaMonitoringError
393 parameter describes the type of error that occurred.
394
395*/
396
397QT_END_NAMESPACE
398
399#include "moc_qgeoareamonitorsource.cpp"
Combined button and popup list for selecting options.