QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
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 
184  } Field;
185 
187  void clear();
189  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
191  bool appendExpressionField( const QgsField& field, int originIndex );
193  void remove( int fieldIdx );
195  void extend( const QgsFields& other );
196 
198  inline bool isEmpty() const { return mFields.isEmpty(); }
200  inline int count() const { return mFields.count(); }
202  inline int size() const { return mFields.count(); }
206  inline bool exists( int i ) const { return i >= 0 && i < mFields.count(); }
207 
209  inline const QgsField& operator[]( int i ) const { return mFields[i].field; }
211  inline QgsField& operator[]( int i ) { return mFields[i].field; }
213  const QgsField& at( int i ) const { return mFields[i].field; }
215  const QgsField& field( int fieldIdx ) const { return mFields[fieldIdx].field; }
217  const QgsField& field( const QString& name ) const { return mFields[ indexFromName( name )].field; }
218 
220  FieldOrigin fieldOrigin( int fieldIdx ) const { return mFields[fieldIdx].origin; }
222  int fieldOriginIndex( int fieldIdx ) const { return mFields[fieldIdx].originIndex; }
223 
224 
225 
227  int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }
228 
232  int fieldNameIndex( const QString& fieldName ) const;
233 
236  QgsAttributeList allAttributesList() const;
237 
239  QList<QgsField> toList() const;
240 
241  protected:
243  QVector<Field> mFields;
244 
246  QHash<QString, int> mNameToIndex;
247 };
248 
249 
250 
251 
252 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:89
int mLength
Length.
Definition: qgsfield.h:144
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:171
QString mTypeName
Type name from provider.
Definition: qgsfield.h:141
field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfield.h:172
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:215
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:181
int mPrecision
Precision.
Definition: qgsfield.h:147
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:213
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:170
it has not been specified where the field comes from
Definition: qgsfield.h:169
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.h:206
QString mName
Name.
Definition: qgsfield.h:135
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:222
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:246
QVariant::Type mType
Variant type.
Definition: qgsfield.h:138
int count() const
Return number of items.
Definition: qgsfield.h:200
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:227
QString mComment
Comment.
Definition: qgsfield.h:150
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:209
const QgsField & field(const QString &name) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:217
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:220
int size() const
Return number of items.
Definition: qgsfield.h:202
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:243
QgsField & operator[](int i)
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:211
QList< int > QgsAttributeList
Definition: qgsfield.h:23
int originIndex
index specific to the origin
Definition: qgsfield.h:183
FieldOrigin origin
origin of the field
Definition: qgsfield.h:182
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfield.h:198