QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
24#include <QMap>
25#include <QString>
26#include <QVariant>
27#include <QList>
28#include <QVector>
29#include <QSet>
30#include <QExplicitlySharedDataPointer>
31
32
33#include "qgsfields.h"
34#include "qgsvariantutils.h"
35
36
37class QgsRectangle;
38class QgsFeature;
39class QgsFeaturePrivate;
40
41// key = field index, value = field value
42typedef QMap<int, QVariant> QgsAttributeMap;
43
44// key = field index, value = field name
45typedef QMap<int, QString> QgsFieldNameMap;
46
47#ifdef SIP_RUN
48typedef QMap<int, QgsField> QgsFieldMap;
49#endif
50
51
57#ifndef SIP_RUN
58class QgsAttributes : public QVector<QVariant>
59{
60 public:
61
63 QgsAttributes() = default;
64
70 QgsAttributes( int size )
71 : QVector<QVariant>( size )
72 {}
73
79 QgsAttributes( int size, const QVariant &v )
80 : QVector<QVariant>( size, v )
81 {}
82
87 QgsAttributes( const QVector<QVariant> &v )
88 : QVector<QVariant>( v )
89 {}
90
100 bool operator==( const QgsAttributes &v ) const
101 {
102 if ( size() != v.size() )
103 return false;
104 const QVariant *b = constData();
105 const QVariant *i = b + size();
106 const QVariant *j = v.constData() + size();
107
108 // note that for non-null values, we need to check that the type is equal too!
109 // QVariant == comparisons do some weird things, like reporting that a QDateTime(2021, 2, 10, 0, 0) variant is equal
110 // to a QString "2021-02-10 00:00" variant!
111 while ( i != b )
112 if ( !( QgsVariantUtils::isNull( *( --i ) ) == QgsVariantUtils::isNull( *( --j ) ) && ( QgsVariantUtils::isNull( *i ) || i->type() == j->type() ) && *i == *j ) )
113 return false;
114 return true;
115 }
116
123 CORE_EXPORT QgsAttributeMap toMap() const SIP_SKIP;
124
130 bool isUnsetValue( int index ) const
131 {
132 if ( index < 0 || index >= size() )
133 return false;
134
135 return at( index ).userType() == QMetaType::type( "QgsUnsetAttributeValue" );
136 }
137
138 inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
139};
140
142CORE_EXPORT uint qHash( const QgsAttributes &attributes );
143
144#else
145typedef QVector<QVariant> QgsAttributes;
146
147% MappedType QgsAttributes
148{
149 % TypeHeaderCode
150#include "qgsfeature.h"
151 % End
152
153 % ConvertFromTypeCode
154 // Create the list.
155 PyObject *l;
156
157 if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
158 return NULL;
159
160 // Set the list elements.
161 for ( int i = 0; i < sipCpp->size(); ++i )
162 {
163 QVariant *v = new QVariant( sipCpp->at( i ) );
164 PyObject *tobj;
165
166 if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL )
167 {
168 Py_DECREF( l );
169 delete v;
170
171 return NULL;
172 }
173
174 PyList_SET_ITEM( l, i, tobj );
175 }
176
177 return l;
178 % End
179
180 % ConvertToTypeCode
181 // Check the type if that is all that is required.
182 if ( sipIsErr == NULL )
183 {
184 if ( !PyList_Check( sipPy ) )
185 return 0;
186
187 for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
188 if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) )
189 return 0;
190
191 return 1;
192 }
193
195 SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
196 qv->reserve( listSize );
197
198 for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
199 {
200 PyObject *obj = PyList_GET_ITEM( sipPy, i );
201 if ( obj == Py_None )
202 {
203 qv->append( QVariant( QVariant::Int ) );
204 }
205 else
206 {
207 int state;
208 QVariant *t = reinterpret_cast<QVariant *>( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) );
209
210 if ( *sipIsErr )
211 {
212 sipReleaseType( t, sipType_QVariant, state );
213
214 delete qv;
215 return 0;
216 }
217
218 qv->append( *t );
219 sipReleaseType( t, sipType_QVariant, state );
220 }
221 }
222
223 *sipCppPtr = qv;
224
225 return sipGetState( sipTransferObj );
226 % End
227};
228#endif
229
230
231#endif // QGSATTRIBUTES_H
A vector of attributes.
Definition: qgsattributes.h:59
bool operator!=(const QgsAttributes &v) const
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsattributes.h:70
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.
Definition: qgsattributes.h:79
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
QgsAttributes()=default
Constructor for QgsAttributes.
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsattributes.h:87
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:56
A rectangle specified with double values.
Definition: qgsrectangle.h:42
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
#define SIP_SKIP
Definition: qgis_sip.h:126
CORE_EXPORT uint qHash(const QgsAttributes &attributes)
Hash for QgsAttributes.
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:45
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:42