QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgssensorwidget.cpp"
20#include "qgsiodevicesensor.h"
21
22#if defined( HAVE_QTSERIALPORT )
23#include <QSerialPort>
24#include <QSerialPortInfo>
25#endif
26
28 : QWidget( parent )
29{
30}
31
32// ------------------------
33
35
36QgsTcpSocketSensorWidget::QgsTcpSocketSensorWidget( QWidget *parent )
37 : QgsAbstractSensorWidget( parent )
38{
39 setupUi( this );
40
41 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
42 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
43}
44
45QgsAbstractSensor *QgsTcpSocketSensorWidget::createSensor()
46{
48 s->setHostName( mHostNameLineEdit->text() );
49 s->setPort( mPortSpinBox->value() );
50 return s;
51}
52
53bool QgsTcpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
54{
55 QgsTcpSocketSensor *s = dynamic_cast<QgsTcpSocketSensor *>( sensor );
56 if ( !s )
57 return false;
58
59 s->setHostName( mHostNameLineEdit->text() );
60 s->setPort( mPortSpinBox->value() );
61
62 return true;
63}
64
65bool QgsTcpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
66{
67 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
68 if ( ts )
69 {
70 mHostNameLineEdit->setText( ts->hostName() );
71 mPortSpinBox->setValue( ts->port() );
72 return true;
73 }
74
75 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
76 if ( us )
77 {
78 mHostNameLineEdit->setText( us->hostName() );
79 mPortSpinBox->setValue( us->port() );
80 return true;
81 }
82
83 return false;
84}
85
86// ------------------------
87
88QgsUdpSocketSensorWidget::QgsUdpSocketSensorWidget( QWidget *parent )
89 : QgsAbstractSensorWidget( parent )
90{
91 setupUi( this );
92
93 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
94 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
95}
96
97QgsAbstractSensor *QgsUdpSocketSensorWidget::createSensor()
98{
100 s->setHostName( mHostNameLineEdit->text() );
101 s->setPort( mPortSpinBox->value() );
102 return s;
103}
104
105bool QgsUdpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
106{
107 QgsUdpSocketSensor *s = dynamic_cast<QgsUdpSocketSensor *>( sensor );
108 if ( !s )
109 return false;
110
111 s->setHostName( mHostNameLineEdit->text() );
112 s->setPort( mPortSpinBox->value() );
113
114 return true;
115}
116
117bool QgsUdpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
118{
119 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
120 if ( ts )
121 {
122 mHostNameLineEdit->setText( ts->hostName() );
123 mPortSpinBox->setValue( ts->port() );
124 return true;
125 }
126
127 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
128 if ( us )
129 {
130 mHostNameLineEdit->setText( us->hostName() );
131 mPortSpinBox->setValue( us->port() );
132 return true;
133 }
134
135 return false;
136}
137
138// ------------------------
139
140#if defined( HAVE_QTSERIALPORT )
141QgsSerialPortSensorWidget::QgsSerialPortSensorWidget( QWidget *parent )
142 : QgsAbstractSensorWidget( parent )
143{
144 setupUi( this );
145
146 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
147 {
148 mSerialPortComboBox->addItem( QStringLiteral( "%1: %2" ).arg( info.portName(), info.description() ), info.portName() );
149 }
150
151 mBaudRateComboBox->addItem( QStringLiteral( "1200 baud" ), static_cast<int>( QSerialPort::Baud1200 ) );
152 mBaudRateComboBox->addItem( QStringLiteral( "2400 baud" ), static_cast<int>( QSerialPort::Baud2400 ) );
153 mBaudRateComboBox->addItem( QStringLiteral( "4800 baud" ), static_cast<int>( QSerialPort::Baud4800 ) );
154 mBaudRateComboBox->addItem( QStringLiteral( "9600 baud" ), static_cast<int>( QSerialPort::Baud9600 ) );
155 mBaudRateComboBox->addItem( QStringLiteral( "19200 baud" ), static_cast<int>( QSerialPort::Baud19200 ) );
156 mBaudRateComboBox->addItem( QStringLiteral( "38400 baud" ), static_cast<int>( QSerialPort::Baud38400 ) );
157 mBaudRateComboBox->addItem( QStringLiteral( "57600 baud" ), static_cast<int>( QSerialPort::Baud57600 ) );
158 mBaudRateComboBox->addItem( QStringLiteral( "115200 baud" ), static_cast<int>( QSerialPort::Baud115200 ) );
159 mBaudRateComboBox->setCurrentIndex( 3 );
160
161 mDataFrameDelimiterComboBox->addItem( tr( "No Delimiter" ), QString() );
162 mDataFrameDelimiterComboBox->addItem( tr( "New Line" ), QString( "\n" ) );
163 mDataFrameDelimiterComboBox->addItem( tr( "Custom Character" ), QString() );
164
165 updateSerialPortDetails();
166
167 connect( mSerialPortComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentTextChanged ), this, [ = ]()
168 {
169 updateSerialPortDetails();
170 emit changed();
171 } );
172
173 connect( mBaudRateComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]()
174 {
175 updateSerialPortDetails();
176 emit changed();
177 } );
178
179 connect( mDataFrameDelimiterComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
180 {
181 if ( index == mDataFrameDelimiterComboBox->count() - 1 )
182 {
183 mDataFrameDelimiterLineEdit->setEnabled( true );
184 mDataFrameDelimiterLineEdit->setFocus();
185 }
186 else
187 {
188 mDataFrameDelimiterLineEdit->setEnabled( false );
189 }
190 emit changed();
191 } );
192
193 connect( mDataFrameDelimiterLineEdit, &QLineEdit::textEdited, this, [ = ]()
194 {
195 emit changed();
196 } );
197}
198
199QgsAbstractSensor *QgsSerialPortSensorWidget::createSensor()
200{
201 QgsSerialPortSensor *s = new QgsSerialPortSensor();
202 s->setPortName( mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText() );
203 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
204 const QString delimiter = mDataFrameDelimiterComboBox->currentIndex() == mDataFrameDelimiterComboBox->count() - 1 ? mDataFrameDelimiterLineEdit->text() : mDataFrameDelimiterComboBox->currentData().toString();
205 s->setDelimiter( delimiter.toLocal8Bit() );
206 return s;
207}
208
209bool QgsSerialPortSensorWidget::updateSensor( QgsAbstractSensor *sensor )
210{
211 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
212 if ( !s )
213 return false;
214
215 s->setPortName( mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText() );
216 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
217 const QString delimiter = mDataFrameDelimiterComboBox->currentIndex() == mDataFrameDelimiterComboBox->count() - 1 ? mDataFrameDelimiterLineEdit->text() : mDataFrameDelimiterComboBox->currentData().toString();
218 s->setDelimiter( delimiter.toLocal8Bit() );
219 return true;
220}
221
222bool QgsSerialPortSensorWidget::setSensor( QgsAbstractSensor *sensor )
223{
224 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
225 if ( !s )
226 return false;
227
228 const int index = mSerialPortComboBox->findData( s->portName() );
229 if ( index >= 0 )
230 {
231 mSerialPortComboBox->setCurrentIndex( index );
232 }
233 else
234 {
235 mSerialPortComboBox->addItem( s->portName(), s->portName() );
236 mSerialPortComboBox->setCurrentIndex( mSerialPortComboBox->count() - 1 );
237 }
238
239 const int baudRateIndex = mBaudRateComboBox->findData( s->baudRate() );
240 if ( index >= 0 )
241 {
242 mBaudRateComboBox->setCurrentIndex( baudRateIndex );
243 }
244 else
245 {
246 mBaudRateComboBox->setCurrentIndex( mBaudRateComboBox->count() - 1 );
247 }
248
249 const QString delimiter = QString( s->delimiter() );
250 if ( !delimiter.isEmpty() )
251 {
252 const int delimiterIndex = mDataFrameDelimiterComboBox->findData( delimiter );
253 if ( delimiterIndex > -1 )
254 {
255 mDataFrameDelimiterComboBox->setCurrentIndex( delimiterIndex );
256 }
257 else
258 {
259 mDataFrameDelimiterComboBox->setCurrentIndex( mDataFrameDelimiterComboBox->count() - 1 );
260 mDataFrameDelimiterLineEdit->setText( delimiter );
261 }
262 }
263 else
264 {
265 mDataFrameDelimiterComboBox->setCurrentIndex( 0 );
266 mDataFrameDelimiterLineEdit->setText( QString() );
267 }
268
269 return true;
270}
271
272void QgsSerialPortSensorWidget::updateSerialPortDetails()
273{
274 if ( mSerialPortComboBox->currentText().isEmpty() )
275 {
276 return;
277 }
278
279 const QString &currentPortName = mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText();
280 bool serialPortFound = false;
281 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
282 {
283 serialPortFound = info.portName() == currentPortName;
284 if ( serialPortFound )
285 {
286 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3\n- %4: %5\n- %6: %7\n- %8: %9\n- %10: %11" ).arg( tr( "Serial port details" ),
287 tr( "Port name" ), info.portName(),
288 tr( "Description" ), info.description(),
289 tr( "Manufacturer" ), info.manufacturer(),
290 tr( "Product identifier" ), QString::number( info.productIdentifier() ),
291 tr( "Serial number" ), info.serialNumber() ) );
292 break;
293 }
294 }
295 if ( !serialPortFound )
296 {
297 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3" ).arg( tr( "Serial port details" ),
298 tr( "Port name" ), currentPortName ) );
299 }
300 else
301 {
302 mSerialPortDetails->setText( QString() );
303 }
304}
305#endif
306
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