23 #include <QDataStream> 36 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
47 const QString &typeName,
int len,
int prec,
const QString &comment,
48 QVariant::Type subType )
50 d =
new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
73 return *( other.d ) == *d;
78 return !( *
this == other );
88 if ( !d->alias.isEmpty() )
126 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
171 return d->defaultValueDefinition;
186 return d->constraints;
213 if ( d->type == QVariant::Double )
216 if ( QLocale().decimalPoint() !=
'.' ||
217 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
219 if ( d->precision > 0 )
221 return QLocale().toString( v.toDouble(),
'f', d->precision );
227 QString s( v.toString() );
228 int dotPosition( s.indexOf(
'.' ) );
230 if ( dotPosition < 0 )
236 precision = s.length() - dotPosition - 1;
238 return QLocale().toString( v.toDouble(),
'f',
precision );
242 else if ( d->type == QVariant::Double && d->precision > 0 )
244 return QString::number( v.toDouble(),
'f', d->precision );
249 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
252 qlonglong converted( v.toLongLong( &ok ) );
254 return QLocale().toString( converted );
270 v.convert( d->type );
274 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
276 v = QVariant( d->type );
282 if ( d->type == QVariant::Double && v.type() == QVariant::String )
285 if ( !tmp.convert( d->type ) )
289 double d = QLocale().toDouble( v.toString(), &ok );
296 if ( QLocale().decimalPoint() !=
'.' )
298 d = QLocale( QLocale::English ).toDouble( v.toString(), &ok );
309 if ( d->type == QVariant::Int && v.type() == QVariant::String )
312 if ( !tmp.convert( d->type ) )
316 int i = QLocale().toInt( v.toString(), &ok );
326 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
329 if ( !tmp.convert( d->type ) )
333 qlonglong l = QLocale().toLongLong( v.toString(), &ok );
344 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
347 double dbl = v.toDouble( &ok );
351 v = QVariant( d->type );
355 double round = std::round( dbl );
356 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
359 v = QVariant( d->type );
362 v = QVariant( static_cast< int >( std::round( dbl ) ) );
367 if ( !v.convert( d->type ) )
369 v = QVariant( d->type );
373 if ( d->type == QVariant::Double && d->precision > 0 )
375 double s = std::pow( 10, d->precision );
376 double d = v.toDouble() * s;
377 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
381 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
383 v = v.toString().left( d->length );
392 d->editorWidgetSetup = v;
397 return d->editorWidgetSetup;
409 out << static_cast< quint32 >( field.
type() );
414 out << field.
alias();
426 out << static_cast< quint32 >( field.
subType() );
437 quint32 originNotNull;
438 quint32 originUnique;
439 quint32 originExpression;
440 quint32 strengthNotNull;
441 quint32 strengthUnique;
442 quint32 strengthExpression;
450 QString defaultValueExpression;
451 QString constraintExpression;
452 QString constraintDescription;
454 in >> name >> type >> typeName >> length >> precision >> comment >> alias
455 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
456 constraintExpression >> constraintDescription >>
subType;
458 field.
setType( static_cast< QVariant::Type >( type ) );
460 field.
setLength( static_cast< int >( length ) );
468 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
469 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
472 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
475 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
476 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
482 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
483 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
486 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
489 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
Gets 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.