QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
19#include "qgssensorregistry.h"
20#include "moc_qgssensorregistry.cpp"
21#include "qgsiodevicesensor.h"
22#include "qgsproject.h"
23#include "qgssensormanager.h"
24
26 : QObject( parent )
27{
28}
29
31{
32 qDeleteAll( mMetadata );
33}
34
36{
37 if ( !mMetadata.isEmpty() )
38 return false;
39
40 addSensorType( new QgsSensorMetadata( QLatin1String( "tcp_socket" ), QObject::tr( "TCP socket sensor" ), QgsTcpSocketSensor::create ) );
41 addSensorType( new QgsSensorMetadata( QLatin1String( "udp_socket" ), QObject::tr( "UDP socket sensor" ), QgsUdpSocketSensor::create ) );
42#if defined( HAVE_QTSERIALPORT )
43 addSensorType( new QgsSensorMetadata( QLatin1String( "serial_port" ), QObject::tr( "Serial port sensor" ), QgsSerialPortSensor::create ) );
44#endif
45
46 return true;
47}
48
50{
51 return mMetadata.value( type );
52}
53
55{
56 if ( !metadata || mMetadata.contains( metadata->type() ) )
57 return false;
58
59 mMetadata[metadata->type()] = metadata;
60 emit sensorAdded( metadata->type(), metadata->visibleName() );
61 return true;
62}
63
64bool QgsSensorRegistry::removeSensorType( const QString &type )
65{
66 if ( !mMetadata.contains( type ) )
67 return false;
68
69 // remove any sensor of this type in the project sensor manager
70 const QList<QgsAbstractSensor *> sensors = QgsProject::instance()->sensorManager()->sensors(); // skip-keyword-check
71 for ( QgsAbstractSensor *sensor : sensors )
72 {
73 if ( sensor->type() == type )
74 {
75 QgsProject::instance()->sensorManager()->removeSensor( sensor->id() ); // skip-keyword-check
76 }
77 }
78
79 delete mMetadata.take( type );
80 return true;
81}
82
83QgsAbstractSensor *QgsSensorRegistry::createSensor( const QString &type, QObject *parent ) const
84{
85 if ( !mMetadata.contains( type ) )
86 return nullptr;
87
88 return mMetadata[type]->createSensor( parent );
89}
90
91QMap<QString, QString> QgsSensorRegistry::sensorTypes() const
92{
93 QMap<QString, QString> types;
94 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
95 {
96 types.insert( it.key(), it.value()->visibleName() );
97 }
98
99 return types;
100}
An abstract base class for sensor classes.
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.