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 
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