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