QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 "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 mDataFrameDelimiterComboBox->addItem( tr( "No Delimiter" ), QString() );
161 mDataFrameDelimiterComboBox->addItem( tr( "New Line" ), QString( "\n" ) );
162 mDataFrameDelimiterComboBox->addItem( tr( "Custom Character" ), QString() );
163
164 updateSerialPortDetails();
165
166 connect( mSerialPortComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentTextChanged ), this, [ = ]()
167 {
168 updateSerialPortDetails();
169 emit changed();
170 } );
171
172 connect( mBaudRateComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]()
173 {
174 updateSerialPortDetails();
175 emit changed();
176 } );
177
178 connect( mDataFrameDelimiterComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
179 {
180 if ( index == mDataFrameDelimiterComboBox->count() - 1 )
181 {
182 mDataFrameDelimiterLineEdit->setEnabled( true );
183 mDataFrameDelimiterLineEdit->setFocus();
184 }
185 else
186 {
187 mDataFrameDelimiterLineEdit->setEnabled( false );
188 }
189 emit changed();
190 } );
191
192 connect( mDataFrameDelimiterLineEdit, &QLineEdit::textEdited, this, [ = ]()
193 {
194 emit changed();
195 } );
196}
197
198QgsAbstractSensor *QgsSerialPortSensorWidget::createSensor()
199{
200 QgsSerialPortSensor *s = new QgsSerialPortSensor();
201 s->setPortName( mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText() );
202 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
203 const QString delimiter = mDataFrameDelimiterComboBox->currentIndex() == mDataFrameDelimiterComboBox->count() - 1 ? mDataFrameDelimiterLineEdit->text() : mDataFrameDelimiterComboBox->currentData().toString();
204 s->setDelimiter( delimiter.toLocal8Bit() );
205 return s;
206}
207
208bool QgsSerialPortSensorWidget::updateSensor( QgsAbstractSensor *sensor )
209{
210 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
211 if ( !s )
212 return false;
213
214 s->setPortName( mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText() );
215 s->setBaudRate( static_cast< QSerialPort::BaudRate >( mBaudRateComboBox->currentData().toInt() ) );
216 const QString delimiter = mDataFrameDelimiterComboBox->currentIndex() == mDataFrameDelimiterComboBox->count() - 1 ? mDataFrameDelimiterLineEdit->text() : mDataFrameDelimiterComboBox->currentData().toString();
217 s->setDelimiter( delimiter.toLocal8Bit() );
218 return true;
219}
220
221bool QgsSerialPortSensorWidget::setSensor( QgsAbstractSensor *sensor )
222{
223 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
224 if ( !s )
225 return false;
226
227 const int index = mSerialPortComboBox->findData( s->portName() );
228 if ( index >= 0 )
229 {
230 mSerialPortComboBox->setCurrentIndex( index );
231 }
232 else
233 {
234 mSerialPortComboBox->addItem( s->portName(), s->portName() );
235 mSerialPortComboBox->setCurrentIndex( mSerialPortComboBox->count() - 1 );
236 }
237
238 const int baudRateIndex = mBaudRateComboBox->findData( s->baudRate() );
239 if ( index >= 0 )
240 {
241 mBaudRateComboBox->setCurrentIndex( baudRateIndex );
242 }
243 else
244 {
245 mBaudRateComboBox->setCurrentIndex( mBaudRateComboBox->count() - 1 );
246 }
247
248 const QString delimiter = QString( s->delimiter() );
249 if ( !delimiter.isEmpty() )
250 {
251 const int delimiterIndex = mDataFrameDelimiterComboBox->findData( delimiter );
252 if ( delimiterIndex > -1 )
253 {
254 mDataFrameDelimiterComboBox->setCurrentIndex( delimiterIndex );
255 }
256 else
257 {
258 mDataFrameDelimiterComboBox->setCurrentIndex( mDataFrameDelimiterComboBox->count() - 1 );
259 mDataFrameDelimiterLineEdit->setText( delimiter );
260 }
261 }
262 else
263 {
264 mDataFrameDelimiterComboBox->setCurrentIndex( 0 );
265 mDataFrameDelimiterLineEdit->setText( QString() );
266 }
267
268 return true;
269}
270
271void QgsSerialPortSensorWidget::updateSerialPortDetails()
272{
273 if ( mSerialPortComboBox->currentText().isEmpty() )
274 {
275 return;
276 }
277
278 const QString &currentPortName = mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText();
279 bool serialPortFound = false;
280 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
281 {
282 serialPortFound = info.portName() == currentPortName;
283 if ( serialPortFound )
284 {
285 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3\n- %4: %5\n- %6: %7\n- %8: %9\n- %10: %11" ).arg( tr( "Serial port details" ),
286 tr( "Port name" ), info.portName(),
287 tr( "Description" ), info.description(),
288 tr( "Manufacturer" ), info.manufacturer(),
289 tr( "Product identifier" ), QString::number( info.productIdentifier() ),
290 tr( "Serial number" ), info.serialNumber() ) );
291 break;
292 }
293 }
294 if ( !serialPortFound )
295 {
296 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3" ).arg( tr( "Serial port details" ),
297 tr( "Port name" ), currentPortName ) );
298 }
299 else
300 {
301 mSerialPortDetails->setText( QString() );
302 }
303}
304#endif
305
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