QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgssensorguiregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensorguiregistry.h
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
20#include "moc_qgssensorguiregistry.cpp"
21#include "qgssensorwidget.h"
22
24 : QObject( parent )
25{
26}
27
29{
30 qDeleteAll( mMetadata );
31}
32
34{
35 if ( !mMetadata.isEmpty() )
36 return false;
37
38 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "tcp_socket" ),
39 QObject::tr( "TCP socket sensor" ),
40 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
42 {
43 QgsTcpSocketSensorWidget *widget = new QgsTcpSocketSensorWidget( nullptr );
44 widget->setSensor( sensor );
45 return widget;
46 }, nullptr ) );
47 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "udp_socket" ),
48 QObject::tr( "UDP socket sensor" ),
49 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
51 {
52 QgsUdpSocketSensorWidget *widget = new QgsUdpSocketSensorWidget( nullptr );
53 widget->setSensor( sensor );
54 return widget;
55 }, nullptr ) );
56#if defined( HAVE_QTSERIALPORT )
57 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "serial_port" ),
58 QObject::tr( "Serial port sensor" ),
59 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
61 {
62 QgsSerialPortSensorWidget *widget = new QgsSerialPortSensorWidget( nullptr );
63 widget->setSensor( sensor );
64 return widget;
65 }, nullptr ) );
66#endif
67 return true;
68}
69
71{
72 return mMetadata.value( type );
73}
74
76{
77 if ( !metadata || mMetadata.contains( metadata->type() ) )
78 return false;
79
80 mMetadata[metadata->type()] = metadata;
81 emit sensorAdded( metadata->type(), metadata->visibleName() );
82 return true;
83}
84
85QgsAbstractSensor *QgsSensorGuiRegistry::createSensor( const QString &type, QObject *parent ) const
86{
87 if ( !mMetadata.contains( type ) )
88 return nullptr;
89
90 std::unique_ptr< QgsAbstractSensor > sensor( mMetadata.value( type )->createSensor( parent ) );
91 if ( sensor )
92 return sensor.release();
93
94 return QgsApplication::sensorRegistry()->createSensor( type, parent );
95}
96
98{
99 if ( !sensor || !mMetadata.contains( sensor->type() ) )
100 return nullptr;
101
102 return mMetadata[sensor->type()]->createSensorWidget( sensor );
103}
104
105QMap<QString, QString> QgsSensorGuiRegistry::sensorTypes() const
106{
107 QMap<QString, QString> types;
108 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
109 {
110 types.insert( it.key(), it.value()->visibleName() );
111 }
112
113 return types;
114}
Base class for widgets which allow control over the properties of sensors.
An abstract base class for sensor classes.
virtual QString type() const
Returns the sensor type.
static QgsSensorRegistry * sensorRegistry()
Returns the application's sensor registry, used for sensor types.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Stores GUI metadata about one sensor class.
QString type() const
Returns the unique type code for the sensor class.
QString visibleName() const
Returns a translated, user visible name identifying the corresponding sensor.
Convenience metadata class that uses static functions to handle sensor GUI behavior.
QgsSensorGuiRegistry(QObject *parent=nullptr)
Creates a new empty sensor GUI registry.
QgsSensorAbstractGuiMetadata * 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.
bool populate()
Populates the registry with standard sensor types.
void sensorAdded(const QString &type, const QString &name)
Emitted whenever a new sensor type is added to the registry, with the specified type.
QMap< QString, QString > sensorTypes() const
Returns a list of sensor types handled by the registry.
QgsAbstractSensorWidget * createSensorWidget(QgsAbstractSensor *sensor) const
Creates a new instance of a sensor configuration widget for the specified sensor.
bool addSensorGuiMetadata(QgsSensorAbstractGuiMetadata *metadata)
Registers the GUI metadata for a new sensor type.
QgsAbstractSensor * createSensor(const QString &type, QObject *parent=nullptr) const
Creates a new instance of a sensor given the type.