Skip to content

Commit 3e18cc1

Browse files
committedMay 13, 2017
Respect relative paths in embedded projects (fixes #16355)
1 parent 8ffd91e commit 3e18cc1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed
 

‎src/core/qgsproject.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
686686
}
687687
else
688688
{
689-
if ( !addLayer( element, brokenNodes ) )
689+
if ( !addLayer( element, brokenNodes, pathResolver() ) )
690690
{
691691
returnStatus = false;
692692
}
@@ -698,7 +698,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
698698
return returnStatus;
699699
}
700700

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

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

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

1723+
QgsPathResolver embeddedPathResolver;
1724+
if ( !useAbsolutePaths )
1725+
embeddedPathResolver = QgsPathResolver( projectFilePath );
1726+
17231727
QDomElement projectLayersElem = sProjectDocument.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) );
17241728
if ( projectLayersElem.isNull() )
17251729
{
@@ -1811,7 +1815,7 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
18111815
dsElem.appendChild( sProjectDocument.createTextNode( datasource ) );
18121816
}
18131817

1814-
if ( addLayer( mapLayerElem, brokenNodes ) )
1818+
if ( addLayer( mapLayerElem, brokenNodes, embeddedPathResolver ) )
18151819
{
18161820
return true;
18171821
}

‎src/core/qgsproject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
996996

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

10011001
//! \note not available in Python bindings
10021002
void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;

‎tests/src/core/testqgsproject.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ void TestQgsProject::testPathResolverSvg()
232232
QCOMPARE( svg1, ourSvgPath );
233233
QCOMPARE( svg2, invalidSvgPath );
234234
QCOMPARE( svg3, librarySvgPath );
235+
236+
//
237+
// now let's use these layers in embedded in another project...
238+
//
239+
240+
QList<QDomNode> brokenNodes;
241+
QgsProject projectMaster;
242+
QVERIFY( projectMaster.createEmbeddedLayer( layer1->id(), projectFilename, brokenNodes ) );
243+
QVERIFY( projectMaster.createEmbeddedLayer( layer2->id(), projectFilename, brokenNodes ) );
244+
QVERIFY( projectMaster.createEmbeddedLayer( layer3->id(), projectFilename, brokenNodes ) );
245+
246+
QString svg1x = _getLayerSvgMarkerPath( projectMaster, "points 1" );
247+
QString svg2x = _getLayerSvgMarkerPath( projectLoaded, "points 2" );
248+
QString svg3x = _getLayerSvgMarkerPath( projectLoaded, "points 3" );
249+
QCOMPARE( svg1x, ourSvgPath );
250+
QCOMPARE( svg2x, invalidSvgPath );
251+
QCOMPARE( svg3x, librarySvgPath );
252+
235253
}
236254

237255

0 commit comments

Comments
 (0)
Please sign in to comment.