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