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