22 #include <QDataStream>
23 #include <QtCore/qmath.h>
29 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
40 const QString& typeName,
int len,
int prec,
const QString& comment )
42 d =
new QgsFieldPrivate( name, type, typeName, len, prec, comment );
64 return *( other.d ) == *d;
69 return !( *
this == other );
136 return settings.
value(
"qgis/nullValue",
"NULL" ).
toString();
139 if ( d->type == QVariant::Double && d->precision > 0 )
161 if ( d->type == QVariant::Int && v.
canConvert( QVariant::Double ) )
173 if ( round > INT_MAX || round < -INT_MAX )
189 if ( d->type == QVariant::Double && d->precision > 0 )
191 double s = qPow( 10, d->precision );
193 v =
QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
197 if ( d->type == QVariant::String && d->length > 0 && v.
toString().
length() > d->length )
210 out << ( quint32 )field.
type();
220 quint32 type, length, precision;
221 QString name, typeName, comment;
222 in >> name >> type >> typeName >> length >> precision >> comment;
224 field.
setType(( QVariant::Type )type );
236 d =
new QgsFieldsPrivate( );
258 d->nameToIndex.clear();
263 if ( d->nameToIndex.contains( field.
name() ) )
267 originIndex = d->fields.count();
268 d->fields.append(
Field( field, origin, originIndex ) );
270 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
276 if ( d->nameToIndex.contains( field.
name() ) )
281 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
287 if ( !
exists( fieldIdx ) )
290 d->fields.remove( fieldIdx );
291 d->nameToIndex.clear();
292 for (
int idx = 0; idx <
count(); ++idx )
294 d->nameToIndex.insert( d->fields[idx].field.name(), idx );
300 for (
int i = 0; i < other.
count(); ++i )
308 return d->fields.isEmpty();
313 return d->fields.count();
318 return d->fields.count();
323 return i >= 0 && i < d->fields.count();
328 return d->fields[i].field;
333 return d->fields[i].field;
338 return d->fields[fieldIdx].field;
348 return d->fields[i].field;
353 if ( !
exists( fieldIdx ) )
356 return d->fields[fieldIdx].origin;
361 return d->fields[fieldIdx].originIndex;
366 return d->nameToIndex.value( name, -1 );
372 for (
int i = 0; i < d->fields.count(); ++i )
373 lst.
append( d->fields[i].field );
379 return d->fields == other.d->fields;
384 for (
int idx = 0; idx <
count(); ++idx )
386 if ( d->fields[idx].field.name() == fieldName )
390 for (
int idx = 0; idx <
count(); ++idx )
392 if (
QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
402 for (
int i = 0; i < d->fields.count(); ++i )
409 out << ( quint32 )fields.
size();
410 for (
int i = 0; i < fields.
size(); i++ )
412 out << fields.
field( i );
422 for ( quint32 i = 0; i < size; i++ )
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
bool canConvert(Type t) const
qlonglong toLongLong(bool *ok) const
const QString & name() const
Gets the name of the field.
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
QgsField & operator=(const QgsField &other)
Assignment operator.
bool operator==(const QgsField &other) const
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
struct QgsFields::Field Field
QgsFields()
Constructor for an empty field container.
virtual ~QgsField()
Destructor.
QgsFields & operator=(const QgsFields &other)
Assignment operator.
QString displayString(const QVariant &v) const
Formats string for display.
void setPrecision(int precision)
Set the field precision.
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString())
Constructor.
int precision() const
Gets the precision of the field.
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Container of fields for a vector layer.
void setName(const QString &name)
Set the field name.
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
it has not been specified where the field comes from
void setLength(int len)
Set the field length.
double qgsRound(double x)
bool exists(int i) const
Return if a field index is valid.
QString number(int n, int base)
void append(const T &value)
int toInt(bool *ok) const
void clear()
Remove all fields.
void setTypeName(const QString &typeName)
Set the field type.
bool operator!=(const QgsField &other) const
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
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) ...
int count() const
Return number of items.
Encapsulate a field in an attribute table or data source.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
void remove(int fieldIdx)
Remove a field with the given index.
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setType(QVariant::Type type)
Set variant type.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
const QString & typeName() const
Gets the field type.
int length() const
Gets the length of the field.
int size() const
Return number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
const QString & comment() const
Returns the field comment.
QString left(int n) const
double toDouble(bool *ok) const
bool isEmpty() const
Check whether the container is empty.
int compare(const QString &other) const
field is calculated from an expression
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
void setComment(const QString &comment)
Set the field comment.
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
bool operator==(const QgsFields &other) const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.