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 
31 class CORE_EXPORT QgsField
32 {
33  public:
46  QgsField( QString name = QString(),
47  QVariant::Type type = QVariant::Invalid,
48  QString typeName = QString(),
49  int len = 0,
50  int prec = 0,
51  QString comment = QString() );
52 
54  ~QgsField();
55 
56  bool operator==( const QgsField& other ) const;
57  bool operator!=( const QgsField& other ) const;
58 
60  const QString & name() const;
61 
63  QVariant::Type type() const;
64 
71  const QString & typeName() const;
72 
73 
78  int length() const;
79 
80 
85  int precision() const;
86 
90  const QString & comment() const;
91 
96  void setName( const QString & nam );
97 
101  void setType( QVariant::Type type );
102 
107  void setTypeName( const QString & typ );
108 
113  void setLength( int len );
114 
119  void setPrecision( int prec );
120 
121 
125  void setComment( const QString & comment );
126 
128  QString displayString( const QVariant& v ) const;
129 
130  private:
131 
133  QString mName;
134 
136  QVariant::Type mType;
137 
139  QString mTypeName;
140 
142  int mLength;
143 
146 
148  QString mComment;
149 
150 }; // class QgsField
151 
152 
161 class CORE_EXPORT QgsFields
162 {
163  public:
164 
166  {
170  OriginEdit
171  };
172 
173  typedef struct Field
174  {
175  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
176  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
177 
181  } Field;
182 
184  void clear();
186  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
188  void remove( int fieldIdx );
190  void extend( const QgsFields& other );
191 
193  inline bool isEmpty() const { return mFields.isEmpty(); }
195  inline int count() const { return mFields.count(); }
197  inline int size() const { return mFields.count(); }
201  inline bool exists( int i ) const { return i >= 0 && i < mFields.count(); }
202 
204  inline const QgsField& operator[]( int i ) const { return mFields[i].field; }
206  inline QgsField& operator[]( int i ) { return mFields[i].field; }
208  const QgsField& at( int i ) const { return mFields[i].field; }
210  const QgsField& field( int fieldIdx ) const { return mFields[fieldIdx].field; }
212  const QgsField& field( const QString& name ) const { return mFields[ indexFromName( name )].field; }
213 
215  FieldOrigin fieldOrigin( int fieldIdx ) const { return mFields[fieldIdx].origin; }
217  int fieldOriginIndex( int fieldIdx ) const { return mFields[fieldIdx].originIndex; }
218 
220  int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }
221 
225  int fieldNameIndex( const QString& fieldName ) const;
226 
229  QgsAttributeList allAttributesList() const;
230 
232  QList<QgsField> toList() const;
233 
234  protected:
236  QVector<Field> mFields;
237 
239  QHash<QString, int> mNameToIndex;
240 };
241 
242 
243 
244 
245 #endif
int mLength
Length.
Definition: qgsfield.h:142
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:169
QString mTypeName
Type name from provider.
Definition: qgsfield.h:139
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:210
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsField field
field
Definition: qgsfield.h:178
int mPrecision
Precision.
Definition: qgsfield.h:145
Container of fields for a vector layer.
Definition: qgsfield.h:161
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:208
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:168
it has not been specified where the field comes from
Definition: qgsfield.h:167
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.h:201
QString mName
Name.
Definition: qgsfield.h:133
Field(const QgsField &f, FieldOrigin o, int oi)
Definition: qgsfield.h:176
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:217
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:239
QVariant::Type mType
Variant type.
Definition: qgsfield.h:136
int count() const
Return number of items.
Definition: qgsfield.h:195
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:31
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
Definition: qgsfield.h:220
QString mComment
Comment.
Definition: qgsfield.h:148
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:204
const QgsField & field(const QString &name) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:212
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:215
int size() const
Return number of items.
Definition: qgsfield.h:197
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:236
QgsField & operator[](int i)
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:206
QList< int > QgsAttributeList
Definition: qgsfield.h:23
int originIndex
index specific to the origin
Definition: qgsfield.h:180
FieldOrigin origin
origin of the field
Definition: qgsfield.h:179
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfield.h:193