QGIS API Documentation  2.4.0-Chugiak
 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 void QgsFields::remove( int fieldIdx )
154 {
155  mNameToIndex.remove( mFields[fieldIdx].field.name() );
156  mFields.remove( fieldIdx );
157 }
158 
159 void QgsFields::extend( const QgsFields& other )
160 {
161  for ( int i = 0; i < other.count(); ++i )
162  {
163  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
164  }
165 }
166 
167 QList<QgsField> QgsFields::toList() const
168 {
169  QList<QgsField> lst;
170  for ( int i = 0; i < mFields.count(); ++i )
171  lst.append( mFields[i].field );
172  return lst;
173 }
174 
175 int QgsFields::fieldNameIndex( const QString& fieldName ) const
176 {
177  for ( int idx = 0; idx < count(); ++idx )
178  {
179  if ( QString::compare( mFields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
180  {
181  return idx;
182  }
183  }
184  return -1;
185 }
186 
188 {
189  QgsAttributeList lst;
190  for ( int i = 0; i < mFields.count(); ++i )
191  lst.append( i );
192  return lst;
193 }
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Definition: qgsfield.cpp:167
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:142
bool operator==(const QgsField &other) const
Definition: qgsfield.cpp:46
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: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:210
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:175
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:78
int mPrecision
Precision.
Definition: qgsfield.h:145
Container of fields for a vector layer.
Definition: qgsfield.h:161
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
Definition: qgsfield.cpp:159
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:208
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:168
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:103
QString mName
Name.
Definition: qgsfield.h:133
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:217
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
Definition: qgsfield.h:239
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:136
int count() const
Return number of items.
Definition: qgsfield.h:195
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:153
QString mComment
Comment.
Definition: qgsfield.h:148
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:215
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:236
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfield.cpp:187
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