QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
115  void remove( int fieldIdx );
116 #ifdef SIP_RUN
117  % MethodCode
118  if ( a0 < 0 || a0 >= sipCpp->count() )
119  {
120  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
121  sipIsErr = 1;
122  }
123  else
124  {
125  sipCpp->remove( a0 );
126  }
127  % End
128 #endif
129 
131  void extend( const QgsFields &other );
132 
134  bool isEmpty() const;
135 
137  int count() const;
138 
139 #ifdef SIP_RUN
140  int __len__() const;
141  % MethodCode
142  sipRes = sipCpp->count();
143  % End
144 
146  int __bool__() const;
147  % MethodCode
148  sipRes = true;
149  % End
150 #endif
151 
153  int size() const;
154 
159  QStringList names() const;
160 
166  bool exists( int i ) const;
167 
168 #ifndef SIP_RUN
170  QgsField operator[]( int i ) const;
171 #endif
172 
174  QgsField &operator[]( int i ) SIP_FACTORY;
175 #ifdef SIP_RUN
176  % MethodCode
177  SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
178  if ( idx < 0 )
179  sipIsErr = 1;
180  else
181  sipRes = new QgsField( sipCpp->operator[]( idx ) );
182  % End
183 #endif
184 
186  QgsField at( int i ) const SIP_FACTORY;
187 #ifdef SIP_RUN
188  % MethodCode
189  if ( a0 < 0 || a0 >= sipCpp->count() )
190  {
191  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
192  sipIsErr = 1;
193  }
194  else
195  {
196  sipRes = new QgsField( sipCpp->at( a0 ) );
197  }
198  % End
199 #endif
200 
202  QgsField field( int fieldIdx ) const SIP_FACTORY;
203 #ifdef SIP_RUN
204  % MethodCode
205  if ( a0 < 0 || a0 >= sipCpp->count() )
206  {
207  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
208  sipIsErr = 1;
209  }
210  else
211  {
212  sipRes = new QgsField( sipCpp->field( a0 ) );
213  }
214  % End
215 #endif
216 
218  QgsField field( const QString &name ) const SIP_FACTORY;
219 #ifdef SIP_RUN
220  % MethodCode
221  int fieldIdx = sipCpp->indexFromName( *a0 );
222  if ( fieldIdx == -1 )
223  {
224  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
225  sipIsErr = 1;
226  }
227  else
228  {
229  sipRes = new QgsField( sipCpp->field( *a0 ) );
230  }
231  % End
232 #endif
233 
235  FieldOrigin fieldOrigin( int fieldIdx ) const;
236 #ifdef SIP_RUN
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 = sipCpp->fieldOrigin( a0 );
246  }
247  % End
248 #endif
249 
251  int fieldOriginIndex( int fieldIdx ) const;
252 #ifdef SIP_RUN
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 = sipCpp->fieldOriginIndex( a0 );
262  }
263  % End
264 #endif
265 
277  int indexFromName( const QString &fieldName ) const;
278 
290  int indexOf( const QString &fieldName ) const;
291 
306  int lookupField( const QString &fieldName ) const;
307 
312  QgsAttributeList allAttributesList() const;
313 
315  QList<QgsField> toList() const;
316 
318  bool operator==( const QgsFields &other ) const;
320  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
321 
328  QIcon iconForField( int fieldIdx, bool considerOrigin = false ) const SIP_FACTORY;
329 #ifdef SIP_RUN
330  % MethodCode
331  if ( a0 < 0 || a0 >= sipCpp->count() )
332  {
333  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
334  sipIsErr = 1;
335  }
336  else
337  {
338  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
339  }
340  % End
341 #endif
342 
343 
348  static QIcon iconForFieldType( const QVariant::Type &type ) SIP_FACTORY;
349 
351  operator QVariant() const
352  {
353  return QVariant::fromValue( *this );
354  }
355 
356 #ifdef SIP_RUN
357 
358  void __setitem__( int key, const QgsField &field );
359  % MethodCode
360  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
361  if ( idx < 0 )
362  sipIsErr = 1;
363  else
364  ( *sipCpp )[idx] = *a1;
365  % End
366 
367 #endif
368 
369 #ifndef SIP_RUN
370 
372 
373  class const_iterator;
374 
375  class iterator
376  {
377  public:
378  QgsFields::Field *d = nullptr;
379  typedef std::random_access_iterator_tag iterator_category;
380  typedef qptrdiff difference_type;
381 
382  inline iterator()
383  {}
384 
385  inline iterator( QgsFields::Field *n )
386  : d( n )
387  {}
388 
389  inline QgsField &operator*() const { return d->field; }
390  inline QgsField *operator->() const { return &d->field; }
391  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
392  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
393  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
394  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
395  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
396  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
397  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
398 
399  inline iterator &operator++() { ++d; return *this; }
400  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
401  inline iterator &operator--() { d--; return *this; }
402  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
403  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
404  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
405  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
406  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
407  inline int operator-( iterator j ) const { return int( d - j.d ); }
408  };
409  friend class iterator;
410 
411  class const_iterator // clazy:exclude=rule-of-three
412  {
413  public:
414  const QgsFields::Field *d = nullptr;
415 
416  typedef std::random_access_iterator_tag iterator_category;
417  typedef qptrdiff difference_type;
418 
419  inline const_iterator()
420  {}
421 
422  inline const_iterator( const QgsFields::Field *f )
423  : d( f ) {}
424  inline const_iterator( const const_iterator &o )
425  : d( o.d ) {}
426  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
427  : d( o.d ) {}
428  inline const QgsField &operator*() const { return d->field; }
429  inline const QgsField *operator->() const { return &d->field; }
430  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
431  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
432  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
433  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
434  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
435  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
436  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
437  inline const_iterator &operator++() { ++d; return *this; }
438  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
439  inline const_iterator &operator--() { d--; return *this; }
440  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
441  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
442  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
443  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
444  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
445  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
446  private:
447  const_iterator &operator= ( const const_iterator & ) = delete;
448  };
449  friend class const_iterator;
451 
452 
459  const_iterator constBegin() const noexcept;
460 
467  const_iterator constEnd() const noexcept;
468 
475  const_iterator begin() const noexcept;
476 
483  const_iterator end() const noexcept;
484 
491  iterator begin();
492 
493 
500  iterator end();
501 
502 #endif
503 
504  private:
505 
506  QSharedDataPointer<QgsFieldsPrivate> d;
507 
508 };
509 
511 
512 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
515 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
516 
517 #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:320
@ 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
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
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:472
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