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 );
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() );
138 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
143 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
188 return d->defaultValueDefinition;
203 return d->constraints;
230 if ( d->type == QVariant::Double )
233 if ( QLocale().decimalPoint() !=
'.' ||
234 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
236 if ( d->precision > 0 )
238 if ( -1 < v.toDouble() && v.toDouble() < 1 )
240 return QLocale().toString( v.toDouble(),
'g', d->precision );
244 return QLocale().toString( v.toDouble(),
'f', d->precision );
251 QString s( v.toString() );
252 int dotPosition( s.indexOf(
'.' ) );
254 if ( dotPosition < 0 && s.indexOf(
'e' ) < 0 )
257 return QLocale().toString( v.toDouble(),
'f',
precision );
261 if ( dotPosition < 0 ) precision = 0;
262 else precision = s.length() - dotPosition - 1;
264 if ( -1 < v.toDouble() && v.toDouble() < 1 )
266 return QLocale().toString( v.toDouble(),
'g',
precision );
270 return QLocale().toString( v.toDouble(),
'f',
precision );
276 else if ( d->type == QVariant::Double && d->precision > 0 )
278 if ( -1 < v.toDouble() && v.toDouble() < 1 )
280 return QString::number( v.toDouble(),
'g', d->precision );
284 return QString::number( v.toDouble(),
'f', d->precision );
290 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
293 qlonglong converted( v.toLongLong( &ok ) );
295 return QLocale().toString( converted );
297 else if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) )
299 QJsonDocument doc = QJsonDocument::fromVariant( v );
300 return QString::fromUtf8( doc.toJson().data() );
302 else if ( d->type == QVariant::ByteArray )
304 return QObject::tr(
"BLOB" );
320 v.convert( d->type );
324 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
326 v = QVariant( d->type );
332 if ( d->type == QVariant::Double && v.type() == QVariant::String )
335 if ( !tmp.convert( d->type ) )
346 if ( QLocale().decimalPoint() !=
'.' )
348 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
359 if ( d->type == QVariant::Int && v.type() == QVariant::String )
362 if ( !tmp.convert( d->type ) )
376 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
379 if ( !tmp.convert( d->type ) )
394 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
397 double dbl = v.toDouble( &ok );
401 v = QVariant( d->type );
405 double round = std::round( dbl );
406 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
409 v = QVariant( d->type );
412 v = QVariant( static_cast< int >( std::round( dbl ) ) );
418 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
422 if ( !tmp.convert( d->type ) )
425 double dbl = v.toDouble( &ok );
429 v = QVariant( d->type );
433 double round = std::round( dbl );
434 if ( round > std::numeric_limits<long long>::max() || round < -std::numeric_limits<long long>::max() )
437 v = QVariant( d->type );
440 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
445 if ( !v.convert( d->type ) )
447 v = QVariant( d->type );
451 if ( d->type == QVariant::Double && d->precision > 0 )
453 double s = std::pow( 10, d->precision );
454 double d = v.toDouble() * s;
455 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
459 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
461 v = v.toString().left( d->length );
470 d->editorWidgetSetup = v;
475 return d->editorWidgetSetup;
487 out << static_cast< quint32 >( field.
type() );
492 out << field.
alias();
504 out << static_cast< quint32 >( field.
subType() );
515 quint32 originNotNull;
516 quint32 originUnique;
517 quint32 originExpression;
518 quint32 strengthNotNull;
519 quint32 strengthUnique;
520 quint32 strengthExpression;
528 QString defaultValueExpression;
529 QString constraintExpression;
530 QString constraintDescription;
532 in >> name >> type >> typeName >> length >> precision >> comment >> alias
533 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
534 constraintExpression >> constraintDescription >>
subType;
536 field.
setType( static_cast< QVariant::Type >( type ) );
538 field.
setLength( static_cast< int >( length ) );
546 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
547 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
550 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintNotNull );
553 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
554 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
560 fieldConstraints.
setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
561 fieldConstraints.
setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
564 fieldConstraints.
removeConstraint( QgsFieldConstraints::ConstraintExpression );
567 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.
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.
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.
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.
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().
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.
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.
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.
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)
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.