23 #include <QDataStream> 24 #include <QtCore/qmath.h> 36 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
47 const QString& typeName,
int len,
int prec,
const QString& comment )
49 d =
new QgsFieldPrivate( name, type, typeName, len, prec, comment );
76 return *( other.d ) == *d;
81 return !( *
this == other );
91 if ( !d->alias.isEmpty() )
124 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
164 return d->defaultValueExpression;
169 d->defaultValueExpression = expression;
193 return settings.
value(
"qgis/nullValue",
"NULL" ).
toString();
196 if ( d->type == QVariant::Double && d->precision > 0 )
224 if ( d->type == QVariant::Int && v.
canConvert( QVariant::Double ) )
236 if ( round > INT_MAX || round < -INT_MAX )
252 if ( d->type == QVariant::Double && d->precision > 0 )
254 double s = qPow( 10, d->precision );
256 v =
QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
260 if ( d->type == QVariant::String && d->length > 0 && v.
toString().
length() > d->length )
278 out << static_cast< quint32 >( field.
type() );
283 out << field.
alias();
294 field.
setType( static_cast< QVariant::Type >( type ) );
296 field.
setLength( static_cast< int >( length ) );
315 d =
new QgsFieldsPrivate();
337 d->nameToIndex.clear();
348 if ( d->nameToIndex.contains( field.
name() ) )
352 originIndex = d->fields.count();
353 d->fields.append(
Field( field, origin, originIndex ) );
355 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
361 if ( d->nameToIndex.contains( field.
name() ) )
366 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
372 if ( !
exists( fieldIdx ) )
375 d->fields.remove( fieldIdx );
376 d->nameToIndex.clear();
377 for (
int idx = 0; idx <
count(); ++idx )
379 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
385 for (
int i = 0; i < other.
count(); ++i )
399 return d->fields.isEmpty();
404 return d->fields.count();
409 return d->fields.count();
414 return i >= 0 && i < d->fields.count();
419 return d->fields[i].field;
424 return d->fields[i].field;
429 return d->fields[fieldIdx].field;
445 return d->fields[i].field;
450 if ( !
exists( fieldIdx ) )
453 return d->fields[fieldIdx].origin;
458 return d->fields[fieldIdx].originIndex;
463 return d->nameToIndex.value( name, -1 );
469 for (
int i = 0; i < d->fields.count(); ++i )
470 lst.
append( d->fields[i].field );
476 return d->fields == other.d->fields;
481 if ( d->fields.isEmpty() )
482 return const_iterator();
484 return const_iterator( &d->fields.first() );
489 if ( d->fields.isEmpty() )
490 return const_iterator();
492 return const_iterator( &d->fields.last() + 1 );
497 if ( d->fields.isEmpty() )
498 return const_iterator();
500 return const_iterator( &d->fields.first() );
505 if ( d->fields.isEmpty() )
506 return const_iterator();
508 return const_iterator( &d->fields.last() + 1 );
513 if ( d->fields.isEmpty() )
517 return iterator( &d->fields.first() );
522 if ( d->fields.isEmpty() )
526 return iterator( &d->fields.last() + 1 );
531 switch ( d->fields.at( fieldIdx ).field.type() )
535 case QVariant::LongLong:
536 case QVariant::ULongLong:
540 case QVariant::Double:
544 case QVariant::String:
552 case QVariant::DateTime:
573 for (
int idx = 0; idx <
count(); ++idx )
575 if ( d->fields[idx].field.name() == fieldName )
579 for (
int idx = 0; idx <
count(); ++idx )
581 if (
QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
591 for (
int i = 0; i < d->fields.count(); ++i )
604 out << static_cast< quint32 >( fields.
size() );
605 for (
int i = 0; i < fields.
size(); i++ )
607 out << fields.
field( i );
617 for ( quint32 i = 0; i <
size; i++ )
bool canConvert(Type t) const
qlonglong toLongLong(bool *ok) const
bool isNumeric() const
Returns if this field is numeric.
QgsField & operator=(const QgsField &other)
Assignment operator.
QString comment() const
Returns the field comment.
int size() const
Return number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
struct QgsFields::Field Field
QString alias() const
Returns the alias for the field (the friendly displayed name of the field ), or an empty string if th...
QgsFields()
Constructor for an empty field container.
virtual ~QgsField()
Destructor.
QgsFields & operator=(const QgsFields &other)
Assignment operator.
const_iterator constEnd() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
QIcon iconForField(int fieldIdx) const
Returns an icon corresponding to a field index, based on the field's type and source.
void setPrecision(int precision)
Set the field precision.
bool exists(int i) const
Return if a field index is valid.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
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.
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
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.
int precision() const
Gets the precision of the field.
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
bool operator==(const QgsFields &other) const
const_iterator end() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
int count() const
Return number of items.
QString name() const
Returns the name of the field.
it has not been specified where the field comes from
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
void setLength(int len)
Set the field length.
const char * name() const
double qgsRound(double x)
A round function which returns a double to guard against overflows.
QString number(int n, int base)
void append(const T &value)
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
int toInt(bool *ok) const
QString typeName() const
Gets the field type.
QString defaultValueExpression
void clear()
Remove all fields.
QString displayName() const
Returns the name to use when displaying this field.
void setTypeName(const QString &typeName)
Set the field type.
bool operator!=(const QgsField &other) const
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) ...
QString displayString(const QVariant &v) const
Formats string for display.
Encapsulate a field in an attribute table or data source.
void remove(int fieldIdx)
Remove a field with the given index.
void setDefaultValueExpression(const QString &expression)
Sets an expression to use when calculating the default value for the field.
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
const_iterator begin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
QVariant value(const QString &key, const QVariant &defaultValue) const
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
void setType(QVariant::Type type)
Set variant type.
const_iterator constBegin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
bool operator==(const QgsField &other) const
QString defaultValueExpression() const
Returns the expression used when calculating the default value for the field.
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
int length() const
Gets the length of the field.
bool isEmpty() const
Check whether the container is empty.
QString left(int n) const
double toDouble(bool *ok) const
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
int compare(const QString &other) const
field is calculated from an expression
void setComment(const QString &comment)
Set the field comment.
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.