QGIS API Documentation 3.41.0-Master (25ec5511245)
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 "qgis.h"
23#include "qgsfield.h"
24
25class QgsFieldsPrivate;
26
27/***************************************************************************
28 * This class is considered CRITICAL and any change MUST be accompanied with
29 * full unit tests in testqgsfields.cpp.
30 * See details in QEP #17
31 ****************************************************************************/
32
45class CORE_EXPORT QgsFields
46{
47 Q_GADGET
48
49 Q_PROPERTY( bool isEmpty READ isEmpty )
50 Q_PROPERTY( int count READ count )
51 Q_PROPERTY( QStringList names READ names )
52
53 public:
54
55#ifndef SIP_RUN
56
57 typedef struct Field
58 {
60 {}
61
62 Field( const QgsField &f, Qgis::FieldOrigin o, int oi )
63 : field( f )
64 , origin( o )
65 , originIndex( oi )
66 {}
67
68 // TODO c++20 - replace with = default
69
70 bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
71 bool operator!=( const Field &other ) const { return !( *this == other ); }
72
75 int originIndex = -1 ;
76 } Field;
77
78#endif
79
84
85 QgsFields( const QgsFields &other ) SIP_HOLDGIL;
86 QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
87
93 QgsFields( const QList< QgsField > &fields ) SIP_HOLDGIL;
94
95 virtual ~QgsFields();
96
98 void clear() SIP_HOLDGIL;
99
111 bool append( const QgsField &field, Qgis::FieldOrigin origin = Qgis::FieldOrigin::Provider, int originIndex = -1 ) SIP_HOLDGIL;
112
120 bool append( const QList< QgsField > &fields, Qgis::FieldOrigin origin = Qgis::FieldOrigin::Provider ) SIP_HOLDGIL;
121
129 bool append( const QgsFields &fields ) SIP_HOLDGIL;
130
135 bool rename( int fieldIdx, const QString &name ) SIP_HOLDGIL;
136
138 bool appendExpressionField( const QgsField &field, int originIndex ) SIP_HOLDGIL;
139
140#ifndef SIP_RUN
141
145 void remove( int fieldIdx );
146#else
147
153 void remove( int fieldIdx ) SIP_HOLDGIL;
154 % MethodCode
155 if ( a0 < 0 || a0 >= sipCpp->count() )
156 {
157 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
158 sipIsErr = 1;
159 }
160 else
161 {
162 sipCpp->remove( a0 );
163 }
164 % End
165#endif
166
168 void extend( const QgsFields &other ) SIP_HOLDGIL;
169
171 bool isEmpty() const SIP_HOLDGIL;
172
174 int count() const SIP_HOLDGIL;
175
176#ifdef SIP_RUN
177 int __len__() const SIP_HOLDGIL;
178 % MethodCode
179 sipRes = sipCpp->count();
180 % End
181
183 int __bool__() const SIP_HOLDGIL;
184 % MethodCode
185 sipRes = true;
186 % End
187#endif
188
190 int size() const SIP_HOLDGIL;
191
195 QStringList names() const SIP_HOLDGIL;
196
202 Q_INVOKABLE bool exists( int i ) const SIP_HOLDGIL;
203
204#ifndef SIP_RUN
206 QgsField operator[]( int i ) const;
207#endif
208
210 QgsField &operator[]( int i ) SIP_HOLDGIL SIP_FACTORY;
211#ifdef SIP_RUN
212 % MethodCode
213 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
214 if ( idx < 0 )
215 sipIsErr = 1;
216 else
217 sipRes = new QgsField( sipCpp->operator[]( idx ) );
218 % End
219#endif
220
221#ifdef SIP_RUN
222 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_HOLDGIL SIP_TYPEHINT( QgsField );
223 % MethodCode
224 const int fieldIdx = sipCpp->lookupField( *a0 );
225 if ( fieldIdx == -1 )
226 {
227 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
228 sipIsErr = 1;
229 }
230 else
231 {
232 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
233 }
234 % End
235#endif
236
237#ifndef SIP_RUN
238
242 QgsField at( int i ) const SIP_FACTORY;
243#else
244
249 QgsField at( int i ) const SIP_HOLDGIL 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->at( a0 ) );
259 }
260 % End
261#endif
262
263#ifndef SIP_RUN
264
268 QgsField field( int fieldIdx ) const SIP_FACTORY;
269#else
270
275 QgsField field( int fieldIdx ) const SIP_HOLDGIL SIP_FACTORY;
276 % MethodCode
277 if ( a0 < 0 || a0 >= sipCpp->count() )
278 {
279 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
280 sipIsErr = 1;
281 }
282 else
283 {
284 sipRes = new QgsField( sipCpp->field( a0 ) );
285 }
286 % End
287#endif
288
289#ifndef SIP_RUN
290
294 QgsField field( const QString &name ) const SIP_FACTORY;
295#else
296
301 QgsField field( const QString &name ) const SIP_HOLDGIL SIP_FACTORY;
302 % MethodCode
303 int fieldIdx = sipCpp->indexFromName( *a0 );
304 if ( fieldIdx == -1 )
305 {
306 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
307 sipIsErr = 1;
308 }
309 else
310 {
311 sipRes = new QgsField( sipCpp->field( *a0 ) );
312 }
313 % End
314#endif
315
316#ifndef SIP_RUN
317
321 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const;
322#else
323
329 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const SIP_HOLDGIL;
330 % MethodCode
331 if ( a0 < 0 || a0 >= sipCpp->count() )
332 {
333 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
334 sipIsErr = 1;
335 }
336 else
337 {
338 sipRes = sipCpp->fieldOrigin( a0 );
339 }
340 % End
341#endif
342
343#ifndef SIP_RUN
344
352 int fieldOriginIndex( int fieldIdx ) const;
353#else
354
364 int fieldOriginIndex( int fieldIdx ) const SIP_HOLDGIL;
365 % MethodCode
366 if ( a0 < 0 || a0 >= sipCpp->count() )
367 {
368 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
369 sipIsErr = 1;
370 }
371 else
372 {
373 sipRes = sipCpp->fieldOriginIndex( a0 );
374 }
375 % End
376#endif
377
391 Q_INVOKABLE int indexFromName( const QString &fieldName ) const SIP_HOLDGIL;
392
404 Q_INVOKABLE int indexOf( const QString &fieldName ) const SIP_HOLDGIL;
405
419 Q_INVOKABLE int lookupField( const QString &fieldName ) const SIP_HOLDGIL;
420
424 QgsAttributeList allAttributesList() const SIP_HOLDGIL;
425
427 QList<QgsField> toList() const SIP_HOLDGIL;
428
429 bool operator==( const QgsFields &other ) const SIP_HOLDGIL;
430 bool operator!=( const QgsFields &other ) const SIP_HOLDGIL { return !( *this == other ); }
431
432#ifndef SIP_RUN
433
439 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
440#else
441
448 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
449 % MethodCode
450 if ( a0 < 0 || a0 >= sipCpp->count() )
451 {
452 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
453 sipIsErr = 1;
454 }
455 else
456 {
457 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
458 }
459 % End
460#endif
461
470 static QIcon iconForFieldType( QMetaType::Type type, QMetaType::Type subType = QMetaType::Type::UnknownType, const QString &typeString = QString() );
471
481 Q_DECL_DEPRECATED static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType, const QString &typeString = QString() ) SIP_DEPRECATED;
482
484 operator QVariant() const
485 {
486 return QVariant::fromValue( *this );
487 }
488
489#ifdef SIP_RUN
490
491 void __setitem__( int key, const QgsField &field ) SIP_HOLDGIL;
492 % MethodCode
493 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
494 if ( idx < 0 )
495 sipIsErr = 1;
496 else
497 ( *sipCpp )[idx] = *a1;
498 % End
499
500#endif
501
502#ifndef SIP_RUN
503
505
506 class const_iterator;
507
508 class iterator
509 {
510 public:
511 QgsFields::Field *d = nullptr;
512 typedef std::random_access_iterator_tag iterator_category;
513 typedef qptrdiff difference_type;
514
515 inline iterator()
516 {}
517
518 inline iterator( QgsFields::Field *n )
519 : d( n )
520 {}
521
522 inline QgsField &operator*() const { return d->field; }
523 inline QgsField *operator->() const { return &d->field; }
524 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
525 inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
526 inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
527 inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
528 inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
529 inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
530 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
531
532 inline iterator &operator++() { ++d; return *this; }
533 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
534 inline iterator &operator--() { d--; return *this; }
535 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
536 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
537 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
538 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
539 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
540 inline int operator-( iterator j ) const { return int( d - j.d ); }
541 };
542 friend class iterator;
543
544 class const_iterator // clazy:exclude=rule-of-three
545 {
546 public:
547 const QgsFields::Field *d = nullptr;
548
549 typedef std::random_access_iterator_tag iterator_category;
550 typedef qptrdiff difference_type;
551
552 inline const_iterator()
553 {}
554
555 inline const_iterator( const QgsFields::Field *f )
556 : d( f ) {}
557 inline const_iterator( const const_iterator &o )
558 : d( o.d ) {}
559 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
560 : d( o.d ) {}
561 inline const QgsField &operator*() const { return d->field; }
562 inline const QgsField *operator->() const { return &d->field; }
563 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
564 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
565 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
566 inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
567 inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
568 inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
569 inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
570 inline const_iterator &operator++() { ++d; return *this; }
571 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
572 inline const_iterator &operator--() { d--; return *this; }
573 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
574 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
575 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
576 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
577 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
578 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
579 private:
580 const_iterator &operator= ( const const_iterator & ) = delete;
581 };
582 friend class const_iterator;
584
585
591 const_iterator constBegin() const noexcept;
592
598 const_iterator constEnd() const noexcept;
599
605 const_iterator begin() const noexcept;
606
612 const_iterator end() const noexcept;
613
619 iterator begin();
620
621
627 iterator end();
628
629#endif
630
631 private:
632
633 QSharedDataPointer<QgsFieldsPrivate> d;
634
635};
636
638
639
640CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
642CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
643
644#endif // QGSFIELDS_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
FieldOrigin
Field origin.
Definition qgis.h:1551
@ Unknown
The field origin has not been specified.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
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:6336
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:6350
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:232
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_HOLDGIL
Definition qgis_sip.h:171
#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:249
int originIndex
Index specific to the origin.
Definition qgsfields.h:75
Qgis::FieldOrigin origin
Origin of the field.
Definition qgsfields.h:74
Field(const QgsField &f, Qgis::FieldOrigin o, int oi)
Definition qgsfields.h:62
bool operator==(const Field &other) const
Definition qgsfields.h:70
QgsField field
Field.
Definition qgsfields.h:73
bool operator!=(const Field &other) const
Definition qgsfields.h:71