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