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 
132  private:
133 
135  QString mName;
136 
138  QVariant::Type mType;
139 
141  QString mTypeName;
142 
144  int mLength;
145 
147  int mPrecision;
148 
150  QString mComment;
151 
152 }; // class QgsField
153 
154 
163 class CORE_EXPORT QgsFields
164 {
165  public:
166 
168  {
173  OriginExpression
174  };
175 
176  typedef struct Field
177  {
178  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
179  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
180 
182  bool operator==( const Field& other ) const { return field == other.field && origin == other.origin && originIndex == other.originIndex; }
184  bool operator!=( const Field& other ) const { return !( *this == other ); }
185 
189  } Field;
190 
192  void clear();
194  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
196  bool appendExpressionField( const QgsField& field, int originIndex );
198  void remove( int fieldIdx );
200  void extend( const QgsFields& other );
201 
203  inline bool isEmpty() const { return mFields.isEmpty(); }
205  inline int count() const { return mFields.count(); }
207  inline int size() const { return mFields.count(); }
211  inline bool exists( int i ) const { return i >= 0 && i < mFields.count(); }
212 
214  inline const QgsField& operator[]( int i ) const { return mFields[i].field; }
216  inline QgsField& operator[]( int i ) { return mFields[i].field; }
218  const QgsField& at( int i ) const { return mFields[i].field; }
220  const QgsField& field( int fieldIdx ) const { return mFields[fieldIdx].field; }
222  const QgsField& field( const QString& name ) const { return mFields[ indexFromName( name )].field; }
223 
225  FieldOrigin fieldOrigin( int fieldIdx ) const { return mFields[fieldIdx].origin; }
227  int fieldOriginIndex( int fieldIdx ) const { return mFields[fieldIdx].originIndex; }
228 
229 
230 
232  int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }
233 
237  int fieldNameIndex( const QString& fieldName ) const;
238 
241  QgsAttributeList allAttributesList() const;
242 
244  QList<QgsField> toList() const;
245 
247  bool operator==( const QgsFields& other ) const { return mFields == other.mFields; }
249  bool operator!=( const QgsFields& other ) const { return ! ( *this == other ); }
250 
251  protected:
253  QVector<Field> mFields;
254 
256  QHash<QString, int> mNameToIndex;
257 };
258 
259 
260 
261 
262 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:89
FieldOrigin
Definition: qgsfield.h:167
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:220
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:219
QgsField field
field
Definition: qgsfield.h:186
Container of fields for a vector layer.
Definition: qgsfield.h:163
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:218
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:170
bool operator==(const Field &other) const
Definition: qgsfield.h:182
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.h:211
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:179
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:227
QList< int > QgsAttributeList
Definition: qgsfeaturerequest.h:26
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:256
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:172
int count() const
Return number of items.
Definition: qgsfield.h:205
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:232
Field()
Definition: qgsfield.h:178
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:214
const QgsField & field(const QString &name) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:222
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:225
it has not been specified where the field comes from
Definition: qgsfield.h:169
int size() const
Return number of items.
Definition: qgsfield.h:207
bool operator!=(const Field &other) const
Definition: qgsfield.h:184
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:253
QgsField & operator[](int i)
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:216
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:171
QList< int > QgsAttributeList
Definition: qgsfield.h:23
int originIndex
index specific to the origin
Definition: qgsfield.h:188
bool operator!=(const QgsFields &other) const
Definition: qgsfield.h:249
FieldOrigin origin
origin of the field
Definition: qgsfield.h:187
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfield.h:203
Definition: qgsfield.h:176
bool operator==(const QgsFields &other) const
Definition: qgsfield.h:247