QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsfield.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfield.cpp - 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  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsfield.h"
18 
19 #include <QSettings>
20 #include <QtCore/qmath.h>
21 
22 #if 0
23 QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
24  QString comment )
25  : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
26  , mComment( comment )
27 {
28  // This function used to lower case the field name since some stores
29  // use upper case (eg. shapefiles), but that caused problems with
30  // attribute actions getting confused between uppercase and
31  // lowercase versions of the attribute names, so just leave the
32  // names how they are now.
33 }
34 #endif
35 
36 QgsField::QgsField( QString name, QVariant::Type type, QString typeName, int len, int prec, QString comment )
37  : mName( name ), mType( type ), mTypeName( typeName )
38  , mLength( len ), mPrecision( prec ), mComment( comment )
39 {
40 }
41 
42 
44 {
45 }
46 
47 bool QgsField::operator==( const QgsField& other ) const
48 {
49  return (( mName == other.mName ) && ( mType == other.mType )
50  && ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
51 }
52 
53 bool QgsField::operator!=( const QgsField& other ) const
54 {
55  return !( *this == other );
56 }
57 
58 
59 const QString & QgsField::name() const
60 {
61  return mName;
62 }
63 
64 QVariant::Type QgsField::type() const
65 {
66  return mType;
67 }
68 
69 const QString & QgsField::typeName() const
70 {
71  return mTypeName;
72 }
73 
74 int QgsField::length() const
75 {
76  return mLength;
77 }
78 
80 {
81  return mPrecision;
82 }
83 
84 const QString & QgsField::comment() const
85 {
86  return mComment;
87 }
88 
89 void QgsField::setName( const QString & nam )
90 {
91  mName = nam;
92 }
93 
94 void QgsField::setType( QVariant::Type type )
95 {
96  mType = type;
97 }
98 
99 void QgsField::setTypeName( const QString & typeName )
100 {
101  mTypeName = typeName;
102 }
103 
104 void QgsField::setLength( int len )
105 {
106  mLength = len;
107 }
108 void QgsField::setPrecision( int prec )
109 {
110  mPrecision = prec;
111 }
112 
113 void QgsField::setComment( const QString & comment )
114 {
115  mComment = comment;
116 }
117 
118 QString QgsField::displayString( const QVariant& v ) const
119 {
120  if ( v.isNull() )
121  {
122  QSettings settings;
123  return settings.value( "qgis/nullValue", "NULL" ).toString();
124  }
125 
126  if ( mType == QVariant::Double && mPrecision > 0 )
127  return QString::number( v.toDouble(), 'f', mPrecision );
128 
129  return v.toString();
130 }
131 
132 bool QgsField::convertCompatible( QVariant& v ) const
133 {
134  if ( v.isNull() )
135  {
136  v.convert( mType );
137  return true;
138  }
139 
140  if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
141  {
142  v = QVariant( mType );
143  return false;
144  }
145 
146  if ( !v.convert( mType ) )
147  {
148  v = QVariant( mType );
149  return false;
150  }
151 
152  if ( mType == QVariant::Double && mPrecision > 0 )
153  {
154  double s = qPow( 10, mPrecision );
155  double d = v.toDouble() * s;
156  v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
157  return true;
158  }
159 
160  if ( mType == QVariant::String && mLength > 0 && v.toString().length() > mLength )
161  {
162  v = v.toString().left( mLength );
163  return false;
164  }
165 
166  return true;
167 }
168 
170 
172 {
173  mFields.clear();
174  mNameToIndex.clear();
175 }
176 
177 bool QgsFields::append( const QgsField& field, FieldOrigin origin, int originIndex )
178 {
179  if ( mNameToIndex.contains( field.name() ) )
180  return false;
181 
182  if ( originIndex == -1 && origin == OriginProvider )
183  originIndex = mFields.count();
184  mFields.append( Field( field, origin, originIndex ) );
185 
186  mNameToIndex.insert( field.name(), mFields.count() - 1 );
187  return true;
188 }
189 
190 bool QgsFields::appendExpressionField( const QgsField& field, int originIndex )
191 {
192  if ( mNameToIndex.contains( field.name() ) )
193  return false;
194 
195  mFields.append( Field( field, OriginExpression, originIndex ) );
196 
197  mNameToIndex.insert( field.name(), mFields.count() - 1 );
198  return true;
199 }
200 
201 void QgsFields::remove( int fieldIdx )
202 {
203  if ( !exists( fieldIdx ) )
204  return;
205 
206  mNameToIndex.remove( mFields[fieldIdx].field.name() );
207  mFields.remove( fieldIdx );
208 }
209 
210 void QgsFields::extend( const QgsFields& other )
211 {
212  for ( int i = 0; i < other.count(); ++i )
213  {
214  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
215  }
216 }
217 
219 {
220  if ( !exists( fieldIdx ) )
221  return OriginUnknown;
222 
223  return mFields[fieldIdx].origin;
224 }
225 
226 QList<QgsField> QgsFields::toList() const
227 {
228  QList<QgsField> lst;
229  for ( int i = 0; i < mFields.count(); ++i )
230  lst.append( mFields[i].field );
231  return lst;
232 }
233 
234 int QgsFields::fieldNameIndex( const QString& fieldName ) const
235 {
236  for ( int idx = 0; idx < count(); ++idx )
237  {
238  if ( QString::compare( mFields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
239  {
240  return idx;
241  }
242  }
243  return -1;
244 }
245 
247 {
248  QgsAttributeList lst;
249  for ( int i = 0; i < mFields.count(); ++i )
250  lst.append( i );
251  return lst;
252 }
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Definition: qgsfield.cpp:226
void setPrecision(int prec)
Set the field precision.
Definition: qgsfield.cpp:108
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:59
bool operator==(const QgsField &other) const
Definition: qgsfield.cpp:47
struct QgsFields::Field Field
QgsField(QString name=QString(), QVariant::Type type=QVariant::Invalid, QString typeName=QString(), int len=0, int prec=0, QString comment=QString())
Constructor.
Definition: qgsfield.cpp:36
~QgsField()
Destructor.
Definition: qgsfield.cpp:43
void setTypeName(const QString &typ)
Set the field type.
Definition: qgsfield.cpp:99
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:118
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:229
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name - case insensitive TODO: sort out case sensitive (indexFromName()) vs...
Definition: qgsfield.cpp:234
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:79
Container of fields for a vector layer.
Definition: qgsfield.h:172
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
Definition: qgsfield.cpp:190
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
Definition: qgsfield.cpp:210
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:227
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:179
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:104
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.h:220
void clear()
Remove all fields.
Definition: qgsfield.cpp:171
bool operator!=(const QgsField &other) const
Definition: qgsfield.cpp:53
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:236
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:263
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfield.cpp:177
int count() const
Return number of items.
Definition: qgsfield.h:214
field is calculated from an expression
Definition: qgsfield.h:182
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:33
void remove(int fieldIdx)
Remove a field with the given index.
Definition: qgsfield.cpp:201
void setName(const QString &nam)
Set the field name.
Definition: qgsfield.cpp:89
void setType(QVariant::Type type)
Set variant type.
Definition: qgsfield.cpp:94
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:132
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:69
it has not been specified where the field comes from
Definition: qgsfield.h:178
int length() const
Gets the length of the field.
Definition: qgsfield.cpp:74
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.cpp:218
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:84
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:260
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfield.cpp:246
void setComment(const QString &comment)
Set the field comment.
Definition: qgsfield.cpp:113
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:64