QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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.h"
21#include "qgis_core.h"
22#include "qgis_sip.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#ifndef SIP_RUN
55
56 typedef struct Field
57 {
58 Field() {}
59
60 Field( const QgsField &f, Qgis::FieldOrigin o, int oi )
61 : field( f )
62 , origin( o )
63 , originIndex( oi )
64 {}
65
66 // TODO c++20 - replace with = default
67
68 bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
69 bool operator!=( const Field &other ) const { return !( *this == other ); }
70
73 int originIndex = -1;
74 } Field;
75
76#endif
77
78 // clang-format off
83 // clang-format on
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// clang-format off
148
154 void remove( int fieldIdx ) SIP_HOLDGIL;
155 % MethodCode
156 if ( a0 < 0 || a0 >= sipCpp->count() )
157 {
158 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
159 sipIsErr = 1;
160 }
161 else
162 {
163 sipCpp->remove( a0 );
164 }
165 % End
166// clang-format on
167#endif
168
170 void extend( const QgsFields &other ) SIP_HOLDGIL;
171
173 bool isEmpty() const SIP_HOLDGIL;
174
176 int count() const SIP_HOLDGIL;
177
178#ifdef SIP_RUN
179// clang-format off
180 int __len__() const SIP_HOLDGIL;
181 % MethodCode
182 sipRes = sipCpp->count();
183 % End
184
186 int __bool__() const SIP_HOLDGIL;
187 % MethodCode
188 sipRes = true;
189 % End
190// clang-format on
191#endif
192
194 int size() const SIP_HOLDGIL;
195
199 QStringList names() const SIP_HOLDGIL;
200
206 Q_INVOKABLE bool exists( int i ) const SIP_HOLDGIL;
207
208#ifndef SIP_RUN
210 QgsField operator[]( int i ) const;
211#endif
212
214 QgsField &operator[]( int i ) SIP_HOLDGIL SIP_FACTORY;
215#ifdef SIP_RUN
216// clang-format off
217 % MethodCode
218 SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
219 if ( idx < 0 )
220 sipIsErr = 1;
221 else
222 sipRes = new QgsField( sipCpp->operator[]( idx ) );
223 % End
224// clang-format on
225#endif
226
227#ifdef SIP_RUN
228// clang-format off
229 SIP_PYOBJECT __getitem__( const QString &name ) const SIP_HOLDGIL SIP_TYPEHINT( QgsField );
230 % MethodCode
231 const int fieldIdx = sipCpp->lookupField( *a0 );
232 if ( fieldIdx == -1 )
233 {
234 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
235 sipIsErr = 1;
236 }
237 else
238 {
239 sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
240 }
241 % End
242// clang-format on
243#endif
244
245#ifndef SIP_RUN
246
250 QgsField at( int i ) const SIP_FACTORY;
251#else
252// clang-format off
253
258 QgsField at( int i ) const SIP_HOLDGIL SIP_FACTORY;
259 % MethodCode
260 if ( a0 < 0 || a0 >= sipCpp->count() )
261 {
262 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
263 sipIsErr = 1;
264 }
265 else
266 {
267 sipRes = new QgsField( sipCpp->at( a0 ) );
268 }
269 % End
270// clang-format on
271#endif
272
273#ifndef SIP_RUN
274
278 QgsField field( int fieldIdx ) const SIP_FACTORY;
279#else
280// clang-format off
281
286 QgsField field( int fieldIdx ) const SIP_HOLDGIL SIP_FACTORY;
287 % MethodCode
288 if ( a0 < 0 || a0 >= sipCpp->count() )
289 {
290 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
291 sipIsErr = 1;
292 }
293 else
294 {
295 sipRes = new QgsField( sipCpp->field( a0 ) );
296 }
297 % End
298// clang-format on
299#endif
300
301#ifndef SIP_RUN
302
306 QgsField field( const QString &name ) const SIP_FACTORY;
307#else
308// clang-format off
309
314 QgsField field( const QString &name ) const SIP_HOLDGIL SIP_FACTORY;
315 % MethodCode
316 int fieldIdx = sipCpp->indexFromName( *a0 );
317 if ( fieldIdx == -1 )
318 {
319 PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
320 sipIsErr = 1;
321 }
322 else
323 {
324 sipRes = new QgsField( sipCpp->field( *a0 ) );
325 }
326 % End
327// clang-format on
328#endif
329
330#ifndef SIP_RUN
331
335 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const;
336#else
337// clang-format off
338
344 Qgis::FieldOrigin fieldOrigin( int fieldIdx ) const SIP_HOLDGIL;
345 % MethodCode
346 if ( a0 < 0 || a0 >= sipCpp->count() )
347 {
348 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
349 sipIsErr = 1;
350 }
351 else
352 {
353 sipRes = sipCpp->fieldOrigin( a0 );
354 }
355 % End
356// clang-format on
357#endif
358
359#ifndef SIP_RUN
360
368 int fieldOriginIndex( int fieldIdx ) const;
369#else
370// clang-format off
371
381 int fieldOriginIndex( int fieldIdx ) const SIP_HOLDGIL;
382 % MethodCode
383 if ( a0 < 0 || a0 >= sipCpp->count() )
384 {
385 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
386 sipIsErr = 1;
387 }
388 else
389 {
390 sipRes = sipCpp->fieldOriginIndex( a0 );
391 }
392 % End
393// clang-format on
394#endif
395
409 Q_INVOKABLE int indexFromName( const QString &fieldName ) const SIP_HOLDGIL;
410
422 Q_INVOKABLE int indexOf( const QString &fieldName ) const SIP_HOLDGIL;
423
437 Q_INVOKABLE int lookupField( const QString &fieldName ) const SIP_HOLDGIL;
438
442 QgsAttributeList allAttributesList() const SIP_HOLDGIL;
443
445 QList<QgsField> toList() const SIP_HOLDGIL;
446
447 bool operator==( const QgsFields &other ) const SIP_HOLDGIL;
448 bool operator!=( const QgsFields &other ) const SIP_HOLDGIL { return !( *this == other ); }
449
450#ifndef SIP_RUN
451
457 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
458#else
459// clang-format off
460
467 QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
468 % MethodCode
469 if ( a0 < 0 || a0 >= sipCpp->count() )
470 {
471 PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
472 sipIsErr = 1;
473 }
474 else
475 {
476 sipRes = new QIcon( sipCpp->iconForField( a0 ) );
477 }
478 % End
479// clang-format on
480#endif
481
490 static QIcon iconForFieldType( QMetaType::Type type, QMetaType::Type subType = QMetaType::Type::UnknownType, const QString &typeString = QString() );
491
501 Q_DECL_DEPRECATED static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType, const QString &typeString = QString() ) SIP_DEPRECATED;
502
504 operator QVariant() const
505 {
506 return QVariant::fromValue( *this );
507 }
508
509#ifdef SIP_RUN
510// clang-format off
511
512 void __setitem__( int key, const QgsField &field ) SIP_HOLDGIL;
513 % MethodCode
514 int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
515 if ( idx < 0 )
516 sipIsErr = 1;
517 else
518 ( *sipCpp )[idx] = *a1;
519 % End
520
521// clang-format on
522#endif
523
524#ifndef SIP_RUN
525
527
528 class const_iterator;
529
530 class iterator
531 {
532 public:
533 QgsFields::Field *d = nullptr;
534 typedef std::random_access_iterator_tag iterator_category;
535 typedef qptrdiff difference_type;
536
537 inline iterator()
538 {}
539
540 inline iterator( QgsFields::Field *n )
541 : d( n )
542 {}
543
544 inline QgsField &operator*() const { return d->field; }
545 inline QgsField *operator->() const { return &d->field; }
546 inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
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 &o ) const noexcept { return d != o.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 inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
553
554 inline iterator &operator++() { ++d; return *this; }
555 inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
556 inline iterator &operator--() { d--; return *this; }
557 inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
558 inline iterator &operator+=( difference_type j ) { d += j; return *this; }
559 inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
560 inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
561 inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
562 inline int operator-( iterator j ) const { return int( d - j.d ); }
563 };
564 friend class iterator;
565
566 class const_iterator // clazy:exclude=rule-of-three
567 {
568 public:
569 const QgsFields::Field *d = nullptr;
570
571 typedef std::random_access_iterator_tag iterator_category;
572 typedef qptrdiff difference_type;
573
574 inline const_iterator()
575 {}
576
577 inline const_iterator( const QgsFields::Field *f )
578 : d( f ) {}
579 inline const_iterator( const const_iterator &o )
580 : d( o.d ) {}
581 inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
582 : d( o.d ) {}
583 inline const QgsField &operator*() const { return d->field; }
584 inline const QgsField *operator->() const { return &d->field; }
585 inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
586 inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
587 inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.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 bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
592 inline const_iterator &operator++() { ++d; return *this; }
593 inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
594 inline const_iterator &operator--() { d--; return *this; }
595 inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
596 inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
597 inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
598 inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
599 inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
600 inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
601 private:
602 const_iterator &operator= ( const const_iterator & ) = delete;
603 };
604 friend class const_iterator;
606
607
613 const_iterator constBegin() const noexcept;
614
620 const_iterator constEnd() const noexcept;
621
627 const_iterator begin() const noexcept;
628
634 const_iterator end() const noexcept;
635
641 iterator begin();
642
643
649 iterator end();
650
651#endif
652
653 private:
654
655 QSharedDataPointer<QgsFieldsPrivate> d;
656
657};
658
660
661
662CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
664CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
665
666#endif // QGSFIELDS_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
FieldOrigin
Field origin.
Definition qgis.h:1783
@ Unknown
The field origin has not been specified.
Definition qgis.h:1784
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
struct QgsFields::Field Field
bool isEmpty
Definition qgsfields.h:49
QgsFields()
Constructor for an empty field container.
Definition qgsfields.cpp:36
QStringList names
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:7380
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:7396
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:239
#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:73
Qgis::FieldOrigin origin
Origin of the field.
Definition qgsfields.h:72
Field(const QgsField &f, Qgis::FieldOrigin o, int oi)
Definition qgsfields.h:60
bool operator==(const Field &other) const
Definition qgsfields.h:68
QgsField field
Field.
Definition qgsfields.h:71
bool operator!=(const Field &other) const
Definition qgsfields.h:69