QGIS API Documentation  3.5.0-Master (bc98f86)
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.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 
42 class CORE_EXPORT QgsFields
43 {
44  public:
45 
47  {
52  OriginExpression
53  };
54 
55 #ifndef SIP_RUN
56 
57  typedef struct Field
58  {
60  {}
61 
62  Field( const QgsField &f, FieldOrigin o, int oi )
63  : field( f )
64  , origin( o )
65  , originIndex( oi )
66  {}
67 
69  bool operator==( const Field &other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
71  bool operator!=( const Field &other ) const { return !( *this == other ); }
72 
74  FieldOrigin origin = OriginUnknown ;
75  int originIndex = -1 ;
76  } Field;
77 
78 #endif
79 
83  QgsFields();
84 
88  QgsFields( const QgsFields &other );
89 
93  QgsFields &operator =( const QgsFields &other ) SIP_SKIP;
94 
95  virtual ~QgsFields();
96 
98  void clear();
99 
101  bool append( const QgsField &field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
102 
104  bool appendExpressionField( const QgsField &field, int originIndex );
105 
107  void remove( int fieldIdx );
108 #ifdef SIP_RUN
109  % MethodCode
110  if ( a0 < 0 || a0 >= sipCpp->count() )
111  {
112  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
113  sipIsErr = 1;
114  }
115  else
116  {
117  sipCpp->remove( a0 );
118  }
119  % End
120 #endif
121 
123  void extend( const QgsFields &other );
124 
126  bool isEmpty() const;
127 
129  int count() const;
130 
131 #ifdef SIP_RUN
132  int __len__() const;
133  % MethodCode
134  sipRes = sipCpp->count();
135  % End
136 
138  int __bool__() const;
139  % MethodCode
140  sipRes = true;
141  % End
142 #endif
143 
145  int size() const;
146 
151  QStringList names() const;
152 
158  bool exists( int i ) const;
159 
160 #ifndef SIP_RUN
161  QgsField operator[]( int i ) const;
163 #endif
164 
166  QgsField &operator[]( int i ) SIP_FACTORY;
167 #ifdef SIP_RUN
168  % MethodCode
169  SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
170  if ( idx < 0 )
171  sipIsErr = 1;
172  else
173  sipRes = new QgsField( sipCpp->operator[]( idx ) );
174  % End
175 #endif
176 
178  QgsField at( int i ) const SIP_FACTORY;
179 #ifdef SIP_RUN
180  % MethodCode
181  if ( a0 < 0 || a0 >= sipCpp->count() )
182  {
183  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
184  sipIsErr = 1;
185  }
186  else
187  {
188  sipRes = new QgsField( sipCpp->at( a0 ) );
189  }
190  % End
191 #endif
192 
194  QgsField field( int fieldIdx ) const SIP_FACTORY;
195 #ifdef SIP_RUN
196  % MethodCode
197  if ( a0 < 0 || a0 >= sipCpp->count() )
198  {
199  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
200  sipIsErr = 1;
201  }
202  else
203  {
204  sipRes = new QgsField( sipCpp->field( a0 ) );
205  }
206  % End
207 #endif
208 
210  QgsField field( const QString &name ) const SIP_FACTORY;
211 #ifdef SIP_RUN
212  % MethodCode
213  int fieldIdx = sipCpp->indexFromName( *a0 );
214  if ( fieldIdx == -1 )
215  {
216  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
217  sipIsErr = 1;
218  }
219  else
220  {
221  sipRes = new QgsField( sipCpp->field( *a0 ) );
222  }
223  % End
224 #endif
225 
227  FieldOrigin fieldOrigin( int fieldIdx ) const;
228 #ifdef SIP_RUN
229  % MethodCode
230  if ( a0 < 0 || a0 >= sipCpp->count() )
231  {
232  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
233  sipIsErr = 1;
234  }
235  else
236  {
237  sipRes = sipCpp->fieldOrigin( a0 );
238  }
239  % End
240 #endif
241 
243  int fieldOriginIndex( int fieldIdx ) const;
244 #ifdef SIP_RUN
245  % MethodCode
246  if ( a0 < 0 || a0 >= sipCpp->count() )
247  {
248  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
249  sipIsErr = 1;
250  }
251  else
252  {
253  sipRes = sipCpp->fieldOriginIndex( a0 );
254  }
255  % End
256 #endif
257 
269  int indexFromName( const QString &fieldName ) const;
270 
282  int indexOf( const QString &fieldName ) const;
283 
298  int lookupField( const QString &fieldName ) const;
299 
304  QgsAttributeList allAttributesList() const;
305 
307  QList<QgsField> toList() const;
308 
310  bool operator==( const QgsFields &other ) const;
312  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
313 
318  QIcon iconForField( int fieldIdx ) const SIP_FACTORY;
319 #ifdef SIP_RUN
320  % MethodCode
321  if ( a0 < 0 || a0 >= sipCpp->count() )
322  {
323  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
324  sipIsErr = 1;
325  }
326  else
327  {
328  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
329  }
330  % End
331 #endif
332 
334  operator QVariant() const
335  {
336  return QVariant::fromValue( *this );
337  }
338 
339 #ifdef SIP_RUN
340 
341  void __setitem__( int key, const QgsField &field );
342  % MethodCode
343  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
344  if ( idx < 0 )
345  sipIsErr = 1;
346  else
347  ( *sipCpp )[idx] = *a1;
348  % End
349 
350 #endif
351 
352 #ifndef SIP_RUN
353 
355 
356  class const_iterator;
357 
358  class iterator
359  {
360  public:
361  QgsFields::Field *d = nullptr;
362  typedef std::random_access_iterator_tag iterator_category;
363  typedef qptrdiff difference_type;
364 
365  inline iterator()
366  {}
367 
368  inline iterator( QgsFields::Field *n )
369  : d( n )
370  {}
371 
372  inline QgsField &operator*() const { return d->field; }
373  inline QgsField *operator->() const { return &d->field; }
374  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
375  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
376  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
377  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
378  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
379  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
380  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
381 
382  inline iterator &operator++() { ++d; return *this; }
383  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
384  inline iterator &operator--() { d--; return *this; }
385  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
386  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
387  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
388  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
389  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
390  inline int operator-( iterator j ) const { return int( d - j.d ); }
391  };
392  friend class iterator;
393 
394  class const_iterator // clazy:exclude=rule-of-three
395  {
396  public:
397  const QgsFields::Field *d = nullptr;
398 
399  typedef std::random_access_iterator_tag iterator_category;
400  typedef qptrdiff difference_type;
401 
402  inline const_iterator()
403  {}
404 
405  inline const_iterator( const QgsFields::Field *f )
406  : d( f ) {}
407  inline const_iterator( const const_iterator &o )
408  : d( o.d ) {}
409  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
410  : d( o.d ) {}
411  inline const QgsField &operator*() const { return d->field; }
412  inline const QgsField *operator->() const { return &d->field; }
413  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
414  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
415  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
416  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
417  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
418  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
419  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
420  inline const_iterator &operator++() { ++d; return *this; }
421  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
422  inline const_iterator &operator--() { d--; return *this; }
423  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
424  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
425  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
426  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
427  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
428  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
429  };
430  friend class const_iterator;
432 
433 
440  const_iterator constBegin() const noexcept;
441 
448  const_iterator constEnd() const noexcept;
449 
456  const_iterator begin() const noexcept;
457 
464  const_iterator end() const noexcept;
465 
472  iterator begin();
473 
474 
481  iterator end();
482 
483 #endif
484 
485  private:
486 
487  QSharedDataPointer<QgsFieldsPrivate> d;
488 
489 };
490 
492 
493 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
496 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
497 
498 #endif // QGSFIELDS_H
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:50
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:51
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
Field.
Definition: qgsfields.h:73
Container of fields for a vector layer.
Definition: qgsfields.h:42
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:49
Q_DECLARE_METATYPE(QModelIndex)
bool operator!=(const Field &other) const
Definition: qgsfields.h:71
It has not been specified where the field comes from.
Definition: qgsfields.h:48
bool operator==(const Field &other) const
Definition: qgsfields.h:69
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfields.h:62
#define SIP_SKIP
Definition: qgis_sip.h:119
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:344
QDateTime operator+(const QDateTime &start, QgsInterval interval)
#define SIP_FACTORY
Definition: qgis_sip.h:69
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:354
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:312
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:27
int originIndex
Index specific to the origin.
Definition: qgsfields.h:75
FieldOrigin origin
Origin of the field.
Definition: qgsfields.h:74