Skip to content

Commit

Permalink
Allow the strength of provider constraints to be set to soft
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Mar 13, 2019
1 parent c458a53 commit 80b2085
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 72 deletions.
3 changes: 1 addition & 2 deletions python/core/auto_generated/qgsfieldconstraints.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ is not present on this field.

void setConstraintStrength( Constraint constraint, ConstraintStrength strength );
%Docstring
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
Sets the strength of a constraint. Constraints default to ConstraintStrengthHard unless
explicitly changed.

.. seealso:: :py:func:`constraintStrength`
Expand Down
96 changes: 46 additions & 50 deletions src/app/qgsattributesformproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,17 @@ void QgsAttributesFormProperties::loadAttributeTypeDialog()
delete mAttributeTypeDialog;
mAttributeTypeDialog = new QgsAttributeTypeDialog( mLayer, index, mAttributeTypeFrame );

QgsFieldConstraints constraints = cfg.mFieldConstraints;

mAttributeTypeDialog->setAlias( cfg.mAlias );
mAttributeTypeDialog->setComment( cfg.mComment );
mAttributeTypeDialog->setFieldEditable( cfg.mEditable );
mAttributeTypeDialog->setLabelOnTop( cfg.mLabelOnTop );
mAttributeTypeDialog->setNotNull( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull );
mAttributeTypeDialog->setNotNullEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setUnique( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique );
mAttributeTypeDialog->setUniqueEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setNotNull( constraints.constraints() & QgsFieldConstraints::ConstraintNotNull );
mAttributeTypeDialog->setNotNullEnforced( constraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setUnique( constraints.constraints() & QgsFieldConstraints::ConstraintUnique );
mAttributeTypeDialog->setUniqueEnforced( constraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) == QgsFieldConstraints::ConstraintStrengthHard );

QgsFieldConstraints constraints = cfg.mFieldConstraints;
QgsFieldConstraints::Constraints providerConstraints = nullptr;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintNotNull;
Expand All @@ -258,9 +259,9 @@ void QgsAttributesFormProperties::loadAttributeTypeDialog()
providerConstraints |= QgsFieldConstraints::ConstraintExpression;
mAttributeTypeDialog->setProviderConstraints( providerConstraints );

mAttributeTypeDialog->setConstraintExpression( cfg.mConstraint );
mAttributeTypeDialog->setConstraintExpressionDescription( cfg.mConstraintDescription );
mAttributeTypeDialog->setConstraintExpressionEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setConstraintExpression( constraints.constraintExpression() );
mAttributeTypeDialog->setConstraintExpressionDescription( constraints.constraintDescription() );
mAttributeTypeDialog->setConstraintExpressionEnforced( constraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setDefaultValueExpression( mLayer->defaultValueDefinition( index ).expression() );
mAttributeTypeDialog->setApplyDefaultValueOnUpdate( mLayer->defaultValueDefinition( index ).applyOnUpdate() );

Expand All @@ -286,54 +287,54 @@ void QgsAttributesFormProperties::storeAttributeTypeDialog()
cfg.mLabelOnTop = mAttributeTypeDialog->labelOnTop();
cfg.mAlias = mAttributeTypeDialog->alias();

//confustion (will be removed): wir laden teilweise sachen einfach beim store anstelle des applys auf die mLayer - eingie Sachen laden wir auch vom layer anstatt über das cfg. wieso
QgsFieldConstraints constraints = mLayer->fields().at( mAttributeTypeDialog->fieldIdx() ).constraints();
QgsFieldConstraints::Constraints providerConstraints = nullptr;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintNotNull;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintUnique ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintUnique;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintExpression ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintExpression;
cfg.mConstraints = nullptr;
if ( mAttributeTypeDialog->notNull() && !( providerConstraints & QgsFieldConstraints::ConstraintNotNull ) )
QgsFieldConstraints constraints;
if ( mAttributeTypeDialog->notNull() )
{
cfg.mConstraints |= QgsFieldConstraints::ConstraintNotNull;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull );
}
if ( mAttributeTypeDialog->unique() && !( providerConstraints & QgsFieldConstraints::ConstraintUnique ) )
else if ( mAttributeTypeDialog->notNullFromProvider() )
{
cfg.mConstraints |= QgsFieldConstraints::ConstraintUnique;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
}
if ( !mAttributeTypeDialog->constraintExpression().isEmpty() && !( providerConstraints & QgsFieldConstraints::ConstraintExpression ) )

