mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[FEATURE] Field constraints can be enforced or not
Non-enforced constraints just show a warning to the user, but do not prevent committing the feature. Enforced constraints block users from comitting non compliant features. Any constraints detected by the provider are always enforced.
This commit is contained in:
parent
e3a608365f
commit
fac5bc0691
@ -41,8 +41,9 @@ class QgsFieldConstraints
|
||||
*/
|
||||
enum ConstraintStrength
|
||||
{
|
||||
ConstraintHard, //!< Constraint must be honored before feature can be accepted
|
||||
ConstraintSoft, //!< User is warned if constraint is violated but feature can still be accepted
|
||||
ConstraintStrengthNotSet, //!< Constraint is not set
|
||||
ConstraintStrengthHard, //!< Constraint must be honored before feature can be accepted
|
||||
ConstraintStrengthSoft, //!< User is warned if constraint is violated but feature can still be accepted
|
||||
};
|
||||
|
||||
/**
|
||||
@ -64,6 +65,22 @@ class QgsFieldConstraints
|
||||
*/
|
||||
ConstraintOrigin constraintOrigin( Constraint constraint ) const;
|
||||
|
||||
/**
|
||||
* Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint
|
||||
* is not present on this field.
|
||||
* @see constraints()
|
||||
* @see setConstraintStrength()
|
||||
*/
|
||||
ConstraintStrength constraintStrength( Constraint constraint ) const;
|
||||
|
||||
/**
|
||||
* Sets the strength of a constraint. Note that the strength of constraints which originate
|
||||
* from a provider cannot be changed. Constraints default to ConstraintStrengthHard unless
|
||||
* explicitly changed.
|
||||
* @see constraintStrength()
|
||||
*/
|
||||
void setConstraintStrength( Constraint constraint, ConstraintStrength strength );
|
||||
|
||||
/**
|
||||
* Sets a constraint on the field.
|
||||
* @see constraints()
|
||||
|
@ -1262,18 +1262,28 @@ class QgsVectorLayer : QgsMapLayer
|
||||
* field index. These constraints may be inherited from the layer's data provider
|
||||
* or may be set manually on the vector layer from within QGIS.
|
||||
* @note added in QGIS 3.0
|
||||
* @see setFieldConstraints()
|
||||
* @see setFieldConstraint()
|
||||
*/
|
||||
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
|
||||
|
||||
/**
|
||||
* Sets the constraints for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be cleared. Ie, calling this method only allows for new
|
||||
* Sets a constraint for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be modified. Ie, calling this method only allows for new
|
||||
* constraints to be added on top of the existing provider constraints.
|
||||
* @note added in QGIS 3.0
|
||||
* @see fieldConstraints()
|
||||
* @see removeFieldConstraint()
|
||||
*/
|
||||
void setFieldConstraints( int index, QgsFieldConstraints::Constraints constraints );
|
||||
void setFieldConstraint( int index, QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthHard );
|
||||
|
||||
/**
|
||||
* Removes a constraint for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be removed.
|
||||
* @note added in QGIS 3.0
|
||||
* @see fieldConstraints()
|
||||
* @see setFieldConstraint()
|
||||
*/
|
||||
void removeFieldConstraint( int index, QgsFieldConstraints::Constraint constraint );
|
||||
|
||||
/**
|
||||
* Returns the constraint expression for for a specified field index, if set.
|
||||
|
@ -69,7 +69,21 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
|
||||
isFieldEditableCheckBox->setEnabled( false );
|
||||
}
|
||||
|
||||
connect( mExpressionWidget, SIGNAL( expressionChanged( QString ) ), this, SLOT( defaultExpressionChanged() ) );
|
||||
connect( mExpressionWidget, &QgsExpressionLineEdit::expressionChanged, this, &QgsAttributeTypeDialog::defaultExpressionChanged );
|
||||
connect( mUniqueCheckBox, &QCheckBox::toggled, this, [=]( bool checked )
|
||||
{
|
||||
mCheckBoxEnforceUnique->setEnabled( checked );
|
||||
if ( !checked )
|
||||
mCheckBoxEnforceUnique->setChecked( false );
|
||||
}
|
||||
);
|
||||
connect( notNullCheckBox, &QCheckBox::toggled, this, [=]( bool checked )
|
||||
{
|
||||
mCheckBoxEnforceNotNull->setEnabled( checked );
|
||||
if ( !checked )
|
||||
mCheckBoxEnforceNotNull->setChecked( false );
|
||||
}
|
||||
);
|
||||
|
||||
QSettings settings;
|
||||
restoreGeometry( settings.value( QStringLiteral( "/Windows/QgsAttributeTypeDialog/geometry" ) ).toByteArray() );
|
||||
@ -154,7 +168,7 @@ void QgsAttributeTypeDialog::setWidgetType( const QString& type )
|
||||
stackedWidget->addWidget( cfgWdg );
|
||||
stackedWidget->setCurrentWidget( cfgWdg );
|
||||
mEditorConfigWidgets.insert( type, cfgWdg );
|
||||
connect( cfgWdg, SIGNAL( changed() ), this, SLOT( defaultExpressionChanged() ) );
|
||||
connect( cfgWdg, &QgsEditorConfigWidget::changed, this, &QgsAttributeTypeDialog::defaultExpressionChanged );
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -183,6 +197,8 @@ void QgsAttributeTypeDialog::setProviderConstraints( QgsFieldConstraints::Constr
|
||||
notNullCheckBox->setChecked( true );
|
||||
notNullCheckBox->setEnabled( false );
|
||||
notNullCheckBox->setToolTip( tr( "The provider for this layer has a NOT NULL constraint set on the field." ) );
|
||||
mCheckBoxEnforceNotNull->setChecked( true );
|
||||
mCheckBoxEnforceNotNull->setEnabled( false );
|
||||
}
|
||||
|
||||
if ( constraints & QgsFieldConstraints::ConstraintUnique )
|
||||
@ -190,6 +206,8 @@ void QgsAttributeTypeDialog::setProviderConstraints( QgsFieldConstraints::Constr
|
||||
mUniqueCheckBox->setChecked( true );
|
||||
mUniqueCheckBox->setEnabled( false );
|
||||
mUniqueCheckBox->setToolTip( tr( "The provider for this layer has a UNIQUE constraint set on the field." ) );
|
||||
mCheckBoxEnforceUnique->setChecked( true );
|
||||
mCheckBoxEnforceUnique->setEnabled( false );
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,6 +236,16 @@ bool QgsAttributeTypeDialog::notNull() const
|
||||
return notNullCheckBox->isChecked();
|
||||
}
|
||||
|
||||
void QgsAttributeTypeDialog::setNotNullEnforced( bool enforced )
|
||||
{
|
||||
mCheckBoxEnforceNotNull->setChecked( enforced );
|
||||
}
|
||||
|
||||
bool QgsAttributeTypeDialog::notNullEnforced() const
|
||||
{
|
||||
return mCheckBoxEnforceNotNull->isChecked();
|
||||
}
|
||||
|
||||
void QgsAttributeTypeDialog::setUnique( bool unique )
|
||||
{
|
||||
mUniqueCheckBox->setChecked( unique );
|
||||
@ -228,11 +256,31 @@ bool QgsAttributeTypeDialog::unique() const
|
||||
return mUniqueCheckBox->isChecked();
|
||||
}
|
||||
|
||||
void QgsAttributeTypeDialog::setUniqueEnforced( bool enforced )
|
||||
{
|
||||
mCheckBoxEnforceUnique->setChecked( enforced );
|
||||
}
|
||||
|
||||
bool QgsAttributeTypeDialog::uniqueEnforced() const
|
||||
{
|
||||
return mCheckBoxEnforceUnique->isChecked();
|
||||
}
|
||||
|
||||
void QgsAttributeTypeDialog::setConstraintExpression( const QString &str )
|
||||
{
|
||||
constraintExpressionWidget->setField( str );
|
||||
}
|
||||
|
||||
void QgsAttributeTypeDialog::setConstraintExpressionEnforced( bool enforced )
|
||||
{
|
||||
mCheckBoxEnforceExpression->setChecked( enforced );
|
||||
}
|
||||
|
||||
bool QgsAttributeTypeDialog::constraintExpressionEnforced() const
|
||||
{
|
||||
return mCheckBoxEnforceExpression->isChecked();
|
||||
}
|
||||
|
||||
QString QgsAttributeTypeDialog::defaultValueExpression() const
|
||||
{
|
||||
return mExpressionWidget->expression();
|
||||
|
@ -85,6 +85,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
|
||||
*/
|
||||
bool notNull() const;
|
||||
|
||||
/**
|
||||
* Sets whether the not null constraint is enforced.
|
||||
*/
|
||||
void setNotNullEnforced( bool enforced );
|
||||
|
||||
/**
|
||||
* Returns whether the not null constraint should be enforced.
|
||||
*/
|
||||
bool notNullEnforced() const;
|
||||
|
||||
/**
|
||||
* Setter for unique constraint checkbox
|
||||
*/
|
||||
@ -95,6 +105,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
|
||||
*/
|
||||
bool unique() const;
|
||||
|
||||
/**
|
||||
* Sets whether the not null constraint is enforced.
|
||||
*/
|
||||
void setUniqueEnforced( bool enforced );
|
||||
|
||||
/**
|
||||
* Returns whether the not null constraint should be enforced.
|
||||
*/
|
||||
bool uniqueEnforced() const;
|
||||
|
||||
/**
|
||||
* Setter for constraint expression description
|
||||
* @param desc the expression description
|
||||
@ -121,6 +141,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
|
||||
*/
|
||||
void setConstraintExpression( const QString &str );
|
||||
|
||||
/**
|
||||
* Sets whether the expression constraint is enforced.
|
||||
*/
|
||||
void setConstraintExpressionEnforced( bool enforced );
|
||||
|
||||
/**
|
||||
* Returns whether the expression constraint should be enforced.
|
||||
*/
|
||||
bool constraintExpressionEnforced() const;
|
||||
|
||||
/**
|
||||
* Returns the expression used for the field's default value, or
|
||||
* an empty string if no default value expression is set.
|
||||
|
@ -559,7 +559,9 @@ void QgsFieldsProperties::attributeTypeDialog()
|
||||
attributeTypeDialog.setFieldEditable( cfg.mEditable );
|
||||
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
|
||||
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull );
|
||||
attributeTypeDialog.setNotNullEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
|
||||
attributeTypeDialog.setUnique( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique );
|
||||
attributeTypeDialog.setUniqueEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
|
||||
|
||||
QgsFieldConstraints constraints = mLayer->fields().at( index ).constraints();
|
||||
QgsFieldConstraints::Constraints providerConstraints = 0;
|
||||
@ -573,6 +575,7 @@ void QgsFieldsProperties::attributeTypeDialog()
|
||||
|
||||
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
|
||||
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
|
||||
attributeTypeDialog.setConstraintExpressionEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
|
||||
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );
|
||||
|
||||
attributeTypeDialog.setWidgetConfig( cfg.mEditorWidgetConfig );
|
||||
@ -593,6 +596,10 @@ void QgsFieldsProperties::attributeTypeDialog()
|
||||
{
|
||||
cfg.mConstraints |= QgsFieldConstraints::ConstraintUnique;
|
||||
}
|
||||
if ( !attributeTypeDialog.constraintExpression().isEmpty() && !( providerConstraints & QgsFieldConstraints::ConstraintExpression ) )
|
||||
{
|
||||
cfg.mConstraints |= QgsFieldConstraints::ConstraintExpression;
|
||||
}
|
||||
|
||||
cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
|
||||
cfg.mConstraint = attributeTypeDialog.constraintExpression();
|
||||
@ -601,6 +608,13 @@ void QgsFieldsProperties::attributeTypeDialog()
|
||||
cfg.mEditorWidgetType = attributeTypeDialog.editorWidgetType();
|
||||
cfg.mEditorWidgetConfig = attributeTypeDialog.editorWidgetConfig();
|
||||
|
||||
cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintNotNull, attributeTypeDialog.notNullEnforced() ?
|
||||
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintUnique, attributeTypeDialog.uniqueEnforced() ?
|
||||
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintExpression, attributeTypeDialog.constraintExpressionEnforced() ?
|
||||
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
|
||||
pb->setText( attributeTypeDialog.editorWidgetText() );
|
||||
|
||||
setConfigForRow( row, cfg );
|
||||
@ -983,7 +997,18 @@ void QgsFieldsProperties::apply()
|
||||
editFormConfig.setWidgetType( name, cfg.mEditorWidgetType );
|
||||
editFormConfig.setWidgetConfig( name, cfg.mEditorWidgetConfig );
|
||||
|
||||
mLayer->setFieldConstraints( i, cfg.mConstraints );
|
||||
if ( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull )
|
||||
{
|
||||
mLayer->setFieldConstraint( i, QgsFieldConstraints::ConstraintNotNull, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintStrengthHard ) );
|
||||
}
|
||||
if ( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique )
|
||||
{
|
||||
mLayer->setFieldConstraint( i, QgsFieldConstraints::ConstraintUnique, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard ) );
|
||||
}
|
||||
if ( cfg.mConstraints & QgsFieldConstraints::ConstraintExpression )
|
||||
{
|
||||
mLayer->setFieldConstraint( i, QgsFieldConstraints::ConstraintExpression, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) );
|
||||
}
|
||||
|
||||
if ( mFieldsList->item( i, attrWMSCol )->checkState() == Qt::Unchecked )
|
||||
{
|
||||
@ -1064,6 +1089,9 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
|
||||
QgsFieldConstraints constraints = layer->fields().at( idx ).constraints();
|
||||
mConstraints = constraints.constraints();
|
||||
mConstraint = constraints.constraintExpression();
|
||||
mConstraintStrength.insert( QgsFieldConstraints::ConstraintNotNull, constraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
|
||||
mConstraintStrength.insert( QgsFieldConstraints::ConstraintUnique, constraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
|
||||
mConstraintStrength.insert( QgsFieldConstraints::ConstraintExpression, constraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
|
||||
mConstraintDescription = constraints.constraintDescription();
|
||||
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer, layer->fields().field( idx ).name() );
|
||||
mEditorWidgetType = setup.type();
|
||||
|
@ -123,6 +123,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
|
||||
bool mEditableEnabled;
|
||||
bool mLabelOnTop;
|
||||
QgsFieldConstraints::Constraints mConstraints;
|
||||
QHash< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > mConstraintStrength;
|
||||
QString mConstraint;
|
||||
QString mConstraintDescription;
|
||||
QPushButton* mButton;
|
||||
|
@ -317,6 +317,9 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
|
||||
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
|
||||
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
|
||||
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
|
||||
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
|
||||
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
|
||||
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
|
||||
out << field.constraints().constraintExpression();
|
||||
out << field.constraints().constraintDescription();
|
||||
out << static_cast< quint32 >( field.subType() );
|
||||
@ -325,10 +328,10 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
|
||||
|
||||
QDataStream& operator>>( QDataStream& in, QgsField& field )
|
||||
{
|
||||
quint32 type, subType, length, precision, constraints, originNotNull, originUnique, originExpression;
|
||||
quint32 type, subType, length, precision, constraints, originNotNull, originUnique, originExpression, strengthNotNull, strengthUnique, strengthExpression;
|
||||
QString name, typeName, comment, alias, defaultValueExpression, constraintExpression, constraintDescription;
|
||||
in >> name >> type >> typeName >> length >> precision >> comment >> alias
|
||||
>> defaultValueExpression >> constraints >> originNotNull >> originUnique >> originExpression >>
|
||||
>> defaultValueExpression >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
|
||||
constraintExpression >> constraintDescription >> subType;
|
||||
field.setName( name );
|
||||
field.setType( static_cast< QVariant::Type >( type ) );
|
||||
@ -340,15 +343,24 @@ QDataStream& operator>>( QDataStream& in, QgsField& field )
|
||||
field.setDefaultValueExpression( defaultValueExpression );
|
||||
QgsFieldConstraints fieldConstraints;
|
||||
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
|
||||
{
|
||||
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
|
||||
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
|
||||
}
|
||||
else
|
||||
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
|
||||
if ( constraints & QgsFieldConstraints::ConstraintUnique )
|
||||
{
|
||||
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
|
||||
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
|
||||
}
|
||||
else
|
||||
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintUnique );
|
||||
if ( constraints & QgsFieldConstraints::ConstraintExpression )
|
||||
{
|
||||
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
|
||||
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
|
||||
}
|
||||
else
|
||||
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintExpression );
|
||||
fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
|
||||
|
@ -29,17 +29,48 @@ QgsFieldConstraints::ConstraintOrigin QgsFieldConstraints::constraintOrigin( Qgs
|
||||
return mConstraintOrigins.value( constraint, ConstraintOriginNotSet );
|
||||
}
|
||||
|
||||
QgsFieldConstraints::ConstraintStrength QgsFieldConstraints::constraintStrength( QgsFieldConstraints::Constraint constraint ) const
|
||||
{
|
||||
if ( !( mConstraints & constraint ) )
|
||||
return ConstraintStrengthNotSet;
|
||||
|
||||
// defaults to hard strength unless explicitly set
|
||||
return mConstraintStrengths.value( constraint, ConstraintStrengthHard );
|
||||
}
|
||||
|
||||
void QgsFieldConstraints::setConstraintStrength( QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength )
|
||||
{
|
||||
if ( constraintOrigin( constraint ) == ConstraintOriginProvider )
|
||||
{
|
||||
// cannot be overwritten
|
||||
return;
|
||||
}
|
||||
else if ( strength == ConstraintStrengthNotSet )
|
||||
{
|
||||
mConstraintStrengths.remove( constraint );
|
||||
}
|
||||
else
|
||||
{
|
||||
mConstraintStrengths.insert( constraint, strength );
|
||||
}
|
||||
}
|
||||
|
||||
void QgsFieldConstraints::setConstraint( QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintOrigin origin )
|
||||
{
|
||||
if ( origin == ConstraintOriginNotSet )
|
||||
{
|
||||
mConstraints &= ~constraint;
|
||||
mConstraintOrigins.remove( constraint );
|
||||
mConstraintStrengths.remove( constraint );
|
||||
}
|
||||
else
|
||||
{
|
||||
mConstraints |= constraint;
|
||||
mConstraintOrigins.insert( constraint, origin );
|
||||
if ( !mConstraintStrengths.contains( constraint ) || origin == ConstraintOriginProvider )
|
||||
{
|
||||
mConstraintStrengths.insert( constraint, ConstraintStrengthHard );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,5 +96,6 @@ void QgsFieldConstraints::setConstraintExpression( const QString& expression, co
|
||||
bool QgsFieldConstraints::operator==( const QgsFieldConstraints& other ) const
|
||||
{
|
||||
return mConstraints == other.mConstraints && mConstraintOrigins == other.mConstraintOrigins
|
||||
&& mExpressionConstraint == other.mExpressionConstraint && mExpressionConstraintDescription == other.mExpressionConstraintDescription;
|
||||
&& mExpressionConstraint == other.mExpressionConstraint && mExpressionConstraintDescription == other.mExpressionConstraintDescription
|
||||
&& mConstraintStrengths == other.mConstraintStrengths;
|
||||
}
|
||||
|
@ -61,8 +61,9 @@ class CORE_EXPORT QgsFieldConstraints
|
||||
*/
|
||||
enum ConstraintStrength
|
||||
{
|
||||
ConstraintHard = 0, //!< Constraint must be honored before feature can be accepted
|
||||
ConstraintSoft, //!< User is warned if constraint is violated but feature can still be accepted
|
||||
ConstraintStrengthNotSet = 0, //!< Constraint is not set
|
||||
ConstraintStrengthHard, //!< Constraint must be honored before feature can be accepted
|
||||
ConstraintStrengthSoft, //!< User is warned if constraint is violated but feature can still be accepted
|
||||
};
|
||||
|
||||
/**
|
||||
@ -84,6 +85,22 @@ class CORE_EXPORT QgsFieldConstraints
|
||||
*/
|
||||
ConstraintOrigin constraintOrigin( Constraint constraint ) const;
|
||||
|
||||
/**
|
||||
* Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint
|
||||
* is not present on this field.
|
||||
* @see constraints()
|
||||
* @see setConstraintStrength()
|
||||
*/
|
||||
ConstraintStrength constraintStrength( Constraint constraint ) const;
|
||||
|
||||
/**
|
||||
* Sets the strength of a constraint. Note that the strength of constraints which originate
|
||||
* from a provider cannot be changed. Constraints default to ConstraintStrengthHard unless
|
||||
* explicitly changed.
|
||||
* @see constraintStrength()
|
||||
*/
|
||||
void setConstraintStrength( Constraint constraint, ConstraintStrength strength );
|
||||
|
||||
/**
|
||||
* Sets a constraint on the field.
|
||||
* @see constraints()
|
||||
@ -133,6 +150,9 @@ class CORE_EXPORT QgsFieldConstraints
|
||||
//! Origin of field constraints
|
||||
QHash< Constraint, ConstraintOrigin > mConstraintOrigins;
|
||||
|
||||
//! Strength of field constraints
|
||||
QHash< Constraint, ConstraintStrength > mConstraintStrengths;
|
||||
|
||||
//! Expression constraint
|
||||
QString mExpressionConstraint;
|
||||
|
||||
|
@ -1443,6 +1443,7 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
|
||||
|
||||
// constraints
|
||||
mFieldConstraints.clear();
|
||||
mFieldConstraintStrength.clear();
|
||||
QDomNode constraintsNode = layer_node.namedItem( "constraints" );
|
||||
if ( !constraintsNode.isNull() )
|
||||
{
|
||||
@ -1457,6 +1458,14 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
|
||||
continue;
|
||||
|
||||
mFieldConstraints.insert( field, static_cast< QgsFieldConstraints::Constraints >( constraints ) );
|
||||
|
||||
int uniqueStrength = constraintElem.attribute( "unique_strength", QString( "1" ) ).toInt();
|
||||
int notNullStrength = constraintElem.attribute( "notnull_strength", QString( "1" ) ).toInt();
|
||||
int expStrength = constraintElem.attribute( "exp_strength", QString( "1" ) ).toInt();
|
||||
|
||||
mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintUnique ), static_cast< QgsFieldConstraints::ConstraintStrength >( uniqueStrength ) );
|
||||
mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintNotNull ), static_cast< QgsFieldConstraints::ConstraintStrength >( notNullStrength ) );
|
||||
mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintExpression ), static_cast< QgsFieldConstraints::ConstraintStrength >( expStrength ) );
|
||||
}
|
||||
}
|
||||
mFieldConstraintExpressions.clear();
|
||||
@ -1691,6 +1700,9 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
|
||||
QDomElement constraintElem = document.createElement( "constraint" );
|
||||
constraintElem.setAttribute( "field", field.name() );
|
||||
constraintElem.setAttribute( "constraints", field.constraints().constraints() );
|
||||
constraintElem.setAttribute( "unique_strength", field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
|
||||
constraintElem.setAttribute( "notnull_strength", field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
|
||||
constraintElem.setAttribute( "exp_strength", field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
|
||||
constraintsElem.appendChild( constraintElem );
|
||||
}
|
||||
layer_node.appendChild( constraintsElem );
|
||||
@ -3000,6 +3012,23 @@ void QgsVectorLayer::updateFields()
|
||||
mFields[ index ].setConstraints( constraints );
|
||||
}
|
||||
|
||||
QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator constraintStrengthIt = mFieldConstraintStrength.constBegin();
|
||||
for ( ; constraintStrengthIt != mFieldConstraintStrength.constEnd(); ++constraintStrengthIt )
|
||||
{
|
||||
int index = mFields.lookupField( constraintStrengthIt.key().first );
|
||||
if ( index < 0 )
|
||||
continue;
|
||||
|
||||
QgsFieldConstraints constraints = mFields.at( index ).constraints();
|
||||
|
||||
// always keep provider constraints intact
|
||||
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintExpression ) == QgsFieldConstraints::ConstraintOriginProvider )
|
||||
continue;
|
||||
|
||||
constraints.setConstraintStrength( constraintStrengthIt.key().second, constraintStrengthIt.value() );
|
||||
mFields[ index ].setConstraints( constraints );
|
||||
}
|
||||
|
||||
if ( oldFields != mFields )
|
||||
{
|
||||
emit updatedFields();
|
||||
@ -4305,19 +4334,37 @@ QgsFieldConstraints::Constraints QgsVectorLayer::fieldConstraints( int fieldInde
|
||||
return constraints;
|
||||
}
|
||||
|
||||
void QgsVectorLayer::setFieldConstraints( int index, QgsFieldConstraints::Constraints constraints )
|
||||
void QgsVectorLayer::setFieldConstraint( int index, QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength )
|
||||
{
|
||||
if ( index < 0 || index >= mFields.count() )
|
||||
return;
|
||||
|
||||
if ( constraints == 0 )
|
||||
{
|
||||
mFieldConstraints.remove( mFields.at( index ).name() );
|
||||
}
|
||||
else
|
||||
{
|
||||
mFieldConstraints.insert( mFields.at( index ).name(), constraints );
|
||||
}
|
||||
QString name = mFields.at( index ).name();
|
||||
|
||||
// add constraint to existing constraints
|
||||
QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, 0 );
|
||||
constraints |= constraint;
|
||||
mFieldConstraints.insert( name, constraints );
|
||||
|
||||
mFieldConstraintStrength.insert( qMakePair( name, constraint ), strength );
|
||||
|
||||
updateFields();
|
||||
}
|
||||
|
||||
void QgsVectorLayer::removeFieldConstraint( int index, QgsFieldConstraints::Constraint constraint )
|
||||
{
|
||||
if ( index < 0 || index >= mFields.count() )
|
||||
return;
|
||||
|
||||
QString name = mFields.at( index ).name();
|
||||
|
||||
// remove constraint from existing constraints
|
||||
QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, 0 );
|
||||
constraints &= ~constraint;
|
||||
mFieldConstraints.insert( name, constraints );
|
||||
|
||||
mFieldConstraintStrength.remove( qMakePair( name, constraint ) );
|
||||
|
||||
updateFields();
|
||||
}
|
||||
|
||||
|
@ -1402,18 +1402,28 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
|
||||
* field index. These constraints may be inherited from the layer's data provider
|
||||
* or may be set manually on the vector layer from within QGIS.
|
||||
* @note added in QGIS 3.0
|
||||
* @see setFieldConstraints()
|
||||
* @see setFieldConstraint()
|
||||
*/
|
||||
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
|
||||
|
||||
/**
|
||||
* Sets the constraints for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be cleared. Ie, calling this method only allows for new
|
||||
* Sets a constraint for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be modified. Ie, calling this method only allows for new
|
||||
* constraints to be added on top of the existing provider constraints.
|
||||
* @note added in QGIS 3.0
|
||||
* @see fieldConstraints()
|
||||
* @see removeFieldConstraint()
|
||||
*/
|
||||
void setFieldConstraints( int index, QgsFieldConstraints::Constraints constraints );
|
||||
void setFieldConstraint( int index, QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthHard );
|
||||
|
||||
/**
|
||||
* Removes a constraint for a specified field index. Any constraints inherited from the layer's
|
||||
* data provider will be kept intact and cannot be removed.
|
||||
* @note added in QGIS 3.0
|
||||
* @see fieldConstraints()
|
||||
* @see setFieldConstraint()
|
||||
*/
|
||||
void removeFieldConstraint( int index, QgsFieldConstraints::Constraint constraint );
|
||||
|
||||
/**
|
||||
* Returns the constraint expression for for a specified field index, if set.
|
||||
@ -1983,6 +1993,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
|
||||
//! Map which stores constraints for fields
|
||||
QMap< QString, QgsFieldConstraints::Constraints > mFieldConstraints;
|
||||
|
||||
//! Map which stores constraint strength for fields
|
||||
QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength > mFieldConstraintStrength;
|
||||
|
||||
//! Map which stores expression constraints for fields. Value is a pair of expression/description.
|
||||
QMap< QString, QPair< QString, QString > > mFieldConstraintExpressions;
|
||||
|
||||
|
@ -266,7 +266,7 @@ void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomEle
|
||||
if ( ewv2CfgElem.attribute( QStringLiteral( "notNull" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) )
|
||||
{
|
||||
// upgrade from older config
|
||||
vectorLayer->setFieldConstraints( idx, vectorLayer->fieldConstraints( idx ) | QgsFieldConstraints::ConstraintNotNull );
|
||||
vectorLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintNotNull );
|
||||
}
|
||||
if ( !ewv2CfgElem.attribute( QStringLiteral( "constraint" ), QString() ).isEmpty() )
|
||||
{
|
||||
|
@ -22,58 +22,84 @@
|
||||
<property name="title">
|
||||
<string>Constraints</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="notNullCheckBox">
|
||||
<property name="text">
|
||||
<string>Not null</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="mUniqueCheckBox">
|
||||
<property name="text">
|
||||
<string>Unique</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
<item row="0" column="0">
|
||||
<widget class="QCheckBox" name="notNullCheckBox">
|
||||
<property name="text">
|
||||
<string>Not null</string>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Constraint</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true">
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::StrongFocus</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
<item row="1" column="1">
|
||||
<widget class="QCheckBox" name="mCheckBoxEnforceUnique">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Constraint description</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
|
||||
</item>
|
||||
</layout>
|
||||
<property name="toolTip">
|
||||
<string>Enforcing the unique constraint prevents committing features which do not meet the constraint. Unenforced constraints display a warning to users, but do not prevent committing the feature.</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Enforce unique constraint</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true">
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::StrongFocus</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Expression description</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QLineEdit" name="leConstraintExpressionDescription">
|
||||
<property name="toolTip">
|
||||
<string>Optional descriptive name for expression constraint</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QCheckBox" name="mCheckBoxEnforceNotNull">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Enforcing the not null constraint prevents committing features which do not meet the constraint. Unenforced constraints display a warning to users, but do not prevent committing the feature.</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Enforce not null constraint</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Expression</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QCheckBox" name="mCheckBoxEnforceExpression">
|
||||
<property name="toolTip">
|
||||
<string>Enforcing the expression constraint prevents committing features which do not meet the constraint. Unenforced constraints display a warning to users, but do not prevent committing the feature.</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Enforce expression constraint</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
@ -176,9 +202,12 @@
|
||||
<tabstop>labelOnTopCheckBox</tabstop>
|
||||
<tabstop>mExpressionWidget</tabstop>
|
||||
<tabstop>notNullCheckBox</tabstop>
|
||||
<tabstop>mCheckBoxEnforceNotNull</tabstop>
|
||||
<tabstop>mUniqueCheckBox</tabstop>
|
||||
<tabstop>mCheckBoxEnforceUnique</tabstop>
|
||||
<tabstop>constraintExpressionWidget</tabstop>
|
||||
<tabstop>leConstraintExpressionDescription</tabstop>
|
||||
<tabstop>mCheckBoxEnforceExpression</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
|
@ -87,6 +87,7 @@ void TestQgsField::copy()
|
||||
QgsFieldConstraints constraints;
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
|
||||
constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
original.setConstraints( constraints );
|
||||
QgsField copy( original );
|
||||
QVERIFY( copy == original );
|
||||
@ -102,6 +103,7 @@ void TestQgsField::assignment()
|
||||
QgsFieldConstraints constraints;
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
|
||||
constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
original.setConstraints( constraints );
|
||||
QgsField copy;
|
||||
copy = original;
|
||||
@ -153,6 +155,20 @@ void TestQgsField::gettersSetters()
|
||||
QCOMPARE( field.constraints().constraintExpression(), QStringLiteral( "constraint expression" ) );
|
||||
QCOMPARE( field.constraints().constraintDescription(), QStringLiteral( "description" ) );
|
||||
QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique | QgsFieldConstraints::ConstraintExpression ); //setting constraint expression should add constraint
|
||||
constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );
|
||||
|
||||
// check a constraint strength which hasn't been set
|
||||
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ), QgsFieldConstraints::ConstraintStrengthNotSet );
|
||||
// check a constraint strength which has not been explicitly set
|
||||
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
field.setConstraints( constraints );
|
||||
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
// try overwriting a provider constraint's strength
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
field.setConstraints( constraints );
|
||||
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard );
|
||||
}
|
||||
|
||||
void TestQgsField::isNumeric()
|
||||
@ -249,9 +265,14 @@ void TestQgsField::equality()
|
||||
field2.setConstraints( constraints );
|
||||
QVERIFY( !( field1 == field2 ) );
|
||||
QVERIFY( field1 != field2 );
|
||||
constraints = field2.constraints();
|
||||
constraints.setConstraintExpression( QString(), QString() );
|
||||
constraints = QgsFieldConstraints();
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard );
|
||||
field2.setConstraints( constraints );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
field1.setConstraints( constraints );
|
||||
QVERIFY( !( field1 == field2 ) );
|
||||
QVERIFY( field1 != field2 );
|
||||
}
|
||||
|
||||
void TestQgsField::asVariant()
|
||||
@ -442,6 +463,7 @@ void TestQgsField::dataStream()
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
|
||||
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginLayer );
|
||||
constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );
|
||||
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft );
|
||||
original.setConstraints( constraints );
|
||||
|
||||
QByteArray ba;
|
||||
|
@ -1782,37 +1782,51 @@ class TestQgsVectorLayer(unittest.TestCase):
|
||||
self.assertFalse(layer.fieldConstraints(1))
|
||||
self.assertFalse(layer.fieldConstraints(2))
|
||||
|
||||
layer.setFieldConstraints(0, QgsFieldConstraints.ConstraintNotNull)
|
||||
layer.setFieldConstraint(0, QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer.fieldConstraints(0), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertFalse(layer.fieldConstraints(1))
|
||||
self.assertFalse(layer.fieldConstraints(2))
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
|
||||
layer.setFieldConstraints(1, QgsFieldConstraints.ConstraintNotNull | QgsFieldConstraints.ConstraintUnique)
|
||||
layer.setFieldConstraint(1, QgsFieldConstraints.ConstraintNotNull)
|
||||
layer.setFieldConstraint(1, QgsFieldConstraints.ConstraintUnique)
|
||||
self.assertEqual(layer.fieldConstraints(0), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer.fieldConstraints(1), QgsFieldConstraints.ConstraintNotNull | QgsFieldConstraints.ConstraintUnique)
|
||||
self.assertFalse(layer.fieldConstraints(2))
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull | QgsFieldConstraints.ConstraintUnique)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintOrigin(QgsFieldConstraints.ConstraintUnique),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintStrength(QgsFieldConstraints.ConstraintUnique),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
|
||||
layer.setFieldConstraints(1, QgsFieldConstraints.Constraints())
|
||||
layer.removeFieldConstraint(1, QgsFieldConstraints.ConstraintNotNull)
|
||||
layer.removeFieldConstraint(1, QgsFieldConstraints.ConstraintUnique)
|
||||
self.assertEqual(layer.fieldConstraints(0), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertFalse(layer.fieldConstraints(1))
|
||||
self.assertFalse(layer.fieldConstraints(2))
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
self.assertFalse(layer.fields().at(1).constraints().constraints())
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginNotSet)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthNotSet)
|
||||
|
||||
def testSaveRestoreConstraints(self):
|
||||
""" test saving and restoring constraints from xml"""
|
||||
@ -1829,8 +1843,9 @@ class TestQgsVectorLayer(unittest.TestCase):
|
||||
self.assertFalse(layer2.fieldConstraints(1))
|
||||
|
||||
# set some constraints
|
||||
layer.setFieldConstraints(0, QgsFieldConstraints.ConstraintNotNull)
|
||||
layer.setFieldConstraints(1, QgsFieldConstraints.ConstraintNotNull | QgsFieldConstraints.ConstraintUnique)
|
||||
layer.setFieldConstraint(0, QgsFieldConstraints.ConstraintNotNull)
|
||||
layer.setFieldConstraint(1, QgsFieldConstraints.ConstraintNotNull, QgsFieldConstraints.ConstraintStrengthSoft)
|
||||
layer.setFieldConstraint(1, QgsFieldConstraints.ConstraintUnique)
|
||||
|
||||
doc = QDomDocument("testdoc")
|
||||
elem = doc.createElement("maplayer")
|
||||
@ -1843,11 +1858,17 @@ class TestQgsVectorLayer(unittest.TestCase):
|
||||
self.assertEqual(layer3.fields().at(0).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull)
|
||||
self.assertEqual(layer3.fields().at(0).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(0).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
self.assertEqual(layer3.fields().at(1).constraints().constraints(), QgsFieldConstraints.ConstraintNotNull | QgsFieldConstraints.ConstraintUnique)
|
||||
self.assertEqual(layer3.fields().at(1).constraints().constraintOrigin(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer3.fields().at(1).constraints().constraintOrigin(QgsFieldConstraints.ConstraintUnique),
|
||||
QgsFieldConstraints.ConstraintOriginLayer)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintStrength(QgsFieldConstraints.ConstraintNotNull),
|
||||
QgsFieldConstraints.ConstraintStrengthSoft)
|
||||
self.assertEqual(layer.fields().at(1).constraints().constraintStrength(QgsFieldConstraints.ConstraintUnique),
|
||||
QgsFieldConstraints.ConstraintStrengthHard)
|
||||
|
||||
def testGetSetConstraintExpressions(self):
|
||||
""" test getting and setting field constraint expressions """
|
||||
|
Loading…
x
Reference in New Issue
Block a user