QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
71  bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
73  bool operator!=( const Field &other ) const { return !( *this == other ); }
74 
76  FieldOrigin origin = OriginUnknown ;
77  int originIndex = -1 ;
78  } Field;
79 
80 #endif
81 
85  QgsFields();
86 
90  QgsFields( const QgsFields &other );
91 
95  QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
96 
97  virtual ~QgsFields();
98 
100  void clear();
101 
103  bool append( const QgsField &field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
104 
109  bool rename( int fieldIdx, const QString &name );
110 
112  bool appendExpressionField( const QgsField &field, int originIndex );
113 
114 #ifndef SIP_RUN
115 
119  void remove( int fieldIdx );
120 #else
121 
127  void remove( int fieldIdx );
128  % MethodCode
129  if ( a0 < 0 || a0 >= sipCpp->count() )
130  {
131  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
132  sipIsErr = 1;
133  }
134  else
135  {
136  sipCpp->remove( a0 );
137  }
138  % End
139 #endif
140 
142  void extend( const QgsFields &other );
143 
145  bool isEmpty() const;
146 
148  int count() const;
149 
150 #ifdef SIP_RUN
151  int __len__() const;
152  % MethodCode
153  sipRes = sipCpp->count();
154  % End
155 
157  int __bool__() const;
158  % MethodCode
159  sipRes = true;
160  % End
161 #endif
162 
164  int size() const;
165 
170  QStringList names() const;
171 
177  bool exists( int i ) const;
178 
179 #ifndef SIP_RUN
181  QgsField operator[]( int i ) const;
182 #endif
183 
185  QgsField &operator[]( int i ) SIP_FACTORY;
186 #ifdef SIP_RUN
187  % MethodCode
188  SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
189  if ( idx < 0 )
190  sipIsErr = 1;
191  else
192  sipRes = new QgsField( sipCpp->operator[]( idx ) );
193  % End
194 #endif
195 
196 #ifndef SIP_RUN
197 
201  QgsField at( int i ) const SIP_FACTORY;
202 #else
203 
208  QgsField at( int i ) const SIP_FACTORY;
209  % MethodCode
210  if ( a0 < 0 || a0 >= sipCpp->count() )
211  {
212  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
213  sipIsErr = 1;
214  }
215  else
216  {
217  sipRes = new QgsField( sipCpp->at( a0 ) );
218  }
219  % End
220 #endif
221 
222 #ifndef SIP_RUN
223 
227  QgsField field( int fieldIdx ) const SIP_FACTORY;
228 #else
229 
234  QgsField field( int fieldIdx ) const SIP_FACTORY;
235  % MethodCode
236  if ( a0 < 0 || a0 >= sipCpp->count() )
237  {
238  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
239  sipIsErr = 1;
240  }
241  else
242  {
243  sipRes = new QgsField( sipCpp->field( a0 ) );
244  }
245  % End
246 #endif
247 
248 #ifndef SIP_RUN
249 
253  QgsField field( const QString &name ) const SIP_FACTORY;
254 #else
255 
260  QgsField field( const QString &name ) const SIP_FACTORY;
261  % MethodCode
262  int fieldIdx = sipCpp->indexFromName( *a0 );
263  if ( fieldIdx == -1 )
264  {
265  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
266  sipIsErr = 1;
267  }
268  else
269  {
270  sipRes = new QgsField( sipCpp->field( *a0 ) );
271  }
272  % End
273 #endif
274 
275 #ifndef SIP_RUN
276 
280  FieldOrigin fieldOrigin( int fieldIdx ) const;
281 #else
282 
288  FieldOrigin fieldOrigin( int fieldIdx ) const;
289  % MethodCode
290  if ( a0 < 0 || a0 >= sipCpp->count() )
291  {
292  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
293  sipIsErr = 1;
294  }
295  else
296  {
297  sipRes = sipCpp->fieldOrigin( a0 );
298  }
299  % End
300 #endif
301 
302 #ifndef SIP_RUN
303 
307  int fieldOriginIndex( int fieldIdx ) const;
308 #else
309 
315  int fieldOriginIndex( int fieldIdx ) const;
316  % MethodCode
317  if ( a0 < 0 || a0 >= sipCpp->count() )
318  {
319  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
320  sipIsErr = 1;
321  }
322  else
323  {
324  sipRes = sipCpp->fieldOriginIndex( a0 );
325  }
326  % End
327 #endif
328 
342  int indexFromName( const QString &fieldName ) const;
343 
356  int indexOf( const QString &fieldName ) const;
357 
372  int lookupField( const QString &fieldName ) const;
373 
378  QgsAttributeList allAttributesList() const;
379 
381  QList<QgsField> toList() const;
382 
384  bool operator==( const QgsFields &other ) const;
386  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
387 
388 #ifndef SIP_RUN
389 
396  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
397 #else
398 
406  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
407  % MethodCode
408  if ( a0 < 0 || a0 >= sipCpp->count() )
409  {
410  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
411  sipIsErr = 1;
412  }
413  else
414  {
415  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
416  }
417  % End
418 #endif
419 
424  static QIcon iconForFieldType( const QVariant::Type &type ) SIP_FACTORY;
425 
427  operator QVariant() const
428  {
429  return QVariant::fromValue( *this );
430  }
431 
432 #ifdef SIP_RUN
433 
434  void __setitem__( int key, const QgsField &field );
435  % MethodCode
436  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
437  if ( idx < 0 )
438  sipIsErr = 1;
439  else
440  ( *sipCpp )[idx] = *a1;
441  % End
442 
443 #endif
444 
445 #ifndef SIP_RUN
446 
448 
449  class const_iterator;
450 
451  class iterator
452  {
453  public:
454  QgsFields::Field *d = nullptr;
455  typedef std::random_access_iterator_tag iterator_category;
456  typedef qptrdiff difference_type;
457 
458  inline iterator()
459  {}
460 
461  inline iterator( QgsFields::Field *n )
462  : d( n )
463  {}
464 
465  inline QgsField &operator*() const { return d->field; }
466  inline QgsField *operator->() const { return &d->field; }
467  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
468  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
469  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
470  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
471  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.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 
475  inline iterator &operator++() { ++d; return *this; }
476  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
477  inline iterator &operator--() { d--; return *this; }
478  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
479  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
480  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
481  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
482  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
483  inline int operator-( iterator j ) const { return int( d - j.d ); }
484  };
485  friend class iterator;
486 
487  class const_iterator // clazy:exclude=rule-of-three
488  {
489  public:
490  const QgsFields::Field *d = nullptr;
491 
492  typedef std::random_access_iterator_tag iterator_category;
493  typedef qptrdiff difference_type;
494 
495  inline const_iterator()
496  {}
497 
498  inline const_iterator( const QgsFields::Field *f )
499  : d( f ) {}
500  inline const_iterator( const const_iterator &o )
501  : d( o.d ) {}
502  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
503  : d( o.d ) {}
504  inline const QgsField &operator*() const { return d->field; }
505  inline const QgsField *operator->() const { return &d->field; }
506  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
507  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
508  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
509  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
510  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.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 const_iterator &operator++() { ++d; return *this; }
514  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
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+=( difference_type j ) { d += j; return *this; }
518  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
519  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
520  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
521  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
522  private:
523  const_iterator &operator= ( const const_iterator & ) = delete;
524  };
525  friend class const_iterator;
527 
528 
535  const_iterator constBegin() const noexcept;
536 
543  const_iterator constEnd() const noexcept;
544 
551  const_iterator begin() const noexcept;
552 
559  const_iterator end() const noexcept;
560 
567  iterator begin();
568 
569 
576  iterator end();
577 
578 #endif
579 
580  private:
581 
582  QSharedDataPointer<QgsFieldsPrivate> d;
583 
584 };
585 
587 
588 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
591 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
592 
593 #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:386
@ 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:881
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:895
#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:77
bool operator==(const Field &other) const
Definition: qgsfields.h:71
QgsField field
Field.
Definition: qgsfields.h:75
bool operator!=(const Field &other) const
Definition: qgsfields.h:73
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfields.h:64
FieldOrigin origin
Origin of the field.
Definition: qgsfields.h:76