QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgssensorregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensorregistry.cpp
3 ------------------------
4 begin : March 2023
5 copyright : (C) 2023 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsconfig.h"
18#include "qgssensorregistry.h"
19
20#include "qgsiodevicesensor.h"
21#include "qgsproject.h"
22#include "qgssensormanager.h"
23
24#include "moc_qgssensorregistry.cpp"
25
27 : QObject( parent )
28{
29}
30
32{
33 qDeleteAll( mMetadata );
34}
35
37{
38 if ( !mMetadata.isEmpty() )
39 return false;
40
41 addSensorType( new QgsSensorMetadata( QLatin1String( "tcp_socket" ), QObject::tr( "TCP socket sensor" ), QgsTcpSocketSensor::create ) );
42 addSensorType( new QgsSensorMetadata( QLatin1String( "udp_socket" ), QObject::tr( "UDP socket sensor" ), QgsUdpSocketSensor::create ) );
43#if defined( HAVE_QTSERIALPORT )
44 addSensorType( new QgsSensorMetadata( QLatin1String( "serial_port" ), QObject::tr( "Serial port sensor" ), QgsSerialPortSensor::create ) );
45#endif
46
47 return true;
48}
49
51{
52 return mMetadata.value( type );
53}
54
56{
57 if ( !metadata || mMetadata.contains( metadata->type() ) )
58 return false;
59
60 mMetadata[metadata->type()] = metadata;
61 emit sensorAdded( metadata->type(), metadata->visibleName() );
62 return true;
63}
64
65bool QgsSensorRegistry::removeSensorType( const QString &type )
66{
67 if ( !mMetadata.contains( type ) )
68 return false;
69
70 // remove any sensor of this type in the project sensor manager
71 const QList<QgsAbstractSensor *> sensors = QgsProject::instance()->sensorManager()->sensors(); // skip-keyword-check
72 for ( QgsAbstractSensor *sensor : sensors )
73 {
74 if ( sensor->type() == type )
75 {
76 QgsProject::instance()->sensorManager()->removeSensor( sensor->id() ); // skip-keyword-check
77 }
78 }
79
80 delete mMetadata.take( type );
81 return true;
82}
83
84QgsAbstractSensor *QgsSensorRegistry::createSensor( const QString &type, QObject *parent ) const
85{
86 if ( !mMetadata.contains( type ) )
87 return nullptr;
88
89 return mMetadata[type]->createSensor( parent );
90}
91
92QMap<QString, QString> QgsSensorRegistry::sensorTypes() const
93{
94 QMap<QString, QString> types;
95 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
96 {
97 types.insert( it.key(), it.value()->visibleName() );
98 }
99
100 return types;
101}
An abstract base class for sensors.
static QgsProject * instance()
Returns the QgsProject singleton instance.
const QgsSensorManager * sensorManager() const
Returns the project's sensor manager, which manages sensors within the project.
Stores metadata about a sensor class.
QString visibleName() const
Returns a translated, user visible name for the sensor class.
QString type() const
Returns the unique type code for the sensor class.
bool removeSensor(const QString &id)
Removes a registered sensor matching a given id.
QList< QgsAbstractSensor * > sensors() const
Returns a list of pointers to all registered sensors.
Convenience metadata class that uses static functions to create sensors and their configuration widge...
bool addSensorType(QgsSensorAbstractMetadata *metadata)
Registers a new sensor type.
bool populate()
Populates the registry with standard sensor types.
QgsSensorRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
void sensorAdded(const QString &type, const QString &name)
Emitted whenever a new sensor type is added to the registry, with the specified type and visible name...
bool removeSensorType(const QString &type)
Removes a new a sensor type from the registry.
QgsSensorAbstractMetadata * sensorMetadata(const QString &type) const
Returns the metadata for the specified sensor type.
QgsAbstractSensor * createSensor(const QString &type, QObject *parent=nullptr) const
Creates a new instance of a sensor given the type.
QMap< QString, QString > sensorTypes() const
Returns a map of available sensor types to translated name.
static QgsTcpSocketSensor * create(QObject *parent)
Returns a new TCP socket sensor.
static QgsUdpSocketSensor * create(QObject *parent)
Returns a new UDP socket sensor.