1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtPositioning/qgeopositioninfosource.h>
30#include <QtPositioning/qgeopositioninfosourcefactory.h>
31#include <QObject>
32#include <QtPlugin>
33#include <QTimer>
34
35QT_USE_NAMESPACE
36
37class DummySource : public QGeoPositionInfoSource
38{
39 Q_OBJECT
40
41public:
42 DummySource(QObject *parent = nullptr);
43 ~DummySource();
44
45 void startUpdates();
46 void stopUpdates();
47 void requestUpdate(int timeout=5000);
48
49 QGeoPositionInfo lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const;
50 PositioningMethods supportedPositioningMethods() const;
51
52 void setUpdateInterval(int msec);
53 int minimumUpdateInterval() const;
54 Error error() const;
55
56private:
57 QTimer *timer;
58 QTimer *timeoutTimer;
59 QTimer *singleTimer;
60 QGeoPositionInfo lastPosition;
61 QDateTime lastUpdateTime;
62
63private slots:
64 void updatePosition();
65 void doTimeout();
66};
67
68DummySource::DummySource(QObject *parent) :
69 QGeoPositionInfoSource(parent),
70 timer(new QTimer(this)),
71 timeoutTimer(new QTimer(this)),
72 singleTimer(new QTimer(this)),
73 lastPosition(QGeoCoordinate(0,0), QDateTime::currentDateTime())
74{
75 timer->setInterval(1000);
76 connect(sender: timer, SIGNAL(timeout()),
77 receiver: this, SLOT(updatePosition()));
78 connect(sender: singleTimer, SIGNAL(timeout()),
79 receiver: this, SLOT(updatePosition()));
80 connect(sender: timeoutTimer, SIGNAL(timeout()),
81 receiver: this, SLOT(doTimeout()));
82}
83
84QGeoPositionInfoSource::Error DummySource::error() const
85{
86 return QGeoPositionInfoSource::NoError;
87}
88
89
90void DummySource::setUpdateInterval(int msec)
91{
92 if (msec == 0) {
93 timer->setInterval(1000);
94 } else if (msec < 1000) {
95 msec = 1000;
96 timer->setInterval(msec);
97 } else {
98 timer->setInterval(msec);
99 }
100
101 QGeoPositionInfoSource::setUpdateInterval(msec);
102}
103
104int DummySource::minimumUpdateInterval() const
105{
106 return 1000;
107}
108
109QGeoPositionInfo DummySource::lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const
110{
111 Q_UNUSED(fromSatellitePositioningMethodsOnly);
112 return lastPosition;
113}
114
115QGeoPositionInfoSource::PositioningMethods DummySource::supportedPositioningMethods() const
116{
117 return QGeoPositionInfoSource::AllPositioningMethods;
118}
119
120void DummySource::startUpdates()
121{
122 timer->start();
123}
124
125void DummySource::stopUpdates()
126{
127 timer->stop();
128}
129
130void DummySource::requestUpdate(int timeout)
131{
132 if (timeout == 0)
133 timeout = 5000;
134 if (timeout < 0)
135 timeout = 0;
136
137 timeoutTimer->setInterval(timeout);
138 timeoutTimer->start();
139
140 if (timer->isActive()) {
141 timer->stop();
142 timer->start();
143 }
144
145 singleTimer->setInterval(1000);
146 singleTimer->start();
147}
148
149DummySource::~DummySource()
150{}
151
152void DummySource::updatePosition()
153{
154 timeoutTimer->stop();
155 singleTimer->stop();
156
157 const QDateTime now = QDateTime::currentDateTime();
158
159 QGeoCoordinate coord(lastPosition.coordinate().latitude() + 0.1,
160 lastPosition.coordinate().longitude() + 0.1);
161
162 QGeoPositionInfo info(coord, now);
163 info.setAttribute(attribute: QGeoPositionInfo::Direction, value: lastPosition.coordinate().azimuthTo(other: coord));
164 if (lastUpdateTime.isValid()) {
165 double speed = lastPosition.coordinate().distanceTo(other: coord) / lastUpdateTime.msecsTo(now);
166 info.setAttribute(attribute: QGeoPositionInfo::GroundSpeed, value: 1000 * speed);
167 }
168
169 lastUpdateTime = now;
170 lastPosition = info;
171 emit positionUpdated(update: info);
172}
173
174void DummySource::doTimeout()
175{
176 timeoutTimer->stop();
177 singleTimer->stop();
178 emit updateTimeout();
179}
180
181
182class QGeoPositionInfoSourceFactoryTestV1 : public QObject, public QGeoPositionInfoSourceFactory
183{
184 Q_OBJECT
185 Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/5.0"
186 FILE "plugin.json")
187 Q_INTERFACES(QGeoPositionInfoSourceFactory)
188
189public:
190 QGeoPositionInfoSource *positionInfoSource(QObject *parent);
191 QGeoSatelliteInfoSource *satelliteInfoSource(QObject *parent);
192 QGeoAreaMonitorSource *areaMonitor(QObject *parent);
193};
194
195QGeoPositionInfoSource *QGeoPositionInfoSourceFactoryTestV1::positionInfoSource(QObject *parent)
196{
197 return new DummySource(parent);
198}
199
200QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactoryTestV1::satelliteInfoSource(QObject *parent)
201{
202 Q_UNUSED(parent);
203 return nullptr;
204}
205
206QGeoAreaMonitorSource *QGeoPositionInfoSourceFactoryTestV1::areaMonitor(QObject* parent)
207{
208 Q_UNUSED(parent);
209 return nullptr;
210}
211
212#include "plugin.moc"
213

source code of qtlocation/tests/auto/positionpluginV1/plugin.cpp