23 #include <QDataStream> 35 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
46 const QString &typeName,
int len,
int prec,
const QString &comment,
47 QVariant::Type subType )
49 d =
new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
72 return *( other.d ) == *d;
77 return !( *
this == other );
87 if ( !d->alias.isEmpty() )
125 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
170 return d->defaultValueDefinition;
185 return d->constraints;
211 if ( d->type == QVariant::Double && d->precision > 0 )
212 return QString::number( v.toDouble(),
'f', d->precision );
227 v.convert( d->type );
231 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
233 v = QVariant( d->type );
239 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
242 double dbl = v.toDouble( &ok );
246 v = QVariant( d->type );
250 double round = std::round( dbl );
251 if ( round > INT_MAX || round < -INT_MAX )
254 v = QVariant( d->type );
257 v = QVariant( static_cast< int >( std::round( dbl ) ) );
261 if ( !v.convert( d->type ) )
263 v = QVariant( d->type );
267 if ( d->type == QVariant::Double && d->precision > 0 )
269 double s = std::pow( 10, d->precision );
270 double d = v.toDouble() * s;
271 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
275 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
277 v = v.toString().left( d->length );
286 d->editorWidgetSetup = v;
291 return d->editorWidgetSetup;
303 out << static_cast< quint32 >( field.
type() );
308 out << field.
alias();
320 out << static_cast< quint32 >( field.
subType() );
331 quint32 originNotNull;
332 quint32 originUnique;
333 quint32 originExpression;
334 quint32 strengthNotNull;
335 quint32 strengthUnique;
336 quint32 strengthExpression;
344 QString defaultValueExpression;
345 QString constraintExpression;
346 QString constraintDescription;
348 in >> name >> type >> typeName >> length >> precision >> comment >> alias
349 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
350 constraintExpression >> constraintDescription >>
subType;
352 field.
setType( static_cast< QVariant::Type >( type ) );
354 field.
setLength( static_cast< int >( length ) );
362 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
363 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
366 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
369 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
370 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
376 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
377 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
380 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
383 field.
setSubType( static_cast< QVariant::Type >( subType ) );
bool isNumeric() const
Returns if this field is numeric.
QgsField & operator=(const QgsField &other)
Assignment operator.
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QVariant::Type subType=QVariant::Invalid)
Constructor.
QString comment() const
Returns the field comment.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
QString alias() const
Returns the alias for the field (the friendly displayed name of the field ), or an empty string if th...
The QgsDefaultValue class provides a container for managing client side default values for fields...
void setPrecision(int precision)
Set the field precision.
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
void setName(const QString &name)
Set the field name.
QgsEditorWidgetSetup editorWidgetSetup() const
Get the editor widget setup for the field.
int precision() const
Gets the precision of the field.
Stores information about constraints which may be present on a field.
QString name() const
Returns the name of the field.
Field has an expression constraint set. See constraintExpression().
void setLength(int len)
Set the field length.
QString typeName() const
Gets the field type.
QString displayName() const
Returns the name to use when displaying this field.
void setTypeName(const QString &typeName)
Set the field type.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
bool operator!=(const QgsField &other) const
QString displayString(const QVariant &v) const
Formats string for display.
Encapsulate a field in an attribute table or data source.
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
void setSubType(QVariant::Type subType)
If the field is a collection, set its element's type.
QgsFieldConstraints constraints
void setType(QVariant::Type type)
Set variant type.
bool operator==(const QgsField &other) const
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
int length() const
Gets the length of the field.
QVariant::Type subType() const
If the field is a collection, gets its element's type.
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
const QgsFieldConstraints & constraints() const
Returns constraints which are present for the field.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
void setComment(const QString &comment)
Set the field comment.
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsDefaultValue defaultValueDefinition
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
QgsDefaultValue defaultValueDefinition() const
Returns the expression used when calculating the default value for the field.
Field must have a unique value.
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.