QGIS API Documentation  2.11.0-Master
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 #include "qgsfield_p.h"
19 
20 #include <QSettings>
21 #include <QtCore/qmath.h>
22 
23 
24 #if 0
25 QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
26  QString comment )
27  : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
28  , mComment( comment )
29 {
30  // This function used to lower case the field name since some stores
31  // use upper case (eg. shapefiles), but that caused problems with
32  // attribute actions getting confused between uppercase and
33  // lowercase versions of the attribute names, so just leave the
34  // names how they are now.
35 }
36 #endif
37 QgsField::QgsField( QString name, QVariant::Type type,
38  QString typeName, int len, int prec, QString comment )
39 {
40  d = new QgsFieldPrivate( name, type, typeName, len, prec, comment );
41 }
42 
44  : d( other.d )
45 {
46 
47 }
48 
50 {
51  d = other.d;
52  return *this;
53 }
54 
55 
57 {
58 }
59 
60 bool QgsField::operator==( const QgsField& other ) const
61 {
62  return *( other.d ) == *d;
63 }
64 
65 bool QgsField::operator!=( const QgsField& other ) const
66 {
67  return !( *this == other );
68 }
69 
70 const QString & QgsField::name() const
71 {
72  return d->name;
73 }
74 
75 QVariant::Type QgsField::type() const
76 {
77  return d->type;
78 }
79 
80 const QString & QgsField::typeName() const
81 {
82  return d->typeName;
83 }
84 
85 int QgsField::length() const
86 {
87  return d->length;
88 }
89 
91 {
92  return d->precision;
93 }
94 
95 const QString & QgsField::comment() const
96 {
97  return d->comment;
98 }
99 
100 void QgsField::setName( const QString& name )
101 {
102  d->name = name;
103 }
104 
105 void QgsField::setType( QVariant::Type type )
106 {
107  d->type = type;
108 }
109 
110 void QgsField::setTypeName( const QString& typeName )
111 {
112  d->typeName = typeName;
113 }
114 
115 void QgsField::setLength( int len )
116 {
117  d->length = len;
118 }
119 void QgsField::setPrecision( int precision )
120 {
121  d->precision = precision;
122 }
123 
124 void QgsField::setComment( const QString& comment )
125 {
126  d->comment = comment;
127 }
128 
130 {
131  if ( v.isNull() )
132  {
133  QSettings settings;
134  return settings.value( "qgis/nullValue", "NULL" ).toString();
135  }
136 
137  if ( d->type == QVariant::Double && d->precision > 0 )
138  return QString::number( v.toDouble(), 'f', d->precision );
139 
140  return v.toString();
141 }
142 
144 {
145  if ( v.isNull() )
146  {
147  v.convert( d->type );
148  return true;
149  }
150 
151  if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
152  {
153  v = QVariant( d->type );
154  return false;
155  }
156 
157  if ( !v.convert( d->type ) )
158  {
159  v = QVariant( d->type );
160  return false;
161  }
162 
163  if ( d->type == QVariant::Double && d->precision > 0 )
164  {
165  double s = qPow( 10, d->precision );
166  double d = v.toDouble() * s;
167  v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
168  return true;
169  }
170 
171  if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
172  {
173  v = v.toString().left( d->length );
174  return false;
175  }
176 
177  return true;
178 }
179 
180 
182 {
183  out << field.name();
184  out << ( quint32 )field.type();
185  out << field.typeName();
186  out << field.length();
187  out << field.precision();
188  out << field.comment();
189  return out;
190 }
191 
193 {
194  quint32 type, length, precision;
195  QString name, typeName, comment;
196  in >> name >> type >> typeName >> length >> precision >> comment;
197  field.setName( name );
198  field.setType(( QVariant::Type )type );
199  field.setTypeName( typeName );
200  field.setLength(( int )length );
201  field.setPrecision(( int )precision );
202  field.setComment( comment );
203  return in;
204 }
205 
207 
209 {
210  d = new QgsFieldsPrivate( );
211 }
212 
214  : d( other.d )
215 {
216 }
217 
219 {
220  d = other.d;
221  return *this;
222 }
223 
225 {
226 
227 }
228 
230 {
231  d->fields.clear();
232  d->nameToIndex.clear();
233 }
234 
235 bool QgsFields::append( const QgsField& field, FieldOrigin origin, int originIndex )
236 {
237  if ( d->nameToIndex.contains( field.name() ) )
238  return false;
239 
240  if ( originIndex == -1 && origin == OriginProvider )
241  originIndex = d->fields.count();
242  d->fields.append( Field( field, origin, originIndex ) );
243 
244  d->nameToIndex.insert( field.name(), d->fields.count() - 1 );
245  return true;
246 }
247 
248 bool QgsFields::appendExpressionField( const QgsField& field, int originIndex )
249 {
250  if ( d->nameToIndex.contains( field.name() ) )
251  return false;
252 
253  d->fields.append( Field( field, OriginExpression, originIndex ) );
254 
255  d->nameToIndex.insert( field.name(), d->fields.count() - 1 );
256  return true;
257 }
258 
259 void QgsFields::remove( int fieldIdx )
260 {
261  if ( !exists( fieldIdx ) )
262  return;
263 
264  d->fields.remove( fieldIdx );
265  d->nameToIndex.clear();
266  for ( int idx = 0; idx < count(); ++idx )
267  {
268  d->nameToIndex.insert( d->fields[idx].field.name(), idx );
269  }
270 }
271 
272 void QgsFields::extend( const QgsFields& other )
273 {
274  for ( int i = 0; i < other.count(); ++i )
275  {
276  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
277  }
278 }
279 
280 bool QgsFields::isEmpty() const
281 {
282  return d->fields.isEmpty();
283 }
284 
285 int QgsFields::count() const
286 {
287  return d->fields.count();
288 }
289 
290 int QgsFields::size() const
291 {
292  return d->fields.count();
293 }
294 
295 bool QgsFields::exists( int i ) const
296 {
297  return i >= 0 && i < d->fields.count();
298 }
299 
301 {
302  return d->fields[i].field;
303 }
304 
305 const QgsField &QgsFields::at( int i ) const
306 {
307  return d->fields[i].field;
308 }
309 
310 const QgsField &QgsFields::field( int fieldIdx ) const
311 {
312  return d->fields[fieldIdx].field;
313 }
314 
315 const QgsField &QgsFields::field( const QString &name ) const
316 {
317  return d->fields[ indexFromName( name )].field;
318 }
319 
320 const QgsField &QgsFields::operator[]( int i ) const
321 {
322  return d->fields[i].field;
323 }
324 
326 {
327  if ( !exists( fieldIdx ) )
328  return OriginUnknown;
329 
330  return d->fields[fieldIdx].origin;
331 }
332 
333 int QgsFields::fieldOriginIndex( int fieldIdx ) const
334 {
335  return d->fields[fieldIdx].originIndex;
336 }
337 
338 int QgsFields::indexFromName( const QString &name ) const
339 {
340  return d->nameToIndex.value( name, -1 );
341 }
342 
344 {
345  QList<QgsField> lst;
346  for ( int i = 0; i < d->fields.count(); ++i )
347  lst.append( d->fields[i].field );
348  return lst;
349 }
350 
351 bool QgsFields::operator==( const QgsFields &other ) const
352 {
353  return d->fields == other.d->fields;
354 }
355 
356 int QgsFields::fieldNameIndex( const QString& fieldName ) const
357 {
358  for ( int idx = 0; idx < count(); ++idx )
359  {
360  if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
361  {
362  return idx;
363  }
364  }
365  return -1;
366 }
367 
369 {
370  QgsAttributeList lst;
371  for ( int i = 0; i < d->fields.count(); ++i )
372  lst.append( i );
373  return lst;
374 }
375 
377 {
378  out << ( quint32 )fields.size();
379  for ( int i = 0; i < fields.size(); i++ )
380  {
381  out << fields.field( i );
382  }
383  return out;
384 }
385 
387 {
388  fields.clear();
389  quint32 size;
390  in >> size;
391  for ( quint32 i = 0; i < size; i++ )
392  {
393  QgsField field;
394  in >> field;
395  fields.append( field );
396  }
397  return in;
398 }
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Definition: qgsfield.cpp:343
qlonglong toLongLong(bool *ok) const
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:70
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:320
QgsField & operator=(const QgsField &other)
Assignment operator.
Definition: qgsfield.cpp:49
bool operator==(const QgsField &other) const
Definition: qgsfield.cpp:60
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:310
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:37
QgsFields()
Constructor for an empty field container.
Definition: qgsfield.cpp:208
virtual ~QgsField()
Destructor.
Definition: qgsfield.cpp:56
QgsFields & operator=(const QgsFields &other)
Assignment operator.
Definition: qgsfield.cpp:218
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:129
void setPrecision(int precision)
Set the field precision.
Definition: qgsfield.cpp:119
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:356
int precision() const
Gets the precision of the field.
Definition: qgsfield.cpp:90
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Definition: qgsfield.cpp:192
Container of fields for a vector layer.
Definition: qgsfield.h:177
void setName(const QString &name)
Set the field name.
Definition: qgsfield.cpp:100
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:248
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
Definition: qgsfield.cpp:272
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfield.h:184
it has not been specified where the field comes from
Definition: qgsfield.h:183
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:115
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfield.cpp:295
QString number(int n, int base)
void append(const T &value)
int toInt(bool *ok) const
bool isNull() const
void clear()
Remove all fields.
Definition: qgsfield.cpp:229
void setTypeName(const QString &typeName)
Set the field type.
Definition: qgsfield.cpp:110
bool operator!=(const QgsField &other) const
Definition: qgsfield.cpp:65
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.cpp:333
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:235
int count() const
Return number of items.
Definition: qgsfield.cpp:285
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:40
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:305
void remove(int fieldIdx)
Remove a field with the given index.
Definition: qgsfield.cpp:259
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
Definition: qgsfield.cpp:338
virtual ~QgsFields()
Definition: qgsfield.cpp:224
QVariant value(const QString &key, const QVariant &defaultValue) const
void setType(QVariant::Type type)
Set variant type.
Definition: qgsfield.cpp:105
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:143
const QString & typeName() const
Gets the field type.
Definition: qgsfield.cpp:80
int length() const
Gets the length of the field.
Definition: qgsfield.cpp:85
int size() const
Return number of items.
Definition: qgsfield.cpp:290
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.cpp:325
int length() const
const QString & comment() const
Returns the field comment.
Definition: qgsfield.cpp:95
QString left(int n) const
double toDouble(bool *ok) const
bool isEmpty() const
Check whether the container is empty.
Definition: qgsfield.cpp:280
int compare(const QString &other) const
field is calculated from an expression
Definition: qgsfield.h:187
bool convert(Type t)
QString toString() const
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfield.cpp:368
void setComment(const QString &comment)
Set the field comment.
Definition: qgsfield.cpp:124
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
Definition: qgsfield.cpp:181
bool operator==(const QgsFields &other) const
Definition: qgsfield.cpp:351
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:75