Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgeopositioninfosource.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
6
7#include <QFile>
8#include <QPluginLoader>
9#include <QStringList>
10#include <QCryptographicHash>
11#include <QtCore/private/qfactoryloader_p.h>
12#include <QtCore/private/qthread_p.h>
13
14#include <algorithm>
15
17
19 ("org.qt-project.qt.position.sourcefactory/6.0",
20 QLatin1String("/position")))
21
22
74{
75
76}
77
79{
80 const int idx = static_cast<int>(meta.value(QStringLiteral("index")).toDouble());
81 if (idx < 0)
82 return nullptr;
83 QObject *instance = loader()->instance(idx);
84 if (!instance)
85 return nullptr;
86 return qobject_cast<QGeoPositionInfoSourceFactory *>(instance);
87}
88
89QMultiHash<QString, QCborMap> QGeoPositionInfoSourcePrivate::plugins(bool reload)
90{
91 static QMultiHash<QString, QCborMap> plugins;
92 static bool alreadyDiscovered = false;
93
94 if (reload == true)
95 alreadyDiscovered = false;
96
97 if (!alreadyDiscovered) {
99 alreadyDiscovered = true;
100 }
101 return plugins;
102}
103
104static bool pluginComparator(const QCborMap &p1, const QCborMap &p2)
105{
106 const QString prio = QStringLiteral("Priority");
107 if (p1.contains(prio) && !p2.contains(prio))
108 return true;
109 if (!p1.contains(prio) && p2.contains(prio))
110 return false;
111 if (p1.value(prio).isDouble() && !p2.value(prio).isDouble())
112 return true;
113 if (!p1.value(prio).isDouble() && p2.value(prio).isDouble())
114 return false;
115 return (p1.value(prio).toDouble() > p2.value(prio).toDouble());
116}
117
119{
120 QList<QCborMap> list = plugins().values();
121 std::stable_sort(list.begin(), list.end(), pluginComparator);
122 return list;
123}
124
125void QGeoPositionInfoSourcePrivate::loadPluginMetadata(QMultiHash<QString, QCborMap> &plugins)
126{
127 QFactoryLoader *l = loader();
128 QList<QPluginParsedMetaData> meta = l->metaData();
129 for (qsizetype i = 0; i < meta.size(); ++i) {
130 QCborMap obj = meta.at(i).value(QtPluginMetaDataKeys::MetaData).toMap();
131 const QLatin1String testableKey("Testable");
132 if (!obj.value(testableKey).toBool(true)) {
133 static bool inTest = qEnvironmentVariableIsSet("QT_QTESTLIB_RUNNING");
134 if (inTest)
135 continue;
136 }
137 obj.insert(QLatin1String("index"), static_cast<qint64>(i));
138 plugins.insert(obj.value(QStringLiteral("Provider")).toString(), obj);
139 }
140}
141
148{
149 qRegisterMetaType<QGeoPositionInfo>();
150}
151
158
168{
169 Q_D(const QGeoPositionInfoSource);
170 return d->sourceName;
171}
172
185{
188 return false;
189}
190
205
236{
238 d->interval = msec;
239}
240
242{
243 Q_D(const QGeoPositionInfoSource);
244 return d->interval.value();
245}
246
248{
250 return QBindable<int>(&d->interval);
251}
252
277{
279 d->methods.removeBindingUnlessInWrapper();
280 // The supported positioning methods can change during the calls to this
281 // method, so we can't have a simple check like:
282 // if (currentMethods == methods) return;
283 // Instead we need to save the current value and compare it afterwards
284 const auto prevMethods = d->methods.valueBypassingBindings();
285
287 d->methods.setValueBypassingBindings(methods & supportedPositioningMethods());
288 if (d->methods.valueBypassingBindings() == 0) {
289 d->methods.setValueBypassingBindings(supportedPositioningMethods());
290 }
291 } else { // avoid that turned of Location service blocks any changes to d->methods
292 d->methods.setValueBypassingBindings(methods);
293 }
294 if (prevMethods != d->methods.valueBypassingBindings())
295 d->methods.notify();
296}
297
298QGeoPositionInfoSource::PositioningMethods QGeoPositionInfoSource::preferredPositioningMethods() const
299{
300 Q_D(const QGeoPositionInfoSource);
301 return d->methods.value();
302}
303
304QBindable<QGeoPositionInfoSource::PositioningMethods>
306{
308 return QBindable<PositioningMethods>(&d->methods);
309}
310
312{
313 QGeoPositionInfoSource *s = nullptr;
315 if (factory)
316 s = factory->positionInfoSource(parent, parameters);
317 if (s)
318 s->d_func()->sourceName = meta.value(QStringLiteral("Provider")).toString();
319
320 return s;
321}
322
336
350{
351 const QList<QCborMap> plugins = QGeoPositionInfoSourcePrivate::pluginsSorted();
352 for (const QCborMap &obj : plugins) {
353 if (obj.value(QStringLiteral("Position")).isBool()
354 && obj.value(QStringLiteral("Position")).toBool()) {
356 if (source)
357 return source;
358 }
359 }
360 return nullptr;
361}
362
373
385{
387 if (plugins.contains(sourceName))
388 return QGeoPositionInfoSourcePrivate::createSourceReal(plugins.value(sourceName), parameters, parent);
389 return nullptr;
390}
391
397{
398 QStringList plugins;
399 const auto meta = QGeoPositionInfoSourcePrivate::plugins();
400 for (auto it = meta.cbegin(), end = meta.cend(); it != end; ++it) {
401 if (it.value().value(QStringLiteral("Position")).isBool()
402 && it.value().value(QStringLiteral("Position")).toBool()) {
403 plugins << it.key();
404 }
405 }
406
407 return plugins;
408}
409
411 : QObject(dd, parent)
412{
413 qRegisterMetaType<QGeoPositionInfo>();
415 d->interval.setValueBypassingBindings(0);
416 d->methods.setValueBypassingBindings(NoPositioningMethods);
417}
418
604
605#include "moc_qgeopositioninfosource.cpp"
static JNINativeMethod methods[]
\inmodule QtCore\reentrant
Definition qcbormap.h:21
QCborValue value(qint64 key) const
Returns the QCborValue element in this map that corresponds to key key, if there is one.
Definition qcbormap.h:248
QString toString(const QString &defaultValue={}) const
Returns the string value stored in this QCborValue, if it is of the string type.
double toDouble(double defaultValue=0) const
Returns the floating point value stored in this QCborValue, if it is of the Double type.
Definition qcborvalue.h:191
MetaDataList metaData() const
static void loadPluginMetadata(QMultiHash< QString, QCborMap > &list)
static QMultiHash< QString, QCborMap > plugins(bool reload=false)
static QList< QCborMap > pluginsSorted()
static QGeoPositionInfoSourceFactory * loadFactory(const QCborMap &meta)
static QGeoPositionInfoSource * createSourceReal(const QCborMap &meta, const QVariantMap &parameters, QObject *parent)
\inmodule QtPositioning
virtual QVariant backendProperty(const QString &name) const
Returns the value of the backend-specific property named name, if present.
int updateInterval
This property holds the requested interval in milliseconds between each update.
virtual PositioningMethods supportedPositioningMethods() const =0
Returns the positioning methods available to this source.
virtual bool setBackendProperty(const QString &name, const QVariant &value)
Sets the backend-specific property named name to value.
static QGeoPositionInfoSource * createDefaultSource(QObject *parent)
Creates and returns a position source with the given parent that reads from the system's default sour...
QBindable< PositioningMethods > bindablePreferredPositioningMethods()
QBindable< int > bindableUpdateInterval()
virtual void setPreferredPositioningMethods(PositioningMethods methods)
QString sourceName
This property holds the unique name of the position source implementation in use.
static QStringList availableSources()
Returns a list of available source plugins.
virtual ~QGeoPositionInfoSource()
Destroys the position source.
QGeoPositionInfoSource(QObject *parent)
Creates a position source with the specified parent.
static QGeoPositionInfoSource * createSource(const QString &sourceName, QObject *parent)
Creates and returns a position source with the given parent, by loading the plugin named sourceName.
virtual void setUpdateInterval(int msec)
PositioningMethods preferredPositioningMethods
Sets the preferred positioning methods for this source.
iterator end()
Definition qlist.h:626
iterator begin()
Definition qlist.h:625
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
const_iterator cbegin() const noexcept
Definition qset.h:138
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
QPixmap p2
QPixmap p1
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QMap< QString, QVariant > QVariantMap
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static bool pluginComparator(const QCborMap &p1, const QCborMap &p2)
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
GLuint GLuint end
GLuint name
GLsizei GLsizei GLchar * source
GLhandleARB obj
[2]
GLdouble s
[6]
Definition qopenglext.h:235
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_UNUSED(x)
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
QList< int > list
[14]
QItemEditorFactory * factory
char * toString(const MyType &t)
[31]