QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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"
19
20#include "qgssensorwidget.h"
21
22#include <QString>
23
24#include "moc_qgssensorguiregistry.cpp"
25
26using namespace Qt::StringLiterals;
27
29 : QObject( parent )
30{
31}
32
34{
35 qDeleteAll( mMetadata );
36}
37
39{
40 if ( !mMetadata.isEmpty() )
41 return false;
42
43 addSensorGuiMetadata( new QgsSensorGuiMetadata( u"tcp_socket"_s, QObject::tr( "TCP socket sensor" ), QgsApplication::getThemeIcon( u"/mSensor.svg"_s ), []( QgsAbstractSensor *sensor ) -> QgsAbstractSensorWidget * {
44 QgsTcpSocketSensorWidget *widget = new QgsTcpSocketSensorWidget( nullptr );
45 widget->setSensor( sensor );
46 return widget; }, nullptr ) );
47 addSensorGuiMetadata( new QgsSensorGuiMetadata( u"udp_socket"_s, QObject::tr( "UDP socket sensor" ), QgsApplication::getThemeIcon( u"/mSensor.svg"_s ), []( QgsAbstractSensor *sensor ) -> QgsAbstractSensorWidget * {
48 QgsUdpSocketSensorWidget *widget = new QgsUdpSocketSensorWidget( nullptr );
49 widget->setSensor( sensor );
50 return widget; }, nullptr ) );
51#if defined( HAVE_QTSERIALPORT )
52 addSensorGuiMetadata( new QgsSensorGuiMetadata( u"serial_port"_s, QObject::tr( "Serial port sensor" ), QgsApplication::getThemeIcon( u"/mSensor.svg"_s ), []( QgsAbstractSensor *sensor ) -> QgsAbstractSensorWidget * {
53 QgsSerialPortSensorWidget *widget = new QgsSerialPortSensorWidget( nullptr );
54 widget->setSensor( sensor );
55 return widget; }, nullptr ) );
56#endif
57 return true;
58}
59
61{
62 return mMetadata.value( type );
63}
64
66{
67 if ( !metadata || mMetadata.contains( metadata->type() ) )
68 return false;
69
70 mMetadata[metadata->type()] = metadata;
71 emit sensorAdded( metadata->type(), metadata->visibleName() );
72 return true;
73}
74
75QgsAbstractSensor *QgsSensorGuiRegistry::createSensor( const QString &type, QObject *parent ) const
76{
77 auto it = mMetadata.constFind( type );
78 if ( it == mMetadata.constEnd() )
79 return nullptr;
80
81 std::unique_ptr<QgsAbstractSensor> sensor( it.value()->createSensor( parent ) );
82 if ( sensor )
83 return sensor.release();
84
85 return QgsApplication::sensorRegistry()->createSensor( type, parent );
86}
87
89{
90 if ( !sensor )
91 return nullptr;
92
93 auto it = mMetadata.constFind( sensor->type() );
94 if ( it == mMetadata.constEnd() )
95 return nullptr;
96
97 return it.value()->createSensorWidget( sensor );
98}
99
100QMap<QString, QString> QgsSensorGuiRegistry::sensorTypes() const
101{
102 QMap<QString, QString> types;
103 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
104 {
105 types.insert( it.key(), it.value()->visibleName() );
106 }
107
108 return types;
109}
Base class for widgets which allow control over the properties of sensors.
An abstract base class for sensors.
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.