QGIS API Documentation  2.7.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 ( !v.convert( mType ) )
141  {
142  return false;
143  }
144 
145  if ( mType == QVariant::Double && mPrecision > 0 )
146  {
147  v = qRound64( v.toDouble() * qPow( 10, mPrecision ) ) / qPow( 10, mPrecision );
148  return true;
149  }
150 
151  return true;
152 }
153 
155 
157 {
158  mFields.clear();
159  mNameToIndex.clear();
160 }
161 
162 bool QgsFields::append( const QgsField& field, FieldOrigin origin, int originIndex )
163 {
164  if ( mNameToIndex.contains( field.name() ) )
165  return false;
166 
167  if ( originIndex == -1 && origin == OriginProvider )
168  originIndex = mFields.count();
169  mFields.append( Field( field, origin, originIndex ) );
170 
171  mNameToIndex.insert( field.name(), mFields.count() - 1 );
172  return true;
173 }
174 
175 bool QgsFields::appendExpressionField( const QgsField& field, int originIndex )
176 {
177  if ( mNameToIndex.contains( field.name() ) )
178  return false;
179 
180  mFields.append( Field( field, OriginExpression, originIndex ) );
181 
182  mNameToIndex.insert( field.name(), mFields.count() - 1 );
183  return true;
184 }
185 
186 void QgsFields::remove( int fieldIdx )
187 {
188  mNameToIndex.remove( mFields[fieldIdx].field.name() );
189  mFields.remove( fieldIdx );
190 }
191 
192 void QgsFields::extend( const QgsFields& other )
193 {
194  for ( int i = 0; i < other.count(); ++i )
195  {
196  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
197  }
198 }
199 
200 QList<QgsField> QgsFields::toList() const
201 {
202  QList<QgsField> lst;
203  for ( int i = 0; i < mFields.count(); ++i )
204  lst.append( mFields[i].field );
205  return lst;
206 }
207 
208 int QgsFields::fieldNameIndex( const QString& fieldName ) const
209 {
210  for ( int idx = 0; idx < count(); ++idx )
211  {
212  if ( QString::compare( mFields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
213  {
214  return idx;
215  }
216  }
217  return -1;
218 }
219 
221 {
222  QgsAttributeList lst;
223  for ( int i = 0; i < mFields.count(); ++i )
224  lst.append( i );
225  return lst;
226 }
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Definition: qgsfield.cpp:200
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:208
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:175
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
Definition: qgsfield.cpp:192
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
void clear()
Remove all fields.
Definition: qgsfield.cpp:156
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:265
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:162
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:186
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
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:234
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:69
int length() const
Gets the length of the field.
Definition: qgsfield.cpp:74
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:84
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:262
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfield.cpp:220
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