QGIS API Documentation  3.21.0-Master (af9a72eda6)
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 
426  static QIcon iconForFieldType( const QVariant::Type &type ) SIP_FACTORY;
427 
429  operator QVariant() const
430  {
431  return QVariant::fromValue( *this );
432  }
433 
434 #ifdef SIP_RUN
435 
436  void __setitem__( int key, const QgsField &field );
437  % MethodCode
438  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
439  if ( idx < 0 )
440  sipIsErr = 1;
441  else
442  ( *sipCpp )[idx] = *a1;
443  % End
444 
445 #endif
446 
447 #ifndef SIP_RUN
448 
450 
451  class const_iterator;
452 
453  class iterator
454  {
455  public:
456  QgsFields::Field *d = nullptr;
457  typedef std::random_access_iterator_tag iterator_category;
458  typedef qptrdiff difference_type;
459 
460  inline iterator()
461  {}
462 
463  inline iterator( QgsFields::Field *n )
464  : d( n )
465  {}
466 
467  inline QgsField &operator*() const { return d->field; }
468  inline QgsField *operator->() const { return &d->field; }
469  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
470  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
471  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
472  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
473  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
474  inline bool operator>( const iterator &other ) const noexcept { return d > other.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 
477  inline iterator &operator++() { ++d; return *this; }
478  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
479  inline iterator &operator--() { d--; return *this; }
480  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
481  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
482  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
483  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
484  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
485  inline int operator-( iterator j ) const { return int( d - j.d ); }
486  };
487  friend class iterator;
488 
489  class const_iterator // clazy:exclude=rule-of-three
490  {
491  public:
492  const QgsFields::Field *d = nullptr;
493 
494  typedef std::random_access_iterator_tag iterator_category;
495  typedef qptrdiff difference_type;
496 
497  inline const_iterator()
498  {}
499 
500  inline const_iterator( const QgsFields::Field *f )
501  : d( f ) {}
502  inline const_iterator( const const_iterator &o )
503  : d( o.d ) {}
504  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
505  : d( o.d ) {}
506  inline const QgsField &operator*() const { return d->field; }
507  inline const QgsField *operator->() const { return &d->field; }
508  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
509  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
510  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
511  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
512  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
513  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
514  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
515  inline const_iterator &operator++() { ++d; return *this; }
516  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
517  inline const_iterator &operator--() { d--; return *this; }
518  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
519  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
520  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
521  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
522  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
523  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
524  private:
525  const_iterator &operator= ( const const_iterator & ) = delete;
526  };
527  friend class const_iterator;
529 
530 
537  const_iterator constBegin() const noexcept;
538 
545  const_iterator constEnd() const noexcept;
546 
553  const_iterator begin() const noexcept;
554 
561  const_iterator end() const noexcept;
562 
569  iterator begin();
570 
571 
578  iterator end();
579 
580 #endif
581 
582  private:
583 
584  QSharedDataPointer<QgsFieldsPrivate> d;
585 
586 };
587 
589 
590 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
593 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
594 
595 #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:1102
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:1116
#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:387
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:397
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