Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
>
7
#
include
"qgeopositioninfosourcefactory.h"
8
#
include
"qgeopositioninfosource_p.h"
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
41
QT_BEGIN_NAMESPACE
42
43
44
45
class
QGeoAreaMonitorSourcePrivate
:
public
QObjectPrivate
46
{
47
public
:
48
QGeoPositionInfoSource
*
source
;
49
QString
providerName
;
50
};
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
*/
94
QGeoAreaMonitorSource::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
*/
104
QGeoAreaMonitorSource::~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
*/
114
QGeoAreaMonitorSource *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
auto
factory = QGeoPositionInfoSourcePrivate::loadFactory(obj);
122
if
(!factory)
123
continue
;
124
if
(QGeoAreaMonitorSource *s = factory->areaMonitor(parent, QVariantMap())) {
125
s->d_func()->providerName = obj.value(QStringLiteral(
"Provider"
)).toString();
126
return
s;
127
}
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
*/
139
QGeoAreaMonitorSource *QGeoAreaMonitorSource::createSource(
const
QString &sourceName, QObject *parent)
140
{
141
const
QList<QCborMap> candidates = QGeoPositionInfoSourcePrivate::plugins().values(sourceName);
142
for
(
const
QCborMap &metaData : candidates) {
143
auto
factory = QGeoPositionInfoSourcePrivate::loadFactory(metaData);
144
if
(!factory)
145
continue
;
146
if
(QGeoAreaMonitorSource *s = factory->areaMonitor(parent, QVariantMap())) {
147
s->d_func()->providerName = metaData.value(QStringLiteral(
"Provider"
)).toString();
148
return
s;
149
}
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
*/
159
QStringList 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
*/
179
QString 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
*/
195
bool
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
*/
210
QVariant 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
*/
229
QGeoPositionInfoSource* 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
*/
251
void
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
396
QT_END_NAMESPACE
397
398
#
include
"moc_qgeoareamonitorsource.cpp"
QGeoAreaMonitorSourcePrivate
Definition
qgeoareamonitorsource.cpp:46
QGeoAreaMonitorSourcePrivate::source
QGeoPositionInfoSource * source
Definition
qgeoareamonitorsource.cpp:48
QGeoAreaMonitorSourcePrivate::providerName
QString providerName
Definition
qgeoareamonitorsource.cpp:49
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtpositioning
src
positioning
qgeoareamonitorsource.cpp
Generated on
for Qt by
1.18.0