Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn relative path into absolute path for reading embedded projects. #34706

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken

if ( element.attribute( QStringLiteral( "embedded" ) ) == QLatin1String( "1" ) )
{
createEmbeddedLayer( element.attribute( QStringLiteral( "id" ) ), readPath( element.attribute( QStringLiteral( "project" ) ) ), brokenNodes, flags );
createEmbeddedLayer( element.attribute( QStringLiteral( "id" ) ), readPath( element.attribute( QStringLiteral( "project" ) ) ), brokenNodes, true, flags );
}
else
{
Expand Down Expand Up @@ -1543,9 +1543,9 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
}


void QgsProject::loadEmbeddedNodes( QgsLayerTreeGroup *group, QgsProject::ReadFlags flags )
bool QgsProject::loadEmbeddedNodes( QgsLayerTreeGroup *group, QgsProject::ReadFlags flags )
{

bool valid = true;
const auto constChildren = group->children();
for ( QgsLayerTreeNode *child : constChildren )
{
Expand Down Expand Up @@ -1579,11 +1579,16 @@ void QgsProject::loadEmbeddedNodes( QgsLayerTreeGroup *group, QgsProject::ReadFl
if ( child->customProperty( QStringLiteral( "embedded" ) ).toInt() )
{
QList<QDomNode> brokenNodes;
createEmbeddedLayer( QgsLayerTree::toLayer( child )->layerId(), child->customProperty( QStringLiteral( "embedded_project" ) ).toString(), brokenNodes, flags );
if ( ! createEmbeddedLayer( QgsLayerTree::toLayer( child )->layerId(), readPath( child->customProperty( QStringLiteral( "embedded_project" ) ).toString() ), brokenNodes, true, flags ) )
{
valid = valid && false;
}
}
}

}

return valid;
}

QVariantMap QgsProject::customVariables() const
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* The optional \a flags argument can be used to control layer reading behavior.
* \note not available in Python bindings
*/
void loadEmbeddedNodes( QgsLayerTreeGroup *group, QgsProject::ReadFlags flags = nullptr ) SIP_SKIP;
bool loadEmbeddedNodes( QgsLayerTreeGroup *group, QgsProject::ReadFlags flags = nullptr ) SIP_SKIP;

//! Read .qgs file
bool readProjectFile( const QString &filename, QgsProject::ReadFlags flags = nullptr );
Expand Down Expand Up @@ -1798,6 +1798,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

// Required by QGIS Server for switching the current project instance
friend class QgsConfigCache;

friend class TestQgsProject;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProject::ReadFlags )
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgsmarkersymbollayer.h"
#include "qgspathresolver.h"
#include "qgsproject.h"
#include "qgslayertree.h"
#include "qgssinglesymbolrenderer.h"
#include "qgssettings.h"
#include "qgsunittypes.h"
Expand Down Expand Up @@ -483,6 +484,18 @@ void TestQgsProject::testEmbeddedLayerGroupFromQgz()

QCOMPARE( p1.layerIsEmbedded( points->id() ), path );
QCOMPARE( p1.layerIsEmbedded( polys->id() ), path );

// test embedded layers when origin project is something like ../XXX
path = QString( TEST_DATA_DIR ) + QStringLiteral( "/embedded_layers/project.qgz" );
QgsProject p2;
p2.read( path );

QgsMapLayer *points2 = p0.mapLayersByName( "points" )[0];
bool saveFlag = p2.mEmbeddedLayers[points2->id()].second;
QCOMPARE( saveFlag, true );

bool valid = p2.loadEmbeddedNodes( p2.layerTreeRoot() );
QCOMPARE( valid, true );
}

void TestQgsProject::testSetGetCrs()
Expand Down
Binary file added tests/testdata/embedded_layers/project.qgz
Binary file not shown.