Skip to content
Permalink
Browse files

Respect relative paths in embedded projects (fixes #16355)

  • Loading branch information
wonder-sk committed May 13, 2017
1 parent 8ffd91e commit 3e18cc15f8565ac74c7b2570ba57a60c2970cdb8
Showing with 28 additions and 6 deletions.
  1. +9 −5 src/core/qgsproject.cpp
  2. +1 −1 src/core/qgsproject.h
  3. +18 −0 tests/src/core/testqgsproject.cpp
@@ -686,7 +686,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
}
else
{
if ( !addLayer( element, brokenNodes ) )
if ( !addLayer( element, brokenNodes, pathResolver() ) )
{
returnStatus = false;
}
@@ -698,7 +698,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
return returnStatus;
}

bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes )
bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsPathResolver &pathResolver )
{
QString type = layerElem.attribute( QStringLiteral( "type" ) );
QgsDebugMsgLevel( "Layer type is " + type, 4 );
@@ -728,7 +728,7 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
Q_CHECK_PTR( mapLayer ); // NOLINT

// have the layer restore state that is stored in Dom node
if ( mapLayer->readLayerXml( layerElem, pathResolver() ) && mapLayer->isValid() )
if ( mapLayer->readLayerXml( layerElem, pathResolver ) && mapLayer->isValid() )
{
emit readMapLayer( mapLayer, layerElem );

@@ -1190,7 +1190,7 @@ void QgsProject::cleanTransactionGroups( bool force )
bool QgsProject::readLayer( const QDomNode &layerNode )
{
QList<QDomNode> brokenNodes;
if ( addLayer( layerNode.toElement(), brokenNodes ) )
if ( addLayer( layerNode.toElement(), brokenNodes, pathResolver() ) )
{
// have to try to update joins for all layers now - a previously added layer may be dependent on this newly
// added layer for joins
@@ -1720,6 +1720,10 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
}
}

QgsPathResolver embeddedPathResolver;
if ( !useAbsolutePaths )
embeddedPathResolver = QgsPathResolver( projectFilePath );

QDomElement projectLayersElem = sProjectDocument.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) );
if ( projectLayersElem.isNull() )
{
@@ -1811,7 +1815,7 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
dsElem.appendChild( sProjectDocument.createTextNode( datasource ) );
}

if ( addLayer( mapLayerElem, brokenNodes ) )
if ( addLayer( mapLayerElem, brokenNodes, embeddedPathResolver ) )
{
return true;
}
@@ -996,7 +996,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

//! Creates layer and adds it to maplayer registry
//! \note not available in Python bindings
bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes ) SIP_SKIP;
bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsPathResolver &pathResolver ) SIP_SKIP;

//! \note not available in Python bindings
void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
@@ -232,6 +232,24 @@ void TestQgsProject::testPathResolverSvg()
QCOMPARE( svg1, ourSvgPath );
QCOMPARE( svg2, invalidSvgPath );
QCOMPARE( svg3, librarySvgPath );

//
// now let's use these layers in embedded in another project...
//

QList<QDomNode> brokenNodes;
QgsProject projectMaster;
QVERIFY( projectMaster.createEmbeddedLayer( layer1->id(), projectFilename, brokenNodes ) );
QVERIFY( projectMaster.createEmbeddedLayer( layer2->id(), projectFilename, brokenNodes ) );
QVERIFY( projectMaster.createEmbeddedLayer( layer3->id(), projectFilename, brokenNodes ) );

QString svg1x = _getLayerSvgMarkerPath( projectMaster, "points 1" );
QString svg2x = _getLayerSvgMarkerPath( projectLoaded, "points 2" );
QString svg3x = _getLayerSvgMarkerPath( projectLoaded, "points 3" );
QCOMPARE( svg1x, ourSvgPath );
QCOMPARE( svg2x, invalidSvgPath );
QCOMPARE( svg3x, librarySvgPath );

}


0 comments on commit 3e18cc1

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