QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsattributes.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributes.h - QgsAttributes
3
4 ---------------------
5 begin : 29.3.2017
6 copyright : (C) 2017 by Denis Rouzaud
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17
18#ifndef QGSATTRIBUTES_H
19#define QGSATTRIBUTES_H
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsfields.h"
25#include "qgsvariantutils.h"
26
27#include <QExplicitlySharedDataPointer>
28#include <QList>
29#include <QMap>
30#include <QSet>
31#include <QString>
32#include <QVariant>
33#include <QVector>
34
35class QgsRectangle;
36class QgsFeature;
37class QgsFeaturePrivate;
38
39// key = field index, value = field value
40typedef QMap<int, QVariant> QgsAttributeMap;
41
42// key = field index, value = field name
43typedef QMap<int, QString> QgsFieldNameMap;
44
45#ifdef SIP_RUN
46typedef QMap<int, QgsField> QgsFieldMap;
47#endif
48
49
55#ifndef SIP_RUN
56class QgsAttributes : public QVector<QVariant>
57{
58 public:
59
60 QgsAttributes() = default;
61
67 QgsAttributes( int size )
68 : QVector<QVariant>( size )
69 {}
70
76 QgsAttributes( int size, const QVariant &v )
77 : QVector<QVariant>( size, v )
78 {}
79
84 QgsAttributes( const QVector<QVariant> &v )
85 : QVector<QVariant>( v )
86 {}
87
97 bool operator==( const QgsAttributes &v ) const
98 {
99 if ( size() != v.size() )
100 return false;
101 const QVariant *b = constData();
102 const QVariant *i = b + size();
103 const QVariant *j = v.constData() + size();
104
105 // note that for non-null values, we need to check that the type is equal too!
106 // QVariant == comparisons do some weird things, like reporting that a QDateTime(2021, 2, 10, 0, 0) variant is equal
107 // to a QString "2021-02-10 00:00" variant!
108 while ( i != b )
109 if ( !( QgsVariantUtils::isNull( *( --i ) ) == QgsVariantUtils::isNull( *( --j ) ) && ( QgsVariantUtils::isNull( *i ) || i->userType() == j->userType() ) && *i == *j ) )
110 return false;
111 return true;
112 }
113
119 CORE_EXPORT QgsAttributeMap toMap() const SIP_SKIP;
120
126 bool isUnsetValue( int index ) const
127 {
128 if ( index < 0 || index >= size() )
129 return false;
130
131 return at( index ).userType() == qMetaTypeId<QgsUnsetAttributeValue>();
132 }
133
134 inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
135};
136
138CORE_EXPORT uint qHash( const QgsAttributes &attributes );
139
140#endif
141
142#ifdef SIP_PYQT5_RUN
143#ifdef SIP_RUN
144typedef QVector<QVariant> QgsAttributes;
145
146% MappedType QgsAttributes
147{
148 % TypeHeaderCode
149#include "qgsfeature.h"
150 % End
151
152 % ConvertFromTypeCode
153 // Create the list.
154 PyObject *l;
155
156 if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
157 return NULL;
158
159 // Set the list elements.
160 for ( int i = 0; i < sipCpp->size(); ++i )
161 {
162 const QVariant v = sipCpp->at( i );
163 PyObject *tobj = NULL;
164 if ( !v.isValid() )
165 {
166 Py_INCREF( Py_None );
167 tobj = Py_None;
168 }
169 // QByteArray null handling is "special"! See null_from_qvariant_converter in conversions.sip
170 else if ( QgsVariantUtils::isNull( v, true ) && v.userType() != QMetaType::Type::QByteArray )
171 {
172 PyObject *vartype = sipConvertFromEnum( v.type(), sipType_QVariant_Type );
173 PyObject *args = PyTuple_Pack( 1, vartype );
174 PyTypeObject *typeObj = sipTypeAsPyTypeObject( sipType_QVariant );
175 tobj = PyObject_Call( ( PyObject * )typeObj, args, nullptr );
176 Py_DECREF( args );
177 Py_DECREF( vartype );
178 }
179 else
180 {
181 switch ( v.userType() )
182 {
183 case QMetaType::Type::Int:
184 tobj = PyLong_FromLong( v.toInt() );
185 break;
186
187 case QMetaType::Type::UInt:
188 tobj = PyLong_FromUnsignedLong( v.toUInt() );
189 break;
190
191 case QMetaType::Type::Long:
192 case QMetaType::Type::LongLong:
193 tobj = PyLong_FromLongLong( v.toLongLong() );
194 break;
195
196 case QMetaType::Type::ULong:
197 case QMetaType::Type::ULongLong:
198 tobj = PyLong_FromUnsignedLongLong( v.toULongLong() );
199 break;
200
201 case QMetaType::Type::Bool:
202 tobj = PyBool_FromLong( v.toBool() ? 1 : 0 );
203 break;
204
205 case QMetaType::Type::Float:
206 case QMetaType::Type::Double:
207 tobj = PyFloat_FromDouble( v.toDouble() );
208 break;
209
210 case QMetaType::Type::QString:
211 tobj = PyUnicode_FromString( v.toString().toUtf8().constData() );
212 break;
213
214 default:
215 {
216 QVariant *newV = new QVariant( v );
217 tobj = sipConvertFromNewType( newV, sipType_QVariant, sipTransferObj );
218 break;
219 }
220 }
221 }
222 if ( tobj == NULL )
223 {
224 Py_DECREF( l );
225 return NULL;
226 }
227
228 PyList_SET_ITEM( l, i, tobj );
229 }
230
231 return l;
232 % End
233
234 % ConvertToTypeCode
235 // Check the type if that is all that is required.
236 if ( sipIsErr == NULL )
237 {
238 if ( !PyList_Check( sipPy ) )
239 return 0;
240
241 for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
242 if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) )
243 return 0;
244
245 return 1;
246 }
247
248 SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
249 // Initialize attributes to null. This has two motivations:
250 // 1. It speeds up the QVector construction, as otherwise we are creating n default QVariant objects (default QVariant constructor is not free!)
251 // 2. It lets us shortcut in the loop below when a Py_None is encountered in the list
252 const QVariant nullVariant( QVariant::Int );
253 QgsAttributes *qv = new QgsAttributes( listSize, nullVariant );
254 QVariant *outData = qv->data();
255
256 for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
257 {
258 PyObject *obj = PyList_GET_ITEM( sipPy, i );
259 if ( obj == Py_None )
260 {
261 // outData was already initialized to null values
262 *outData++;
263 }
264 else if ( PyBool_Check( obj ) )
265 {
266 *outData++ = QVariant( PyObject_IsTrue( obj ) == 1 );
267 }
268 else if ( PyLong_Check( obj ) )
269 {
270 *outData++ = QVariant( PyLong_AsLongLong( obj ) );
271 }
272 else if ( PyFloat_Check( obj ) )
273 {
274 *outData++ = QVariant( PyFloat_AsDouble( obj ) );
275 }
276 else if ( PyUnicode_Check( obj ) )
277 {
278 *outData++ = QVariant( QString::fromUtf8( PyUnicode_AsUTF8( obj ) ) );
279 }
280 else
281 {
282 int state;
283 QVariant *t = reinterpret_cast<QVariant *>( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) );
284
285 if ( *sipIsErr )
286 {
287 sipReleaseType( t, sipType_QVariant, state );
288
289 delete qv;
290 return 0;
291 }
292
293 *outData++ = *t;
294 sipReleaseType( t, sipType_QVariant, state );
295 }
296 }
297
298 *sipCppPtr = qv;
299
300 return sipGetState( sipTransferObj );
301 % End
302};
303#endif
304#endif
305
306#ifdef SIP_PYQT6_RUN
307#ifdef SIP_RUN
308typedef QVector<QVariant> QgsAttributes;
309
310% MappedType QgsAttributes
311{
312 % TypeHeaderCode
313#include "qgsfeature.h"
314 % End
315
316 % ConvertFromTypeCode
317 // Create the list.
318 PyObject *l;
319
320 if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
321 return NULL;
322
323 // Set the list elements.
324 for ( int i = 0; i < sipCpp->size(); ++i )
325 {
326 const QVariant v = sipCpp->at( i );
327 PyObject *tobj = NULL;
328 // QByteArray null handling is "special"! See null_from_qvariant_converter in conversions.sip
329 if ( QgsVariantUtils::isNull( v, true ) && v.userType() != QMetaType::Type::QByteArray )
330 {
331 Py_INCREF( Py_None );
332 tobj = Py_None;
333 }
334 else
335 {
336 switch ( v.userType() )
337 {
338 case QMetaType::Type::Int:
339 tobj = PyLong_FromLong( v.toInt() );
340 break;
341
342 case QMetaType::Type::UInt:
343 tobj = PyLong_FromUnsignedLong( v.toUInt() );
344 break;
345
346 case QMetaType::Type::Long:
347 case QMetaType::Type::LongLong:
348 tobj = PyLong_FromLongLong( v.toLongLong() );
349 break;
350
351 case QMetaType::Type::ULong:
352 case QMetaType::Type::ULongLong:
353 tobj = PyLong_FromUnsignedLongLong( v.toULongLong() );
354 break;
355
356 case QMetaType::Type::Bool:
357 tobj = PyBool_FromLong( v.toBool() ? 1 : 0 );
358 break;
359
360 case QMetaType::Type::Float:
361 case QMetaType::Type::Double:
362 tobj = PyFloat_FromDouble( v.toDouble() );
363 break;
364
365 case QMetaType::Type::QString:
366 tobj = PyUnicode_FromString( v.toString().toUtf8().constData() );
367 break;
368
369 default:
370 {
371 QVariant *newV = new QVariant( v );
372 tobj = sipConvertFromNewType( newV, sipType_QVariant, sipTransferObj );
373 break;
374 }
375 }
376 }
377 if ( tobj == NULL )
378 {
379 Py_DECREF( l );
380 return NULL;
381 }
382
383 PyList_SET_ITEM( l, i, tobj );
384 }
385
386 return l;
387 % End
388
389 % ConvertToTypeCode
390 // Check the type if that is all that is required.
391 if ( sipIsErr == NULL )
392 {
393 if ( !PyList_Check( sipPy ) )
394 return 0;
395
396 for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
397 if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) )
398 return 0;
399
400 return 1;
401 }
402
403 SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
404 // Initialize attributes to null. This has two motivations:
405 // 1. It speeds up the QVector construction, as otherwise we are creating n default QVariant objects (default QVariant constructor is not free!)
406 // 2. It lets us shortcut in the loop below when a Py_None is encountered in the list
407 const QVariant nullVariant( QVariant::Int );
408 QgsAttributes *qv = new QgsAttributes( listSize, nullVariant );
409 QVariant *outData = qv->data();
410
411 for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
412 {
413 PyObject *obj = PyList_GET_ITEM( sipPy, i );
414 if ( obj == Py_None )
415 {
416 // outData was already initialized to null values
417 *outData++;
418 }
419 else if ( PyBool_Check( obj ) )
420 {
421 *outData++ = QVariant( PyObject_IsTrue( obj ) == 1 );
422 }
423 else if ( PyLong_Check( obj ) )
424 {
425 *outData++ = QVariant( PyLong_AsLongLong( obj ) );
426 }
427 else if ( PyFloat_Check( obj ) )
428 {
429 *outData++ = QVariant( PyFloat_AsDouble( obj ) );
430 }
431 else if ( PyUnicode_Check( obj ) )
432 {
433 *outData++ = QVariant( QString::fromUtf8( PyUnicode_AsUTF8( obj ) ) );
434 }
435 else
436 {
437 int state;
438 QVariant *t = reinterpret_cast<QVariant *>( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) );
439
440 if ( *sipIsErr )
441 {
442 sipReleaseType( t, sipType_QVariant, state );
443
444 delete qv;
445 return 0;
446 }
447
448 *outData++ = *t;
449 sipReleaseType( t, sipType_QVariant, state );
450 }
451 }
452
453 *sipCppPtr = qv;
454
455 return sipGetState( sipTransferObj );
456 % End
457};
458#endif
459#endif
460#endif // QGSATTRIBUTES_H
A vector of attributes.
bool operator!=(const QgsAttributes &v) const
QgsAttributes(int size)
Create a new vector of attributes with the given size.
bool isUnsetValue(int index) const
Returns true if the attribute at the specified index is an unset value.
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
QgsAttributes()=default
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
CORE_EXPORT QgsAttributeMap toMap() const
Returns a QgsAttributeMap of the attribute values.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
A rectangle specified with double values.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define SIP_SKIP
Definition qgis_sip.h:134
CORE_EXPORT uint qHash(const QgsAttributes &attributes)
Hash for QgsAttributes.
QMap< int, QString > QgsFieldNameMap
QMap< int, QVariant > QgsAttributeMap