QGIS API Documentation 3.39.0-Master (52f98f8c831)
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
88 QgsFields( const QgsFields &other ) SIP_HOLDGIL;
89
93 QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
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
117 bool rename( int fieldIdx, const QString &name ) SIP_HOLDGIL;
118
120 bool appendExpressionField( const QgsField &field, int originIndex ) SIP_HOLDGIL;
121
122#ifndef SIP_RUN
123
127 void remove( int fieldIdx );
128#else
129
135 void remove( int fieldIdx ) SIP_HOLDGIL;
136 % MethodCode
137 if ( a0 < 0 || a0 >= sipCpp->count() )
138 {
139 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
140 sipIsErr = 1;
141 }
142 else
143 {
144 sipCpp->remove( a0 );
145 }
146 % End
147#endif
148
150 void extend( const QgsFields &other ) SIP_HOLDGIL;
151
153 bool isEmpty() const SIP_HOLDGIL;
154
156 int count() const SIP_HOLDGIL;
157
158#ifdef SIP_RUN
159 int __len__() const SIP_HOLDGIL;
160 % MethodCode
161 sipRes = sipCpp->count();
162 % End
163
165 int __bool__() const SIP_HOLDGIL;
166 % MethodCode
167 sipRes = true;
168 % End
169#endif
170
172 int size() const SIP_HOLDGIL;
173
177 QStringList names() const SIP_HOLDGIL;
178
184 Q_INVOKABLE bool exists( int i ) const SIP_HOLDGIL;
185
186#ifndef SIP_RUN
188 QgsField operator[]( int i ) const;
189#endif
190
192 QgsField &operator[]( int i ) SIP_HOLDGIL SIP_FACTORY;
193#ifdef SIP_RUN
194 % MethodCode
195 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
196 if ( idx < 0 )
197 sipIsErr = 1;
198 else
199 sipRes = new QgsField( sipCpp->operator[]( idx ) );
200 % End
201#endif
202
203#ifdef SIP_RUN
204 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_HOLDGIL SIP_TYPEHINT( QgsField );
205 % MethodCode
206 const int fieldIdx = sipCpp->lookupField( *a0 );
207 if ( fieldIdx == -1 )
208 {
209 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
210 sipIsErr = 1;
211 }
212 else
213 {
214 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
215 }
216 % End
217#endif
218
219#ifndef SIP_RUN
220
224 QgsField at( int i ) const SIP_FACTORY;
225#else
226
231 QgsField at( int i ) const SIP_HOLDGIL SIP_FACTORY;
232 % MethodCode
233 if ( a0 < 0 || a0 >= sipCpp->count() )
234 {
235 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
236 sipIsErr = 1;
237 }
238 else
239 {
240 sipRes = new QgsField( sipCpp->at( a0 ) );
241 }
242 % End
243#endif
244
245#ifndef SIP_RUN
246
250 QgsField field( int fieldIdx ) const SIP_FACTORY;
251#else
252
257 QgsField field( int fieldIdx ) const SIP_HOLDGIL SIP_FACTORY;
258 % MethodCode
259 if ( a0 < 0 || a0 >= sipCpp->count() )
260 {
261 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
262 sipIsErr = 1;
263 }
264 else
265 {
266 sipRes = new QgsField( sipCpp->field( a0 ) );
267 }
268 % End
269#endif
270
271#ifndef SIP_RUN
272
276 QgsField field( const QString &name ) const SIP_FACTORY;
277#else
278
283 QgsField field( const QString &name ) const SIP_HOLDGIL SIP_FACTORY;
284 % MethodCode
285 int fieldIdx = sipCpp->indexFromName( *a0 );
286 if ( fieldIdx == -1 )
287 {
288 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
289 sipIsErr = 1;
290 }
291 else
292 {
293 sipRes = new QgsField( sipCpp->field( *a0 ) );
294 }
295 % End
296#endif
297
298#ifndef SIP_RUN
299
303 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const;
304#else
305
311 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const SIP_HOLDGIL;
312 % MethodCode
313 if ( a0 < 0 || a0 >= sipCpp->count() )
314 {
315 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
316 sipIsErr = 1;
317 }
318 else
319 {
320 sipRes = sipCpp->fieldOrigin( a0 );
321 }
322 % End
323#endif
324
325#ifndef SIP_RUN
326
334 int fieldOriginIndex( int fieldIdx ) const;
335#else
336
346 int fieldOriginIndex( int fieldIdx ) const SIP_HOLDGIL;
347 % MethodCode
348 if ( a0 < 0 || a0 >= sipCpp->count() )
349 {
350 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
351 sipIsErr = 1;
352 }
353 else
354 {
355 sipRes = sipCpp->fieldOriginIndex( a0 );
356 }
357 % End
358#endif
359
373 Q_INVOKABLE int indexFromName( const QString &fieldName ) const SIP_HOLDGIL;
374
386 Q_INVOKABLE int indexOf( const QString &fieldName ) const SIP_HOLDGIL;
387
401 Q_INVOKABLE int lookupField( const QString &fieldName ) const SIP_HOLDGIL;
402
406 QgsAttributeList allAttributesList() const SIP_HOLDGIL;
407
409 QList<QgsField> toList() const SIP_HOLDGIL;
410
411 bool operator==( const QgsFields &other ) const SIP_HOLDGIL;
412 bool operator!=( const QgsFields &other ) const SIP_HOLDGIL { return !( *this == other ); }
413
414#ifndef SIP_RUN
415
421 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
422#else
423
430 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
431 % MethodCode
432 if ( a0 < 0 || a0 >= sipCpp->count() )
433 {
434 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
435 sipIsErr = 1;
436 }
437 else
438 {
439 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
440 }
441 % End
442#endif
443
452 static QIcon iconForFieldType( QMetaType::Type type, QMetaType::Type subType = QMetaType::Type::UnknownType, const QString &typeString = QString() );
453
463 Q_DECL_DEPRECATED static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType, const QString &typeString = QString() ) SIP_DEPRECATED;
464
466 operator QVariant() const
467 {
468 return QVariant::fromValue( *this );
469 }
470
471#ifdef SIP_RUN
472
473 void __setitem__( int key, const QgsField &field ) SIP_HOLDGIL;
474 % MethodCode
475 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
476 if ( idx < 0 )
477 sipIsErr = 1;
478 else
479 ( *sipCpp )[idx] = *a1;
480 % End
481
482#endif
483
484#ifndef SIP_RUN
485
487
488 class const_iterator;
489
490 class iterator
491 {
492 public:
493 QgsFields::Field *d = nullptr;
494 typedef std::random_access_iterator_tag iterator_category;
495 typedef qptrdiff difference_type;
496
497 inline iterator()
498 {}
499
500 inline iterator( QgsFields::Field *n )
501 : d( n )
502 {}
503
504 inline QgsField &operator*() const { return d->field; }
505 inline QgsField *operator->() const { return &d->field; }
506 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
507 inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
508 inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
509 inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
510 inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
511 inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
512 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
513
514 inline iterator &operator++() { ++d; return *this; }
515 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
516 inline iterator &operator--() { d--; return *this; }
517 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
518 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
519 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
520 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
521 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
522 inline int operator-( iterator j ) const { return int( d - j.d ); }
523 };
524 friend class iterator;
525
526 class const_iterator // clazy:exclude=rule-of-three
527 {
528 public:
529 const QgsFields::Field *d = nullptr;
530
531 typedef std::random_access_iterator_tag iterator_category;
532 typedef qptrdiff difference_type;
533
534 inline const_iterator()
535 {}
536
537 inline const_iterator( const QgsFields::Field *f )
538 : d( f ) {}
539 inline const_iterator( const const_iterator &o )
540 : d( o.d ) {}
541 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
542 : d( o.d ) {}
543 inline const QgsField &operator*() const { return d->field; }
544 inline const QgsField *operator->() const { return &d->field; }
545 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
546 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
547 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
548 inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
549 inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
550 inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
551 inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
552 inline const_iterator &operator++() { ++d; return *this; }
553 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
554 inline const_iterator &operator--() { d--; return *this; }
555 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
556 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
557 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
558 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
559 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
560 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
561 private:
562 const_iterator &operator= ( const const_iterator & ) = delete;
563 };
564 friend class const_iterator;
566
567
573 const_iterator constBegin() const noexcept;
574
580 const_iterator constEnd() const noexcept;
581
587 const_iterator begin() const noexcept;
588
594 const_iterator end() const noexcept;
595
601 iterator begin();
602
603
609 iterator end();
610
611#endif
612
613 private:
614
615 QSharedDataPointer<QgsFieldsPrivate> d;
616
617};
618
620
621
622CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
624CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
625
626#endif // QGSFIELDS_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
FieldOrigin
Field origin.
Definition qgis.h:1374
@ 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:5852
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:5866
#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:241
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