19#include "moc_qgssensorwidget.cpp"
22#if defined( HAVE_QTSERIALPORT )
24#include <QSerialPortInfo>
36QgsTcpSocketSensorWidget::QgsTcpSocketSensorWidget( QWidget *parent )
49 s->
setPort( mPortSpinBox->value() );
60 s->
setPort( mPortSpinBox->value() );
70 mHostNameLineEdit->setText( ts->
hostName() );
71 mPortSpinBox->setValue( ts->
port() );
78 mHostNameLineEdit->setText( us->
hostName() );
79 mPortSpinBox->setValue( us->
port() );
88QgsUdpSocketSensorWidget::QgsUdpSocketSensorWidget( QWidget *parent )
101 s->
setPort( mPortSpinBox->value() );
112 s->
setPort( mPortSpinBox->value() );
122 mHostNameLineEdit->setText( ts->
hostName() );
123 mPortSpinBox->setValue( ts->
port() );
130 mHostNameLineEdit->setText( us->
hostName() );
131 mPortSpinBox->setValue( us->
port() );
140#if defined( HAVE_QTSERIALPORT )
141QgsSerialPortSensorWidget::QgsSerialPortSensorWidget( QWidget *parent )
146 for (
const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
148 mSerialPortComboBox->addItem( QStringLiteral(
"%1: %2" ).arg( info.portName(), info.description() ), info.portName() );
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 );
161 mDataFrameDelimiterComboBox->addItem( tr(
"No Delimiter" ), QString() );
162 mDataFrameDelimiterComboBox->addItem( tr(
"New Line" ), QString(
"\n" ) );
163 mDataFrameDelimiterComboBox->addItem( tr(
"Custom Character" ), QString() );
165 updateSerialPortDetails();
167 connect( mSerialPortComboBox,
static_cast<void ( QComboBox::* )(
const QString & )
>( &QComboBox::currentTextChanged ),
this, [ = ]()
169 updateSerialPortDetails();
173 connect( mBaudRateComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ]()
175 updateSerialPortDetails();
179 connect( mDataFrameDelimiterComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ](
int index )
181 if ( index == mDataFrameDelimiterComboBox->count() - 1 )
183 mDataFrameDelimiterLineEdit->setEnabled( true );
184 mDataFrameDelimiterLineEdit->setFocus();
188 mDataFrameDelimiterLineEdit->setEnabled( false );
193 connect( mDataFrameDelimiterLineEdit, &QLineEdit::textEdited,
this, [ = ]()
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() );
211 QgsSerialPortSensor *s =
dynamic_cast<QgsSerialPortSensor *
>( sensor );
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() );
224 QgsSerialPortSensor *s =
dynamic_cast<QgsSerialPortSensor *
>( sensor );
228 const int index = mSerialPortComboBox->findData( s->portName() );
231 mSerialPortComboBox->setCurrentIndex( index );
235 mSerialPortComboBox->addItem( s->portName(), s->portName() );
236 mSerialPortComboBox->setCurrentIndex( mSerialPortComboBox->count() - 1 );
239 const int baudRateIndex = mBaudRateComboBox->findData( s->baudRate() );
242 mBaudRateComboBox->setCurrentIndex( baudRateIndex );
246 mBaudRateComboBox->setCurrentIndex( mBaudRateComboBox->count() - 1 );
249 const QString delimiter = QString( s->delimiter() );
250 if ( !delimiter.isEmpty() )
252 const int delimiterIndex = mDataFrameDelimiterComboBox->findData( delimiter );
253 if ( delimiterIndex > -1 )
255 mDataFrameDelimiterComboBox->setCurrentIndex( delimiterIndex );
259 mDataFrameDelimiterComboBox->setCurrentIndex( mDataFrameDelimiterComboBox->count() - 1 );
260 mDataFrameDelimiterLineEdit->setText( delimiter );
265 mDataFrameDelimiterComboBox->setCurrentIndex( 0 );
266 mDataFrameDelimiterLineEdit->setText( QString() );
272void QgsSerialPortSensorWidget::updateSerialPortDetails()
274 if ( mSerialPortComboBox->currentText().isEmpty() )
279 const QString ¤tPortName = mSerialPortComboBox->findText( mSerialPortComboBox->currentText() ) != -1 ? mSerialPortComboBox->currentData().toString() : mSerialPortComboBox->currentText();
280 bool serialPortFound =
false;
281 for (
const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
283 serialPortFound = info.portName() == currentPortName;
284 if ( serialPortFound )
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() ) );
295 if ( !serialPortFound )
297 mSerialPortDetails->setText( QStringLiteral(
"%1:\n- %2: %3" ).arg( tr(
"Serial port details" ),
298 tr(
"Port name" ), currentPortName ) );
302 mSerialPortDetails->setText( QString() );
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.