QGIS API Documentation  2.0.1-Dufour
 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 
29 class CORE_EXPORT QgsField
30 {
31  public:
44  QgsField( QString name = QString(),
45  QVariant::Type type = QVariant::Invalid,
46  QString typeName = QString(),
47  int len = 0,
48  int prec = 0,
49  QString comment = QString() );
50 
52  ~QgsField();
53 
54  bool operator==( const QgsField& other ) const;
55  bool operator!=( const QgsField& other ) const;
56 
58  const QString & name() const;
59 
61  QVariant::Type type() const;
62 
69  const QString & typeName() const;
70 
71 
76  int length() const;
77 
78 
83  int precision() const;
84 
88  const QString & comment() const;
89 
94  void setName( const QString & nam );
95 
99  void setType( QVariant::Type type );
100 
105  void setTypeName( const QString & typ );
106 
111  void setLength( int len );
112 
117  void setPrecision( int prec );
118 
119 
123  void setComment( const QString & comment );
124 
126  QString displayString( const QVariant& v ) const;
127 
128  private:
129 
131  QString mName;
132 
134  QVariant::Type mType;
135 
137  QString mTypeName;
138 
140  int mLength;
141 
144 
146  QString mComment;
147 
148 }; // class QgsField
149 
150 // key = field index, value=field data
151 typedef QMap<int, QgsField> QgsFieldMap;
152 
153 
162 class CORE_EXPORT QgsFields
163 {
164  public:
165 
167  {
171  OriginEdit
172  };
173 
174  typedef struct Field
175  {
176  Field(): origin( OriginUnknown ), originIndex( -1 ) {}
177  Field( const QgsField& f, FieldOrigin o, int oi ): field( f ), origin( o ), originIndex( oi ) {}
178 
182  } Field;
183 
185  void clear();
187  bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
189  void remove( int fieldIdx );
191  void extend( const QgsFields& other );
192 
194  inline bool isEmpty() const { return mFields.isEmpty(); }
196  inline int count() const { return mFields.count(); }
198  inline int size() const { return mFields.count(); }
199 
201  inline const QgsField& operator[]( int i ) const { return mFields[i].field; }
203  inline QgsField& operator[]( int i ) { return mFields[i].field; }
205  const QgsField& at( int i ) const { return mFields[i].field; }
207  const QgsField& field( int fieldIdx ) const { return mFields[fieldIdx].field; }
209  const QgsField& field( const QString& name ) const { return mFields[ indexFromName( name )].field; }
210 
212  FieldOrigin fieldOrigin( int fieldIdx ) const { return mFields[fieldIdx].origin; }
214  int fieldOriginIndex( int fieldIdx ) const { return mFields[fieldIdx].originIndex; }
215 
217  int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }
218 
220  QList<QgsField> toList() const;
221 
222  protected:
224  QVector<Field> mFields;
225 
227  QHash<QString, int> mNameToIndex;
228 };
229 
230 
231 
232 
233 #endif