Skip to content
Permalink
Browse files
Correctly copy provider default editor widget config over to layer ed…
…itor widget config

so that these values aren't lost whenever the layer properties are
modified and layer fields are updated
  • Loading branch information
nyalldawson committed Apr 30, 2021
1 parent a762302 commit b3ef0ccf29817b517f3f51f5b85f9415179db414
Showing with 17 additions and 0 deletions.
  1. +12 −0 src/core/vector/qgsvectorlayer.cpp
  2. +5 −0 tests/src/python/test_provider_ogr.py
@@ -1792,6 +1792,18 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv
// get and store the feature type
mWkbType = mDataProvider->wkbType();

// before we update the layer fields from the provider, we first copy any default set alias and
// editor widget config from the data provider fields, if present
const QgsFields providerFields = mDataProvider->fields();
for ( const QgsField &field : providerFields )
{
// we only copy defaults from the provider if we aren't overriding any configuration made in the layer
if ( !field.editorWidgetSetup().isNull() && mFieldWidgetSetups.value( field.name() ).isNull() )
{
mFieldWidgetSetups[ field.name() ] = field.editorWidgetSetup();
}
}

if ( profile )
profile->switchTask( tr( "Read layer fields" ) );
updateFields();
@@ -1251,6 +1251,8 @@ def testFieldDomains(self):
self.assertEqual(range_int_setup.config()['Precision'], 0)
self.assertEqual(range_int_setup.config()['Step'], 1)
self.assertEqual(range_int_setup.config()['Style'], 'SpinBox')
# make sure editor widget config from provider has been copied to layer!
self.assertEqual(vl.editorWidgetSetup(fields.lookupField('with_range_domain_int')).type(), 'Range')

range_int64_field = fields[fields.lookupField('with_range_domain_int64')]
range_int64_setup = range_int64_field.editorWidgetSetup()
@@ -1261,6 +1263,7 @@ def testFieldDomains(self):
self.assertEqual(range_int64_setup.config()['Precision'], 0)
self.assertEqual(range_int64_setup.config()['Step'], 1)
self.assertEqual(range_int64_setup.config()['Style'], 'SpinBox')
self.assertEqual(vl.editorWidgetSetup(fields.lookupField('with_range_domain_int64')).type(), 'Range')

range_real_field = fields[fields.lookupField('with_range_domain_real')]
range_real_setup = range_real_field.editorWidgetSetup()
@@ -1271,11 +1274,13 @@ def testFieldDomains(self):
self.assertEqual(range_real_setup.config()['Precision'], 0)
self.assertEqual(range_real_setup.config()['Step'], 1)
self.assertEqual(range_real_setup.config()['Style'], 'SpinBox')
self.assertEqual(vl.editorWidgetSetup(fields.lookupField('with_range_domain_real')).type(), 'Range')

enum_field = fields[fields.lookupField('with_enum_domain')]
enum_setup = enum_field.editorWidgetSetup()
self.assertEqual(enum_setup.type(), 'ValueMap')
self.assertTrue(enum_setup.config()['map'], [{'one': '1'}, {'2': '2'}])
self.assertEqual(vl.editorWidgetSetup(fields.lookupField('with_enum_domain')).type(), 'ValueMap')


if __name__ == '__main__':

0 comments on commit b3ef0cc

Please sign in to comment.