QGIS API Documentation 4.3.0-Master (621ced71bd7)
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
7 email : matthias@opengis.ch
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#include "qgis.h"
20#include "qgis_core.h"
21#include "qgis_sip.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 Q_GADGET
47
48 Q_PROPERTY( bool isEmpty READ isEmpty )
49 Q_PROPERTY( int count READ count )
50 Q_PROPERTY( QStringList names READ names )
51
52 public:
53#ifndef SIP_RUN
54
55 typedef struct Field
56 {
57 Field() {}
58
59 Field( const QgsField &f, Qgis::FieldOrigin o, int oi )
60 : field( f )
61 , origin( o )
62 , originIndex( oi )
63 {}
64
65 // TODO c++20 - replace with = default
66
67 bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
68 bool operator!=( const Field &other ) const { return !( *this == other ); }
69
72 int originIndex = -1;
73 } Field;
74
75#endif
76
77 // clang-format off
82 // clang-format on
83
84 QgsFields( const QgsFields &other ) SIP_HOLDGIL;
85 QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
86
92 QgsFields( const QList< QgsField > &fields ) SIP_HOLDGIL;
93
94 virtual ~QgsFields();
95
97 void clear() SIP_HOLDGIL;
98
110 bool append( const QgsField &field, Qgis::FieldOrigin origin = Qgis::FieldOrigin::Provider, int originIndex = -1 ) SIP_HOLDGIL;
111
119 bool append( const QList< QgsField > &fields, Qgis::FieldOrigin origin = Qgis::FieldOrigin::Provider ) SIP_HOLDGIL;
120
128 bool append( const QgsFields &fields ) SIP_HOLDGIL;
129
134 bool rename( int fieldIdx, const QString &name ) SIP_HOLDGIL;
135
137 bool appendExpressionField( const QgsField &field, int originIndex ) SIP_HOLDGIL;
138
139#ifndef SIP_RUN
140
144 void remove( int fieldIdx );
145#else
146// clang-format off
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// clang-format on
166#endif
167
169 void extend( const QgsFields &other ) SIP_HOLDGIL;
170
172 bool isEmpty() const SIP_HOLDGIL;
173
175 int count() const SIP_HOLDGIL;
176
177#ifdef SIP_RUN
178// clang-format off
179 int __len__() const SIP_HOLDGIL;
180 % MethodCode
181 sipRes = sipCpp->count();
182 % End
183
185 int __bool__() const SIP_HOLDGIL;
186 % MethodCode
187 sipRes = true;
188 % End
189// clang-format on
190#endif
191
193 int size() const SIP_HOLDGIL;
194
198 QStringList names() const SIP_HOLDGIL;
199
205 Q_INVOKABLE bool exists( int i ) const SIP_HOLDGIL;
206
207#ifndef SIP_RUN
209 QgsField operator[]( int i ) const;
210#endif
211
213 QgsField &operator[]( int i ) SIP_HOLDGIL SIP_FACTORY;
214#ifdef SIP_RUN
215// clang-format off
216 % MethodCode
217 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
218 if ( idx < 0 )
219 sipIsErr = 1;
220 else
221 sipRes = new QgsField( sipCpp->operator[]( idx ) );
222 % End
223// clang-format on
224#endif
225
226#ifdef SIP_RUN
227// clang-format off
228 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_HOLDGIL SIP_TYPEHINT( QgsField );
229 % MethodCode
230 const int fieldIdx = sipCpp->lookupField( *a0 );
231 if ( fieldIdx == -1 )
232 {
233 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
234 sipIsErr = 1;
235 }
236 else
237 {
238 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
239 }
240 % End
241// clang-format on
242#endif
243
244#ifndef SIP_RUN
245
249 QgsField at( int i ) const SIP_FACTORY;
250#else
251// clang-format off
252
257 QgsField at( int i ) 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->at( a0 ) );
267 }
268 % End
269// clang-format on
270#endif
271
272#ifndef SIP_RUN
273
277 QgsField field( int fieldIdx ) const SIP_FACTORY;
278#else
279// clang-format off
280
285 QgsField field( int fieldIdx ) const SIP_HOLDGIL SIP_FACTORY;
286 % MethodCode
287 if ( a0 < 0 || a0 >= sipCpp->count() )
288 {
289 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
290 sipIsErr = 1;
291 }
292 else
293 {
294 sipRes = new QgsField( sipCpp->field( a0 ) );
295 }
296 % End
297// clang-format on
298#endif
299
300#ifndef SIP_RUN
301
305 QgsField field( const QString &name ) const SIP_FACTORY;
306#else
307// clang-format off
308
313 QgsField field( const QString &name ) const SIP_HOLDGIL SIP_FACTORY;
314 % MethodCode
315 int fieldIdx = sipCpp->indexFromName( *a0 );
316 if ( fieldIdx == -1 )
317 {
318 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
319 sipIsErr = 1;
320 }
321 else
322 {
323 sipRes = new QgsField( sipCpp->field( *a0 ) );
324 }
325 % End
326// clang-format on
327#endif
328
329#ifndef SIP_RUN
330
334 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const;
335#else
336// clang-format off
337
343 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const SIP_HOLDGIL;
344 % MethodCode
345 if ( a0 < 0 || a0 >= sipCpp->count() )
346 {
347 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
348 sipIsErr = 1;
349 }
350 else
351 {
352 sipRes = sipCpp->fieldOrigin( a0 );
353 }
354 % End
355// clang-format on
356#endif
357
358#ifndef SIP_RUN
359
367 int fieldOriginIndex( int fieldIdx ) const;
368#else
369// clang-format off
370
380 int fieldOriginIndex( int fieldIdx ) const SIP_HOLDGIL;
381 % MethodCode
382 if ( a0 < 0 || a0 >= sipCpp->count() )
383 {
384 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
385 sipIsErr = 1;
386 }
387 else
388 {
389 sipRes = sipCpp->fieldOriginIndex( a0 );
390 }
391 % End
392// clang-format on
393#endif
394
408 Q_INVOKABLE int indexFromName( const QString &fieldName ) const SIP_HOLDGIL;
409
421 Q_INVOKABLE int indexOf( const QString &fieldName ) const SIP_HOLDGIL;
422
436 Q_INVOKABLE int lookupField( const QString &fieldName ) const SIP_HOLDGIL;
437
441 QgsAttributeList allAttributesList() const SIP_HOLDGIL;
442
444 QList<QgsField> toList() const SIP_HOLDGIL;
445
446 bool operator==( const QgsFields &other ) const SIP_HOLDGIL;
447 bool operator!=( const QgsFields &other ) const SIP_HOLDGIL { return !( *this == other ); }
448
449#ifndef SIP_RUN
450
456 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
457#else
458// clang-format off
459
466 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
467 % MethodCode
468 if ( a0 < 0 || a0 >= sipCpp->count() )
469 {
470 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
471 sipIsErr = 1;
472 }
473 else
474 {
475 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
476 }
477 % End
478// clang-format on
479#endif
480
489 static QIcon iconForFieldType( QMetaType::Type type, QMetaType::Type subType = QMetaType::Type::UnknownType, const QString &typeString = QString() );
490
500 Q_DECL_DEPRECATED static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType, const QString &typeString = QString() ) SIP_DEPRECATED;
501
503 operator QVariant() const
504 {
505 return QVariant::fromValue( *this );
506 }
507
508#ifdef SIP_RUN
509// clang-format off
510
511 void __setitem__( int key, const QgsField &field ) SIP_HOLDGIL;
512 % MethodCode
513 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
514 if ( idx < 0 )
515 sipIsErr = 1;
516 else
517 ( *sipCpp )[idx] = *a1;
518 % End
519
520// clang-format on
521#endif
522
523#ifndef SIP_RUN
524
526
527 class const_iterator;
528
529 class iterator
530 {
531 public:
532 QgsFields::Field *d = nullptr;
533 typedef std::random_access_iterator_tag iterator_category;
534 typedef qptrdiff difference_type;
535
536 inline iterator()
537 {}
538
539 inline iterator( QgsFields::Field *n )
540 : d( n )
541 {}
542
543 inline QgsField &operator*() const { return d->field; }
544 inline QgsField *operator->() const { return &d->field; }
545 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
546 inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
547 inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
548 inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
549 inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
550 inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
551 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
552
553 inline iterator &operator++() { ++d; return *this; }
554 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
555 inline iterator &operator--() { d--; return *this; }
556 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
557 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
558 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
559 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
560 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
561 inline int operator-( iterator j ) const { return int( d - j.d ); }
562 };
563 friend class iterator;
564
565 class const_iterator // clazy:exclude=rule-of-three
566 {
567 public:
568 const QgsFields::Field *d = nullptr;
569
570 typedef std::random_access_iterator_tag iterator_category;
571 typedef qptrdiff difference_type;
572
573 inline const_iterator()
574 {}
575
576 inline const_iterator( const QgsFields::Field *f )
577 : d( f ) {}
578 inline const_iterator( const const_iterator &o )
579 : d( o.d ) {}
580 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
581 : d( o.d ) {}
582 inline const QgsField &operator*() const { return d->field; }
583 inline const QgsField *operator->() const { return &d->field; }
584 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
585 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
586 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
587 inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
588 inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
589 inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
590 inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
591 inline const_iterator &operator++() { ++d; return *this; }
592 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
593 inline const_iterator &operator--() { d--; return *this; }
594 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
595 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
596 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
597 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
598 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
599 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
600 private:
601 const_iterator &operator= ( const const_iterator & ) = delete;
602 };
603 friend class const_iterator;
605
606
612 const_iterator constBegin() const noexcept;
613
619 const_iterator constEnd() const noexcept;
620
626 const_iterator begin() const noexcept;
627
633 const_iterator end() const noexcept;
634
640 iterator begin();
641
642
648 iterator end();
649
650#endif
651
652 private:
653
654 QSharedDataPointer<QgsFieldsPrivate> d;
655
656};
657
659
660
661CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
663CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
664
665#endif // QGSFIELDS_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
FieldOrigin
Field origin.
Definition qgis.h:1854
@ Unknown
The field origin has not been specified.
Definition qgis.h:1855
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
int count
Definition qgsfields.h:49
struct QgsFields::Field Field
bool isEmpty
Definition qgsfields.h:48
QgsFields()
Constructor for an empty field container.
Definition qgsfields.cpp:36
QStringList names
Definition qgsfields.h:50
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:7936
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:7952
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:237
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_HOLDGIL
Definition qgis_sip.h:178
#define SIP_FACTORY
Definition qgis_sip.h:83
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:30
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:245
int originIndex
Index specific to the origin.
Definition qgsfields.h:72
Qgis::FieldOrigin origin
Origin of the field.
Definition qgsfields.h:71
Field(const QgsField &f, Qgis::FieldOrigin o, int oi)
Definition qgsfields.h:59
bool operator==(const Field &other) const
Definition qgsfields.h:67
QgsField field
Field.
Definition qgsfields.h:70
bool operator!=(const Field &other) const
Definition qgsfields.h:68