Skip to content
Permalink
Browse files

Merge pull request #7206 from m-kuhn/fix18380

Fix loading layers within embedded groups
  • Loading branch information
m-kuhn committed Jun 11, 2018
2 parents 4e084b6 + c3747e9 commit 6e3ee464147b9658641984ce64e919d1fe917a05
Showing with 65 additions and 0 deletions.
  1. +1 −0 src/core/qgsproject.cpp
  2. +64 −0 tests/src/core/testqgslayertree.cpp
@@ -2151,6 +2151,7 @@ QgsLayerTreeGroup *QgsProject::createEmbeddedGroup( const QString &groupName, co
QgsLayerTreeLayer *layer = newGroup->findLayer( layerId );
if ( layer )
{
layer->resolveReferences( this );
layer->setItemVisibilityChecked( invisibleLayers.contains( layerId ) );
}
}
@@ -46,6 +46,7 @@ class TestQgsLayerTree : public QObject
void testLegendSymbolGraduated();
void testLegendSymbolRuleBased();
void testResolveReferences();
void testEmbeddedGroup();

private:

@@ -525,5 +526,68 @@ void TestQgsLayerTree::testRendererLegend( QgsFeatureRenderer *renderer )
}


void TestQgsLayerTree::testEmbeddedGroup()
{
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString layerPath = dataDir + QStringLiteral( "/points.shp" );

// build a project with 3 layers, each having a simple renderer with SVG marker
// - existing SVG file in project dir
// - existing SVG file in QGIS dir
// - non-exsiting SVG file

QTemporaryDir dir;
QVERIFY( dir.isValid() );
// on mac the returned path was not canonical and the resolver failed to convert paths properly
QString dirPath = QFileInfo( dir.path() ).canonicalFilePath();

QString projectFilename = dirPath + QStringLiteral( "/project.qgs" );

QgsVectorLayer *layer1 = new QgsVectorLayer( layerPath, QStringLiteral( "points 1" ), QStringLiteral( "ogr" ) );
QgsVectorLayer *layer2 = new QgsVectorLayer( layerPath, QStringLiteral( "points 2" ), QStringLiteral( "ogr" ) );
QgsVectorLayer *layer3 = new QgsVectorLayer( layerPath, QStringLiteral( "points 3" ), QStringLiteral( "ogr" ) );

QVERIFY( layer1->isValid() );

QgsProject project;
project.addMapLayers( QList<QgsMapLayer *>() << layer1 << layer2 << layer3, false );
QgsLayerTreeGroup *grp = project.layerTreeRoot()->addGroup( QStringLiteral( "Embed" ) );
grp->addLayer( layer1 );
grp->addLayer( layer2 );
grp->addLayer( layer3 );
project.write( projectFilename );

//
// now let's use the layer group embedded in another project...
//

QgsProject projectMaster;
QgsLayerTreeGroup *embeddedGroup = projectMaster.createEmbeddedGroup( grp->name(), projectFilename, QStringList() );
QVERIFY( embeddedGroup );
QCOMPARE( embeddedGroup->children().size(), 3 );

for ( QgsLayerTreeNode *child : embeddedGroup->children() )
{
QVERIFY( QgsLayerTree::toLayer( child )->layer() );
}
projectMaster.layerTreeRoot()->addChildNode( embeddedGroup );

QString projectMasterFilename = dirPath + QStringLiteral( "/projectMaster.qgs" );
projectMaster.write( projectMasterFilename );
projectMaster.clear();

QgsProject projectMasterCopy;
projectMasterCopy.read( projectMasterFilename );
QgsLayerTreeGroup *masterEmbeddedGroup = projectMasterCopy.layerTreeRoot()->findGroup( QStringLiteral( "Embed" ) );
QVERIFY( masterEmbeddedGroup );
QCOMPARE( masterEmbeddedGroup->children().size(), 3 );

for ( QgsLayerTreeNode *child : masterEmbeddedGroup->children() )
{
QVERIFY( QgsLayerTree::toLayer( child )->layer() );
}
}


QGSTEST_MAIN( TestQgsLayerTree )
#include "testqgslayertree.moc"

0 comments on commit 6e3ee46

Please sign in to comment.
You can’t perform that action at this time.