21 #include <QtCore/qmath.h>
26 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
39 d =
new QgsFieldPrivate( name, type, typeName, len, prec, comment );
61 return *( other.d ) == *d;
66 return !( *
this == other );
133 return settings.
value(
"qgis/nullValue",
"NULL" ).
toString();
136 if ( d->type == QVariant::Double && d->precision > 0 )
162 if ( d->type == QVariant::Double && d->precision > 0 )
164 double s = qPow( 10, d->precision );
166 v =
QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
170 if ( d->type == QVariant::String && d->length > 0 && v.
toString().
length() > d->length )
182 out << ( quint32 )field.
type();
192 quint32 type, length, precision;
193 QString name, typeName, comment;
194 in >> name >> type >> typeName >> length >> precision >> comment;
196 field.
setType(( QVariant::Type )type );
208 d =
new QgsFieldsPrivate( );
230 d->nameToIndex.clear();
235 if ( d->nameToIndex.contains( field.
name() ) )
239 originIndex = d->fields.count();
240 d->fields.append(
Field( field, origin, originIndex ) );
242 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
248 if ( d->nameToIndex.contains( field.
name() ) )
253 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
259 if ( !
exists( fieldIdx ) )
262 d->fields.remove( fieldIdx );
263 d->nameToIndex.clear();
264 for (
int idx = 0; idx <
count(); ++idx )
266 d->nameToIndex.insert( d->fields[idx].field.name(), idx );
272 for (
int i = 0; i < other.
count(); ++i )
280 return d->fields.isEmpty();
285 return d->fields.count();
290 return d->fields.count();
295 return i >= 0 && i < d->fields.count();
300 return d->fields[i].field;
305 return d->fields[i].field;
310 return d->fields[fieldIdx].field;
320 return d->fields[i].field;
325 if ( !
exists( fieldIdx ) )
328 return d->fields[fieldIdx].origin;
333 return d->fields[fieldIdx].originIndex;
338 return d->nameToIndex.value( name, -1 );
344 for (
int i = 0; i < d->fields.count(); ++i )
345 lst.
append( d->fields[i].field );
351 return d->fields == other.d->fields;
356 for (
int idx = 0; idx <
count(); ++idx )
358 if (
QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
369 for (
int i = 0; i < d->fields.count(); ++i )
376 out << ( quint32 )fields.
size();
377 for (
int i = 0; i < fields.
size(); i++ )
379 out << fields.
field( i );
389 for ( quint32 i = 0; i < size; i++ )
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
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
QgsField(QString name=QString(), QVariant::Type type=QVariant::Invalid, QString typeName=QString(), int len=0, int prec=0, QString comment=QString())
Constructor.
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 - case insensitive TODO: sort out case sensitive (indexFromName()) vs...
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.
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.