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