QGIS API Documentation  3.0.2-Girona (307d082)
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 #endif
137 
139  int size() const;
140 
145  QStringList names() const;
146 
152  bool exists( int i ) const;
153 
154 #ifndef SIP_RUN
155  QgsField operator[]( int i ) const;
157 #endif
158 
160  QgsField &operator[]( int i ) SIP_FACTORY;
161 #ifdef SIP_RUN
162  % MethodCode
163  SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
164  if ( idx < 0 )
165  sipIsErr = 1;
166  else
167  sipRes = new QgsField( sipCpp->operator[]( idx ) );
168  % End
169 #endif
170 
172  QgsField at( int i ) const SIP_FACTORY;
173 #ifdef SIP_RUN
174  % MethodCode
175  if ( a0 < 0 || a0 >= sipCpp->count() )
176  {
177  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
178  sipIsErr = 1;
179  }
180  else
181  {
182  sipRes = new QgsField( sipCpp->at( a0 ) );
183  }
184  % End
185 #endif
186 
188  QgsField field( int fieldIdx ) const SIP_FACTORY;
189 #ifdef SIP_RUN
190  % MethodCode
191  if ( a0 < 0 || a0 >= sipCpp->count() )
192  {
193  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
194  sipIsErr = 1;
195  }
196  else
197  {
198  sipRes = new QgsField( sipCpp->field( a0 ) );
199  }
200  % End
201 #endif
202 
204  QgsField field( const QString &name ) const SIP_FACTORY;
205 #ifdef SIP_RUN
206  % MethodCode
207  int fieldIdx = sipCpp->indexFromName( *a0 );
208  if ( fieldIdx == -1 )
209  {
210  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
211  sipIsErr = 1;
212  }
213  else
214  {
215  sipRes = new QgsField( sipCpp->field( *a0 ) );
216  }
217  % End
218 #endif
219 
221  FieldOrigin fieldOrigin( int fieldIdx ) const;
222 #ifdef SIP_RUN
223  % MethodCode
224  if ( a0 < 0 || a0 >= sipCpp->count() )
225  {
226  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
227  sipIsErr = 1;
228  }
229  else
230  {
231  sipRes = sipCpp->fieldOrigin( a0 );
232  }
233  % End
234 #endif
235 
237  int fieldOriginIndex( int fieldIdx ) const;
238 #ifdef SIP_RUN
239  % MethodCode
240  if ( a0 < 0 || a0 >= sipCpp->count() )
241  {
242  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
243  sipIsErr = 1;
244  }
245  else
246  {
247  sipRes = sipCpp->fieldOriginIndex( a0 );
248  }
249  % End
250 #endif
251 
263  int indexFromName( const QString &fieldName ) const;
264 
276  int indexOf( const QString &fieldName ) const;
277 
292  int lookupField( const QString &fieldName ) const;
293 
298  QgsAttributeList allAttributesList() const;
299 
301  QList<QgsField> toList() const;
302 
304  bool operator==( const QgsFields &other ) const;
306  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
307 
312  QIcon iconForField( int fieldIdx ) const SIP_FACTORY;
313 #ifdef SIP_RUN
314  % MethodCode
315  if ( a0 < 0 || a0 >= sipCpp->count() )
316  {
317  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
318  sipIsErr = 1;
319  }
320  else
321  {
322  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
323  }
324  % End
325 #endif
326 
328  operator QVariant() const
329  {
330  return QVariant::fromValue( *this );
331  }
332 
333 #ifdef SIP_RUN
334 
335  void __setitem__( int key, const QgsField &field );
336  % MethodCode
337  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
338  if ( idx < 0 )
339  sipIsErr = 1;
340  else
341  ( *sipCpp )[idx] = *a1;
342  % End
343 
344 #endif
345 
346 #ifndef SIP_RUN
347 
349 
350  class const_iterator;
351 
352  class iterator
353  {
354  public:
355  QgsFields::Field *d = nullptr;
356  typedef std::random_access_iterator_tag iterator_category;
357  typedef qptrdiff difference_type;
358 
359  inline iterator()
360  {}
361 
362  inline iterator( QgsFields::Field *n )
363  : d( n )
364  {}
365 
366  inline QgsField &operator*() const { return d->field; }
367  inline QgsField *operator->() const { return &d->field; }
368  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
369  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
370  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
371  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
372  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
373  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
374  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
375 
376  inline iterator &operator++() { ++d; return *this; }
377  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
378  inline iterator &operator--() { d--; return *this; }
379  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
380  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
381  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
382  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
383  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
384  inline int operator-( iterator j ) const { return int( d - j.d ); }
385  };
386  friend class iterator;
387 
388  class const_iterator // clazy:exclude=rule-of-three
389  {
390  public:
391  const QgsFields::Field *d = nullptr;
392 
393  typedef std::random_access_iterator_tag iterator_category;
394  typedef qptrdiff difference_type;
395 
396  inline const_iterator()
397  {}
398 
399  inline const_iterator( const QgsFields::Field *f )
400  : d( f ) {}
401  inline const_iterator( const const_iterator &o )
402  : d( o.d ) {}
403  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
404  : d( o.d ) {}
405  inline const QgsField &operator*() const { return d->field; }
406  inline const QgsField *operator->() const { return &d->field; }
407  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
408  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
409  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
410  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
411  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
412  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
413  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
414  inline const_iterator &operator++() { ++d; return *this; }
415  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
416  inline const_iterator &operator--() { d--; return *this; }
417  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
418  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
419  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
420  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
421  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
422  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
423  };
424  friend class const_iterator;
426 
427 
434  const_iterator constBegin() const noexcept;
435 
442  const_iterator constEnd() const noexcept;
443 
450  const_iterator begin() const noexcept;
451 
458  const_iterator end() const noexcept;
459 
466  iterator begin();
467 
468 
475  iterator end();
476 
477 #endif
478 
479  private:
480 
481  QSharedDataPointer<QgsFieldsPrivate> d;
482 
483 };
484 
486 
487 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
490 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
491 
492 #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:340
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:350
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:306
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