QGIS API Documentation  3.6.0-Noosa (5873452)
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 
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 
107  bool rename( int fieldIdx, const QString &name );
108 
110  bool appendExpressionField( const QgsField &field, int originIndex );
111 
113  void remove( int fieldIdx );
114 #ifdef SIP_RUN
115  % MethodCode
116  if ( a0 < 0 || a0 >= sipCpp->count() )
117  {
118  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
119  sipIsErr = 1;
120  }
121  else
122  {
123  sipCpp->remove( a0 );
124  }
125  % End
126 #endif
127 
129  void extend( const QgsFields &other );
130 
132  bool isEmpty() const;
133 
135  int count() const;
136 
137 #ifdef SIP_RUN
138  int __len__() const;
139  % MethodCode
140  sipRes = sipCpp->count();
141  % End
142 
144  int __bool__() const;
145  % MethodCode
146  sipRes = true;
147  % End
148 #endif
149 
151  int size() const;
152 
157  QStringList names() const;
158 
164  bool exists( int i ) const;
165 
166 #ifndef SIP_RUN
167  QgsField operator[]( int i ) const;
169 #endif
170 
172  QgsField &operator[]( int i ) SIP_FACTORY;
173 #ifdef SIP_RUN
174  % MethodCode
175  SIP_SSIZE_T idx = sipConvertFromSequenceIndex( a0, sipCpp->count() );
176  if ( idx < 0 )
177  sipIsErr = 1;
178  else
179  sipRes = new QgsField( sipCpp->operator[]( idx ) );
180  % End
181 #endif
182 
184  QgsField at( int i ) const SIP_FACTORY;
185 #ifdef SIP_RUN
186  % MethodCode
187  if ( a0 < 0 || a0 >= sipCpp->count() )
188  {
189  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
190  sipIsErr = 1;
191  }
192  else
193  {
194  sipRes = new QgsField( sipCpp->at( a0 ) );
195  }
196  % End
197 #endif
198 
200  QgsField field( int fieldIdx ) const SIP_FACTORY;
201 #ifdef SIP_RUN
202  % MethodCode
203  if ( a0 < 0 || a0 >= sipCpp->count() )
204  {
205  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
206  sipIsErr = 1;
207  }
208  else
209  {
210  sipRes = new QgsField( sipCpp->field( a0 ) );
211  }
212  % End
213 #endif
214 
216  QgsField field( const QString &name ) const SIP_FACTORY;
217 #ifdef SIP_RUN
218  % MethodCode
219  int fieldIdx = sipCpp->indexFromName( *a0 );
220  if ( fieldIdx == -1 )
221  {
222  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
223  sipIsErr = 1;
224  }
225  else
226  {
227  sipRes = new QgsField( sipCpp->field( *a0 ) );
228  }
229  % End
230 #endif
231 
233  FieldOrigin fieldOrigin( int fieldIdx ) const;
234 #ifdef SIP_RUN
235  % MethodCode
236  if ( a0 < 0 || a0 >= sipCpp->count() )
237  {
238  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
239  sipIsErr = 1;
240  }
241  else
242  {
243  sipRes = sipCpp->fieldOrigin( a0 );
244  }
245  % End
246 #endif
247 
249  int fieldOriginIndex( int fieldIdx ) const;
250 #ifdef SIP_RUN
251  % MethodCode
252  if ( a0 < 0 || a0 >= sipCpp->count() )
253  {
254  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
255  sipIsErr = 1;
256  }
257  else
258  {
259  sipRes = sipCpp->fieldOriginIndex( a0 );
260  }
261  % End
262 #endif
263 
275  int indexFromName( const QString &fieldName ) const;
276 
288  int indexOf( const QString &fieldName ) const;
289 
304  int lookupField( const QString &fieldName ) const;
305 
310  QgsAttributeList allAttributesList() const;
311 
313  QList<QgsField> toList() const;
314 
316  bool operator==( const QgsFields &other ) const;
318  bool operator!=( const QgsFields &other ) const { return !( *this == other ); }
319 
324  QIcon iconForField( int fieldIdx ) const SIP_FACTORY;
325 #ifdef SIP_RUN
326  % MethodCode
327  if ( a0 < 0 || a0 >= sipCpp->count() )
328  {
329  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
330  sipIsErr = 1;
331  }
332  else
333  {
334  sipRes = new QIcon( sipCpp->iconForField( a0 ) );
335  }
336  % End
337 #endif
338 
340  operator QVariant() const
341  {
342  return QVariant::fromValue( *this );
343  }
344 
345 #ifdef SIP_RUN
346 
347  void __setitem__( int key, const QgsField &field );
348  % MethodCode
349  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
350  if ( idx < 0 )
351  sipIsErr = 1;
352  else
353  ( *sipCpp )[idx] = *a1;
354  % End
355 
356 #endif
357 
358 #ifndef SIP_RUN
359 
361 
362  class const_iterator;
363 
364  class iterator
365  {
366  public:
367  QgsFields::Field *d = nullptr;
368  typedef std::random_access_iterator_tag iterator_category;
369  typedef qptrdiff difference_type;
370 
371  inline iterator()
372  {}
373 
374  inline iterator( QgsFields::Field *n )
375  : d( n )
376  {}
377 
378  inline QgsField &operator*() const { return d->field; }
379  inline QgsField *operator->() const { return &d->field; }
380  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
381  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
382  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; } // clazy:exclude=function-args-by-value
383  inline bool operator<( const iterator &other ) const noexcept { return d < other.d; } // clazy:exclude=function-args-by-value
384  inline bool operator<=( const iterator &other ) const noexcept { return d <= other.d; } // clazy:exclude=function-args-by-value
385  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
386  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
387 
388  inline iterator &operator++() { ++d; return *this; }
389  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
390  inline iterator &operator--() { d--; return *this; }
391  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
392  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
393  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
394  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
395  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
396  inline int operator-( iterator j ) const { return int( d - j.d ); }
397  };
398  friend class iterator;
399 
400  class const_iterator // clazy:exclude=rule-of-three
401  {
402  public:
403  const QgsFields::Field *d = nullptr;
404 
405  typedef std::random_access_iterator_tag iterator_category;
406  typedef qptrdiff difference_type;
407 
408  inline const_iterator()
409  {}
410 
411  inline const_iterator( const QgsFields::Field *f )
412  : d( f ) {}
413  inline const_iterator( const const_iterator &o )
414  : d( o.d ) {}
415  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
416  : d( o.d ) {}
417  inline const QgsField &operator*() const { return d->field; }
418  inline const QgsField *operator->() const { return &d->field; }
419  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
420  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
421  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
422  inline bool operator<( const const_iterator &other ) const noexcept { return d < other.d; }
423  inline bool operator<=( const const_iterator &other ) const noexcept { return d <= other.d; }
424  inline bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
425  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
426  inline const_iterator &operator++() { ++d; return *this; }
427  inline const_iterator operator++( int ) { const QgsFields::Field *n = d; ++d; return n; }
428  inline const_iterator &operator--() { d--; return *this; }
429  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
430  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
431  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
432  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
433  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
434  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
435  };
436  friend class const_iterator;
438 
439 
446  const_iterator constBegin() const noexcept;
447 
454  const_iterator constEnd() const noexcept;
455 
462  const_iterator begin() const noexcept;
463 
470  const_iterator end() const noexcept;
471 
478  iterator begin();
479 
480 
487  iterator end();
488 
489 #endif
490 
491  private:
492 
493  QSharedDataPointer<QgsFieldsPrivate> d;
494 
495 };
496 
498 
499 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
502 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
503 
504 #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:365
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:375
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:318
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