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 )
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(),
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" )
130 if ( d->type == QVariant::UserType )
132 if ( d->typeName.compare( QLatin1String(
"geometry" ), Qt::CaseInsensitive ) == 0 )
134 return QObject::tr(
"Geometry" );
172 return d->metadata.value( property );
182 return d->metadata.value(
static_cast< int >( property ) );
192 d->metadata[
static_cast< int >( property )] = value;
197 d->metadata[ property ] = value;
202 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
207 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
252 return d->defaultValueDefinition;
267 return d->constraints;
303 if ( v.userType() == QMetaType::type(
"QgsReferencedGeometry" ) )
310 QString wkt = geom.
asWkt();
311 if ( wkt.length() >= 1050 )
316 return formattedText;
321 if ( d->type == QVariant::Double )
331 if ( QLocale().decimalPoint() !=
'.' ||
332 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
334 if ( d->precision > 0 )
336 if ( -1 < v.toDouble() && v.toDouble() < 1 )
338 return QLocale().toString( v.toDouble(),
'g', d->precision );
342 return QLocale().toString( v.toDouble(),
'f', d->precision );
349 const QString s( v.toString() );
350 const int dotPosition( s.indexOf(
'.' ) );
352 if ( dotPosition < 0 && s.indexOf(
'e' ) < 0 )
355 return QLocale().toString( v.toDouble(),
'f',
precision );
360 else precision = s.length() - dotPosition - 1;
362 if ( -1 < v.toDouble() && v.toDouble() < 1 )
364 return QLocale().toString( v.toDouble(),
'g',
precision );
368 return QLocale().toString( v.toDouble(),
'f',
precision );
374 else if ( d->precision > 0 )
376 if ( -1 < v.toDouble() && v.toDouble() < 1 )
378 return QString::number( v.toDouble(),
'g', d->precision );
382 return QString::number( v.toDouble(),
'f', d->precision );
387 const double vDouble = v.toDouble();
389 if ( std::fabs( vDouble ) < 1e-04 )
390 return QString::number( vDouble,
'g', QLocale::FloatingPointShortest );
392 return QString::number( vDouble,
'f', QLocale::FloatingPointShortest );
397 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
400 const qlonglong converted( v.toLongLong( &ok ) );
402 return QLocale().toString( converted );
404 else if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) )
406 const QJsonDocument doc = QJsonDocument::fromVariant( v );
407 return QString::fromUtf8( doc.toJson().constData() );
409 else if ( d->type == QVariant::ByteArray )
411 return QObject::tr(
"BLOB" );
413 else if ( d->type == QVariant::StringList || d->type == QVariant::List )
416 const QVariantList list = v.toList();
417 for (
const QVariant &var : list )
419 if ( !result.isEmpty() )
420 result.append( QStringLiteral(
", " ) );
421 result.append( var.toString() );
435 return QObject::tr(
"None" );
437 return QObject::tr(
"Not searchable" );
439 return QObject::tr(
"Do not expose via WMS" );
441 return QObject::tr(
"Do not expose via WFS" );
454 const QVariant original = v;
456 errorMessage->clear();
460 v.convert( d->type );
464 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
466 v = QVariant( d->type );
468 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
474 if ( d->type == QVariant::Double && v.type() == QVariant::String )
477 if ( !tmp.convert( d->type ) )
488 if ( QLocale().decimalPoint() !=
'.' )
490 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
501 if ( d->type == QVariant::Int && v.type() == QVariant::String )
504 if ( !tmp.convert( d->type ) )
518 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
521 if ( !tmp.convert( d->type ) )
536 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
539 const double dbl = v.toDouble( &ok );
543 v = QVariant( d->type );
546 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
551 const double round = std::round( dbl );
552 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
555 v = QVariant( d->type );
558 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
562 v = QVariant(
static_cast< int >( std::round( dbl ) ) );
568 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
572 if ( !tmp.convert( d->type ) )
575 const double dbl = v.toDouble( &ok );
579 v = QVariant( d->type );
582 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
587 const double round = std::round( dbl );
588 if ( round >
static_cast<double>( std::numeric_limits<long long>::max() ) || round <
static_cast<double>( -std::numeric_limits<long long>::max() ) )
591 v = QVariant( d->type );
594 *errorMessage = QObject::tr(
"Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
598 v = QVariant(
static_cast< long long >( std::round( dbl ) ) );
603 if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName.compare( QLatin1String(
"jsonb" ), Qt::CaseInsensitive ) == 0 )
605 if ( d->type == QVariant::String )
607 const QJsonDocument doc = QJsonDocument::fromVariant( v );
610 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
613 v = QVariant( d->type );
616 else if ( d->type == QVariant::Map )
618 if ( v.type() == QVariant::StringList || v.type() == QVariant::List || v.type() == QVariant::Map )
622 v = QVariant( d->type );
627 if ( ( d->type == QVariant::StringList || ( d->type == QVariant::List && d->subType == QVariant::String ) )
628 && ( v.type() == QVariant::String ) )
630 v = QStringList( { v.toString() } );
634 if ( ( d->type == QVariant::StringList || d->type == QVariant::List ) && !( v.type() == QVariant::StringList || v.type() == QVariant::List ) )
636 v = QVariant( d->type );
639 *errorMessage = QObject::tr(
"Could not convert value \"%1\" to target list type" ).arg( original.toString() );
645 if ( d->type == QVariant::String && v.userType() == QMetaType::type(
"QgsReferencedGeometry" ) )
650 v = QVariant( d->type );
654 v = QVariant( geom.asWkt() );
658 else if ( d->type == QVariant::UserType && d->typeName.compare( QLatin1String(
"geometry" ), Qt::CaseInsensitive ) == 0 )
660 if ( v.userType() == QMetaType::type(
"QgsReferencedGeometry" ) || v.userType() == QMetaType::type(
"QgsGeometry" ) )
664 else if ( v.type() == QVariant::String )
669 v = QVariant::fromValue( geom );
675 else if ( !v.convert( d->type ) )
677 v = QVariant( d->type );
680 *errorMessage = QObject::tr(
"Could not convert value \"%1\" to target type \"%2\"" )
681 .arg( original.toString(),
687 if ( d->type == QVariant::Double && d->precision > 0 )
689 const double s = std::pow( 10, d->precision );
690 const double d = v.toDouble() * s;
691 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
695 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
697 const int length = v.toString().length();
698 v = v.toString().left( d->length );
701 *errorMessage = QObject::tr(
"String of length %1 exceeds maximum field length (%2)" ).arg(
length ).arg( d->length );
711 d->editorWidgetSetup = v;
716 return d->editorWidgetSetup;
721 d->isReadOnly = readOnly;
726 return d->isReadOnly;
731 return d->splitPolicy;
736 d->splitPolicy = policy;
748 out << static_cast< quint32 >( field.
type() );
753 out << field.
alias();
765 out << static_cast< quint32 >( field.
subType() );
778 quint32 originNotNull;
779 quint32 originUnique;
780 quint32 originExpression;
781 quint32 strengthNotNull;
782 quint32 strengthUnique;
783 quint32 strengthExpression;
792 QString defaultValueExpression;
793 QString constraintExpression;
794 QString constraintDescription;
795 QMap< int, QVariant > metadata;
798 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
799 constraintExpression >> constraintDescription >> subType >> splitPolicy >> metadata;
801 field.
setType(
static_cast< QVariant::Type
>( type ) );
803 field.
setLength(
static_cast< int >( length ) );
833 field.
setSubType(
static_cast< QVariant::Type
>( subType ) );
FieldDomainSplitPolicy
Split policy for field domains.
FieldMetadataProperty
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
@ NoFlag
No flag is defined.
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance)
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QString userFriendlyIdentifier(IdentifierType type=MediumString) const
Returns a user friendly identifier for the CRS.
The QgsDefaultValue class provides a container for managing client side default values for fields.
Stores information about constraints which may be present on a field.
ConstraintStrength
Strength of constraints.
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
ConstraintOrigin
Origin of constraints.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QString constraintExpression() const
Returns the constraint expression for the field, if set.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
Encapsulate a field in an attribute table or data source.
void setSplitPolicy(Qgis::FieldDomainSplitPolicy policy)
Sets the field's split policy, which indicates how field values should be handled during a split oper...
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.
QString typeName() const
Gets the field type.
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
bool operator!=(const QgsField &other) const
bool operator==(const QgsField &other) const
QgsField & operator=(const QgsField &other)
Assignment operator.
QString displayString(const QVariant &v) const
Formats string for display.
void setPrecision(int precision)
Set the field precision.
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
void setName(const QString &name)
Set the field name.
void setComment(const QString &comment)
Set the field comment.
QString displayType(bool showConstraints=false) const
Returns the type to use when displaying this field, including the length and precision of the datatyp...
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Qgis::FieldDomainSplitPolicy splitPolicy() const
Returns the field's split policy, which indicates how field values should be handled during a split o...
void setLength(int len)
Set the field length.
QString displayName() const
Returns the name to use when displaying this field.
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Qgis::FieldConfigurationFlags configurationFlags
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.
static constexpr int MAX_WKT_LENGTH
QgsDefaultValue defaultValueDefinition
void setSubType(QVariant::Type subType)
If the field is a collection, set its element's type.
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
void setType(QVariant::Type type)
Set variant type.
QgsFieldConstraints constraints
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
void setTypeName(const QString &typeName)
Set the field type.
A geometry is the spatial representation of a feature.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString asWkt(int precision=17) const
Exports the geometry to WKT.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
static QString typeToDisplayString(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
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...
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...
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.