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