QGIS API Documentation  2.17.0-Master (2f94c66)
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  Q_GADGET
47 
48  Q_PROPERTY( bool isNumeric READ isNumeric )
49  Q_PROPERTY( int length READ length WRITE setLength )
50  Q_PROPERTY( int precision READ precision WRITE setPrecision )
51  Q_PROPERTY( QString comment READ comment WRITE setComment )
52  Q_PROPERTY( QString name READ name WRITE setName )
53  Q_PROPERTY( QString alias READ alias WRITE setAlias )
54  Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
55 
56  public:
68  QgsField( const QString& name = QString(),
69  QVariant::Type type = QVariant::Invalid,
70  const QString& typeName = QString(),
71  int len = 0,
72  int prec = 0,
73  const QString& comment = QString() );
74 
77  QgsField( const QgsField& other );
78 
81  QgsField& operator =( const QgsField &other );
82 
84  virtual ~QgsField();
85 
86  bool operator==( const QgsField& other ) const;
87  bool operator!=( const QgsField& other ) const;
88 
93  QString name() const;
94 
101  QString displayName() const;
102 
104  QVariant::Type type() const;
105 
112  QString typeName() const;
113 
118  int length() const;
119 
124  int precision() const;
125 
129  QString comment() const;
130 
137  bool isNumeric() const;
138 
143  void setName( const QString& name );
144 
148  void setType( QVariant::Type type );
149 
154  void setTypeName( const QString& typeName );
155 
160  void setLength( int len );
161 
166  void setPrecision( int precision );
167 
171  void setComment( const QString& comment );
172 
179  QString defaultValueExpression() const;
180 
187  void setDefaultValueExpression( const QString& expression );
188 
194  QString alias() const;
195 
201  void setAlias( const QString& alias );
202 
204  QString displayString( const QVariant& v ) const;
205 
213  bool convertCompatible( QVariant& v ) const;
214 
216  operator QVariant() const
217  {
218  return QVariant::fromValue( *this );
219  }
220 
221  private:
222 
224 
225 
226 }; // class QgsField
227 
229 
230 
231 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsField& field );
233 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsField& field );
234 
235 
236 
237 /***************************************************************************
238  * This class is considered CRITICAL and any change MUST be accompanied with
239  * full unit tests in testqgsfields.cpp.
240  * See details in QEP #17
241  ****************************************************************************/
242 
252 class CORE_EXPORT QgsFields
253 {
254  public:
255 
257  {
262  OriginExpression
263  };
264 
265  typedef struct Field
266  {
267  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
268  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
269 
271  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
273  bool operator!=( const Field& other ) const { return !( *this == other ); }
274 
278  } Field;
279 
282  QgsFields();
283 
286  QgsFields( const QgsFields& other );
287 
290  QgsFields& operator =( const QgsFields &other );
291 
292  virtual ~QgsFields();
293 
295  void clear();
297  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
299  bool appendExpressionField( const QgsField& field, int originIndex );
301  void remove( int fieldIdx );
303  void extend( const QgsFields& other );
304 
306  bool isEmpty() const;
308  int count() const;
310  int size() const;
314  bool exists( int i ) const;
315 
317  const QgsField& operator[]( int i ) const;
319  QgsField& operator[]( int i );
321  const QgsField& at( int i ) const;
323  const QgsField& field( int fieldIdx ) const;
325  const QgsField& field( const QString& name ) const;
326 
328  FieldOrigin fieldOrigin( int fieldIdx ) const;
330  int fieldOriginIndex( int fieldIdx ) const;
331 
333  int indexFromName( const QString& name ) const;
334 
338  int fieldNameIndex( const QString& fieldName ) const;
339 
342  QgsAttributeList allAttributesList() const;
343 
345  QList<QgsField> toList() const;
346 
348  bool operator==( const QgsFields& other ) const;
350  bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
354  QIcon iconForField( int fieldIdx ) const;
355 
357  operator QVariant() const
358  {
359  return QVariant::fromValue( *this );
360  }
361 
363 
364  class const_iterator;
365 
366  class iterator
367  {
368  public:
369  QgsFields::Field* d;
370  typedef std::random_access_iterator_tag iterator_category;
371  typedef qptrdiff difference_type;
372 
373  inline iterator()
374  : d( nullptr )
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; }
384  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; }
385  inline bool operator<( const iterator& other ) const noexcept { return d < other.d; }
386  inline bool operator<=( const iterator& other ) const noexcept { return d <= other.d; }
387  inline bool operator>( const iterator& other ) const noexcept { return d > other.d; }
388  inline bool operator>=( const iterator& other ) const noexcept { return d >= other.d; }
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
403  {
404  public:
405  const QgsFields::Field* d;
406 
407  typedef std::random_access_iterator_tag iterator_category;
408  typedef qptrdiff difference_type;
409 
410  inline const_iterator()
411  : d( nullptr ) {}
412  inline const_iterator( const QgsFields::Field* f )
413  : d( f ) {}
414  inline const_iterator( const const_iterator &o )
415  : d( o.d ) {}
416  inline explicit const_iterator( const iterator &o )
417  : d( o.d ) {}
418  inline const QgsField& operator*() const { return d->field; }
419  inline const QgsField* operator->() const { return &d->field; }
420  inline const QgsField& operator[]( difference_type j ) const noexcept { return d[j].field; }
421  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
422  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.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 bool operator>=( const const_iterator& other ) const noexcept { return d >= other.d; }
427  inline const_iterator& operator++() { ++d; return *this; }
428  inline const_iterator operator++( int ) { const QgsFields::Field* n = d; ++d; return n; }
429  inline const_iterator& operator--() { d--; return *this; }
430  inline const_iterator operator--( int ) { const QgsFields::Field* n = d; --d; return n; }
431  inline const_iterator& operator+=( difference_type j ) { d += j; return *this; }
432  inline const_iterator& operator-=( difference_type j ) { d -= j; return *this; }
433  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
434  inline const_iterator operator-( difference_type j ) const { return const_iterator( d -j ); }
435  inline int operator-( const_iterator j ) const { return int( d - j.d ); }
436  };
437  friend class const_iterator;
439 
440 
447  const_iterator constBegin() const noexcept;
448 
455  const_iterator constEnd() const noexcept;
456 
463  const_iterator begin() const noexcept;
464 
471  const_iterator end() const noexcept;
472 
479  iterator begin();
480 
481 
488  iterator end();
489 
490  private:
491 
493 
494 };
495 
497 
498 
499 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFields& fields );
501 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFields& fields );
502 
503 #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:260
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:261
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:275
Container of fields for a vector layer.
Definition: qgsfield.h:252
field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfield.h:259
Q_DECLARE_METATYPE(QModelIndex)
it has not been specified where the field comes from
Definition: qgsfield.h:258
bool operator==(const Field &other) const
Definition: qgsfield.h:271
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:268
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
Definition: qgsfield.cpp:275
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
QVariant fromValue(const T &value)
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
bool operator!=(const Field &other) const
Definition: qgsfield.h:273
QList< int > QgsAttributeList
Definition: qgsfield.h:24
int originIndex
index specific to the origin
Definition: qgsfield.h:277
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:350
FieldOrigin origin
origin of the field
Definition: qgsfield.h:276
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Definition: qgsfield.cpp:288