Skip to content

Commit

Permalink
Fix value relation QLR loading bug
Browse files Browse the repository at this point in the history
Fixes #43978
  • Loading branch information
elpaso committed Jul 9, 2021
1 parent f083876 commit 48deba5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/core/qgslayerdefinition.cpp
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion tests/src/core/testqgslayerdefinition.cpp
Expand Up @@ -21,6 +21,7 @@

#include <qgsapplication.h>
#include <qgsproject.h>
#include <qgsvectorlayer.h>
#include <qgslayertree.h>
#include <qgslayerdefinition.h>

Expand All @@ -42,11 +43,17 @@ class TestQgsLayerDefinition: public QObject
void testFindLayers();

/**
* test that export does not crash: regression #18981
* test that export does not crash
* https://github.com/qgis/QGIS/issues/26812 - Save QLR crashes QGIS 3
*/
void testExportDoesNotCrash();

/**
* Test valueRelation in Attribute form wrong loaded from layer definition file (qlr)
* https://github.com/qgis/QGIS/issues/43978
*/
void testWidgetConfig();

private:
QTemporaryFile *mTempFile;
};
Expand Down Expand Up @@ -102,6 +109,16 @@ void TestQgsLayerDefinition::testExportDoesNotCrash()
testFindLayers();
}

void TestQgsLayerDefinition::testWidgetConfig()
{
const auto vl { static_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayersByName( QStringLiteral( "NewMemory" ) ).first() ) };
QVERIFY( vl );
const auto field { vl->fields().at( 0 ) };
const auto config { field.editorWidgetSetup().config() };
QCOMPARE( config[ QStringLiteral( "Description" ) ].toString(), QString() );
QCOMPARE( config[ QStringLiteral( "FilterExpression" ) ].toString(), QString() );
}



QGSTEST_MAIN( TestQgsLayerDefinition )
Expand Down
28 changes: 25 additions & 3 deletions tests/testdata/bug_18981_broken.qlr
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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/>
Expand Down

0 comments on commit 48deba5

Please sign in to comment.