if ( mAttributeTypeDialog->unique() )
{
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
}
else if ( mAttributeTypeDialog->uniqueFromProvider() )
{
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
}

if ( !mAttributeTypeDialog->constraintExpression().isEmpty() )
{
cfg.mConstraints |= QgsFieldConstraints::ConstraintExpression;
constraints.setConstraint( QgsFieldConstraints::ConstraintExpression );
}

cfg.mConstraintDescription = mAttributeTypeDialog->constraintExpressionDescription();
cfg.mConstraint = mAttributeTypeDialog->constraintExpression();
constraints.setConstraintExpression( mAttributeTypeDialog->constraintExpressionDescription(), mAttributeTypeDialog->constraintExpression() );

constraints.setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, mAttributeTypeDialog->notNullEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, mAttributeTypeDialog->uniqueEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, mAttributeTypeDialog->constraintExpressionEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );

cfg.mFieldConstraints = constraints;

mLayer->setDefaultValueDefinition( mAttributeTypeDialog->fieldIdx(), QgsDefaultValue( mAttributeTypeDialog->defaultValueExpression(), mAttributeTypeDialog->applyDefaultValueOnUpdate() ) );

cfg.mEditorWidgetType = mAttributeTypeDialog->editorWidgetType();
cfg.mEditorWidgetConfig = mAttributeTypeDialog->editorWidgetConfig();

cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintNotNull, mAttributeTypeDialog->notNullEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintUnique, mAttributeTypeDialog->uniqueEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );
cfg.mConstraintStrength.insert( QgsFieldConstraints::ConstraintExpression, mAttributeTypeDialog->constraintExpressionEnforced() ?
QgsFieldConstraints::ConstraintStrengthHard : QgsFieldConstraints::ConstraintStrengthSoft );

QString fieldName = mLayer->fields().at( mAttributeTypeDialog->fieldIdx() ).name();

QTreeWidgetItemIterator itemIt( mAvailableWidgetsTree );
while ( *itemIt )
for ( auto itemIt = QTreeWidgetItemIterator( mAvailableWidgetsTree ); *itemIt; ++itemIt )
{
QTreeWidgetItem *item = *itemIt;
if ( item->data( 0, FieldNameRole ).toString() == fieldName )
item->setData( 0, FieldConfigRole, QVariant::fromValue<FieldConfig>( cfg ) );
++itemIt;
}

}


Expand Down Expand Up @@ -693,28 +694,29 @@ void QgsAttributesFormProperties::apply()

editFormConfig.setReadOnly( idx, !cfg.mEditable );
editFormConfig.setLabelOnTop( idx, cfg.mLabelOnTop );
mLayer->setConstraintExpression( idx, cfg.mConstraint, cfg.mConstraintDescription );
mLayer->setEditorWidgetSetup( idx, QgsEditorWidgetSetup( cfg.mEditorWidgetType, cfg.mEditorWidgetConfig ) );

if ( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull )
QgsFieldConstraints constraints = cfg.mFieldConstraints;
mLayer->setConstraintExpression( idx, constraints.constraintExpression(), constraints.constraintDescription() );
if ( constraints.constraints() & QgsFieldConstraints::ConstraintNotNull )
{
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintNotNull, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintStrengthHard ) );
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintNotNull, constraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
}
else
{
mLayer->removeFieldConstraint( idx, QgsFieldConstraints::ConstraintNotNull );
}
if ( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique )
if ( constraints.constraints() & QgsFieldConstraints::ConstraintUnique )
{
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintUnique, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard ) );
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintUnique, constraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
}
else
{
mLayer->removeFieldConstraint( idx, QgsFieldConstraints::ConstraintUnique );
}
if ( cfg.mConstraints & QgsFieldConstraints::ConstraintExpression )
if ( constraints.constraints() & QgsFieldConstraints::ConstraintExpression )
{
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintExpression, cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) );
mLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintExpression, constraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
}
else
{
Expand Down Expand Up @@ -777,12 +779,6 @@ QgsAttributesFormProperties::FieldConfig::FieldConfig( QgsVectorLayer *layer, in
&& layer->fields().fieldOrigin( idx ) != QgsFields::OriginExpression;
mLabelOnTop = layer->editFormConfig().labelOnTop( idx );
mFieldConstraints = layer->fields().at( idx ).constraints();
mConstraints = mFieldConstraints.constraints();
mConstraint = mFieldConstraints.constraintExpression();
mConstraintStrength.insert( QgsFieldConstraints::ConstraintNotNull, mFieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
mConstraintStrength.insert( QgsFieldConstraints::ConstraintUnique, mFieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
mConstraintStrength.insert( QgsFieldConstraints::ConstraintExpression, mFieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
mConstraintDescription = mFieldConstraints.constraintDescription();
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( layer, layer->fields().field( idx ).name() );
mEditorWidgetType = setup.type();
mEditorWidgetConfig = setup.config();
Expand Down
4 changes: 0 additions & 4 deletions src/app/qgsattributesformproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
bool mEditableEnabled = true ;
bool mLabelOnTop = false ;
QgsFieldConstraints mFieldConstraints;
QgsFieldConstraints::Constraints mConstraints = nullptr;
QHash< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > mConstraintStrength;
QString mConstraint;
QString mConstraintDescription;
QPushButton *mButton = nullptr;
QString mEditorWidgetType;
QMap<QString, QVariant> mEditorWidgetConfig;
Expand Down
18 changes: 12 additions & 6 deletions src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,13 @@ 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 )
{
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 );
}
}

Expand All @@ -230,7 +226,12 @@ QString QgsAttributeTypeDialog::constraintExpressionDescription()

bool QgsAttributeTypeDialog::notNull() const
{
return notNullCheckBox->isChecked();
return notNullCheckBox->isEnabled() && notNullCheckBox->isChecked();
}

bool QgsAttributeTypeDialog::notNullFromProvider() const
{
return ( !notNullCheckBox->isEnabled() ) && notNullCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setNotNullEnforced( bool enforced )
Expand All @@ -250,7 +251,12 @@ void QgsAttributeTypeDialog::setUnique( bool unique )

bool QgsAttributeTypeDialog::unique() const
{
return mUniqueCheckBox->isChecked();
return mUniqueCheckBox->isEnabled() && mUniqueCheckBox->isChecked();
}

bool QgsAttributeTypeDialog::uniqueFromProvider() const
{
return ( !mUniqueCheckBox->isEnabled() ) && mUniqueCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setUniqueEnforced( bool enforced )
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsattributetypedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QWidget, private Ui::QgsAttribut
*/
bool notNull() const;

/**
* Returns whether the not null constraint is set by the provider
*/
bool notNullFromProvider() const;

/**
* Returns whether the unique constraint is set by the provider
*/
bool uniqueFromProvider() const;

/**
* Sets whether the not null constraint is enforced.
*/
Expand Down
9 changes: 2 additions & 7 deletions src/core/qgsfieldconstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ QgsFieldConstraints::ConstraintStrength QgsFieldConstraints::constraintStrength(

void QgsFieldConstraints::setConstraintStrength( QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength )
{
if ( constraintOrigin( constraint ) == ConstraintOriginProvider )
{
// cannot be overwritten
return;
}
else if ( strength == ConstraintStrengthNotSet )
if ( strength == ConstraintStrengthNotSet )
{
mConstraintStrengths.remove( constraint );
}
Expand All @@ -62,7 +57,7 @@ void QgsFieldConstraints::setConstraint( QgsFieldConstraints::Constraint constra
{
mConstraints |= constraint;
mConstraintOrigins.insert( constraint, origin );
if ( !mConstraintStrengths.contains( constraint ) || origin == ConstraintOriginProvider )
if ( !mConstraintStrengths.contains( constraint ) )
{
mConstraintStrengths.insert( constraint, ConstraintStrengthHard );
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsfieldconstraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class CORE_EXPORT QgsFieldConstraints
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
* Sets the strength of a constraint. Constraints default to ConstraintStrengthHard unless
* explicitly changed.
* \see constraintStrength()
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void TestQgsField::gettersSetters()
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
field.setConstraints( constraints );
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard );
QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthSoft );
}

void TestQgsField::isNumeric()
Expand Down

0 comments on commit 80b2085

Please sign in to comment.