QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgssensormodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensormodel.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 "qgssensormodel.h"
17#include "moc_qgssensormodel.cpp"
18
19#include "qgis.h"
20#include "qgssensormanager.h"
21#include "qgsabstractsensor.h"
22
23
25 : QAbstractItemModel( parent )
26 , mSensorManager( manager )
27{
28 connect( mSensorManager, &QgsSensorManager::sensorAdded, this, &QgsSensorModel::sensorAdded );
29 connect( mSensorManager, &QgsSensorManager::sensorAboutToBeRemoved, this, &QgsSensorModel::sensorRemoved );
30
31 connect( mSensorManager, &QgsSensorManager::sensorNameChanged, this, &QgsSensorModel::sensorNameChanged );
32 connect( mSensorManager, &QgsSensorManager::sensorStatusChanged, this, &QgsSensorModel::sensorStatusChanged );
33 connect( mSensorManager, &QgsSensorManager::sensorDataCaptured, this, &QgsSensorModel::sensorDataCaptured );
34
35 beginResetModel();
36 const QList<QgsAbstractSensor *> sensors = manager->sensors();
37 for ( const QgsAbstractSensor *sensor : sensors )
38 {
39 if ( !mSensorIds.contains( sensor->id() ) )
40 {
41 mSensorIds << sensor->id();
42 }
43 }
44 endResetModel();
45}
46
47QVariant QgsSensorModel::data( const QModelIndex &index, int role ) const
48{
49 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
50 return QVariant();
51
52 QgsAbstractSensor *sensor = mSensorManager->sensor( mSensorIds[index.row()] );
53 if ( !sensor )
54 return QVariant();
55
56 switch ( role )
57 {
58 case Qt::DisplayRole:
59 case Qt::ToolTipRole:
60 case Qt::EditRole:
61 {
62 switch ( index.column() )
63 {
64 case static_cast<int>( Column::Name ):
65 {
66 return sensor->name();
67 }
68
69 case static_cast<int>( Column::LastValue ):
70 {
71 switch ( sensor->status() )
72 {
74 return tr( "Disconnected" );
75
77 return tr( "Connecting" );
78
80 return sensor->data().lastValue.toString();
81 }
82 }
83
84 default:
85 break;
86 }
87
88 return QVariant();
89 }
90
91 case static_cast< int >( CustomRole::SensorType ):
92 {
93 return sensor->type();
94 }
95
96 case static_cast< int >( CustomRole::SensorId ):
97 {
98 return sensor->id();
99 }
100
101 case static_cast< int >( CustomRole::SensorName ):
102 {
103 return sensor->name();
104 }
105
106 case static_cast< int >( CustomRole::SensorStatus ):
107 {
108 return QVariant::fromValue<Qgis::DeviceConnectionStatus>( sensor->status() );
109 }
110
111 case static_cast< int >( CustomRole::SensorLastValue ):
112 {
113 return sensor->data().lastValue;
114 }
115
116 case static_cast< int >( CustomRole::SensorLastTimestamp ):
117 {
118 return sensor->data().lastTimestamp;
119 }
120
121 case static_cast< int >( CustomRole::Sensor ):
122 {
123 return QVariant::fromValue<QgsAbstractSensor *>( sensor );
124 }
125
126 default:
127 return QVariant();
128 }
130}
131
132bool QgsSensorModel::setData( const QModelIndex &index, const QVariant &value, int role )
133{
134 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) || role != Qt::EditRole )
135 return false;
136
137 QgsAbstractSensor *sensor = mSensorManager->sensor( mSensorIds[index.row()] );
138 if ( !sensor )
139 return false;
140
141 switch ( index.column() )
142 {
143 case static_cast<int>( Column::Name ):
144 {
145 sensor->setName( value.toString() );
146 return true;
147 }
148
149 default:
150 break;
151 }
152
153 return false;
154}
155
156Qt::ItemFlags QgsSensorModel::flags( const QModelIndex &index ) const
157{
158 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
159 if ( index.isValid() && index.column() == static_cast<int>( Column::Name ) )
160 {
161 QgsAbstractSensor *sensor = mSensorManager->sensor( mSensorIds[index.row()] );
162 if ( sensor )
163 {
164 return flags | Qt::ItemIsEditable;
165 }
166 }
167 return flags;
168}
169
170QVariant QgsSensorModel::headerData( int section, Qt::Orientation orientation, int role ) const
171{
172 if ( role == Qt::DisplayRole )
173 {
174 if ( orientation == Qt::Vertical ) //row
175 {
176 return QVariant( section + 1 );
177 }
178 else
179 {
180 switch ( section )
181 {
182 case static_cast<int>( Column::Name ):
183 return QVariant( tr( "Name" ) );
184
185 case static_cast<int>( Column::LastValue ):
186 return QVariant( tr( "Last Value" ) );
187
188 default:
189 return QVariant();
190 }
191 }
192 }
193 else
194 {
195 return QVariant();
196 }
197}
198
199QModelIndex QgsSensorModel::index( int row, int column, const QModelIndex &parent ) const
200{
201 if ( !hasIndex( row, column, parent ) )
202 return QModelIndex();
203
204 if ( !parent.isValid() )
205 {
206 return createIndex( row, column );
207 }
208
209 return QModelIndex();
210}
211
212QModelIndex QgsSensorModel::parent( const QModelIndex & ) const
213{
214 return QModelIndex();
215}
216
217int QgsSensorModel::rowCount( const QModelIndex &parent ) const
218{
219 if ( !parent.isValid() )
220 {
221 return mSensorIds.size();
222 }
223 return 0;
224}
225
226int QgsSensorModel::columnCount( const QModelIndex & ) const
227{
228 return 2;
229}
230
231void QgsSensorModel::sensorAdded( const QString &id )
232{
233 beginInsertRows( QModelIndex(), mSensorIds.size(), mSensorIds.size() );
234 mSensorIds << id;
235 endInsertRows();
236}
237
238void QgsSensorModel::sensorRemoved( const QString &id )
239{
240 const int sensorIndex = mSensorIds.indexOf( id );
241 if ( sensorIndex < 0 )
242 return;
243
244 beginRemoveRows( QModelIndex(), sensorIndex, sensorIndex );
245 mSensorIds.removeAt( sensorIndex );
246 endRemoveRows();
247}
248
249void QgsSensorModel::sensorNameChanged( const QString &id )
250{
251 const int sensorIndex = mSensorIds.indexOf( id );
252 if ( sensorIndex < 0 )
253 return;
254
255 emit dataChanged( index( sensorIndex, static_cast<int>( Column::Name ) ), index( sensorIndex, static_cast<int>( Column::Name ) ), QVector< int >() << Qt::DisplayRole << static_cast< int >( CustomRole::SensorName ) );
256}
257
258void QgsSensorModel::sensorStatusChanged( const QString &id )
259{
260 const int sensorIndex = mSensorIds.indexOf( id );
261 if ( sensorIndex < 0 )
262 return;
263
264 emit dataChanged( index( sensorIndex, static_cast<int>( Column::LastValue ) ), index( sensorIndex, static_cast<int>( Column::LastValue ) ), QVector< int >() << static_cast< int >( CustomRole::SensorStatus ) );
265}
266
267void QgsSensorModel::sensorDataCaptured( const QString &id )
268{
269 const int sensorIndex = mSensorIds.indexOf( id );
270 if ( sensorIndex < 0 )
271 return;
272
273 emit dataChanged( index( sensorIndex, static_cast<int>( Column::LastValue ) ), index( sensorIndex, static_cast<int>( Column::LastValue ) ), QVector< int >() << static_cast< int >( CustomRole::SensorLastValue ) << static_cast< int >( CustomRole::SensorLastTimestamp ) );
274}
@ Connected
Device is successfully connected.
@ Connecting
Device is connecting.
@ Disconnected
Device is disconnected.
An abstract base class for sensor classes.
QString id() const
Returns the sensor ID.
Qgis::DeviceConnectionStatus status() const
Returns the current sensor status.
QgsAbstractSensor::SensorData data() const
Returns the latest captured data from the sensor.
QString name() const
Returns the user-friendly name identifying the sensor.
virtual QString type() const
Returns the sensor type.
void setName(const QString &name)
Sets the user-friendly name identfying the sensor.
Manages sensors.
void sensorNameChanged(const QString &id)
Emitted when a sensor name has changed.
QList< QgsAbstractSensor * > sensors() const
Returns a list of pointers to all registered sensors.
void sensorDataCaptured(const QString &id)
Emitted when newly captured data from a sensor has occurred.
QgsAbstractSensor * sensor(const QString &id) const
Returns a registered sensor pointer matching a given id.
void sensorAdded(const QString &id)
Emitted when a sensor has been registered.
void sensorAboutToBeRemoved(const QString &id)
Emitted when a sensor is about to be removed.
void sensorStatusChanged(const QString &id)
Emitted when a sensor status has changed.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsSensorModel(QgsSensorManager *manager, QObject *parent=nullptr)
Constructor for QgsSensorModel, for the specified manager and parent object.
QModelIndex parent(const QModelIndex &index) const override
@ LastValue
Last value.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
@ SensorLastTimestamp
Sensor timestamp of last captured value.
@ Sensor
Sensor object pointer.
@ SensorStatus
Sensor status (disconnected, connected, etc.)
@ SensorLastValue
Sensor last captured value.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
#define BUILTIN_UNREACHABLE
Definition qgis.h:6571
QVariant lastValue
Last captured sensor value stored as a QVariant.
QDateTime lastTimestamp
Timestamp of last captured sensor value.