QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
21#include <QString>
22
23#include "moc_qgssensormanager.cpp"
24
25using namespace Qt::StringLiterals;
26
28 : QObject( parent )
29{}
30
35
37{
38 const QList<QgsAbstractSensor *> sensors = mSensors;
40 {
41 if ( sensor )
42 {
43 removeSensor( sensor->id() );
44 }
45 }
46 mSensors.clear();
47 mSensorsData.clear();
48}
49
50QList<QgsAbstractSensor *> QgsSensorManager::sensors() const
51{
52 return mSensors;
53}
54
56{
57 for ( QgsAbstractSensor *sensor : mSensors )
58 {
59 if ( sensor->id() == id )
60 {
61 return sensor;
62 }
63 }
64
65 return nullptr;
66}
67
69{
70 return mSensorsData.value( name );
71}
72
73QMap<QString, QgsAbstractSensor::SensorData> QgsSensorManager::sensorsData() const
74{
75 return mSensorsData;
76}
77
79{
80 QStringList names;
81 for ( const QgsAbstractSensor *sensor : std::as_const( mSensors ) )
82 {
83 names << sensor->name();
84 }
85 return names;
86}
87
89{
90 if ( !sensor || mSensors.contains( sensor ) )
91 return;
92
93 connect( sensor, &QgsAbstractSensor::nameChanged, this, &QgsSensorManager::handleSensorNameChanged );
94 connect( sensor, &QgsAbstractSensor::statusChanged, this, &QgsSensorManager::handleSensorStatusChanged );
95 connect( sensor, &QgsAbstractSensor::dataChanged, this, &QgsSensorManager::captureSensorData );
96 connect( sensor, &QgsAbstractSensor::errorOccurred, this, &QgsSensorManager::handleSensorErrorOccurred );
97 mSensors << sensor;
98
99 emit sensorAdded( sensor->id() );
100
101 return;
102}
103
104bool QgsSensorManager::removeSensor( const QString &id )
105{
106 for ( QgsAbstractSensor *sensor : mSensors )
107 {
108 if ( sensor->id() == id )
109 {
110 emit sensorAboutToBeRemoved( id );
111 mSensors.removeAll( sensor );
112 mSensorsData.remove( sensor->name() );
113 sensor->disconnectSensor();
114 sensor->deleteLater();
115 emit sensorRemoved( id );
116 return true;
117 }
118 }
119
120 return false;
121}
122
123void QgsSensorManager::handleSensorNameChanged()
124{
125 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
126 if ( !sensor )
127 return;
128
129 emit sensorNameChanged( sensor->id() );
130}
131
132void QgsSensorManager::handleSensorStatusChanged()
133{
134 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
135 if ( !sensor )
136 return;
137
139 {
140 mSensorsData.remove( sensor->name() );
141 }
142
143 emit sensorStatusChanged( sensor->id() );
144}
145
146void QgsSensorManager::captureSensorData()
147{
148 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
149 if ( !sensor )
150 return;
151
152 mSensorsData.insert( sensor->name(), sensor->data() );
153 emit sensorDataCaptured( sensor->id() );
154}
155
156void QgsSensorManager::handleSensorErrorOccurred( const QString & )
157{
158 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
159 if ( !sensor )
160 return;
161
162 emit sensorErrorOccurred( sensor->id() );
163}
164
165bool QgsSensorManager::readXml( const QDomElement &element, const QDomDocument &document )
166{
167 clear();
168
169 QDomElement sensorsElem = element;
170 if ( element.tagName() != "Sensors"_L1 )
171 {
172 sensorsElem = element.firstChildElement( u"Sensors"_s );
173 }
174
175 QDomNodeList sensorNodes = element.elementsByTagName( u"Sensor"_s );
176 for ( int i = 0; i < sensorNodes.size(); ++i )
177 {
178 const QDomElement sensorElement = sensorNodes.at( i ).toElement();
179 const QString sensorType = sensorElement.attribute( u"type"_s );
181 if ( !sensor )
182 {
183 continue;
184 }
185
186 sensor->readXml( sensorElement, document );
187 addSensor( sensor );
188 }
189
190 return true;
191}
192
193QDomElement QgsSensorManager::writeXml( QDomDocument &document ) const
194{
195 QDomElement sensorsElem = document.createElement( u"Sensors"_s );
196
197 for ( const QgsAbstractSensor *sensor : mSensors )
198 {
199 sensor->writeXml( sensorsElem, document );
200 }
201
202 return sensorsElem;
203}
@ Disconnected
Device is disconnected.
Definition qgis.h:1954
An abstract base class for sensors.
QString id() const
Returns the sensor ID.
void statusChanged()
Emitted when the sensor status has changed.
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.
void dataChanged()
Emitted when the captured sensor data has changed.
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.