QGIS API Documentation
qgsfield.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfield.h - Describes a field in a layer or table
3  --------------------------------------
4  Date : 01-Jan-2004
5  Copyright : (C) 2004 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSFIELD_H
17 #define QGSFIELD_H
18 
19 #include <QString>
20 #include <QVariant>
21 #include <QVector>
22 #include <QSharedDataPointer>
23 
25 
26 class QgsExpression;
27 class QgsFieldPrivate;
28 class QgsFieldsPrivate;
29 
30 /***************************************************************************
31  * This class is considered CRITICAL and any change MUST be accompanied with
32  * full unit tests in testqgsfield.cpp.
33  * See details in QEP #17
34  ****************************************************************************/
35 
44 class CORE_EXPORT QgsField
45 {
46  public:
58  QgsField( const QString& name = QString(),
59  QVariant::Type type = QVariant::Invalid,
60  const QString& typeName = QString(),
61  int len = 0,
62  int prec = 0,
63  const QString& comment = QString() );
64 
67  QgsField( const QgsField& other );
68 
71  QgsField& operator =( const QgsField &other );
72 
74  virtual ~QgsField();
75 
76  bool operator==( const QgsField& other ) const;
77  bool operator!=( const QgsField& other ) const;
78 
80  QString name() const;
81 
83  QVariant::Type type() const;
84 
91  QString typeName() const;
92 
97  int length() const;
98 
103  int precision() const;
104 
108  QString comment() const;
109 
114  void setName( const QString& name );
115 
119  void setType( QVariant::Type type );
120 
125  void setTypeName( const QString& typeName );
126 
131  void setLength( int len );
132 
137  void setPrecision( int precision );
138 
142  void setComment( const QString& comment );
143 
145  QString displayString( const QVariant& v ) const;
146 
154  bool convertCompatible( QVariant& v ) const;
155 
156  private:
157 
159 
160 
161 }; // class QgsField
162 
163 Q_DECLARE_METATYPE( QgsField )
164 
165 
166 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsField& field );
168 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsField& field );
169 
170 
171 
172 /***************************************************************************
173  * This class is considered CRITICAL and any change MUST be accompanied with
174  * full unit tests in testqgsfields.cpp.
175  * See details in QEP #17
176  ****************************************************************************/
177 
187 class CORE_EXPORT QgsFields
188 {
189  public:
190 
192  {
197  OriginExpression
198  };
199 
200  typedef struct Field
201  {
202  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
203  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
204 
206  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
208  bool operator!=( const Field& other ) const { return !( *this == other ); }
209 
213  } Field;
214 
217  QgsFields();
218 
221  QgsFields( const QgsFields& other );
222 
225  QgsFields& operator =( const QgsFields &other );
226 
227  virtual ~QgsFields();
228 
230  void clear();
232  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
234  bool appendExpressionField( const QgsField& field, int originIndex );
236  void remove( int fieldIdx );
238  void extend( const QgsFields& other );
239 
241  bool isEmpty() const;
243  int count() const;
245  int size() const;
249  bool exists( int i ) const;
250 
252  const QgsField& operator[]( int i ) const;
254  QgsField& operator[]( int i );
256  const QgsField& at( int i ) const;
258  const QgsField& field( int fieldIdx ) const;
260  const QgsField& field( const QString& name ) const;
261 
263  FieldOrigin fieldOrigin( int fieldIdx ) const;
265  int fieldOriginIndex( int fieldIdx ) const;
266 
268  int indexFromName( const QString& name ) const;
269 
273  int fieldNameIndex( const QString& fieldName ) const;
274 
277  QgsAttributeList allAttributesList() const;
278 
280  QList<QgsField> toList() const;
281 
283  bool operator==( const QgsFields& other ) const;
285  bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
289  QIcon iconForField( int fieldIdx ) const;
290 
292 
293  class const_iterator;
294 
295  class iterator
296  {
297  public:
298  QgsFields::Field* d;
299  typedef std::random_access_iterator_tag iterator_category;
300  typedef qptrdiff difference_type;
301 
302  inline iterator()
303  : d( nullptr )
304  {}
305  inline iterator( QgsFields::Field *n )
306  : d( n )
307  {}
308 
309  inline QgsField& operator*() const { return d->field; }
310  inline QgsField* operator->() const { return &d->field; }
311  inline QgsField& operator[]( difference_type j ) const { return d[j].field; }
312  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; }
313  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; }
314  inline bool operator<( const iterator& other ) const noexcept { return d < other.d; }
315  inline bool operator<=( const iterator& other ) const noexcept { return d <= other.d; }
316  inline bool operator>( const iterator& other ) const noexcept { return d > other.d; }
317  inline bool operator>=( const iterator& other ) const noexcept { return d >= other.d; }
318 
319  inline iterator& operator++() { ++d; return *this; }
320  inline iterator operator++( int ) { QgsFields::Field* n = d; ++d; return n; }
321  inline iterator& operator--() { d--; return *this; }
322  inline iterator operator--( int ) { QgsFields::Field* n = d; d--; return n; }
323  inline iterator& operator+=( difference_type j ) { d += j; return *this; }
324  inline iterator& operator-=( difference_type j ) { d -= j; return *this; }
325  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
326  inline iterator operator-( difference_type j ) const { return iterator( d -j ); }
327  inline int operator-( iterator j ) const { return int( d - j.d ); }
328  };
329  friend class iterator;
330 
331  class const_iterator
332  {
333  public:
334  const QgsFields::Field* d;
335 
336  typedef std::random_access_iterator_tag iterator_category;
337  typedef qptrdiff difference_type;
338 
339  inline const_iterator()
340  : d( nullptr ) {}
341  inline const_iterator( const QgsFields::Field* f )
342  : d( f ) {}
343  inline const_iterator( const const_iterator &o )
344  : d( o.d ) {}
345  inline explicit const_iterator( const iterator &o )
346  : d( o.d ) {}
347  inline const QgsField& operator*() const { return d->field; }
348  inline const QgsField* operator->() const { return &d->field; }
349  inline const QgsField& operator[]( difference_type j ) const noexcept { return d[j].field; }
350  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
351  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
352  inline bool operator<( const const_iterator& other ) const noexcept { return d < other.d; }
353  inline bool operator<=( const const_iterator& other ) const noexcept { return d <= other.d; }
354  inline bool operator>( const const_iterator& other ) const noexcept { return d > other.d; }
355  inline bool operator>=( const const_iterator& other ) const noexcept { return d >= other.d; }
356  inline const_iterator& operator++() { ++d; return *this; }
357  inline const_iterator operator++( int ) { const QgsFields::Field* n = d; ++d; return n; }
358  inline const_iterator& operator--() { d--; return *this; }
359  inline const_iterator operator--( int ) { const QgsFields::Field* n = d; --d; return n; }
360  inline const_iterator& operator+=( difference_type j ) { d += j; return *this; }
361  inline const_iterator& operator-=( difference_type j ) { d -= j; return *this; }
362  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
363  inline const_iterator operator-( difference_type j ) const { return const_iterator( d -j ); }
364  inline int operator-( const_iterator j ) const { return int( d - j.d ); }
365  };
366  friend class const_iterator;
368 
369 
376  const_iterator constBegin() const noexcept;
377 
384  const_iterator constEnd() const noexcept;
385 
392  const_iterator begin() const noexcept;
393 
400  const_iterator end() const noexcept;
401 
408  iterator begin();
409 
410 
417  iterator end();
418 
419  private:
420 
422 
423 };
424 
425 Q_DECLARE_METATYPE( QgsFields )
426 
427 
428 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFields& fields );
430 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFields& fields );
431 
432 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:195
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:196
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:210
Container of fields for a vector layer.
Definition: qgsfield.h:187
field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfield.h:194
it has not been specified where the field comes from
Definition: qgsfield.h:193
bool operator==(const Field &other) const
Definition: qgsfield.h:206
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:203
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
Definition: qgsfield.cpp:242
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
bool operator!=(const Field &other) const
Definition: qgsfield.h:208
QList< int > QgsAttributeList
Definition: qgsfield.h:24
int originIndex
index specific to the origin
Definition: qgsfield.h:212
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:285
FieldOrigin origin
origin of the field
Definition: qgsfield.h:211
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Definition: qgsfield.cpp:253