QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
182  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 #ifdef SIP_RUN
199  SIP_PYOBJECT __getitem__( const QString &name ) const SIP_TYPEHINT( QgsField );
200  % MethodCode
201  const int fieldIdx = sipCpp->lookupField( *a0 );
202  if ( fieldIdx == -1 )
203  {
204  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
205  sipIsErr = 1;
206  }
207  else
208  {
209  sipRes = sipConvertFromType( new QgsField( sipCpp->at( fieldIdx ) ), sipType_QgsField, Py_None );
210  }
211  % End
212 #endif
213 
214 #ifndef SIP_RUN
215 
219  QgsField at( int i ) const SIP_FACTORY;
220 #else
221 
226  QgsField at( int i ) const SIP_FACTORY;
227  % MethodCode
228  if ( a0 < 0 || a0 >= sipCpp->count() )
229  {
230  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
231  sipIsErr = 1;
232  }
233  else
234  {
235  sipRes = new QgsField( sipCpp->at( a0 ) );
236  }
237  % End
238 #endif
239 
240 #ifndef SIP_RUN
241 
245  QgsField field( int fieldIdx ) const SIP_FACTORY;
246 #else
247 
252  QgsField field( int fieldIdx ) const SIP_FACTORY;
253  % MethodCode
254  if ( a0 < 0 || a0 >= sipCpp->count() )
255  {
256  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
257  sipIsErr = 1;
258  }
259  else
260  {
261  sipRes = new QgsField( sipCpp->field( a0 ) );
262  }
263  % End
264 #endif
265 
266 #ifndef SIP_RUN
267 
271  QgsField field( const QString &name ) const SIP_FACTORY;
272 #else
273 
278  QgsField field( const QString &name ) const SIP_FACTORY;
279  % MethodCode
280  int fieldIdx = sipCpp->indexFromName( *a0 );
281  if ( fieldIdx == -1 )
282  {
283  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
284  sipIsErr = 1;
285  }
286  else
287  {
288  sipRes = new QgsField( sipCpp->field( *a0 ) );
289  }
290  % End
291 #endif
292 
293 #ifndef SIP_RUN
294 
298  FieldOrigin fieldOrigin( int fieldIdx ) const;
299 #else
300 
306  FieldOrigin fieldOrigin( int fieldIdx ) const;
307  % MethodCode
308  if ( a0 < 0 || a0 >= sipCpp->count() )
309  {
310  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
311  sipIsErr = 1;
312  }
313  else
314  {
315  sipRes = sipCpp->fieldOrigin( a0 );
316  }
317  % End
318 #endif
319 
320 #ifndef SIP_RUN
321 
325  int fieldOriginIndex( int fieldIdx ) const;
326 #else
327 
333  int fieldOriginIndex( int fieldIdx ) const;
334  % MethodCode
335  if ( a0 < 0 || a0 >= sipCpp->count() )
336  {
337  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
338  sipIsErr = 1;
339  }
340  else
341  {
342  sipRes = sipCpp->fieldOriginIndex( a0 );
343  }
344  % End
345 #endif
346 
360  int indexFromName( const QString &fieldName ) const;
361 
374  int indexOf( const QString &fieldName ) const;
375 
390  int lookupField( const QString &fieldName ) const;
391 
396  QgsAttributeList allAttributesList() const;
397 
399  QList<QgsField> toList() const;
400 
402  bool operator==( const QgsFields &other ) const;
404  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
405 
406 #ifndef SIP_RUN
407 
414  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
415 #else
416 
424  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
425  % MethodCode
426  if ( a0 < 0 || a0 >= sipCpp->count() )
427  {
428  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
429  sipIsErr = 1;
430  }
431  else
432  {
433  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
434  }
435  % End
436 #endif
437 
445  static QIcon iconForFieldType( QVariant::Type type, QVariant::Type subType = QVariant::Type::Invalid );
446 
448  operator QVariant() const
449  {
450  return QVariant::fromValue( *this );
451  }
452 
453 #ifdef SIP_RUN
454 
455  void __setitem__( int key, const QgsField &field );
456  % MethodCode
457  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
458  if ( idx < 0 )
459  sipIsErr = 1;
460  else
461  ( *sipCpp )[idx] = *a1;
462  % End
463 
464 #endif
465 
466 #ifndef SIP_RUN
467 
469 
470  class const_iterator;
471 
472  class iterator
473  {
474  public:
475  QgsFields::Field *d = nullptr;
476  typedef std::random_access_iterator_tag iterator_category;
477  typedef qptrdiff difference_type;
478 
479  inline iterator()
480  {}
481 
482  inline iterator( QgsFields::Field *n )
483  : d( n )
484  {}
485 
486  inline QgsField &operator*() const { return d->field; }
487  inline QgsField *operator->() const { return &d->field; }
488  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
489  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
490  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
491  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
492  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
493  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
494  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
495 
496  inline iterator &operator++() { ++d; return *this; }
497  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
498  inline iterator &operator--() { d--; return *this; }
499  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
500  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
501  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
502  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
503  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
504  inline int operator-( iterator j ) const { return int( d - j.d ); }
505  };
506  friend class iterator;
507 
508  class const_iterator // clazy:exclude=rule-of-three
509  {
510  public:
511  const QgsFields::Field *d = nullptr;
512 
513  typedef std::random_access_iterator_tag iterator_category;
514  typedef qptrdiff difference_type;
515 
516  inline const_iterator()
517  {}
518 
519  inline const_iterator( const QgsFields::Field *f )
520  : d( f ) {}
521  inline const_iterator( const const_iterator &o )
522  : d( o.d ) {}
523  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
524  : d( o.d ) {}
525  inline const QgsField &operator*() const { return d->field; }
526  inline const QgsField *operator->() const { return &d->field; }
527  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
528  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
529  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
530  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
531  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
532  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
533  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
534  inline const_iterator &operator++() { ++d; return *this; }
535  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
536  inline const_iterator &operator--() { d--; return *this; }
537  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
538  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
539  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
540  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
541  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
542  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
543  private:
544  const_iterator &operator= ( const const_iterator & ) = delete;
545  };
546  friend class const_iterator;
548 
549 
556  const_iterator constBegin() const noexcept;
557 
564  const_iterator constEnd() const noexcept;
565 
572  const_iterator begin() const noexcept;
573 
580  const_iterator end() const noexcept;
581 
588  iterator begin();
589 
590 
597  iterator end();
598 
599 #endif
600 
601  private:
602 
603  QSharedDataPointer<QgsFieldsPrivate> d;
604 
605 };
606 
608 
609 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
612 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
613 
614 #endif // QGSFIELDS_H
QgsFields::OriginProvider
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
QgsFields::operator!=
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:404
QgsFields::FieldOrigin
FieldOrigin
Definition: qgsfields.h:48
QgsFields::OriginEdit
@ OriginEdit
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:53
QgsFields::Field::operator!=
bool operator!=(const Field &other) const
Definition: qgsfields.h:75
operator>
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:2630
operator>>
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
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
QgsFields::Field::origin
FieldOrigin origin
Origin of the field.
Definition: qgsfields.h:78
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
field
const QgsField & field
Definition: qgsfield.h:463
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
SIP_TYPEHINT
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:227
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsFields::Field::field
QgsField field
Field.
Definition: qgsfields.h:77
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsFields::OriginUnknown
@ OriginUnknown
It has not been specified where the field comes from.
Definition: qgsfields.h:50
QgsFields::Field::Field
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfields.h:64
qgis_sip.h
operator*
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
QgsFields::Field
Definition: qgsfields.h:59
operator<
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:2644
QgsFields::Field::operator==
bool operator==(const Field &other) const
Definition: qgsfields.h:73
operator-
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:299
qgsfield.h
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
operator+
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:305
QgsFields::Field::Field
Field()
Definition: qgsfields.h:61
operator>=
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
Definition: qgscoordinatereferencesystem.cpp:3081
QgsFields::Field::originIndex
int originIndex
Index specific to the origin.
Definition: qgsfields.h:79
operator<<
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
operator<=
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
Definition: qgscoordinatereferencesystem.cpp:3085
QgsFields::OriginJoin
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:52
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50