QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
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
169  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 
326  QIcon iconForField( int fieldIdx ) const SIP_FACTORY;
327 #ifdef SIP_RUN
328  % MethodCode
329  if ( a0 < 0 || a0 >= sipCpp->count() )
330  {
331  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
332  sipIsErr = 1;
333  }
334  else
335  {
336  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
337  }
338  % End
339 #endif
340 
341 
346  static QIcon iconForFieldType( const QVariant::Type &type ) SIP_FACTORY;
347 
349  operator QVariant() const
350  {
351  return QVariant::fromValue( *this );
352  }
353 
354 #ifdef SIP_RUN
355 
356  void __setitem__( int key, const QgsField &field );
357  % MethodCode
358  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
359  if ( idx < 0 )
360  sipIsErr = 1;
361  else
362  ( *sipCpp )[idx] = *a1;
363  % End
364 
365 #endif
366 
367 #ifndef SIP_RUN
368 
370 
371  class const_iterator;
372 
373  class iterator
374  {
375  public:
376  QgsFields::Field *d = nullptr;
377  typedef std::random_access_iterator_tag iterator_category;
378  typedef qptrdiff difference_type;
379 
380  inline iterator()
381  {}
382 
383  inline iterator( QgsFields::Field *n )
384  : d( n )
385  {}
386 
387  inline QgsField &operator*() const { return d->field; }
388  inline QgsField *operator->() const { return &d->field; }
389  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
390  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
391  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
392  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
393  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.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 
397  inline iterator &operator++() { ++d; return *this; }
398  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
399  inline iterator &operator--() { d--; return *this; }
400  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
401  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
402  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
403  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
404  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
405  inline int operator-( iterator j ) const { return int( d - j.d ); }
406  };
407  friend class iterator;
408 
409  class const_iterator // clazy:exclude=rule-of-three
410  {
411  public:
412  const QgsFields::Field *d = nullptr;
413 
414  typedef std::random_access_iterator_tag iterator_category;
415  typedef qptrdiff difference_type;
416 
417  inline const_iterator()
418  {}
419 
420  inline const_iterator( const QgsFields::Field *f )
421  : d( f ) {}
422  inline const_iterator( const const_iterator &o )
423  : d( o.d ) {}
424  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
425  : d( o.d ) {}
426  inline const QgsField &operator*() const { return d->field; }
427  inline const QgsField *operator->() const { return &d->field; }
428  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
429  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
430  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
431  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
432  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.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 const_iterator &operator++() { ++d; return *this; }
436  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
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+=( difference_type j ) { d += j; return *this; }
440  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
441  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
442  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
443  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
444  private:
445  const_iterator &operator= ( const const_iterator & ) = delete;
446  };
447  friend class const_iterator;
449 
450 
457  const_iterator constBegin() const noexcept;
458 
465  const_iterator constEnd() const noexcept;
466 
473  const_iterator begin() const noexcept;
474 
481  const_iterator end() const noexcept;
482 
489  iterator begin();
490 
491 
498  iterator end();
499 
500 #endif
501 
502  private:
503 
504  QSharedDataPointer<QgsFieldsPrivate> d;
505 
506 };
507 
509 
510 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
513 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
514 
515 #endif // QGSFIELDS_H
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:52
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:53
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
Field.
Definition: qgsfields.h:75
Container of fields for a vector layer.
Definition: qgsfields.h:44
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:51
bool operator!=(const Field &other) const
Definition: qgsfields.h:73
It has not been specified where the field comes from.
Definition: qgsfields.h:50
bool operator==(const Field &other) const
Definition: qgsfields.h:71
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfields.h:64
#define SIP_SKIP
Definition: qgis_sip.h:126
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:372
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
#define SIP_FACTORY
Definition: qgis_sip.h:76
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:49
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.
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:382
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:320
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
QList< int > QgsAttributeList
Definition: qgsfield.h:26
int originIndex
Index specific to the origin.
Definition: qgsfields.h:77
FieldOrigin origin
Origin of the field.
Definition: qgsfields.h:76