QGIS API Documentation  2.15.0-Master (af20121)
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 
157  operator QVariant() const
158  {
159  return QVariant::fromValue( *this );
160  }
161 
162  private:
163 
165 
166 
167 }; // class QgsField
168 
169 Q_DECLARE_METATYPE( QgsField )
170 
171 
172 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsField& field );
174 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsField& field );
175 
176 
177 
178 /***************************************************************************
179  * This class is considered CRITICAL and any change MUST be accompanied with
180  * full unit tests in testqgsfields.cpp.
181  * See details in QEP #17
182  ****************************************************************************/
183 
193 class CORE_EXPORT QgsFields
194 {
195  public:
196 
198  {
203  OriginExpression
204  };
205 
206  typedef struct Field
207  {
208  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
209  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
210 
212  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
214  bool operator!=( const Field& other ) const { return !( *this == other ); }
215 
219  } Field;
220 
223  QgsFields();
224 
227  QgsFields( const QgsFields& other );
228 
231  QgsFields& operator =( const QgsFields &other );
232 
233  virtual ~QgsFields();
234 
236  void clear();
238  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
240  bool appendExpressionField( const QgsField& field, int originIndex );
242  void remove( int fieldIdx );
244  void extend( const QgsFields& other );
245 
247  bool isEmpty() const;
249  int count() const;
251  int size() const;
255  bool exists( int i ) const;
256 
258  const QgsField& operator[]( int i ) const;
260  QgsField& operator[]( int i );
262  const QgsField& at( int i ) const;
264  const QgsField& field( int fieldIdx ) const;
266  const QgsField& field( const QString& name ) const;
267 
269  FieldOrigin fieldOrigin( int fieldIdx ) const;
271  int fieldOriginIndex( int fieldIdx ) const;
272 
274  int indexFromName( const QString& name ) const;
275 
279  int fieldNameIndex( const QString& fieldName ) const;
280 
283  QgsAttributeList allAttributesList() const;
284 
286  QList<QgsField> toList() const;
287 
289  bool operator==( const QgsFields& other ) const;
291  bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
295  QIcon iconForField( int fieldIdx ) const;
296 
298  operator QVariant() const
299  {
300  return QVariant::fromValue( *this );
301  }
302 
304 
305  class const_iterator;
306 
307  class iterator
308  {
309  public:
310  QgsFields::Field* d;
311  typedef std::random_access_iterator_tag iterator_category;
312  typedef qptrdiff difference_type;
313 
314  inline iterator()
315  : d( nullptr )
316  {}
317  inline iterator( QgsFields::Field *n )
318  : d( n )
319  {}
320 
321  inline QgsField& operator*() const { return d->field; }
322  inline QgsField* operator->() const { return &d->field; }
323  inline QgsField& operator[]( difference_type j ) const { return d[j].field; }
324  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; }
325  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; }
326  inline bool operator<( const iterator& other ) const noexcept { return d < other.d; }
327  inline bool operator<=( const iterator& other ) const noexcept { return d <= other.d; }
328  inline bool operator>( const iterator& other ) const noexcept { return d > other.d; }
329  inline bool operator>=( const iterator& other ) const noexcept { return d >= other.d; }
330 
331  inline iterator& operator++() { ++d; return *this; }
332  inline iterator operator++( int ) { QgsFields::Field* n = d; ++d; return n; }
333  inline iterator& operator--() { d--; return *this; }
334  inline iterator operator--( int ) { QgsFields::Field* n = d; d--; return n; }
335  inline iterator& operator+=( difference_type j ) { d += j; return *this; }
336  inline iterator& operator-=( difference_type j ) { d -= j; return *this; }
337  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
338  inline iterator operator-( difference_type j ) const { return iterator( d -j ); }
339  inline int operator-( iterator j ) const { return int( d - j.d ); }
340  };
341  friend class iterator;
342 
343  class const_iterator
344  {
345  public:
346  const QgsFields::Field* d;
347 
348  typedef std::random_access_iterator_tag iterator_category;
349  typedef qptrdiff difference_type;
350 
351  inline const_iterator()
352  : d( nullptr ) {}
353  inline const_iterator( const QgsFields::Field* f )
354  : d( f ) {}
355  inline const_iterator( const const_iterator &o )
356  : d( o.d ) {}
357  inline explicit const_iterator( const iterator &o )
358  : d( o.d ) {}
359  inline const QgsField& operator*() const { return d->field; }
360  inline const QgsField* operator->() const { return &d->field; }
361  inline const QgsField& operator[]( difference_type j ) const noexcept { return d[j].field; }
362  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
363  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
364  inline bool operator<( const const_iterator& other ) const noexcept { return d < other.d; }
365  inline bool operator<=( const const_iterator& other ) const noexcept { return d <= other.d; }
366  inline bool operator>( const const_iterator& other ) const noexcept { return d > other.d; }
367  inline bool operator>=( const const_iterator& other ) const noexcept { return d >= other.d; }
368  inline const_iterator& operator++() { ++d; return *this; }
369  inline const_iterator operator++( int ) { const QgsFields::Field* n = d; ++d; return n; }
370  inline const_iterator& operator--() { d--; return *this; }
371  inline const_iterator operator--( int ) { const QgsFields::Field* n = d; --d; return n; }
372  inline const_iterator& operator+=( difference_type j ) { d += j; return *this; }
373  inline const_iterator& operator-=( difference_type j ) { d -= j; return *this; }
374  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
375  inline const_iterator operator-( difference_type j ) const { return const_iterator( d -j ); }
376  inline int operator-( const_iterator j ) const { return int( d - j.d ); }
377  };
378  friend class const_iterator;
380 
381 
388  const_iterator constBegin() const noexcept;
389 
396  const_iterator constEnd() const noexcept;
397 
404  const_iterator begin() const noexcept;
405 
412  const_iterator end() const noexcept;
413 
420  iterator begin();
421 
422 
429  iterator end();
430 
431  private:
432 
434 
435 };
436 
437 Q_DECLARE_METATYPE( QgsFields )
438 
439 
440 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFields& fields );
442 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFields& fields );
443 
444 #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:201
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:202
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:216
Container of fields for a vector layer.
Definition: qgsfield.h:193
field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfield.h:200
it has not been specified where the field comes from
Definition: qgsfield.h:199
bool operator==(const Field &other) const
Definition: qgsfield.h:212
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:209
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
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:214
QList< int > QgsAttributeList
Definition: qgsfield.h:24
int originIndex
index specific to the origin
Definition: qgsfield.h:218
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:291
FieldOrigin origin
origin of the field
Definition: qgsfield.h:217
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Definition: qgsfield.cpp:253