QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgssensorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensorwidget.cpp
3 ---------------------
4 begin : March 2023
5 copyright : (C) 2023 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsconfig.h"
17
18#include "qgssensorwidget.h"
19#include "qgsiodevicesensor.h"
20
21#if defined( HAVE_QTSERIALPORT )
22#include <QSerialPort>
23#include <QSerialPortInfo>
24#endif
25
27 : QWidget( parent )
28{
29}
30
31// ------------------------
32
34
35QgsTcpSocketSensorWidget::QgsTcpSocketSensorWidget( QWidget *parent )
36 : QgsAbstractSensorWidget( parent )
37{
38 setupUi( this );
39
40 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
41 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
42}
43
44QgsAbstractSensor *QgsTcpSocketSensorWidget::createSensor()
45{
47 s->setHostName( mHostNameLineEdit->text() );
48 s->setPort( mPortSpinBox->value() );
49 return s;
50}
51
52bool QgsTcpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
53{
54 QgsTcpSocketSensor *s = dynamic_cast<QgsTcpSocketSensor *>( sensor );
55 if ( !s )
56 return false;
57
58 s->setHostName( mHostNameLineEdit->text() );
59 s->setPort( mPortSpinBox->value() );
60
61 return true;
62}
63
64bool QgsTcpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
65{
66 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
67 if ( ts )
68 {
69 mHostNameLineEdit->setText( ts->hostName() );
70 mPortSpinBox->setValue( ts->port() );
71 return true;
72 }
73
74 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
75 if ( us )
76 {
77 mHostNameLineEdit->setText( us->hostName() );
78 mPortSpinBox->setValue( us->port() );
79 return true;
80 }
81
82 return false;
83}
84
85// ------------------------
86
87QgsUdpSocketSensorWidget::QgsUdpSocketSensorWidget( QWidget *parent )
88 : QgsAbstractSensorWidget( parent )
89{
90 setupUi( this );
91
92 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
93 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
94}
95
96QgsAbstractSensor *QgsUdpSocketSensorWidget::createSensor()
97{
99 s->setHostName( mHostNameLineEdit->text() );
100 s->setPort( mPortSpinBox->value() );
101 return s;
102}
103
104bool QgsUdpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
105{
106 QgsUdpSocketSensor *s = dynamic_cast<QgsUdpSocketSensor *>( sensor );
107 if ( !s )
108 return false;
109
110 s->setHostName( mHostNameLineEdit->text() );
111 s->setPort( mPortSpinBox->value() );
112
113 return true;
114}
115
116bool QgsUdpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
117{
118 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
119 if ( ts )
120 {
121 mHostNameLineEdit->setText( ts->hostName() );
122 mPortSpinBox->setValue( ts->port() );
123 return true;
124 }
125
126 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
127 if ( us )
128 {
129 mHostNameLineEdit->setText( us->hostName() );
130 mPortSpinBox->setValue( us->port() );
131 return true;
132 }
133
134 return false;
135}
136
137// ------------------------
138
139#if defined( HAVE_QTSERIALPORT )
140QgsSerialPortSensorWidget::QgsSerialPortSensorWidget( QWidget *parent )
141 : QgsAbstractSensorWidget( parent )
142{
143 setupUi( this );
144
145 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
146 {
147 mSerialPortComboBox->addItem( QStringLiteral( "%1: %2" ).arg( info.portName(), info.description() ), info.portName() );
148 }
149
150 mBaudRateComboBox->addItem( QStringLiteral( "1200 baud" ), static_cast<int>( QSerialPort::Baud1200 ) );
151 mBaudRateComboBox->addItem( QStringLiteral( "2400 baud" ), static_cast<int>( QSerialPort::Baud2400 ) );
152 mBaudRateComboBox->addItem( QStringLiteral( "4800 baud" ), static_cast<int>( QSerialPort::Baud4800 ) );
153 mBaudRateComboBox->addItem( QStringLiteral( "9600 baud" ), static_cast<int>( QSerialPort::Baud9600 ) );
154 mBaudRateComboBox->addItem( QStringLiteral( "19200 baud" ), static_cast<int>( QSerialPort::Baud19200 ) );
155 mBaudRateComboBox->addItem( QStringLiteral( "38400 baud" ), static_cast<int>( QSerialPort::Baud38400 ) );
156 mBaudRateComboBox->addItem( QStringLiteral( "57600 baud" ), static_cast<int>( QSerialPort::Baud57600 ) );
157 mBaudRateComboBox->addItem( QStringLiteral( "115200 baud" ), static_cast<int>( QSerialPort::Baud115200 ) );
158 mBaudRateComboBox->setCurrentIndex( 3 );
159
160 updateSerialPortDetails();
161
162 connect( mSerialPortComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]()
163 {
164 updateSerialPortDetails();
165 emit changed();
166 } );
167
168 connect( mBaudRateComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]()
169 {
170 updateSerialPortDetails();
171 emit changed();
172 } );
173
174}
175
176QgsAbstractSensor *QgsSerialPortSensorWidget::createSensor()
177{
178 QgsSerialPortSensor *s = new QgsSerialPortSensor();
179 s->setPortName( mSerialPortComboBox->currentData().toString() );
180 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
181 return s;
182}
183
184bool QgsSerialPortSensorWidget::updateSensor( QgsAbstractSensor *sensor )
185{
186 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
187 if ( !s )
188 return false;
189
190 s->setPortName( mSerialPortComboBox->currentData().toString() );
191 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
192 return true;
193}
194
195bool QgsSerialPortSensorWidget::setSensor( QgsAbstractSensor *sensor )
196{
197 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
198 if ( !s )
199 return false;
200
201 const int index = mSerialPortComboBox->findData( s->portName() );
202 if ( index >= 0 )
203 {
204 mSerialPortComboBox->setCurrentIndex( index );
205 }
206 else
207 {
208 mSerialPortComboBox->addItem( s->portName(), s->portName() );
209 mSerialPortComboBox->setCurrentIndex( mSerialPortComboBox->count() - 1 );
210 }
211
212 const int baudRateIndex = mBaudRateComboBox->findData( s->baudRate() );
213 if ( index >= 0 )
214 {
215 mBaudRateComboBox->setCurrentIndex( baudRateIndex );
216 }
217 else
218 {
219 mBaudRateComboBox->setCurrentIndex( mBaudRateComboBox->count() - 1 );
220 }
221
222
223 return true;
224}
225
226void QgsSerialPortSensorWidget::updateSerialPortDetails()
227{
228 if ( mSerialPortComboBox->currentIndex() < 0 )
229 {
230 return;
231 }
232
233 const QString &currentPortName = mSerialPortComboBox->currentData().toString();
234 bool serialPortFound = false;
235 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
236 {
237 serialPortFound = info.portName() == currentPortName;
238 if ( serialPortFound )
239 {
240 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3\n- %4: %5\n- %6: %7\n- %8: %9\n- %10: %11" ).arg( tr( "Serial port details" ),
241 tr( "Port name" ), info.portName(),
242 tr( "Description" ), info.description(),
243 tr( "Manufacturer" ), info.manufacturer(),
244 tr( "Product identifier" ), QString::number( info.productIdentifier() ),
245 tr( "Serial number" ), info.serialNumber() ) );
246 break;
247 }
248 }
249 if ( !serialPortFound )
250 {
251 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3" ).arg( tr( "Serial port details" ),
252 tr( "Port name" ), currentPortName ) );
253 }
254}
255#endif
256
Base class for widgets which allow control over the properties of sensors.
void changed()
Emitted whenever configuration changes happened on this sensor configuration.
QgsAbstractSensorWidget(QWidget *parent=nullptr)
Constructor for QgsAbstractSensorWidget.
An abstract base class for sensor classes.
A TCP socket sensor class.
void setHostName(const QString &hostName)
Sets the host name the socket connects to.
int port() const
Returns the port the socket connects to.
void setPort(int port)
Sets the port the socket connects to.
QString hostName() const
Returns the host name the socket connects to.
A UDP socket sensor class.
void setHostName(const QString &hostName)
Sets the host name the socket connects to.
int port() const
Returns the port the socket connects to.
void setPort(int port)
Sets the port the socket connects to.
QString hostName() const
Returns the host name the socket connects to.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53