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