QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgsfields.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfields.h - QgsFields
3
4 ---------------------
5 begin : 22.9.2016
6 copyright : (C) 2016 by Matthias Kuhn
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#ifndef QGSFIELDS_H
17#define QGSFIELDS_H
18
19
20#include "qgis_sip.h"
21#include "qgis_core.h"
22#include "qgsfield.h"
23
24class QgsFieldsPrivate;
25
26/***************************************************************************
27 * This class is considered CRITICAL and any change MUST be accompanied with
28 * full unit tests in testqgsfields.cpp.
29 * See details in QEP #17
30 ****************************************************************************/
31
44class CORE_EXPORT QgsFields
45{
46 public:
47
56
57#ifndef SIP_RUN
58
59 typedef struct Field
60 {
62 {}
63
64 Field( const QgsField &f, FieldOrigin o, int oi )
65 : field( f )
66 , origin( o )
67 , originIndex( oi )
68 {}
69
70 // TODO c++20 - replace with = default
71
72 bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
73 bool operator!=( const Field &other ) const { return !( *this == other ); }
74
76 FieldOrigin origin = OriginUnknown ;
77 int originIndex = -1 ;
78 } Field;
79
80#endif
81
85 QgsFields();
86
90 QgsFields( const QgsFields &other );
91
95 QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
96
97 virtual ~QgsFields();
98
100 void clear();
101
103 bool append( const QgsField &field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
104
109 bool rename( int fieldIdx, const QString &name );
110
112 bool appendExpressionField( const QgsField &field, int originIndex );
113
114#ifndef SIP_RUN
115
119 void remove( int fieldIdx );
120#else
121
127 void remove( int fieldIdx );
128 % MethodCode
129 if ( a0 < 0 || a0 >= sipCpp->count() )
130 {
131 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
132 sipIsErr = 1;
133 }
134 else
135 {
136 sipCpp->remove( a0 );
137 }
138 % End
139#endif
140
142 void extend( const QgsFields &other );
143
145 bool isEmpty() const;
146
148 int count() const;
149
150#ifdef SIP_RUN
151 int __len__() const;
152 % MethodCode
153 sipRes = sipCpp->count();
154 % End
155
157 int __bool__() const;
158 % MethodCode
159 sipRes = true;
160 % End
161#endif
162
164 int size() const;
165
169 QStringList names() const;
170
176 bool exists( int i ) const;
177
178#ifndef SIP_RUN
180 QgsField operator[]( int i ) const;
181#endif
182
184 QgsField &operator[]( int i ) SIP_FACTORY;
185#ifdef SIP_RUN
186 % MethodCode
187 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
188 if ( idx < 0 )
189 sipIsErr = 1;
190 else
191 sipRes = new QgsField( sipCpp->operator[]( idx ) );
192 % End
193#endif
194
195#ifdef SIP_RUN
196 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_TYPEHINT( QgsField );
197 % MethodCode
198 const int fieldIdx = sipCpp->lookupField( *a0 );
199 if ( fieldIdx == -1 )
200 {
201 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
202 sipIsErr = 1;
203 }
204 else
205 {
206 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
207 }
208 % End
209#endif
210
211#ifndef SIP_RUN
212
216 QgsField at( int i ) const SIP_FACTORY;
217#else
218
223 QgsField at( int i ) const SIP_FACTORY;
224 % MethodCode
225 if ( a0 < 0 || a0 >= sipCpp->count() )
226 {
227 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
228 sipIsErr = 1;
229 }
230 else
231 {
232 sipRes = new QgsField( sipCpp->at( a0 ) );
233 }
234 % End
235#endif
236
237#ifndef SIP_RUN
238
242 QgsField field( int fieldIdx ) const SIP_FACTORY;
243#else
244
249 QgsField field( int fieldIdx ) const SIP_FACTORY;
250 % MethodCode
251 if ( a0 < 0 || a0 >= sipCpp->count() )
252 {
253 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
254 sipIsErr = 1;
255 }
256 else
257 {
258 sipRes = new QgsField( sipCpp->field( a0 ) );
259 }
260 % End
261#endif
262
263#ifndef SIP_RUN
264
268 QgsField field( const QString &name ) const SIP_FACTORY;
269#else
270
275 QgsField field( const QString &name ) const SIP_FACTORY;
276 % MethodCode
277 int fieldIdx = sipCpp->indexFromName( *a0 );
278 if ( fieldIdx == -1 )
279 {
280 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
281 sipIsErr = 1;
282 }
283 else
284 {
285 sipRes = new QgsField( sipCpp->field( *a0 ) );
286 }
287 % End
288#endif
289
290#ifndef SIP_RUN
291
295 FieldOrigin fieldOrigin( int fieldIdx ) const;
296#else
297
303 FieldOrigin fieldOrigin( int fieldIdx ) const;
304 % MethodCode
305 if ( a0 < 0 || a0 >= sipCpp->count() )
306 {
307 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
308 sipIsErr = 1;
309 }
310 else
311 {
312 sipRes = sipCpp->fieldOrigin( a0 );
313 }
314 % End
315#endif
316
317#ifndef SIP_RUN
318
322 int fieldOriginIndex( int fieldIdx ) const;
323#else
324
330 int fieldOriginIndex( int fieldIdx ) const;
331 % MethodCode
332 if ( a0 < 0 || a0 >= sipCpp->count() )
333 {
334 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
335 sipIsErr = 1;
336 }
337 else
338 {
339 sipRes = sipCpp->fieldOriginIndex( a0 );
340 }
341 % End
342#endif
343
357 int indexFromName( const QString &fieldName ) const;
358
370 int indexOf( const QString &fieldName ) const;
371
385 int lookupField( const QString &fieldName ) const;
386
390 QgsAttributeList allAttributesList() const;
391
393 QList<QgsField> toList() const;
394
395 bool operator==( const QgsFields &other ) const;
396 bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
397
398#ifndef SIP_RUN
399
405 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
406#else
407
414 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
415 % MethodCode
416 if ( a0 < 0 || a0 >= sipCpp->count() )
417 {
418 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
419 sipIsErr = 1;
420 }
421 else
422 {
423 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
424 }
425 % End
426#endif
427
436 static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType = QVariant::Type::Invalid, const QString &typeString = QString() );
437
439 operator QVariant() const
440 {
441 return QVariant::fromValue( *this );
442 }
443
444#ifdef SIP_RUN
445
446 void __setitem__( int key, const QgsField &field );
447 % MethodCode
448 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
449 if ( idx < 0 )
450 sipIsErr = 1;
451 else
452 ( *sipCpp )[idx] = *a1;
453 % End
454
455#endif
456
457#ifndef SIP_RUN
458
460
461 class const_iterator;
462
463 class iterator
464 {
465 public:
466 QgsFields::Field *d = nullptr;
467 typedef std::random_access_iterator_tag iterator_category;
468 typedef qptrdiff difference_type;
469
470 inline iterator()
471 {}
472
473 inline iterator( QgsFields::Field *n )
474 : d( n )
475 {}
476
477 inline QgsField &operator*() const { return d->field; }
478 inline QgsField *operator->() const { return &d->field; }
479 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
480 inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
481 inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
482 inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
483 inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
484 inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
485 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
486
487 inline iterator &operator++() { ++d; return *this; }
488 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
489 inline iterator &operator--() { d--; return *this; }
490 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
491 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
492 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
493 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
494 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
495 inline int operator-( iterator j ) const { return int( d - j.d ); }
496 };
497 friend class iterator;
498
499 class const_iterator // clazy:exclude=rule-of-three
500 {
501 public:
502 const QgsFields::Field *d = nullptr;
503
504 typedef std::random_access_iterator_tag iterator_category;
505 typedef qptrdiff difference_type;
506
507 inline const_iterator()
508 {}
509
510 inline const_iterator( const QgsFields::Field *f )
511 : d( f ) {}
512 inline const_iterator( const const_iterator &o )
513 : d( o.d ) {}
514 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
515 : d( o.d ) {}
516 inline const QgsField &operator*() const { return d->field; }
517 inline const QgsField *operator->() const { return &d->field; }
518 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
519 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
520 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
521 inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
522 inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
523 inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
524 inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
525 inline const_iterator &operator++() { ++d; return *this; }
526 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
527 inline const_iterator &operator--() { d--; return *this; }
528 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
529 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
530 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
531 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
532 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
533 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
534 private:
535 const_iterator &operator= ( const const_iterator & ) = delete;
536 };
537 friend class const_iterator;
539
540
546 const_iterator constBegin() const noexcept;
547
553 const_iterator constEnd() const noexcept;
554
560 const_iterator begin() const noexcept;
561
567 const_iterator end() const noexcept;
568
574 iterator begin();
575
576
582 iterator end();
583
584#endif
585
586 private:
587
588 QSharedDataPointer<QgsFieldsPrivate> d;
589
590};
591
593
594
595CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
597CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
598
599#endif // QGSFIELDS_H
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:45
bool operator!=(const QgsFields &other) const
Definition qgsfields.h:396
@ OriginEdit
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition qgsfields.h:53
@ OriginUnknown
It has not been specified where the field comes from.
Definition qgsfields.h:50
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition qgsfields.h:52
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition qgsfields.h:51
bool operator>(const QVariant &v1, const QVariant &v2)
Compares two QVariant values and returns whether the first is greater than the second.
Definition qgis.h:5523
bool operator<(const QVariant &v1, const QVariant &v2)
Compares two QVariant values and returns whether the first is less than the second.
Definition qgis.h:5537
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:232
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition qgsfield.h:27
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
QgsInterval operator-(QDate date1, QDate date2)
Returns the interval between two dates.
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition qgsmargins.h:241
int originIndex
Index specific to the origin.
Definition qgsfields.h:77
bool operator==(const Field &other) const
Definition qgsfields.h:72
QgsField field
Field.
Definition qgsfields.h:75
bool operator!=(const Field &other) const
Definition qgsfields.h:73
Field(const QgsField &f, FieldOrigin o, int oi)
Definition qgsfields.h:64
FieldOrigin origin
Origin of the field.
Definition qgsfields.h:76