QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgssensormanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensormanager.cpp
3 ------------------
4 Date : March 2023
5 Copyright : (C) 2023 Mathieu Pellerin
6 Email : mathieu at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgssensormanager.h"
17#include "moc_qgssensormanager.cpp"
18
19#include "qgsapplication.h"
20#include "qgssensorregistry.h"
21
23 : QObject( parent )
24{
25}
26
31
33{
34 const QList<QgsAbstractSensor *> sensors = mSensors;
36 {
37 if ( sensor )
38 {
40 }
41 }
42 mSensors.clear();
43 mSensorsData.clear();
44}
45
46QList<QgsAbstractSensor *> QgsSensorManager::sensors() const
47{
48 return mSensors;
49}
50
52{
53 for ( QgsAbstractSensor *sensor : mSensors )
54 {
55 if ( sensor->id() == id )
56 {
57 return sensor;
58 }
59 }
60
61 return nullptr;
62}
63
65{
66 return mSensorsData.value( name );
67}
68
69QMap<QString, QgsAbstractSensor::SensorData> QgsSensorManager::sensorsData() const
70{
71 return mSensorsData;
72}
73
75{
76 QStringList names;
77 for ( const QgsAbstractSensor *sensor : std::as_const( mSensors ) )
78 {
79 names << sensor->name();
80 }
81 return names;
82}
83
85{
86 if ( !sensor || mSensors.contains( sensor ) )
87 return;
88
89 connect( sensor, &QgsAbstractSensor::nameChanged, this, &QgsSensorManager::handleSensorNameChanged );
90 connect( sensor, &QgsAbstractSensor::statusChanged, this, &QgsSensorManager::handleSensorStatusChanged );
91 connect( sensor, &QgsAbstractSensor::dataChanged, this, &QgsSensorManager::captureSensorData );
92 connect( sensor, &QgsAbstractSensor::errorOccurred, this, &QgsSensorManager::handleSensorErrorOccurred );
93 mSensors << sensor;
94
95 emit sensorAdded( sensor->id() );
96
97 return;
98}
99
100bool QgsSensorManager::removeSensor( const QString &id )
101{
102 for ( QgsAbstractSensor *sensor : mSensors )
103 {
104 if ( sensor->id() == id )
105 {
106 emit sensorAboutToBeRemoved( id );
107 mSensors.removeAll( sensor );
108 mSensorsData.remove( sensor->name() );
110 sensor->deleteLater();
111 emit sensorRemoved( id );
112 return true;
113 }
114 }
115
116 return false;
117}
118
119void QgsSensorManager::handleSensorNameChanged()
120{
121 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
122 if ( !sensor )
123 return;
124
125 emit sensorNameChanged( sensor->id() );
126}
127
128void QgsSensorManager::handleSensorStatusChanged()
129{
130 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
131 if ( !sensor )
132 return;
133
135 {
136 mSensorsData.remove( sensor->name() );
137 }
138
139 emit sensorStatusChanged( sensor->id() );
140}
141
142void QgsSensorManager::captureSensorData()
143{
144 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
145 if ( !sensor )
146 return;
147
148 mSensorsData.insert( sensor->name(), sensor->data() );
149 emit sensorDataCaptured( sensor->id() );
150}
151
152void QgsSensorManager::handleSensorErrorOccurred( const QString & )
153{
154 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
155 if ( !sensor )
156 return;
157
158 emit sensorErrorOccurred( sensor->id() );
159}
160
161bool QgsSensorManager::readXml( const QDomElement &element, const QDomDocument &document )
162{
163 clear();
164
165 QDomElement sensorsElem = element;
166 if ( element.tagName() != QLatin1String( "Sensors" ) )
167 {
168 sensorsElem = element.firstChildElement( QStringLiteral( "Sensors" ) );
169 }
170
171 QDomNodeList sensorNodes = element.elementsByTagName( QStringLiteral( "Sensor" ) );
172 for ( int i = 0; i < sensorNodes.size(); ++i )
173 {
174 const QDomElement sensorElement = sensorNodes.at( i ).toElement();
175 const QString sensorType = sensorElement.attribute( QStringLiteral( "type" ) );
177 if ( !sensor )
178 {
179 continue;
180 }
181
182 sensor->readXml( sensorElement, document );
183 addSensor( sensor );
184 }
185
186 return true;
187}
188
189QDomElement QgsSensorManager::writeXml( QDomDocument &document ) const
190{
191 QDomElement sensorsElem = document.createElement( QStringLiteral( "Sensors" ) );
192
193 for ( const QgsAbstractSensor *sensor : mSensors )
194 {
195 sensor->writeXml( sensorsElem, document );
196 }
197
198 return sensorsElem;
199}
@ Disconnected
Device is disconnected.
An abstract base class for sensor classes.
QString id() const
Returns the sensor ID.
Qgis::DeviceConnectionStatus status() const
Returns the current sensor status.
QgsAbstractSensor::SensorData data() const
Returns the latest captured data from the sensor.
void statusChanged()
Emitted when the sensor status has changed.
void disconnectSensor()
Disconnects the sensor from its source.
QString name() const
Returns the user-friendly name identifying the sensor.
void errorOccurred(const QString &errorString)
Emitted when an error has occurred. The errorString describes the error.
void nameChanged()
Emitted when the sensor name has changed.
bool readXml(const QDomElement &element, const QDomDocument &document)
Restores generic sensor details from a DOM element.
void dataChanged()
Emitted when the captured sensor data has changed.
bool writeXml(QDomElement &parentElement, QDomDocument &document) const
Write generic sensor properties into a DOM element.
static QgsSensorRegistry * sensorRegistry()
Returns the application's sensor registry, used for sensor types.
void addSensor(QgsAbstractSensor *sensor)
Registers a new sensor.
void sensorRemoved(const QString &id)
Emitted when a sensor has been removed.
bool removeSensor(const QString &id)
Removes a registered sensor matching a given id.
void sensorErrorOccurred(const QString &id)
Emitted when a sensor error has occurred.
QgsSensorManager(QObject *parent=nullptr)
Constructor for QgsSensorManager, with the specified parent object.
QMap< QString, QgsAbstractSensor::SensorData > sensorsData() const
Returns the last captured data of all registered sensors.
void sensorNameChanged(const QString &id)
Emitted when a sensor name has changed.
QList< QgsAbstractSensor * > sensors() const
Returns a list of pointers to all registered sensors.
void sensorDataCaptured(const QString &id)
Emitted when newly captured data from a sensor has occurred.
QDomElement writeXml(QDomDocument &document) const
Returns a DOM element representing the state of the manager.
QgsAbstractSensor * sensor(const QString &id) const
Returns a registered sensor pointer matching a given id.
QStringList sensorNames() const
Returns a list of registered sensor names.
QgsAbstractSensor::SensorData sensorData(const QString &name) const
Returns the last captured data from a registered sensor matching a given name.
void clear()
Deregisters and removes all sensors from the manager.
void sensorAdded(const QString &id)
Emitted when a sensor has been registered.
~QgsSensorManager() override
void sensorAboutToBeRemoved(const QString &id)
Emitted when a sensor is about to be removed.
void sensorStatusChanged(const QString &id)
Emitted when a sensor status has changed.
bool readXml(const QDomElement &element, const QDomDocument &document)
Reads the manager's state from a DOM element, restoring all sensors present in the XML document.
QgsAbstractSensor * createSensor(const QString &type, QObject *parent=nullptr) const
Creates a new instance of a sensor given the type.
Contains details of a sensor data capture.