QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
21 #if 0
22 QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
23  QString comment )
24  : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
25  , mComment( comment )
26 {
27  // This function used to lower case the field name since some stores
28  // use upper case (eg. shapefiles), but that caused problems with
29  // attribute actions getting confused between uppercase and
30  // lowercase versions of the attribute names, so just leave the
31  // names how they are now.
32 }
33 #endif
34 
35 QgsField::QgsField( QString name, QVariant::Type type, QString typeName, int len, int prec, QString comment )
36  : mName( name ), mType( type ), mTypeName( typeName )
37  , mLength( len ), mPrecision( prec ), mComment( comment )
38 {
39 }
40 
41 
43 {
44 }
45 
46 bool QgsField::operator==( const QgsField& other ) const
47 {
48  return (( mName == other.mName ) && ( mType == other.mType )
49  && ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
50 }
51 
52 bool QgsField::operator!=( const QgsField& other ) const
53 {
54  return !( *this == other );
55 }
56 
57 
58 const QString & QgsField::name() const
59 {
60  return mName;
61 }
62 
63 QVariant::Type QgsField::type() const
64 {
65  return mType;
66 }
67 
68 const QString & QgsField::typeName() const
69 {
70  return mTypeName;
71 }
72 
73 int QgsField::length() const
74 {
75  return mLength;
76 }
77 
79 {
80  return mPrecision;
81 }
82 
83 const QString & QgsField::comment() const
84 {
85  return mComment;
86 }
87 
88 void QgsField::setName( const QString & nam )
89 {
90  mName = nam;
91 }
92 
93 void QgsField::setType( QVariant::Type type )
94 {
95  mType = type;
96 }
97 
98 void QgsField::setTypeName( const QString & typeName )
99 {
101 }
102 
103 void QgsField::setLength( int len )
104 {
105  mLength = len;
106 }
107 void QgsField::setPrecision( int prec )
108 {
109  mPrecision = prec;
110 }
111 
112 void QgsField::setComment( const QString & comment )
113 {
114  mComment = comment;
115 }
116 
117 QString QgsField::displayString( const QVariant& v ) const
118 {
119  if ( v.isNull() )
120  {
121  QSettings settings;
122  return settings.value( "qgis/nullValue", "NULL" ).toString();
123  }
124 
125  if ( mType == QVariant::Double && mPrecision > 0 )
126  return QString::number( v.toDouble(), 'f', mPrecision );
127 
128  return v.toString();
129 }
130 
131 
133 
135 {
136  mFields.clear();
137  mNameToIndex.clear();
138 }
139 
140 bool QgsFields::append( const QgsField& field, FieldOrigin origin, int originIndex )
141 {
142  if ( mNameToIndex.contains( field.name() ) )
143  return false;
144 
145  if ( originIndex == -1 && origin == OriginProvider )
146  originIndex = mFields.count();
147  mFields.append( Field( field, origin, originIndex ) );
148 
149  mNameToIndex.insert( field.name(), mFields.count() - 1 );
150  return true;
151 }
152 
153 bool QgsFields::appendExpressionField(const QgsField& field, int originIndex)
154 {
155  if ( mNameToIndex.contains( field.name() ) )
156  return false;
157 
158  mFields.append( Field( field, OriginExpression, originIndex ) );
159 
160  mNameToIndex.insert( field.name(), mFields.count() - 1 );
161  return true;
162 }
163 
164 void QgsFields::remove( int fieldIdx )
165 {
166  mNameToIndex.remove( mFields[fieldIdx].field.name() );
167  mFields.remove( fieldIdx );
168 }
169 
170 void QgsFields::extend( const QgsFields& other )
171 {
172  for ( int i = 0; i < other.count(); ++i )
173  {
174  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
175  }
176 }
177 
178 QList<QgsField> QgsFields::toList() const
179 {
180  QList<QgsField> lst;
181  for ( int i = 0; i < mFields.count(); ++i )
182  lst.append( mFields[i].field );
183  return lst;
184 }
185 
186 int QgsFields::fieldNameIndex( const QString& fieldName ) const
187 {
188  for ( int idx = 0; idx < count(); ++idx )
189  {
190  if ( QString::compare( mFields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
191  {
192  return idx;
193  }
194  }
195  return -1;
196 }
197 
199 {
200  QgsAttributeList lst;
201  for ( int i = 0; i < mFields.count(); ++i )
202  lst.append( i );
203  return lst;
204 }
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Definition: qgsfield.cpp:178
void setPrecision(int prec)
Set the field precision.
Definition: qgsfield.cpp:107
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:58
int mLength
Length.
Definition: qgsfield.h:144
bool operator==(const QgsField &other) const
Definition: qgsfield.cpp:46
QString mTypeName
Type name from provider.
Definition: qgsfield.h:141
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:35
~QgsField()
Destructor.
Definition: qgsfield.cpp:42
void setTypeName(const QString &typ)
Set the field type.
Definition: qgsfield.cpp:98
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:117
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:215
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:186
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:78
int mPrecision
Precision.
Definition: qgsfield.h:147
Container of fields for a vector layer.
Definition: qgsfield.h:163
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:153
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
Definition: qgsfield.cpp:170
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:213
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:170
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:103
QString mName
Name.
Definition: qgsfield.h:135
void clear()
Remove all fields.
Definition: qgsfield.cpp:134
bool operator!=(const QgsField &other) const
Definition: qgsfield.cpp:52
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:222
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:246
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:140
QVariant::Type mType
Variant type.
Definition: qgsfield.h:138
int count() const
Return number of items.
Definition: qgsfield.h:200
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:164
QString mComment
Comment.
Definition: qgsfield.h:150
void setName(const QString &nam)
Set the field name.
Definition: qgsfield.cpp:88
void setType(QVariant::Type type)
Set variant type.
Definition: qgsfield.cpp:93
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:220
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:68
int length() const
Gets the length of the field.
Definition: qgsfield.cpp:73
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:83
QVector< Field > mFields
internal storage of the container
Definition: qgsfield.h:243
field is calculated from an expression
Definition: qgsfield.h:173
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfield.cpp:198
void setComment(const QString &comment)
Set the field comment.
Definition: qgsfield.cpp:112
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:63