QGIS API Documentation  2.6.0-Brighton
 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 
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