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,
49 QVariant::Type subType )
51 d =
new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
74 return *( other.d ) == *d;
79 return !( *
this == other );
89 if ( !d->alias.isEmpty() )
127 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
172 return d->defaultValueDefinition;
187 return d->constraints;
214 if ( d->type == QVariant::Double )
217 if ( QLocale().decimalPoint() !=
'.' ||
218 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
220 if ( d->precision > 0 )
222 return QLocale().toString( v.toDouble(),
'f', d->precision );
228 QString s( v.toString() );
229 int dotPosition( s.indexOf(
'.' ) );
231 if ( dotPosition < 0 )
237 precision = s.length() - dotPosition - 1;
239 return QLocale().toString( v.toDouble(),
'f',
precision );
243 else if ( d->type == QVariant::Double && d->precision > 0 )
245 return QString::number( v.toDouble(),
'f', d->precision );
250 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
253 qlonglong converted( v.toLongLong( &ok ) );
255 return QLocale().toString( converted );
257 else if ( d->typeName == QLatin1String(
"json" ) || d->typeName == QLatin1String(
"jsonb" ) )
259 QJsonDocument doc = QJsonDocument::fromVariant( v );
260 return QString::fromUtf8( doc.toJson().data() );
276 v.convert( d->type );
280 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
282 v = QVariant( d->type );
288 if ( d->type == QVariant::Double && v.type() == QVariant::String )
291 if ( !tmp.convert( d->type ) )
302 if ( QLocale().decimalPoint() !=
'.' )
304 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
315 if ( d->type == QVariant::Int && v.type() == QVariant::String )
318 if ( !tmp.convert( d->type ) )
332 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
335 if ( !tmp.convert( d->type ) )
350 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
353 double dbl = v.toDouble( &ok );
357 v = QVariant( d->type );
361 double round = std::round( dbl );
362 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
365 v = QVariant( d->type );
368 v = QVariant( static_cast< int >( std::round( dbl ) ) );
374 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
378 if ( !tmp.convert( d->type ) )
381 double dbl = v.toDouble( &ok );
385 v = QVariant( d->type );
389 double round = std::round( dbl );
390 if ( round > std::numeric_limits<long long>::max() || round < -std::numeric_limits<long long>::max() )
393 v = QVariant( d->type );
396 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
401 if ( !v.convert( d->type ) )
403 v = QVariant( d->type );
407 if ( d->type == QVariant::Double && d->precision > 0 )
409 double s = std::pow( 10, d->precision );
410 double d = v.toDouble() * s;
411 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
415 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
417 v = v.toString().left( d->length );
426 d->editorWidgetSetup = v;
431 return d->editorWidgetSetup;
443 out << static_cast< quint32 >( field.
type() );
448 out << field.
alias();
460 out << static_cast< quint32 >( field.
subType() );
471 quint32 originNotNull;
472 quint32 originUnique;
473 quint32 originExpression;
474 quint32 strengthNotNull;
475 quint32 strengthUnique;
476 quint32 strengthExpression;
484 QString defaultValueExpression;
485 QString constraintExpression;
486 QString constraintDescription;
488 in >> name >> type >> typeName >> length >> precision >> comment >> alias
489 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
490 constraintExpression >> constraintDescription >>
subType;
492 field.
setType( static_cast< QVariant::Type >( type ) );
494 field.
setLength( static_cast< int >( length ) );
502 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
503 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
506 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
509 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
510 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
516 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
517 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
520 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
523 field.
setSubType( static_cast< QVariant::Type >( subType ) );
QString displayName() const
Returns the name to use when displaying this field.
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.
bool operator==(const QgsField &other) const
The QgsDefaultValue class provides a container for managing client side default values for fields...
QString displayString(const QVariant &v) const
Formats string for display.
void setPrecision(int precision)
Set the field precision.
QString alias() const
Returns the alias for the field (the friendly displayed name of the field ), or an empty string if th...
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 precision() const
Gets the precision of the field.
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.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void setName(const QString &name)
Set the field name.
QString comment() const
Returns the field comment.
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...
Field has an expression constraint set. See constraintExpression().
void setLength(int len)
Set the field length.
QString typeName() const
Gets the field type.
QgsDefaultValue defaultValueDefinition() const
Returns the expression used when calculating the default value for the field.
bool isNumeric() const
Returns if this field is numeric.
void setTypeName(const QString &typeName)
Set the field type.
QString name() const
Returns the name of the field.
bool operator!=(const QgsField &other) const
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
Encapsulate a field in an attribute table or data source.
const QgsFieldConstraints & constraints() const
Returns constraints which are present for the field.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
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.
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.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
int length() const
Gets the length of the field.
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
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
QVariant::Type subType() const
If the field is a collection, gets its element's type.
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Field must have a unique value.
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.