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