Skip to content

Commit

Permalink
Fix default value 'apply on update' setting not correctly restored
Browse files Browse the repository at this point in the history
Fixes #17712
  • Loading branch information
nyalldawson committed Jan 16, 2018
1 parent 749468a commit fe2292c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsdefaultvalue.sip
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ not set, the default value will only be used when a feature is created.
%End
public:

QgsDefaultValue( const QString &expression = QString(), bool applyOnUpdate = false );
explicit QgsDefaultValue( const QString &expression = QString(), bool applyOnUpdate = false );
%Docstring
Create a new default value with the given ``expression`` and ``applyOnUpdate`` flag.

Expand Down
1 change: 0 additions & 1 deletion src/app/qgsattributesformproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ void QgsAttributesFormProperties::loadAttributeTypeDialog()
mAttributeTypeDialog->setConstraintExpressionEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
mAttributeTypeDialog->setDefaultValueExpression( mLayer->defaultValueDefinition( index ).expression() );
mAttributeTypeDialog->setApplyDefaultValueOnUpdate( mLayer->defaultValueDefinition( index ).applyOnUpdate() );
//confustion (will be removed): das hier funktioniert nicht, es is neu, aber ich weiss nicht woher: mAttributeTypeDialog->setDefaultValueExpression( mLayer->defaultValueExpression( index ) );

mAttributeTypeDialog->setEditorWidgetConfig( cfg.mEditorWidgetConfig );
mAttributeTypeDialog->setEditorWidgetType( cfg.mEditorWidgetType );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdefaultvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CORE_EXPORT QgsDefaultValue
* Create a new default value with the given \a expression and \a applyOnUpdate flag.
* \see QgsVectorLayer::setDefaultValueDefinition
*/
QgsDefaultValue( const QString &expression = QString(), bool applyOnUpdate = false );
explicit QgsDefaultValue( const QString &expression = QString(), bool applyOnUpdate = false );
bool operator==( const QgsDefaultValue &other ) const;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3159,9 +3159,9 @@ void QgsVectorLayer::setDefaultValueDefinition( int index, const QgsDefaultValue
QgsDefaultValue QgsVectorLayer::defaultValueDefinition( int index ) const
{
if ( index < 0 || index >= mFields.count() )
return QString();
return QgsDefaultValue();
else
return mFields.at( index ).defaultValueDefinition().expression();
return mFields.at( index ).defaultValueDefinition();
}

QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const
Expand Down
8 changes: 4 additions & 4 deletions tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void TestQgsField::gettersSetters()
QCOMPARE( field.comment(), QString( "comment" ) );
field.setAlias( QStringLiteral( "alias" ) );
QCOMPARE( field.alias(), QString( "alias" ) );
field.setDefaultValueDefinition( QStringLiteral( "1+2" ) );
field.setDefaultValueDefinition( QgsDefaultValue( QStringLiteral( "1+2" ) ) );
QCOMPARE( field.defaultValueDefinition().expression(), QString( "1+2" ) );
QgsFieldConstraints constraints;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
Expand Down Expand Up @@ -244,10 +244,10 @@ void TestQgsField::equality()
QVERIFY( !( field1 == field2 ) );
QVERIFY( field1 != field2 );
field2.setAlias( QString() );
field2.setDefaultValueDefinition( QStringLiteral( "1+2" ) );
field2.setDefaultValueDefinition( QgsDefaultValue( QStringLiteral( "1+2" ) ) );
QVERIFY( !( field1 == field2 ) );
QVERIFY( field1 != field2 );
field2.setDefaultValueDefinition( QString() );
field2.setDefaultValueDefinition( QgsDefaultValue() );
constraints = field2.constraints();
constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
field2.setConstraints( constraints );
Expand Down Expand Up @@ -461,7 +461,7 @@ void TestQgsField::dataStream()
original.setTypeName( QStringLiteral( "typename1" ) );
original.setComment( QStringLiteral( "comment1" ) );
original.setAlias( QStringLiteral( "alias" ) );
original.setDefaultValueDefinition( QStringLiteral( "default" ) );
original.setDefaultValueDefinition( QgsDefaultValue( QStringLiteral( "default" ) ) );
QgsFieldConstraints constraints;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginLayer );
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,14 +2061,28 @@ def testGetSetDefaults(self):
layer.setDefaultValueDefinition(0, QgsDefaultValue("'test'"))
self.assertTrue(layer.defaultValueDefinition(0))
self.assertEqual(layer.defaultValueDefinition(0).expression(), "'test'")
self.assertFalse(layer.defaultValueDefinition(0).applyOnUpdate())
self.assertFalse(layer.defaultValueDefinition(1))
self.assertFalse(layer.defaultValueDefinition(1).applyOnUpdate())
self.assertFalse(layer.defaultValueDefinition(2))
self.assertFalse(layer.defaultValueDefinition(2).applyOnUpdate())
self.assertEqual(layer.fields().at(0).defaultValueDefinition().expression(), "'test'")

layer.setDefaultValueDefinition(1, QgsDefaultValue("2+2"))
self.assertEqual(layer.defaultValueDefinition(0).expression(), "'test'")
self.assertFalse(layer.defaultValueDefinition(0).applyOnUpdate())
self.assertEqual(layer.defaultValueDefinition(1).expression(), "2+2")
self.assertFalse(layer.defaultValueDefinition(1).applyOnUpdate())
self.assertFalse(layer.defaultValueDefinition(2))
self.assertFalse(layer.defaultValueDefinition(2).applyOnUpdate())
self.assertEqual(layer.fields().at(0).defaultValueDefinition().expression(), "'test'")
self.assertEqual(layer.fields().at(1).defaultValueDefinition().expression(), "2+2")

layer.setDefaultValueDefinition(1, QgsDefaultValue("2+2", True))
self.assertEqual(layer.defaultValueDefinition(0).expression(), "'test'")
self.assertFalse(layer.defaultValueDefinition(0).applyOnUpdate())
self.assertEqual(layer.defaultValueDefinition(1).expression(), "2+2")
self.assertTrue(layer.defaultValueDefinition(1).applyOnUpdate())
self.assertEqual(layer.fields().at(0).defaultValueDefinition().expression(), "'test'")
self.assertEqual(layer.fields().at(1).defaultValueDefinition().expression(), "2+2")

Expand Down
Binary file modified tests/testdata/polys_overlapping_with_id.dbf
Binary file not shown.

0 comments on commit fe2292c

Please sign in to comment.