QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
18#include "qgsapplication.h"
19#include "qgssensorregistry.h"
20
22 : QObject( parent )
23{
24}
25
30
32{
33 const QList<QgsAbstractSensor *> sensors = mSensors;
35 {
36 if ( sensor )
37 {
39 }
40 }
41 mSensors.clear();
42 mSensorsData.clear();
43}
44
45QList<QgsAbstractSensor *> QgsSensorManager::sensors() const
46{
47 return mSensors;
48}
49
51{
52 for ( QgsAbstractSensor *sensor : mSensors )
53 {
54 if ( sensor->id() == id )
55 {
56 return sensor;
57 }
58 }
59
60 return nullptr;
61}
62
64{
65 return mSensorsData.value( name );
66}
67
68QMap<QString, QgsAbstractSensor::SensorData> QgsSensorManager::sensorsData() const
69{
70 return mSensorsData;
71}
72
74{
75 QStringList names;
76 for ( const QgsAbstractSensor *sensor : std::as_const( mSensors ) )
77 {
78 names << sensor->name();
79 }
80 return names;
81}
82
84{
85 if ( !sensor || mSensors.contains( sensor ) )
86 return;
87
88 connect( sensor, &QgsAbstractSensor::nameChanged, this, &QgsSensorManager::handleSensorNameChanged );
89 connect( sensor, &QgsAbstractSensor::statusChanged, this, &QgsSensorManager::handleSensorStatusChanged );
90 connect( sensor, &QgsAbstractSensor::dataChanged, this, &QgsSensorManager::captureSensorData );
91 connect( sensor, &QgsAbstractSensor::errorOccurred, this, &QgsSensorManager::handleSensorErrorOccurred );
92 mSensors << sensor;
93
94 emit sensorAdded( sensor->id() );
95
96 return;
97}
98
99bool QgsSensorManager::removeSensor( const QString &id )
100{
101 for ( QgsAbstractSensor *sensor : mSensors )
102 {
103 if ( sensor->id() == id )
104 {
105 emit sensorAboutToBeRemoved( id );
106 mSensors.removeAll( sensor );
107 mSensorsData.remove( sensor->name() );
109 sensor->deleteLater();
110 emit sensorRemoved( id );
111 return true;
112 }
113 }
114
115 return false;
116}
117
118void QgsSensorManager::handleSensorNameChanged()
119{
120 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
121 if ( !sensor )
122 return;
123
124 emit sensorNameChanged( sensor->id() );
125}
126
127void QgsSensorManager::handleSensorStatusChanged()
128{
129 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
130 if ( !sensor )
131 return;
132
134 {
135 mSensorsData.remove( sensor->name() );
136 }
137
138 emit sensorStatusChanged( sensor->id() );
139}
140
141void QgsSensorManager::captureSensorData()
142{
143 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
144 if ( !sensor )
145 return;
146
147 mSensorsData.insert( sensor->name(), sensor->data() );
148 emit sensorDataCaptured( sensor->id() );
149}
150
151void QgsSensorManager::handleSensorErrorOccurred( const QString & )
152{
153 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
154 if ( !sensor )
155 return;
156
157 emit sensorErrorOccurred( sensor->id() );
158}
159
160bool QgsSensorManager::readXml( const QDomElement &element, const QDomDocument &document )
161{
162 clear();
163
164 QDomElement sensorsElem = element;
165 if ( element.tagName() != QLatin1String( "Sensors" ) )
166 {
167 sensorsElem = element.firstChildElement( QStringLiteral( "Sensors" ) );
168 }
169
170 QDomNodeList sensorNodes = element.elementsByTagName( QStringLiteral( "Sensor" ) );
171 for ( int i = 0; i < sensorNodes.size(); ++i )
172 {
173 const QDomElement sensorElement = sensorNodes.at( i ).toElement();
174 const QString sensorType = sensorElement.attribute( QStringLiteral( "type" ) );
176 if ( !sensor )
177 {
178 continue;
179 }
180
181 sensor->readXml( sensorElement, document );
182 addSensor( sensor );
183 }
184
185 return true;
186}
187
188QDomElement QgsSensorManager::writeXml( QDomDocument &document ) const
189{
190 QDomElement sensorsElem = document.createElement( QStringLiteral( "Sensors" ) );
191
192 for ( const QgsAbstractSensor *sensor : mSensors )
193 {
194 sensor->writeXml( sensorsElem, document );
195 }
196
197 return sensorsElem;
198}
@ 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.