QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 )
50  Q_PROPERTY( int precision READ precision )
51  Q_PROPERTY( QString comment READ comment )
52  Q_PROPERTY( QString name READ name )
53 
54  public:
66  QgsField( const QString& name = QString(),
67  QVariant::Type type = QVariant::Invalid,
68  const QString& typeName = QString(),
69  int len = 0,
70  int prec = 0,
71  const QString& comment = QString() );
72 
75  QgsField( const QgsField& other );
76 
79  QgsField& operator =( const QgsField &other );
80 
82  virtual ~QgsField();
83 
84  bool operator==( const QgsField& other ) const;
85  bool operator!=( const QgsField& other ) const;
86 
88  QString name() const;
89 
91  QVariant::Type type() const;
92 
99  QString typeName() const;
100 
105  int length() const;
106 
111  int precision() const;
112 
116  QString comment() const;
117 
124  bool isNumeric() const;
125 
130  void setName( const QString& name );
131 
135  void setType( QVariant::Type type );
136 
141  void setTypeName( const QString& typeName );
142 
147  void setLength( int len );
148 
153  void setPrecision( int precision );
154 
158  void setComment( const QString& comment );
159 
161  QString displayString( const QVariant& v ) const;
162 
170  bool convertCompatible( QVariant& v ) const;
171 
173  operator QVariant() const
174  {
175  return QVariant::fromValue( *this );
176  }
177 
178  private:
179 
181 
182 
183 }; // class QgsField
184 
186 
187 
188 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsField& field );
190 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsField& field );
191 
192 
193 
194 /***************************************************************************
195  * This class is considered CRITICAL and any change MUST be accompanied with
196  * full unit tests in testqgsfields.cpp.
197  * See details in QEP #17
198  ****************************************************************************/
199 
209 class CORE_EXPORT QgsFields
210 {
211  public:
212 
214  {
219  OriginExpression
220  };
221 
222  typedef struct Field
223  {
224  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
225  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
226 
228  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
230  bool operator!=( const Field& other ) const { return !( *this == other ); }
231 
235  } Field;
236 
239  QgsFields();
240 
243  QgsFields( const QgsFields& other );
244 
247  QgsFields& operator =( const QgsFields &other );
248 
249  virtual ~QgsFields();
250 
252  void clear();
254  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
256  bool appendExpressionField( const QgsField& field, int originIndex );
258  void remove( int fieldIdx );
260  void extend( const QgsFields& other );
261 
263  bool isEmpty() const;
265  int count() const;
267  int size() const;
271  bool exists( int i ) const;
272 
274  const QgsField& operator[]( int i ) const;
276  QgsField& operator[]( int i );
278  const QgsField& at( int i ) const;
280  const QgsField& field( int fieldIdx ) const;
282  const QgsField& field( const QString& name ) const;
283 
285  FieldOrigin fieldOrigin( int fieldIdx ) const;
287  int fieldOriginIndex( int fieldIdx ) const;
288 
290  int indexFromName( const QString& name ) const;
291 
295  int fieldNameIndex( const QString& fieldName ) const;
296 
299  QgsAttributeList allAttributesList() const;
300 
302  QList<QgsField> toList() const;
303 
305  bool operator==( const QgsFields& other ) const;
307  bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
311  QIcon iconForField( int fieldIdx ) const;
312 
314  operator QVariant() const
315  {
316  return QVariant::fromValue( *this );
317  }
318 
320 
321  class const_iterator;
322 
323  class iterator
324  {
325  public:
326  QgsFields::Field* d;
327  typedef std::random_access_iterator_tag iterator_category;
328  typedef qptrdiff difference_type;
329 
330  inline iterator()
331  : d( nullptr )
332  {}
333  inline iterator( QgsFields::Field *n )
334  : d( n )
335  {}
336 
337  inline QgsField& operator*() const { return d->field; }
338  inline QgsField* operator->() const { return &d->field; }
339  inline QgsField& operator[]( difference_type j ) const { return d[j].field; }
340  inline bool operator==( const iterator &o ) const noexcept { return d == o.d; }
341  inline bool operator!=( const iterator &o ) const noexcept { return d != o.d; }
342  inline bool operator<( const iterator& other ) const noexcept { return d < other.d; }
343  inline bool operator<=( const iterator& other ) const noexcept { return d <= other.d; }
344  inline bool operator>( const iterator& other ) const noexcept { return d > other.d; }
345  inline bool operator>=( const iterator& other ) const noexcept { return d >= other.d; }
346 
347  inline iterator& operator++() { ++d; return *this; }
348  inline iterator operator++( int ) { QgsFields::Field* n = d; ++d; return n; }
349  inline iterator& operator--() { d--; return *this; }
350  inline iterator operator--( int ) { QgsFields::Field* n = d; d--; return n; }
351  inline iterator& operator+=( difference_type j ) { d += j; return *this; }
352  inline iterator& operator-=( difference_type j ) { d -= j; return *this; }
353  inline iterator operator+( difference_type j ) const { return iterator( d + j ); }
354  inline iterator operator-( difference_type j ) const { return iterator( d -j ); }
355  inline int operator-( iterator j ) const { return int( d - j.d ); }
356  };
357  friend class iterator;
358 
359  class const_iterator
360  {
361  public:
362  const QgsFields::Field* d;
363 
364  typedef std::random_access_iterator_tag iterator_category;
365  typedef qptrdiff difference_type;
366 
367  inline const_iterator()
368  : d( nullptr ) {}
369  inline const_iterator( const QgsFields::Field* f )
370  : d( f ) {}
371  inline const_iterator( const const_iterator &o )
372  : d( o.d ) {}
373  inline explicit const_iterator( const iterator &o )
374  : d( o.d ) {}
375  inline const QgsField& operator*() const { return d->field; }
376  inline const QgsField* operator->() const { return &d->field; }
377  inline const QgsField& operator[]( difference_type j ) const noexcept { return d[j].field; }
378  inline bool operator==( const const_iterator &o ) const noexcept { return d == o.d; }
379  inline bool operator!=( const const_iterator &o ) const noexcept { return d != o.d; }
380  inline bool operator<( const const_iterator& other ) const noexcept { return d < other.d; }
381  inline bool operator<=( const const_iterator& other ) const noexcept { return d <= other.d; }
382  inline bool operator>( const const_iterator& other ) const noexcept { return d > other.d; }
383  inline bool operator>=( const const_iterator& other ) const noexcept { return d >= other.d; }
384  inline const_iterator& operator++() { ++d; return *this; }
385  inline const_iterator operator++( int ) { const QgsFields::Field* n = d; ++d; return n; }
386  inline const_iterator& operator--() { d--; return *this; }
387  inline const_iterator operator--( int ) { const QgsFields::Field* n = d; --d; return n; }
388  inline const_iterator& operator+=( difference_type j ) { d += j; return *this; }
389  inline const_iterator& operator-=( difference_type j ) { d -= j; return *this; }
390  inline const_iterator operator+( difference_type j ) const { return const_iterator( d + j ); }
391  inline const_iterator operator-( difference_type j ) const { return const_iterator( d -j ); }
392  inline int operator-( const_iterator j ) const { return int( d - j.d ); }
393  };
394  friend class const_iterator;
396 
397 
404  const_iterator constBegin() const noexcept;
405 
412  const_iterator constEnd() const noexcept;
413 
420  const_iterator begin() const noexcept;
421 
428  const_iterator end() const noexcept;
429 
436  iterator begin();
437 
438 
445  iterator end();
446 
447  private:
448 
450 
451 };
452 
454 
455 
456 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFields& fields );
458 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFields& fields );
459 
460 #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:217
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:218
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:232
Container of fields for a vector layer.
Definition: qgsfield.h:209
field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfield.h:216
Q_DECLARE_METATYPE(QModelIndex)
it has not been specified where the field comes from
Definition: qgsfield.h:215
bool operator==(const Field &other) const
Definition: qgsfield.h:228
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:225
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
Definition: qgsfield.cpp:247
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:230
QList< int > QgsAttributeList
Definition: qgsfield.h:24
int originIndex
index specific to the origin
Definition: qgsfield.h:234
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:307
FieldOrigin origin
origin of the field
Definition: qgsfield.h:233
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Definition: qgsfield.cpp:258