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