QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
73 bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
75 bool operator!=( const Field &other ) const { return !( *this == other ); }
76
78 FieldOrigin origin = OriginUnknown ;
79 int originIndex = -1 ;
80 } Field;
81
82#endif
83
87 QgsFields();
88
92 QgsFields( const QgsFields &other );
93
97 QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
98
99 virtual ~QgsFields();
100
102 void clear();
103
105 bool append( const QgsField &field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
106
111 bool rename( int fieldIdx, const QString &name );
112
114 bool appendExpressionField( const QgsField &field, int originIndex );
115
116#ifndef SIP_RUN
117
121 void remove( int fieldIdx );
122#else
123
129 void remove( int fieldIdx );
130 % MethodCode
131 if ( a0 < 0 || a0 >= sipCpp->count() )
132 {
133 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
134 sipIsErr = 1;
135 }
136 else
137 {
138 sipCpp->remove( a0 );
139 }
140 % End
141#endif
142
144 void extend( const QgsFields &other );
145
147 bool isEmpty() const;
148
150 int count() const;
151
152#ifdef SIP_RUN
153 int __len__() const;
154 % MethodCode
155 sipRes = sipCpp->count();
156 % End
157
159 int __bool__() const;
160 % MethodCode
161 sipRes = true;
162 % End
163#endif
164
166 int size() const;
167
172 QStringList names() const;
173
179 bool exists( int i ) const;
180
181#ifndef SIP_RUN
183 QgsField operator[]( int i ) const;
184#endif
185
187 QgsField &operator[]( int i ) SIP_FACTORY;
188#ifdef SIP_RUN
189 % MethodCode
190 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
191 if ( idx < 0 )
192 sipIsErr = 1;
193 else
194 sipRes = new QgsField( sipCpp->operator[]( idx ) );
195 % End
196#endif
197
198#ifdef SIP_RUN
199 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_TYPEHINT( QgsField );
200 % MethodCode
201 const int fieldIdx = sipCpp->lookupField( *a0 );
202 if ( fieldIdx == -1 )
203 {
204 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
205 sipIsErr = 1;
206 }
207 else
208 {
209 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
210 }
211 % End
212#endif
213
214#ifndef SIP_RUN
215
219 QgsField at( int i ) const SIP_FACTORY;
220#else
221
226 QgsField at( int i ) const SIP_FACTORY;
227 % MethodCode
228 if ( a0 < 0 || a0 >= sipCpp->count() )
229 {
230 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
231 sipIsErr = 1;
232 }
233 else
234 {
235 sipRes = new QgsField( sipCpp->at( a0 ) );
236 }
237 % End
238#endif
239
240#ifndef SIP_RUN
241
245 QgsField field( int fieldIdx ) const SIP_FACTORY;
246#else
247
252 QgsField field( int fieldIdx ) const SIP_FACTORY;
253 % MethodCode
254 if ( a0 < 0 || a0 >= sipCpp->count() )
255 {
256 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
257 sipIsErr = 1;
258 }
259 else
260 {
261 sipRes = new QgsField( sipCpp->field( a0 ) );
262 }
263 % End
264#endif
265
266#ifndef SIP_RUN
267
271 QgsField field( const QString &name ) const SIP_FACTORY;
272#else
273
278 QgsField field( const QString &name ) const SIP_FACTORY;
279 % MethodCode
280 int fieldIdx = sipCpp->indexFromName( *a0 );
281 if ( fieldIdx == -1 )
282 {
283 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
284 sipIsErr = 1;
285 }
286 else
287 {
288 sipRes = new QgsField( sipCpp->field( *a0 ) );
289 }
290 % End
291#endif
292
293#ifndef SIP_RUN
294
298 FieldOrigin fieldOrigin( int fieldIdx ) const;
299#else
300
306 FieldOrigin fieldOrigin( int fieldIdx ) const;
307 % MethodCode
308 if ( a0 < 0 || a0 >= sipCpp->count() )
309 {
310 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
311 sipIsErr = 1;
312 }
313 else
314 {
315 sipRes = sipCpp->fieldOrigin( a0 );
316 }
317 % End
318#endif
319
320#ifndef SIP_RUN
321
325 int fieldOriginIndex( int fieldIdx ) const;
326#else
327
333 int fieldOriginIndex( int fieldIdx ) const;
334 % MethodCode
335 if ( a0 < 0 || a0 >= sipCpp->count() )
336 {
337 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
338 sipIsErr = 1;
339 }
340 else
341 {
342 sipRes = sipCpp->fieldOriginIndex( a0 );
343 }
344 % End
345#endif
346
360 int indexFromName( const QString &fieldName ) const;
361
374 int indexOf( const QString &fieldName ) const;
375
390 int lookupField( const QString &fieldName ) const;
391
396 QgsAttributeList allAttributesList() const;
397
399 QList<QgsField> toList() const;
400
402 bool operator==( const QgsFields &other ) const;
404 bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
405
406#ifndef SIP_RUN
407
414 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
415#else
416
424 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
425 % MethodCode
426 if ( a0 < 0 || a0 >= sipCpp->count() )
427 {
428 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
429 sipIsErr = 1;
430 }
431 else
432 {
433 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
434 }
435 % End
436#endif
437
446 static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType = QVariant::Type::Invalid, const QString &typeString = QString() );
447
449 operator QVariant() const
450 {
451 return QVariant::fromValue( *this );
452 }
453
454#ifdef SIP_RUN
455
456 void __setitem__( int key, const QgsField &field );
457 % MethodCode
458 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
459 if ( idx < 0 )
460 sipIsErr = 1;
461 else
462 ( *sipCpp )[idx] = *a1;
463 % End
464
465#endif
466
467#ifndef SIP_RUN
468
470
471 class const_iterator;
472
473 class iterator
474 {
475 public:
476 QgsFields::Field *d = nullptr;
477 typedef std::random_access_iterator_tag iterator_category;
478 typedef qptrdiff difference_type;
479
480 inline iterator()
481 {}
482
483 inline iterator( QgsFields::Field *n )
484 : d( n )
485 {}
486
487 inline QgsField &operator*() const { return d->field; }
488 inline QgsField *operator->() const { return &d->field; }
489 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
490 inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
491 inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
492 inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
493 inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
494 inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
495 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
496
497 inline iterator &operator++() { ++d; return *this; }
498 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
499 inline iterator &operator--() { d--; return *this; }
500 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
501 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
502 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
503 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
504 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
505 inline int operator-( iterator j ) const { return int( d - j.d ); }
506 };
507 friend class iterator;
508
509 class const_iterator // clazy:exclude=rule-of-three
510 {
511 public:
512 const QgsFields::Field *d = nullptr;
513
514 typedef std::random_access_iterator_tag iterator_category;
515 typedef qptrdiff difference_type;
516
517 inline const_iterator()
518 {}
519
520 inline const_iterator( const QgsFields::Field *f )
521 : d( f ) {}
522 inline const_iterator( const const_iterator &o )
523 : d( o.d ) {}
524 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
525 : d( o.d ) {}
526 inline const QgsField &operator*() const { return d->field; }
527 inline const QgsField *operator->() const { return &d->field; }
528 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
529 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
530 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
531 inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
532 inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
533 inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
534 inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
535 inline const_iterator &operator++() { ++d; return *this; }
536 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
537 inline const_iterator &operator--() { d--; return *this; }
538 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
539 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
540 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
541 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
542 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
543 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
544 private:
545 const_iterator &operator= ( const const_iterator & ) = delete;
546 };
547 friend class const_iterator;
549
550
557 const_iterator constBegin() const noexcept;
558
565 const_iterator constEnd() const noexcept;
566
573 const_iterator begin() const noexcept;
574
581 const_iterator end() const noexcept;
582
589 iterator begin();
590
591
598 iterator end();
599
600#endif
601
602 private:
603
604 QSharedDataPointer<QgsFieldsPrivate> d;
605
606};
607
609
610
611CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
613CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
614
615#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:404
@ 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:4726
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:4740
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:227
#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:242
int originIndex
Index specific to the origin.
Definition qgsfields.h:79
bool operator==(const Field &other) const
Definition qgsfields.h:73
QgsField field
Field.
Definition qgsfields.h:77
bool operator!=(const Field &other) const
Definition qgsfields.h:75
Field(const QgsField &f, FieldOrigin o, int oi)
Definition qgsfields.h:64
FieldOrigin origin
Origin of the field.
Definition qgsfields.h:78