QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "qgsiodevicesensor.h"
20
21#if defined( HAVE_QTSERIALPORT )
22#include <QSerialPort>
23#include <QSerialPortInfo>
24#endif
25
27 : QWidget( parent )
28{
29}
30
31// ------------------------
32
34
35QgsTcpSocketSensorWidget::QgsTcpSocketSensorWidget( QWidget *parent )
36 : QgsAbstractSensorWidget( parent )
37{
38 setupUi( this );
39
40 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
41 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
42}
43
44QgsAbstractSensor *QgsTcpSocketSensorWidget::createSensor()
45{
47 s->setHostName( mHostNameLineEdit->text() );
48 s->setPort( mPortSpinBox->value() );
49 return s;
50}
51
52bool QgsTcpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
53{
54 QgsTcpSocketSensor *s = dynamic_cast<QgsTcpSocketSensor *>( sensor );
55 if ( !s )
56 return false;
57
58 s->setHostName( mHostNameLineEdit->text() );
59 s->setPort( mPortSpinBox->value() );
60
61 return true;
62}
63
64bool QgsTcpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
65{
66 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
67 if ( ts )
68 {
69 mHostNameLineEdit->setText( ts->hostName() );
70 mPortSpinBox->setValue( ts->port() );
71 return true;
72 }
73
74 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
75 if ( us )
76 {
77 mHostNameLineEdit->setText( us->hostName() );
78 mPortSpinBox->setValue( us->port() );
79 return true;
80 }
81
82 return false;
83}
84
85// ------------------------
86
87QgsUdpSocketSensorWidget::QgsUdpSocketSensorWidget( QWidget *parent )
88 : QgsAbstractSensorWidget( parent )
89{
90 setupUi( this );
91
92 connect( mHostNameLineEdit, &QLineEdit::textChanged, this, &QgsAbstractSensorWidget::changed );
93 connect( mPortSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsAbstractSensorWidget::changed );
94}
95
96QgsAbstractSensor *QgsUdpSocketSensorWidget::createSensor()
97{
99 s->setHostName( mHostNameLineEdit->text() );
100 s->setPort( mPortSpinBox->value() );
101 return s;
102}
103
104bool QgsUdpSocketSensorWidget::updateSensor( QgsAbstractSensor *sensor )
105{
106 QgsUdpSocketSensor *s = dynamic_cast<QgsUdpSocketSensor *>( sensor );
107 if ( !s )
108 return false;
109
110 s->setHostName( mHostNameLineEdit->text() );
111 s->setPort( mPortSpinBox->value() );
112
113 return true;
114}
115
116bool QgsUdpSocketSensorWidget::setSensor( QgsAbstractSensor *sensor )
117{
118 QgsTcpSocketSensor *ts = dynamic_cast<QgsTcpSocketSensor *>( sensor );
119 if ( ts )
120 {
121 mHostNameLineEdit->setText( ts->hostName() );
122 mPortSpinBox->setValue( ts->port() );
123 return true;
124 }
125
126 QgsUdpSocketSensor *us = dynamic_cast<QgsUdpSocketSensor *>( sensor );
127 if ( us )
128 {
129 mHostNameLineEdit->setText( us->hostName() );
130 mPortSpinBox->setValue( us->port() );
131 return true;
132 }
133
134 return false;
135}
136
137// ------------------------
138
139#if defined( HAVE_QTSERIALPORT )
140QgsSerialPortSensorWidget::QgsSerialPortSensorWidget( QWidget *parent )
141 : QgsAbstractSensorWidget( parent )
142{
143 setupUi( this );
144
145 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
146 {
147 mSerialPortComboBox->addItem( QStringLiteral( "%1: %2" ).arg( info.portName(), info.description() ), info.portName() );
148 }
149
150 updateSerialPortDetails();
151
152 connect( mSerialPortComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]()
153 {
154 updateSerialPortDetails();
155 emit changed();
156 } );
157}
158
159QgsAbstractSensor *QgsSerialPortSensorWidget::createSensor()
160{
161 QgsSerialPortSensor *s = new QgsSerialPortSensor();
162 s->setPortName( mSerialPortComboBox->currentData().toString() );
163 return s;
164}
165
166bool QgsSerialPortSensorWidget::updateSensor( QgsAbstractSensor *sensor )
167{
168 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
169 if ( !s )
170 return false;
171
172 s->setPortName( mSerialPortComboBox->currentData().toString() );
173
174 return true;
175}
176
177bool QgsSerialPortSensorWidget::setSensor( QgsAbstractSensor *sensor )
178{
179 QgsSerialPortSensor *s = dynamic_cast<QgsSerialPortSensor *>( sensor );
180 if ( !s )
181 return false;
182
183 const int index = mSerialPortComboBox->findData( s->portName() );
184 if ( index >= 0 )
185 {
186 mSerialPortComboBox->setCurrentIndex( index );
187 }
188 else
189 {
190 mSerialPortComboBox->addItem( s->portName(), s->portName() );
191 mSerialPortComboBox->setCurrentIndex( mSerialPortComboBox->count() - 1 );
192 }
193
194 return true;
195}
196
197void QgsSerialPortSensorWidget::updateSerialPortDetails()
198{
199 if ( mSerialPortComboBox->currentIndex() < 0 )
200 {
201 return;
202 }
203
204 const QString &currentPortName = mSerialPortComboBox->currentData().toString();
205 bool serialPortFound = false;
206 for ( const QSerialPortInfo &info : QSerialPortInfo::availablePorts() )
207 {
208 serialPortFound = info.portName() == currentPortName;
209 if ( serialPortFound )
210 {
211 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3\n- %4: %5\n- %6: %7\n- %8: %9\n- %10: %11" ).arg( tr( "Serial port details" ),
212 tr( "Port name" ), info.portName(),
213 tr( "Description" ), info.description(),
214 tr( "Manufacturer" ), info.manufacturer(),
215 tr( "Product identifier" ), QString::number( info.productIdentifier() ),
216 tr( "Serial number" ), info.serialNumber() ) );
217 break;
218 }
219 }
220 if ( !serialPortFound )
221 {
222 mSerialPortDetails->setText( QStringLiteral( "%1:\n- %2: %3" ).arg( tr( "Serial port details" ),
223 tr( "Port name" ), currentPortName ) );
224 }
225}
226#endif
227
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