QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "qgssensorwidget.h"
21
23 : QObject( parent )
24{
25}
26
28{
29 qDeleteAll( mMetadata );
30}
31
33{
34 if ( !mMetadata.isEmpty() )
35 return false;
36
37 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "tcp_socket" ),
38 QObject::tr( "TCP socket sensor" ),
39 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
41 {
42 QgsTcpSocketSensorWidget *widget = new QgsTcpSocketSensorWidget( nullptr );
43 widget->setSensor( sensor );
44 return widget;
45 }, nullptr ) );
46 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "udp_socket" ),
47 QObject::tr( "UDP socket sensor" ),
48 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
50 {
51 QgsUdpSocketSensorWidget *widget = new QgsUdpSocketSensorWidget( nullptr );
52 widget->setSensor( sensor );
53 return widget;
54 }, nullptr ) );
55#if defined( HAVE_QTSERIALPORT )
56 addSensorGuiMetadata( new QgsSensorGuiMetadata( QStringLiteral( "serial_port" ),
57 QObject::tr( "Serial port sensor" ),
58 QgsApplication::getThemeIcon( QStringLiteral( "/mSensor.svg" ) ),
60 {
61 QgsSerialPortSensorWidget *widget = new QgsSerialPortSensorWidget( nullptr );
62 widget->setSensor( sensor );
63 return widget;
64 }, nullptr ) );
65#endif
66 return true;
67}
68
70{
71 return mMetadata.value( type );
72}
73
75{
76 if ( !metadata || mMetadata.contains( metadata->type() ) )
77 return false;
78
79 mMetadata[metadata->type()] = metadata;
80 emit sensorAdded( metadata->type(), metadata->visibleName() );
81 return true;
82}
83
84QgsAbstractSensor *QgsSensorGuiRegistry::createSensor( const QString &type, QObject *parent ) const
85{
86 if ( !mMetadata.contains( type ) )
87 return nullptr;
88
89 std::unique_ptr< QgsAbstractSensor > sensor( mMetadata.value( type )->createSensor( parent ) );
90 if ( sensor )
91 return sensor.release();
92
93 return QgsApplication::sensorRegistry()->createSensor( type, parent );
94}
95
97{
98 if ( !sensor || !mMetadata.contains( sensor->type() ) )
99 return nullptr;
100
101 return mMetadata[sensor->type()]->createSensorWidget( sensor );
102}
103
104QMap<QString, QString> QgsSensorGuiRegistry::sensorTypes() const
105{
106 QMap<QString, QString> types;
107 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
108 {
109 types.insert( it.key(), it.value()->visibleName() );
110 }
111
112 return types;
113}
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.