23 #include <QDataStream> 26 #include <QJsonDocument> 37 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
48 const QString &
typeName,
int len,
int prec,
const QString &comment, QVariant::Type subType )
50 d =
new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
75 return *( other.d ) == *d;
80 return !( *
this == other );
90 if ( !d->alias.isEmpty() )
98 if (
alias().isEmpty() )
102 return QStringLiteral(
"%1 (%2)" ).arg(
name() ).arg(
alias() );
110 typeStr += QStringLiteral(
"(%1, %2)" ).arg(
length() ).arg(
precision() );
112 typeStr += QStringLiteral(
"(%1)" ).arg(
length() );
114 if ( showConstraints )
117 ? QStringLiteral(
" NOT NULL" )
118 : QStringLiteral(
" NULL" );
121 ? QStringLiteral(
" UNIQUE" )
160 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
165 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
210 return d->defaultValueDefinition;
225 return d->constraints;
262 if ( d->type == QVariant::Double )
272 if ( QLocale().decimalPoint() !=
'.' ||
273 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
275 if ( d->precision > 0 )
277 if ( -1 < v.toDouble() && v.toDouble() < 1 )
279 return QLocale().toString( v.toDouble(),
'g', d->precision );
283 return QLocale().toString( v.toDouble(),
'f', d->precision );
290 QString s( v.toString() );
291 int dotPosition( s.indexOf(
'.' ) );
293 if ( dotPosition < 0 && s.indexOf(
'e' ) < 0 )
296 return QLocale().toString( v.toDouble(),
'f',
precision );
300 if ( dotPosition < 0 ) precision = 0;
301 else precision = s.length() - dotPosition - 1;
303 if ( -1 < v.toDouble() && v.toDouble() < 1 )
305 return QLocale().toString( v.toDouble(),
'g',
precision );
309 return QLocale().toString( v.toDouble(),
'f',
precision );
315 else if ( d->type == QVariant::Double && d->precision > 0 )
317 if ( -1 < v.toDouble() && v.toDouble() < 1 )
319 return QString::number( v.toDouble(),
'g', d->precision );
323 return QString::number( v.toDouble(),
'f', d->precision );
329 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
332 qlonglong converted( v.toLongLong( &ok ) );
334 return QLocale().toString( converted );
336 else if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) )
338 QJsonDocument doc = QJsonDocument::fromVariant( v );
339 return QString::fromUtf8( doc.toJson().data() );
341 else if ( d->type == QVariant::ByteArray )
343 return QObject::tr(
"BLOB" );
354 return QObject::tr(
"None" );
356 return QObject::tr(
"Not searchable" );
358 return QStringLiteral(
"Do not expose via WMS" );
360 return QStringLiteral(
"Do not expose via WFS" );
373 const QVariant original = v;
375 errorMessage->clear();
379 v.convert( d->type );
383 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
385 v = QVariant( d->type );
387 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
393 if ( d->type == QVariant::Double && v.type() == QVariant::String )
396 if ( !tmp.convert( d->type ) )
407 if ( QLocale().decimalPoint() !=
'.' )
409 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
420 if ( d->type == QVariant::Int && v.type() == QVariant::String )
423 if ( !tmp.convert( d->type ) )
437 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
440 if ( !tmp.convert( d->type ) )
455 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
458 double dbl = v.toDouble( &ok );
462 v = QVariant( d->type );
465 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
470 double round = std::round( dbl );
471 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
474 v = QVariant( d->type );
477 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
481 v = QVariant( static_cast< int >( std::round( dbl ) ) );
487 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
491 if ( !tmp.convert( d->type ) )
494 double dbl = v.toDouble( &ok );
498 v = QVariant( d->type );
501 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
506 double round = std::round( dbl );
507 if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
510 v = QVariant( d->type );
513 *errorMessage = QObject::tr(
"Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
517 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
522 if ( !v.convert( d->type ) )
524 v = QVariant( d->type );
527 *errorMessage = QObject::tr(
"Could not convert value \"%1\" to target type" ).arg( original.toString() );
532 if ( d->type == QVariant::Double && d->precision > 0 )
534 double s = std::pow( 10, d->precision );
535 double d = v.toDouble() * s;
536 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
540 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
542 const int length = v.toString().length();
543 v = v.toString().left( d->length );
546 *errorMessage = QObject::tr(
"String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
556 d->editorWidgetSetup = v;
561 return d->editorWidgetSetup;
566 d->isReadOnly = readOnly;
571 return d->isReadOnly;
584 out << static_cast< quint32 >( field.
type() );
589 out << field.
alias();
601 out << static_cast< quint32 >( field.
subType() );
612 quint32 originNotNull;
613 quint32 originUnique;
614 quint32 originExpression;
615 quint32 strengthNotNull;
616 quint32 strengthUnique;
617 quint32 strengthExpression;
625 QString defaultValueExpression;
626 QString constraintExpression;
627 QString constraintDescription;
629 in >> name >> type >> typeName >> length >> precision >> comment >> alias
630 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
631 constraintExpression >> constraintDescription >>
subType;
633 field.
setType( static_cast< QVariant::Type >( type ) );
635 field.
setLength( static_cast< int >( length ) );
643 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
644 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
647 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
650 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
651 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
657 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
658 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
661 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
664 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.
bool isDateOrTime() const
Returns if this field is a date and/or time type.
Fields is available if layer is served as WFS from QGIS server.
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...
QgsField::ConfigurationFlags configurationFlags() const
Returns the Flags for the field (searchable, …)
QString displayType(bool showConstraints=false) const
Returns the type to use when displaying this field, including the length and precision of the datatyp...
void setPrecision(int precision)
Set the field precision.
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
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.
Fields is available if layer is served as WMS from QGIS server.
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.
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
QString name() const
Returns the name of the field.
Field has an expression constraint set. See constraintExpression().
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
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.
static QString readableConfigurationFlag(QgsField::ConfigurationFlag flag)
Returns the reabable and translated value of the configuration flag.
void setTypeName(const QString &typeName)
Set the field type.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
void setConfigurationFlags(QgsField::ConfigurationFlags configurationFlags)
Sets the Flags for the field (searchable, …)
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
bool operator!=(const QgsField &other) const
bool isReadOnly() const
Returns true if this field is a read-only field.
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.
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
int length() const
Gets the length of the field.
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Defines if the field is searchable (used in the locator search for instance)
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.
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
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)
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.