QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
23 typedef QList<int> QgsAttributeList;
24 
25 class QgsExpression;
26 
33 class CORE_EXPORT QgsField
34 {
35  public:
48  QgsField( QString name = QString(),
49  QVariant::Type type = QVariant::Invalid,
50  QString typeName = QString(),
51  int len = 0,
52  int prec = 0,
53  QString comment = QString() );
54 
56  ~QgsField();
57 
58  bool operator==( const QgsField& other ) const;
59  bool operator!=( const QgsField& other ) const;
60 
62  const QString & name() const;
63 
65  QVariant::Type type() const;
66 
73  const QString & typeName() const;
74 
75 
80  int length() const;
81 
82 
87  int precision() const;
88 
92  const QString & comment() const;
93 
98  void setName( const QString & nam );
99 
103  void setType( QVariant::Type type );
104 
109  void setTypeName( const QString & typ );
110 
115  void setLength( int len );
116 
121  void setPrecision( int prec );
122 
123 
127  void setComment( const QString & comment );
128 
130  QString displayString( const QVariant& v ) const;
131 
139  bool convertCompatible( QVariant& v ) const;
140 
141  private:
142 
144  QString mName;
145 
147  QVariant::Type mType;
148 
150  QString mTypeName;
151 
153  int mLength;
154 
156  int mPrecision;
157 
159  QString mComment;
160 
161 }; // class QgsField
162 
163 
172 class CORE_EXPORT QgsFields
173 {
174  public:
175 
177  {
182  OriginExpression
183  };
184 
185  typedef struct Field
186  {
187  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
188  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
189 
191  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
193  bool operator!=( const Field& other ) const { return !( *this == other ); }
194 
198  } Field;
199 
201  void clear();
203  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
205  bool appendExpressionField( const QgsField& field, int originIndex );
207  void remove( int fieldIdx );
209  void extend( const QgsFields& other );
210 
212  inline bool isEmpty() const { return mFields.isEmpty(); }
214  inline int count() const { return mFields.count(); }
216  inline int size() const { return mFields.count(); }
220  inline bool exists( int i ) const { return i >= 0 && i < mFields.count(); }
221 
223  inline const QgsField& operator[]( int i ) const { return mFields[i].field; }
225  inline QgsField& operator[]( int i ) { return mFields[i].field; }
227  const QgsField& at( int i ) const { return mFields[i].field; }
229  const QgsField& field( int fieldIdx ) const { return mFields[fieldIdx].field; }
231  const QgsField& field( const QString& name ) const { return mFields[ indexFromName( name )].field; }
232 
234  FieldOrigin fieldOrigin( int fieldIdx ) const { return mFields[fieldIdx].origin; }
236  int fieldOriginIndex( int fieldIdx ) const { return mFields[fieldIdx].originIndex; }
237 
238 
239 
241  int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }
242 
246  int fieldNameIndex( const QString& fieldName ) const;
247 
250  QgsAttributeList allAttributesList() const;
251 
253  QList<QgsField> toList() const;
254 
256  bool operator==( const QgsFields& other ) const { return mFields == other.mFields; }
258  bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
259 
260  protected:
262  QVector<Field> mFields;
263 
265  QHash<QString, int> mNameToIndex;
266 };
267 
268 
269 
270 
271 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
FieldOrigin
Definition: qgsfield.h:176
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:214
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:229
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:219
QgsField field
field
Definition: qgsfield.h:195
Container of fields for a vector layer.
Definition: qgsfield.h:172
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:227
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:179
bool operator==(const Field &other) const
Definition: qgsfield.h:191
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.h:220
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:188
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:236
QList< int > QgsAttributeList
Definition: qgsfeaturerequest.h:26
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:265
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:181
int count() const
Return number of items.
Definition: qgsfield.h:214
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:33
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
Definition: qgsfield.h:241
Field()
Definition: qgsfield.h:187
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:223
const QgsField & field(const QString &name) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:231
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:234
it has not been specified where the field comes from
Definition: qgsfield.h:178
int size() const
Return number of items.
Definition: qgsfield.h:216
bool operator!=(const Field &other) const
Definition: qgsfield.h:193
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:262
QgsField & operator[](int i)
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:225
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:180
QList< int > QgsAttributeList
Definition: qgsfield.h:23
int originIndex
index specific to the origin
Definition: qgsfield.h:197
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:258
FieldOrigin origin
origin of the field
Definition: qgsfield.h:196
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfield.h:212
Definition: qgsfield.h:185
bool operator==(const QgsFields &other) const
Definition: qgsfield.h:256