QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
33{
34 qDeleteAll( mMetadata );
35}
36
38{
39 if ( !mMetadata.isEmpty() )
40 return false;
41
43 u"tcp_socket"_s,
44 QObject::tr( "TCP socket sensor" ),
45 QgsApplication::getThemeIcon( u"/mSensor.svg"_s ),
47 QgsTcpSocketSensorWidget *widget = new QgsTcpSocketSensorWidget( nullptr );
48 widget->setSensor( sensor );
49 return widget;
50 },
51 nullptr
52 ) );
54 u"udp_socket"_s,
55 QObject::tr( "UDP socket sensor" ),
56 QgsApplication::getThemeIcon( u"/mSensor.svg"_s ),
58 QgsUdpSocketSensorWidget *widget = new QgsUdpSocketSensorWidget( nullptr );
59 widget->setSensor( sensor );
60 return widget;
61 },
62 nullptr
63 ) );
64#if defined( HAVE_QTSERIALPORT )
66 u"serial_port"_s,
67 QObject::tr( "Serial port sensor" ),
68 QgsApplication::getThemeIcon( u"/mSensor.svg"_s ),
70 QgsSerialPortSensorWidget *widget = new QgsSerialPortSensorWidget( nullptr );
71 widget->setSensor( sensor );
72 return widget;
73 },
74 nullptr
75 ) );
76#endif
77 return true;
78}
79
81{
82 return mMetadata.value( type );
83}
84
86{
87 if ( !metadata || mMetadata.contains( metadata->type() ) )
88 return false;
89
90 mMetadata[metadata->type()] = metadata;
91 emit sensorAdded( metadata->type(), metadata->visibleName() );
92 return true;
93}
94
95QgsAbstractSensor *QgsSensorGuiRegistry::createSensor( const QString &type, QObject *parent ) const
96{
97 auto it = mMetadata.constFind( type );
98 if ( it == mMetadata.constEnd() )
99 return nullptr;
100
101 std::unique_ptr<QgsAbstractSensor> sensor( it.value()->createSensor( parent ) );
102 if ( sensor )
103 return sensor.release();
104
105 return QgsApplication::sensorRegistry()->createSensor( type, parent );
106}
107
109{
110 if ( !sensor )
111 return nullptr;
112
113 auto it = mMetadata.constFind( sensor->type() );
114 if ( it == mMetadata.constEnd() )
115 return nullptr;
116
117 return it.value()->createSensorWidget( sensor );
118}
119
120QMap<QString, QString> QgsSensorGuiRegistry::sensorTypes() const
121{
122 QMap<QString, QString> types;
123 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
124 {
125 types.insert( it.key(), it.value()->visibleName() );
126 }
127
128 return types;
129}
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.