QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsgpsconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgpsconnection.cpp - description
3 --------------------
4 begin : November 30th, 2009
5 copyright : (C) 2009 by Marco Hugentobler
6 email : marco at hugis dot net
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsgpsconnection.h"
21#include "qgssettingstree.h"
22
23
24#include <QIODevice>
25
26
28
29const QgsSettingsEntryEnumFlag<Qt::TimeSpec> *QgsGpsConnection::settingsGpsTimeStampSpecification = new QgsSettingsEntryEnumFlag<Qt::TimeSpec>( QStringLiteral( "timestamp-time-spec" ), QgsSettingsTree::sTreeGps, Qt::TimeSpec::LocalTime, QStringLiteral( "GPS time stamp specification" ) ) SIP_SKIP;
30
31const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdHostName = new QgsSettingsEntryString( QStringLiteral( "gpsd-host-name" ), QgsSettingsTree::sTreeGps, QString(), QStringLiteral( "GPSD connection host name" ) ) SIP_SKIP;
32
33const QgsSettingsEntryInteger *QgsGpsConnection::settingsGpsdPortNumber = new QgsSettingsEntryInteger( QStringLiteral( "gpsd-port" ), QgsSettingsTree::sTreeGps, 2947, QStringLiteral( "GPSD port number" ) ) SIP_SKIP;
34
35const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdDeviceName = new QgsSettingsEntryString( QStringLiteral( "gpsd-device-name" ), QgsSettingsTree::sTreeGps, QString(), QStringLiteral( "GPSD connection device name" ) ) SIP_SKIP;
36
37const QgsSettingsEntryString *QgsGpsConnection::settingsGpsSerialDevice = new QgsSettingsEntryString( QStringLiteral( "gpsd-serial-device" ), QgsSettingsTree::sTreeGps, QString(), QStringLiteral( "GPS serial device name" ) ) SIP_SKIP;
38
39const QgsSettingsEntryInteger *QgsGpsConnection::settingGpsAcquisitionInterval = new QgsSettingsEntryInteger( QStringLiteral( "acquisition-interval" ), QgsSettingsTree::sTreeGps, 0, QStringLiteral( "GPS track point acquisition interval" ) ) SIP_SKIP;
40
41const QgsSettingsEntryDouble *QgsGpsConnection::settingGpsDistanceThreshold = new QgsSettingsEntryDouble( QStringLiteral( "distance-threshold" ), QgsSettingsTree::sTreeGps, 0, QStringLiteral( "GPS track point distance threshold" ) ) SIP_SKIP;
42
43const QgsSettingsEntryBool *QgsGpsConnection::settingGpsBearingFromTravelDirection = new QgsSettingsEntryBool( QStringLiteral( "calculate-bearing-from-travel" ), QgsSettingsTree::sTreeGps, false, QStringLiteral( "Calculate GPS bearing from travel direction" ) ) SIP_SKIP;
44
45const QgsSettingsEntryBool *QgsGpsConnection::settingGpsApplyLeapSecondsCorrection = new QgsSettingsEntryBool( QStringLiteral( "apply-leap-seconds-correction" ), QgsSettingsTree::sTreeGps, true, QStringLiteral( "Whether leap seconds corrections should be applied to GPS timestamps" ) ) SIP_SKIP;
46
47const QgsSettingsEntryInteger *QgsGpsConnection::settingGpsLeapSeconds = new QgsSettingsEntryInteger( QStringLiteral( "leap-seconds" ), QgsSettingsTree::sTreeGps, 18, QStringLiteral( "Leap seconds correction amount (in seconds)" ) ) SIP_SKIP;
48
49const QgsSettingsEntryString *QgsGpsConnection::settingsGpsTimeStampTimeZone = new QgsSettingsEntryString( QStringLiteral( "timestamp-time-zone" ), QgsSettingsTree::sTreeGps, QString(), QStringLiteral( "GPS time stamp time zone" ) ) SIP_SKIP;
50
51const QgsSettingsEntryInteger *QgsGpsConnection::settingsGpsTimeStampOffsetFromUtc = new QgsSettingsEntryInteger( QStringLiteral( "timestamp-offset-from-utc" ), QgsSettingsTree::sTreeGps, 0, QStringLiteral( "GPS time stamp offset from UTC (in seconds)" ) ) SIP_SKIP;
52
54 : QObject( nullptr )
55 , mSource( dev )
56{
57 if ( mSource )
58 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
59
60 QObject::connect( this, &QgsGpsConnection::stateChanged, this, &QgsGpsConnection::onStateChanged );
61}
62
64{
65 cleanupSource();
66}
67
69{
70 if ( !mSource )
71 {
72 return false;
73 }
74
75 const bool connected = mSource->open( QIODevice::ReadWrite | QIODevice::Unbuffered );
76 if ( connected )
77 {
79 }
80 return connected;
81}
82
84{
85 if ( !mSource )
86 {
87 return false;
88 }
89
90 mSource->close();
91
92 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
93 {
94 mLastFixStatus = Qgis::GpsFixStatus::NoData;
95 emit fixStatusChanged( mLastFixStatus );
96 }
97
98 return true;
99}
100
101void QgsGpsConnection::cleanupSource()
102{
103 if ( mSource )
104 {
105 mSource->close();
106 }
107 mSource.reset();
108
109 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
110 {
111 mLastFixStatus = Qgis::GpsFixStatus::NoData;
112 emit fixStatusChanged( mLastFixStatus );
113 }
114}
115
116void QgsGpsConnection::setSource( QIODevice *source )
117{
118 cleanupSource();
119 mSource.reset( source );
120 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
121
122 clearLastGPSInformation();
123}
124
125void QgsGpsConnection::onStateChanged( const QgsGpsInformation &info )
126{
127 if ( info.isValid() )
128 {
129 const QgsPoint oldPosition = mLastLocation;
130 mLastLocation = QgsPoint( info.longitude, info.latitude, info.elevation );
131 if ( mLastLocation != oldPosition )
132 {
133 emit positionChanged( mLastLocation );
134 }
135 }
136
138 Qgis::GpsFixStatus bestFix = info.bestFixStatus( bestFixConstellation );
139 if ( bestFix != mLastFixStatus )
140 {
141 mLastFixStatus = bestFix;
142 emit fixStatusChanged( mLastFixStatus );
143 }
144}
145
146void QgsGpsConnection::clearLastGPSInformation()
147{
149}
GnssConstellation
GNSS constellation.
Definition: qgis.h:1142
@ Unknown
Unknown/other system.
GpsFixStatus
GPS fix status.
Definition: qgis.h:1127
@ NoData
No fix data available.
@ Automatic
Automatically detected GPS device connection.
Abstract base class for connection to a GPS device.
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...)
virtual void parseData()=0
Parse available data source content.
void setSource(QIODevice *source)
Sets the GPS source. The class takes ownership of the device class.
~QgsGpsConnection() override
static const QgsSettingsEntryEnumFlag< Qgis::GpsConnectionType > * settingsGpsConnectionType
Settings entry GPS connection type.
bool connect()
Opens connection to device.
bool close()
Closes connection to device.
void positionChanged(const QgsPoint &point)
Emitted when the GPS position changes.
std::unique_ptr< QIODevice > mSource
Data source (e.g. serial device, socket, file,...)
void fixStatusChanged(Qgis::GpsFixStatus status)
Emitted when the GPS device fix status is changed.
Status mStatus
Connection status.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
Encapsulates information relating to a GPS position fix.
bool isValid() const
Returns whether the connection information is valid.
double latitude
Latitude in decimal degrees, using the WGS84 datum.
double longitude
Longitude in decimal degrees, using the WGS84 datum.
double elevation
Altitude (in meters) above or below the mean sea level.
Qgis::GpsFixStatus bestFixStatus(Qgis::GnssConstellation &constellation) const
Returns the best fix status and corresponding constellation.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
A boolean settings entry.
A double settings entry.
An integer settings entry.
A string settings entry.
QgsSettingsTree holds the tree structure for the settings in QGIS core.
static QgsSettingsTreeNode * sTreeGps
#define SIP_SKIP
Definition: qgis_sip.h:126