QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
342  operator QVariant() const
343  {
344  return QVariant::fromValue( *this );
345  }
346 
347 #ifdef SIP_RUN
348 
349  void __setitem__( int key, const QgsField &field );
350  % MethodCode
351  int idx = ( int )sipConvertFromSequenceIndex( a0, sipCpp->count() );
352  if ( idx < 0 )
353  sipIsErr = 1;
354  else
355  ( *sipCpp )[idx] = *a1;
356  % End
357 
358 #endif
359 
360 #ifndef SIP_RUN
361 
363 
364  class const_iterator;
365 
366  class iterator
367  {
368  public:
369  QgsFields::Field *d = nullptr;
370  typedef std::random_access_iterator_tag iterator_category;
371  typedef qptrdiff difference_type;
372 
373  inline iterator()
374  {}
375 
376  inline iterator( QgsFields::Field *n )
377  : d( n )
378  {}
379 
380  inline QgsField &operator*() const { return d->field; }
381  inline QgsField *operator->() const { return &d->field; }
382  inline QgsField &operator[]( difference_type j ) const { return d[j].field; }
383  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; } // clazy:exclude=function-args-by-value
384  inline bool operator!=( const iterator &o ) const noexcept { return d != o.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  inline bool operator>( const iterator &other ) const noexcept { return d > other.d; } // clazy:exclude=function-args-by-value
388  inline bool operator>=( const iterator &other ) const noexcept { return d >= other.d; } // clazy:exclude=function-args-by-value
389 
390  inline iterator &operator++() { ++d; return *this; }
391  inline iterator operator++( int ) { QgsFields::Field *n = d; ++d; return n; }
392  inline iterator &operator--() { d--; return *this; }
393  inline iterator operator--( int ) { QgsFields::Field *n = d; d--; return n; }
394  inline iterator &operator+=( difference_type j ) { d += j; return *this; }
395  inline iterator &operator-=( difference_type j ) { d -= j; return *this; }
396  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
397  inline iterator operator-( difference_type j ) const { return iterator( d - j ); }
398  inline int operator-( iterator j ) const { return int( d - j.d ); }
399  };
400  friend class iterator;
401 
402  class const_iterator // clazy:exclude=rule-of-three
403  {
404  public:
405  const QgsFields::Field *d = nullptr;
406 
407  typedef std::random_access_iterator_tag iterator_category;
408  typedef qptrdiff difference_type;
409 
410  inline const_iterator()
411  {}
412 
413  inline const_iterator( const QgsFields::Field *f )
414  : d( f ) {}
415  inline const_iterator( const const_iterator &o )
416  : d( o.d ) {}
417  inline explicit const_iterator( const iterator &o ) // clazy:exclude=function-args-by-value
418  : d( o.d ) {}
419  inline const QgsField &operator*() const { return d->field; }
420  inline const QgsField *operator->() const { return &d->field; }
421  inline const QgsField &operator[]( difference_type j ) const noexcept { return d[j].field; }
422  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
423  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.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 bool operator>( const const_iterator &other ) const noexcept { return d > other.d; }
427  inline bool operator>=( const const_iterator &other ) const noexcept { return d >= other.d; }
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--() { d--; return *this; }
431  inline const_iterator operator--( int ) { const QgsFields::Field *n = d; --d; return n; }
432  inline const_iterator &operator+=( difference_type j ) { d += j; return *this; }
433  inline const_iterator &operator-=( difference_type j ) { d -= j; return *this; }
434  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
435  inline const_iterator operator-( difference_type j ) const { return const_iterator( d - j ); }
436  inline int operator-( const_iterator j ) const { return int( d - j.d ); } // clazy:exclude=function-args-by-ref
437  private:
438  const_iterator &operator= ( const const_iterator & ) = delete;
439  };
440  friend class const_iterator;
442 
443 
450  const_iterator constBegin() const noexcept;
451 
458  const_iterator constEnd() const noexcept;
459 
466  const_iterator begin() const noexcept;
467 
474  const_iterator end() const noexcept;
475 
482  iterator begin();
483 
484 
491  iterator end();
492 
493 #endif
494 
495  private:
496 
497  QSharedDataPointer<QgsFieldsPrivate> d;
498 
499 };
500 
502 
503 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFields &fields );
506 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFields &fields );
507 
508 #endif // QGSFIELDS_H
QgsFields::OriginProvider
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
QgsFields::operator!=
bool operator!=(const QgsFields &other) const
Definition: qgsfields.h:320
QgsFields::FieldOrigin
FieldOrigin
Definition: qgsfields.h:48
QgsFields::OriginEdit
@ OriginEdit
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:53
QgsFields::Field::operator!=
bool operator!=(const Field &other) const
Definition: qgsfields.h:73
operator>>
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:377
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:399
QgsFields::Field::origin
FieldOrigin origin
Origin of the field.
Definition: qgsfields.h:76
QgsFields
Definition: qgsfields.h:44
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:404
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsFields::Field::field
QgsField field
Field.
Definition: qgsfields.h:75
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsFields::OriginUnknown
@ OriginUnknown
It has not been specified where the field comes from.
Definition: qgsfields.h:50
QgsFields::Field::Field
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfields.h:64
qgis_sip.h
operator*
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
QgsFields::Field
Definition: qgsfields.h:59
QgsFields::Field::operator==
bool operator==(const Field &other) const
Definition: qgsfields.h:71
operator-
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:138
qgsfield.h
operator+
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:144
QgsFields::Field::Field
Field()
Definition: qgsfields.h:61
QgsFields::Field::originIndex
int originIndex
Index specific to the origin.
Definition: qgsfields.h:77
operator<<
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfields.cpp:367
QgsFields::OriginJoin
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:52
QgsField
Definition: qgsfield.h:49