QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 : [email protected]
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 
24 class 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 
44 class CORE_EXPORT QgsFields
45 {
46  public:
47 
49  {
54  OriginExpression
55  };
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 #ifndef SIP_RUN
199 
203  QgsField at( int i ) const SIP_FACTORY;
204 #else
205 
210  QgsField at( int i ) const SIP_FACTORY;
211  % MethodCode
212  if ( a0 < 0 || a0 >= sipCpp->count() )
213  {
214  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
215  sipIsErr = 1;
216  }
217  else
218  {
219  sipRes = new QgsField( sipCpp->at( a0 ) );
220  }
221  % End
222 #endif
223 
224 #ifndef SIP_RUN
225 
229  QgsField field( int fieldIdx ) const SIP_FACTORY;
230 #else
231 
236  QgsField field( int fieldIdx ) const SIP_FACTORY;
237  % MethodCode
238  if ( a0 < 0 || a0 >= sipCpp->count() )
239  {
240  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
241  sipIsErr = 1;
242  }
243  else
244  {
245  sipRes = new QgsField( sipCpp->field( a0 ) );
246  }
247  % End
248 #endif
249 
250 #ifndef SIP_RUN
251 
255  QgsField field( const QString &name ) const SIP_FACTORY;
256 #else
257 
262  QgsField field( const QString &name ) const SIP_FACTORY;
263  % MethodCode
264  int fieldIdx = sipCpp->indexFromName( *a0 );
265  if ( fieldIdx == -1 )
266  {
267  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
268  sipIsErr = 1;
269  }
270  else
271  {
272  sipRes = new QgsField( sipCpp->field( *a0 ) );
273  }
274  % End
275 #endif
276 
277 #ifndef SIP_RUN
278 
282  FieldOrigin fieldOrigin( int fieldIdx ) const;
283 #else
284 
290  FieldOrigin fieldOrigin( int fieldIdx ) const;
291  % MethodCode
292  if ( a0 < 0 || a0 >= sipCpp->count() )
293  {
294  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
295  sipIsErr = 1;
296  }
297  else
298  {
299  sipRes = sipCpp->fieldOrigin( a0 );
300  }
301  % End
302 #endif
303 
304 #ifndef SIP_RUN
305 
309  int fieldOriginIndex( int fieldIdx ) const;
310 #else
311 
317  int fieldOriginIndex( int fieldIdx ) const;
318  % MethodCode
319  if ( a0 < 0 || a0 >= sipCpp->count() )
320  {
321  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
322  sipIsErr = 1;
323  }
324  else
325  {
326  sipRes = sipCpp->fieldOriginIndex( a0 );
327  }
328  % End
329 #endif
330 
344  int indexFromName( const QString &fieldName ) const;
345 
358  int indexOf( const QString &fieldName ) const;
359 
374  int lookupField( const QString &fieldName ) const;
375 
380  QgsAttributeList allAttributesList() const;
381 
383  QList<QgsField> toList() const;
384 
386  bool operator==( const QgsFields &other ) const;
388  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
389 
390 #ifndef SIP_RUN
391 
398  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
399 #else
400 
408  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
409  % MethodCode
410  if ( a0 < 0 || a0 >= sipCpp->count() )
411  {
412  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
413  sipIsErr = 1;
414  }
415  else
416  {
417  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
418  }
419  % End
420 #endif
421 
429  static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType = QVariant::Type::Invalid );
430 
432  operator QVariant() const
433  {
434  return QVariant::fromValue( *this );
435  }
436 
437 #ifdef SIP_RUN
438 
439  void __setitem__( int key, const QgsField &field );
440  % MethodCode
441  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
442  if ( idx < 0 )
443  sipIsErr = 1;
444  else
445  ( *sipCpp )[idx] = *a1;
446  % End
447 
448 #endif
449 
450 #ifndef SIP_RUN
451 
453 
454  class const_iterator;
455 
456  class iterator
457  {
458  public:
459  QgsFields::Field *d = nullptr;
460  typedef std::random_access_iterator_tag iterator_category;
461  typedef qptrdiff difference_type;
462 
463  inline iterator()
464  {}
465 
466  inline iterator( QgsFields::Field *n )
467  : d( n )
468  {}
469 
470  inline QgsField &operator*() const { return d->field; }
471  inline QgsField *operator->() const { return &d->field; }
472  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
473  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
474  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
475  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
476  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
477  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
478  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
479 
480  inline iterator &operator++() { ++d; return *this; }
481  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
482  inline iterator &operator--() { d--; return *this; }
483  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
484  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
485  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
486  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
487  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
488  inline int operator-( iterator j ) const { return int( d - j.d ); }
489  };
490  friend class iterator;
491 
492  class const_iterator // clazy:exclude=rule-of-three
493  {
494  public:
495  const QgsFields::Field *d = nullptr;
496 
497  typedef std::random_access_iterator_tag iterator_category;
498  typedef qptrdiff difference_type;
499 
500  inline const_iterator()
501  {}
502 
503  inline const_iterator( const QgsFields::Field *f )
504  : d( f ) {}
505  inline const_iterator( const const_iterator &o )
506  : d( o.d ) {}
507  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
508  : d( o.d ) {}
509  inline const QgsField &operator*() const { return d->field; }
510  inline const QgsField *operator->() const { return &d->field; }
511  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
512  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
513  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
514  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
515  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
516  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
517  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
518  inline const_iterator &operator++() { ++d; return *this; }
519  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
520  inline const_iterator &operator--() { d--; return *this; }
521  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
522  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
523  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
524  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
525  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
526  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
527  private:
528  const_iterator &operator= ( const const_iterator & ) = delete;
529  };
530  friend class const_iterator;
532 
533 
540  const_iterator constBegin() const noexcept;
541 
548  const_iterator constEnd() const noexcept;
549 
556  const_iterator begin() const noexcept;
557 
564  const_iterator end() const noexcept;
565 
572  iterator begin();
573 
574 
581  iterator end();
582 
583 #endif
584 
585  private:
586 
587  QSharedDataPointer<QgsFieldsPrivate> d;
588 
589 };
590 
592 
593 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
596 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
597 
598 #endif // QGSFIELDS_H
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:388
@ 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:1875
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:1889
#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)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:463
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:392
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:402
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
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
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
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