QGIS API Documentation  2.8.2-Wien
 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 #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 }