24 #include <QDataStream>
27 #include <QJsonDocument>
38 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
49 const QString &
typeName,
int len,
int prec,
const QString &comment, QVariant::Type subType )
76 return *( other.d ) == *d;
81 return !( *
this == other );
91 if ( !d->alias.isEmpty() )
99 if (
alias().isEmpty() )
103 return QStringLiteral(
"%1 (%2)" ).arg(
name() ).arg(
alias() );
111 typeStr += QStringLiteral(
"(%1, %2)" ).arg(
length() ).arg(
precision() );
113 typeStr += QStringLiteral(
"(%1)" ).arg(
length() );
115 if ( showConstraints )
118 ? QStringLiteral(
" NOT NULL" )
119 : QStringLiteral(
" NULL" );
122 ? QStringLiteral(
" UNIQUE" )
161 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
166 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
211 return d->defaultValueDefinition;
226 return d->constraints;
262 if ( v.userType() == QMetaType::type(
"QgsReferencedGeometry" ) )
269 QString wkt = geom.
asWkt();
270 if ( wkt.length() >= 1050 )
272 wkt = wkt.left( 999 ) + QChar( 0x2026 );
275 return formattedText;
280 if ( d->type == QVariant::Double )
290 if ( QLocale().decimalPoint() !=
'.' ||
291 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
293 if ( d->precision > 0 )
295 if ( -1 < v.toDouble() && v.toDouble() < 1 )
297 return QLocale().toString( v.toDouble(),
'g', d->precision );
301 return QLocale().toString( v.toDouble(),
'f', d->precision );
308 const QString s( v.toString() );
309 const int dotPosition( s.indexOf(
'.' ) );
311 if ( dotPosition < 0 && s.indexOf(
'e' ) < 0 )
314 return QLocale().toString( v.toDouble(),
'f',
precision );
319 else precision = s.length() - dotPosition - 1;
321 if ( -1 < v.toDouble() && v.toDouble() < 1 )
323 return QLocale().toString( v.toDouble(),
'g',
precision );
327 return QLocale().toString( v.toDouble(),
'f',
precision );
333 else if ( d->precision > 0 )
335 if ( -1 < v.toDouble() && v.toDouble() < 1 )
337 return QString::number( v.toDouble(),
'g', d->precision );
341 return QString::number( v.toDouble(),
'f', d->precision );
347 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
350 const qlonglong converted( v.toLongLong( &ok ) );
352 return QLocale().toString( converted );
354 else if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) )
356 const QJsonDocument doc = QJsonDocument::fromVariant( v );
357 return QString::fromUtf8( doc.toJson().data() );
359 else if ( d->type == QVariant::ByteArray )
361 return QObject::tr(
"BLOB" );
363 else if ( d->type == QVariant::StringList || d->type == QVariant::List )
366 const QVariantList list = v.toList();
367 for (
const QVariant &var : list )
369 if ( !result.isEmpty() )
370 result.append( QStringLiteral(
", " ) );
371 result.append( var.toString() );
385 return QObject::tr(
"None" );
387 return QObject::tr(
"Not searchable" );
389 return QObject::tr(
"Do not expose via WMS" );
391 return QObject::tr(
"Do not expose via WFS" );
404 const QVariant original = v;
406 errorMessage->clear();
410 v.convert( d->type );
414 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
416 v = QVariant( d->type );
418 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
424 if ( d->type == QVariant::Double && v.type() == QVariant::String )
427 if ( !tmp.convert( d->type ) )
438 if ( QLocale().decimalPoint() !=
'.' )
440 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
451 if ( d->type == QVariant::Int && v.type() == QVariant::String )
454 if ( !tmp.convert( d->type ) )
468 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
471 if ( !tmp.convert( d->type ) )
486 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
489 const double dbl = v.toDouble( &ok );
493 v = QVariant( d->type );
496 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
501 const double round = std::round( dbl );
502 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
505 v = QVariant( d->type );
508 *errorMessage = QObject::tr(
"Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
512 v = QVariant(
static_cast< int >( std::round( dbl ) ) );
518 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
522 if ( !tmp.convert( d->type ) )
525 const double dbl = v.toDouble( &ok );
529 v = QVariant( d->type );
532 *errorMessage = QObject::tr(
"Value \"%1\" is not a number" ).arg( original.toString() );
537 const double round = std::round( dbl );
538 if ( round >
static_cast<double>( std::numeric_limits<long long>::max() ) || round <
static_cast<double>( -std::numeric_limits<long long>::max() ) )
541 v = QVariant( d->type );
544 *errorMessage = QObject::tr(
"Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
548 v = QVariant(
static_cast< long long >( std::round( dbl ) ) );
553 if ( d->type == QVariant::String && ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) ) )
555 const QJsonDocument doc = QJsonDocument::fromVariant( v );
558 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
561 v = QVariant( d->type );
565 if ( !v.convert( d->type ) )
567 v = QVariant( d->type );
570 *errorMessage = QObject::tr(
"Could not convert value \"%1\" to target type" ).arg( original.toString() );
575 if ( d->type == QVariant::Double && d->precision > 0 )
577 const double s = std::pow( 10, d->precision );
578 const double d = v.toDouble() * s;
579 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
583 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
585 const int length = v.toString().length();
586 v = v.toString().left( d->length );
589 *errorMessage = QObject::tr(
"String of length %1 exceeds maximum field length (%2)" ).arg(
length ).arg( d->length );
599 d->editorWidgetSetup = v;
604 return d->editorWidgetSetup;
609 d->isReadOnly = readOnly;
614 return d->isReadOnly;
627 out << static_cast< quint32 >(
field.
type() );
655 quint32 originNotNull;
656 quint32 originUnique;
657 quint32 originExpression;
658 quint32 strengthNotNull;
659 quint32 strengthUnique;
660 quint32 strengthExpression;
668 QString defaultValueExpression;
669 QString constraintExpression;
670 QString constraintDescription;
673 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
674 constraintExpression >> constraintDescription >> subType;
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.
Q_GADGET QString expression
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.
Q_GADGET Constraints constraints
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
Encapsulate a field in an attribute table or data source.
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
static QString readableConfigurationFlag(QgsField::ConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
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 setLength(int len)
Set the field length.
void setConfigurationFlags(QgsField::ConfigurationFlags configurationFlags)
Sets the Flags for the field (searchable, …)
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.
ConfigurationFlags configurationFlags
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
QVariant::Type subType() const
If the field is a collection, gets its element's type.
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.
QgsDefaultValue defaultValueDefinition
void setSubType(QVariant::Type subType)
If the field is a collection, set its element's type.
void setType(QVariant::Type type)
Set variant type.
QgsFieldConstraints constraints
ConfigurationFlag
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.
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance)
@ None
No flag is defined.
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
void setTypeName(const QString &typeName)
Set the field type.
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.
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)