Skip to content
Permalink
Browse files
Merge pull request #43993 from elpaso/bugfix-gh43978-value-relation-q…
…lr-load

Fix value relation QLR loading bug
  • Loading branch information
elpaso committed Jul 11, 2021
2 parents df60454 + 5428063 commit aa64b29a5d7cc9bbf63d4481e08db99fa933b468
Showing with 130 additions and 9 deletions.
  1. +5 −6 src/core/qgslayerdefinition.cpp
  2. +100 −0 tests/src/python/test_qgslayerdefinition.py
  3. +25 −3 tests/testdata/bug_18981_broken.qlr
@@ -95,14 +95,13 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
// IDs of layers should be changed otherwise we may have more then one layer with the same id
// We have to replace the IDs before we load them because it's too late once they are loaded
const QDomNodeList treeLayerNodes = doc.elementsByTagName( QStringLiteral( "layer-tree-layer" ) );
QDomNode treeLayerNode = treeLayerNodes.at( 0 );
for ( int i = 0; ! treeLayerNode.isNull(); ++i )
for ( int i = 0; i < treeLayerNodes.length(); ++i )
{
treeLayerNode = treeLayerNodes.at( i );
const QDomNode treeLayerNode = treeLayerNodes.item( i );
QDomElement treeLayerElem = treeLayerNode.toElement();
QString oldid = treeLayerElem.attribute( QStringLiteral( "id" ) );
QString layername = treeLayerElem.attribute( QStringLiteral( "name" ) );
QString newid = QgsMapLayer::generateId( layername );
const QString oldid = treeLayerElem.attribute( QStringLiteral( "id" ) );
const QString layername = treeLayerElem.attribute( QStringLiteral( "name" ) );
const QString newid = QgsMapLayer::generateId( layername );
treeLayerElem.setAttribute( QStringLiteral( "id" ), newid );

// Replace IDs for map layers
@@ -25,6 +25,7 @@
from utilities import unitTestDataPath

from qgis.PyQt.QtXml import QDomDocument
from qgis.PyQt.QtCore import QTemporaryDir

start_app()
TEST_DATA_DIR = unitTestDataPath()
@@ -163,6 +164,105 @@ def test_path_storage(self):
lines = f.readlines()
self.assertIn(f'source="{gpkg_path}"', '\n'.join(lines))

def testWidgetConfig(self):

temp = QTemporaryDir()
temp_path = temp.path()
temp_qlr = os.path.join(temp_path, 'widget_config.qlr')

qlr = """<!DOCTYPE qgis-layer-definition>
<qlr>
<layer-tree-group expanded="1" checked="Qt::Checked" name="">
<customproperties/>
<layer-tree-group name="group">
<customproperties/>
<layer-tree-layer providerKey="memory" name="OldMemory" source="NoGeometry?crs=EPSG:4326&amp;field=BA_ART_LAT:string&amp;uid={908cbaab-cabe-4bd9-9058-2b80ebf87a72}" id="OldMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd" />
<layer-tree-layer providerKey="memory" name="NewMemory" source="NoGeometry?crs=EPSG:4326&amp;field=BA_ART_LAT:string&amp;uid={64c9bcbf-cabe-4bd9-9058-2b80ebf87a72}" id="NewMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd" />
<customproperties/>
</layer-tree-group>
</layer-tree-group>
<maplayers>
<maplayer type="vector" autoRefreshTime="0" geometry="No geometry">
<id>OldMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd</id>
<datasource>memory?geometry=NoGeometry&amp;field=BA_ART_LAT:string&amp;crs=EPSG:4326</datasource>
<keywordList>
<value></value>
</keywordList>
<layername>OldMemory</layername>
<srs>
<spatialrefsys>
<proj4>+proj=longlat +datum=WGS84 +no_defs</proj4>
<srsid>3452</srsid>
<srid>4326</srid>
<authid>EPSG:4326</authid>
<description>WGS 84</description>
<projectionacronym>longlat</projectionacronym>
<ellipsoidacronym>WGS84</ellipsoidacronym>
<geographicflag>true</geographicflag>
</spatialrefsys>
</srs>
<provider encoding="UTF-8">memory</provider>
</maplayer>
<maplayer type="vector"geometry="No geometry">
<id>NewMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd</id>
<datasource>memory?geometry=NoGeometry&amp;field=BA_ART_LAT:string&amp;crs=EPSG:4326</datasource>
<layername>NewMemory</layername>
<srs>
<spatialrefsys>
<proj4>+proj=longlat +datum=WGS84 +no_defs</proj4>
<srsid>3452</srsid>
<srid>4326</srid>
<authid>EPSG:4326</authid>
<description>WGS 84</description>
<projectionacronym>longlat</projectionacronym>
<ellipsoidacronym>WGS84</ellipsoidacronym>
<geographicflag>true</geographicflag>
</spatialrefsys>
</srs>
<provider encoding="UTF-8">memory</provider>
<fieldConfiguration>
<field name="BA_ART_LAT" configurationFlags="None">
<editWidget type="ValueRelation">
<config>
<Option type="Map">
<Option value="false" name="AllowMulti" type="bool"/>
<Option value="false" name="AllowNull" type="bool"/>
<Option value="" name="Description" type="QString"/>
<Option value="" name="FilterExpression" type="QString"/>
<Option value="Wert" name="Key" type="QString"/>
<Option value="OldMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd" name="Layer" type="QString"/>
<Option value="SL_BA_ART_LAT" name="LayerName" type="QString"/>
<Option value="ogr" name="LayerProviderName" type="QString"/>
<Option value="NoGeometry?crs=EPSG:4326&amp;field=BA_ART_LAT:string&amp;uid={908cbaab-cabe-4bd9-9058-2b80ebf87a72}" name="LayerSource" type="QString"/>
<Option value="1" name="NofColumns" type="int"/>
<Option value="false" name="OrderByValue" type="bool"/>
<Option value="false" name="UseCompleter" type="bool"/>
<Option value="Bezeichnung" name="Value" type="QString"/>
</Option>
</config>
</editWidget>
</field>
</fieldConfiguration>
</maplayer>
</maplayers>
</qlr>
"""

with open(temp_qlr, 'w+') as f:
f.write(qlr)

(result, errMsg) = QgsLayerDefinition.loadLayerDefinition(temp_qlr, QgsProject.instance(), QgsProject.instance().layerTreeRoot())
self.assertTrue(result)
self.assertFalse(errMsg)

vl = QgsProject.instance().mapLayersByName('NewMemory')[0]
field = vl.fields().at(0)
config = field.editorWidgetSetup().config()

self.assertEqual(config['Description'], '')
self.assertEqual(config['FilterExpression'], '')


if __name__ == '__main__':
unittest.main()
@@ -9,7 +9,7 @@
<layer-tree-layer expanded="1" providerKey="wms" checked="Qt::Checked" id="DTK_D8502016120170201702017020170201702017201801241314424672312" source="IgnoreGetMapUrl=1&amp;contextualWMSLegend=0&amp;crs=EPSG:25832&amp;dpiMode=7&amp;featureCount=10&amp;format=image/png&amp;layers=dtk_d850&amp;styles=&amp;url=http://kortforsyningen.kms.dk/service?servicename%3Dtopo_basis%26client%3DQGIS%26version%3D1.1.1%26login%3Dj%26password%3Dj" name="DTK/D850">
<customproperties/>
</layer-tree-layer>
<layer-tree-layer providerKey="memory" checked="Qt::Checked" expanded="1" name="NewMemory" source="NoGeometry?crs=EPSG:4326&amp;uid={64c9bcbf-cabe-4bd9-9058-2b80ebf87a72}" id="NewMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd">
<layer-tree-layer providerKey="memory" checked="Qt::Checked" expanded="1" name="NewMemory" source="NoGeometry?crs=EPSG:4326&amp;field=BA_ART_LAT:string&amp;uid={64c9bcbf-cabe-4bd9-9058-2b80ebf87a72}" id="NewMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd">
<customproperties/>
</layer-tree-layer>
</layer-tree-group>
@@ -63,7 +63,7 @@
</maplayer>
<maplayer refreshOnNotifyEnabled="0" readOnly="0" minScale="1e+8" type="vector" autoRefreshTime="0" hasScaleBasedVisibilityFlag="0" geometry="No geometry" autoRefreshEnabled="0" refreshOnNotifyMessage="" maxScale="0">
<id>NewMemory_ffa4d8a4_e5be_46a9_a0e0_fb6ee924f3cd</id>
<datasource>memory?geometry=NoGeometry&amp;crs=EPSG:4326</datasource>
<datasource>memory?geometry=NoGeometry&amp;field=BA_ART_LAT:string&amp;crs=EPSG:4326</datasource>
<keywordList>
<value></value>
</keywordList>
@@ -115,7 +115,29 @@
<map-layer-style name="predefinito"/>
</map-layer-style-manager>
<auxiliaryLayer/>
<fieldConfiguration/>
<fieldConfiguration>
<field name="BA_ART_LAT" configurationFlags="None">
<editWidget type="ValueRelation">
<config>
<Option type="Map">
<Option value="false" name="AllowMulti" type="bool"/>
<Option value="false" name="AllowNull" type="bool"/>
<Option value="" name="Description" type="QString"/>
<Option value="" name="FilterExpression" type="QString"/>
<Option value="Wert" name="Key" type="QString"/>
<Option value="SL_BA_ART_LAT_c020f946_145a_485c_b7a5_70fede18d31c" name="Layer" type="QString"/>
<Option value="SL_BA_ART_LAT" name="LayerName" type="QString"/>
<Option value="ogr" name="LayerProviderName" type="QString"/>
<Option value="D:/z_VB-BW/Bearbeitung/20201207_Datenmodell/BA_DAT_test.gpkg|layername=SL_BA_ART_LAT" name="LayerSource" type="QString"/>
<Option value="1" name="NofColumns" type="int"/>
<Option value="false" name="OrderByValue" type="bool"/>
<Option value="false" name="UseCompleter" type="bool"/>
<Option value="Bezeichnung" name="Value" type="QString"/>
</Option>
</config>
</editWidget>
</field>
</fieldConfiguration>
<aliases/>
<excludeAttributesWMS/>
<excludeAttributesWFS/>

0 comments on commit aa64b29

Please sign in to comment.