Skip to content

Commit

Permalink
[BUGFIX] Issue #11109 - Write relative path in project XML for embedd…
Browse files Browse the repository at this point in the history
…ed groups and layers when needed
  • Loading branch information
mdouchin committed Aug 28, 2014
1 parent 6130505 commit 092b7fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9648,6 +9648,7 @@ void QgisApp::writeProject( QDomDocument &doc )

QgsLayerTreeNode* clonedRoot = QgsProject::instance()->layerTreeRoot()->clone();
QgsLayerTreeUtils::removeChildrenOfEmbeddedGroups( QgsLayerTree::toGroup( clonedRoot ) );
QgsLayerTreeUtils::updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( clonedRoot ) ); // convert absolute paths to relative paths if required
QDomElement oldLegendElem = QgsLayerTreeUtils::writeOldLegend( doc, QgsLayerTree::toGroup( clonedRoot ),
mLayerTreeCanvasBridge->hasCustomLayerOrder(), mLayerTreeCanvasBridge->customLayerOrder() );
delete clonedRoot;
Expand Down
22 changes: 22 additions & 0 deletions src/core/layertree/qgslayertreeutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

#include "qgsvectorlayer.h"

#include "qgsproject.h"

#include <QDomElement>


static void _readOldLegendGroup( const QDomElement& groupElem, QgsLayerTreeGroup* parent );
static void _readOldLegendLayer( const QDomElement& layerElem, QgsLayerTreeGroup* parent );

Expand Down Expand Up @@ -314,3 +317,22 @@ void QgsLayerTreeUtils::removeChildrenOfEmbeddedGroups( QgsLayerTreeGroup* group
}
}
}


void QgsLayerTreeUtils::updateEmbeddedGroupsProjectPath( QgsLayerTreeGroup* group )
{
foreach ( QgsLayerTreeNode* node, group->children() )
{
if ( !node->customProperty( "embedded_project" ).toString().isEmpty() )
{
// may change from absolute path to relative path
QString newPath = QgsProject::instance()->writePath( node->customProperty( "embedded_project" ).toString() );
node->setCustomProperty( "embedded_project", newPath );
}

if ( QgsLayerTree::isGroup( node ) )
{
updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( node ) );
}
}
}
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreeutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class CORE_EXPORT QgsLayerTreeUtils
//! Remove subtree of embedded groups. Useful when saving layer tree
static void removeChildrenOfEmbeddedGroups( QgsLayerTreeGroup* group );

//! @note not available in python bindings
static void updateEmbeddedGroupsProjectPath( QgsLayerTreeGroup* group );

};

#endif // QGSLAYERTREEUTILS_H
21 changes: 2 additions & 19 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,24 +1003,6 @@ void QgsProject::loadEmbeddedNodes( QgsLayerTreeGroup* group )
}
}

void QgsProject::updateEmbeddedGroupsProjectPath( QgsLayerTreeGroup* group )
{
foreach ( QgsLayerTreeNode* node, group->children() )
{
if ( QgsLayerTree::isGroup( node ) )
{
if ( !node->customProperty( "embedded_project" ).toString().isEmpty() )
{
// may change from absolute path to relative path
QString newPath = writePath( node->customProperty( "embedded_project" ).toString() );
node->setCustomProperty( "embedded_project", newPath );
}
else
updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( node ) );
}
}
}


bool QgsProject::read( QDomNode & layerNode )
{
Expand Down Expand Up @@ -1102,7 +1084,7 @@ bool QgsProject::write()
// write layer tree - make sure it is without embedded subgroups
QgsLayerTreeNode* clonedRoot = mRootGroup->clone();
QgsLayerTreeUtils::removeChildrenOfEmbeddedGroups( QgsLayerTree::toGroup( clonedRoot ) );
updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( clonedRoot ) ); // convert absolute paths to relative paths if required
QgsLayerTreeUtils::updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( clonedRoot ) ); // convert absolute paths to relative paths if required
clonedRoot->writeXML( qgisNode );
delete clonedRoot;

Expand Down Expand Up @@ -1716,6 +1698,7 @@ bool QgsProject::createEmbeddedLayer( const QString& layerId, const QString& pro

//does project store pathes absolute or relative?
bool useAbsolutePathes = true;

QDomElement propertiesElem = projectDocument.documentElement().firstChildElement( "properties" );
if ( !propertiesElem.isNull() )
{
Expand Down
3 changes: 0 additions & 3 deletions src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,6 @@ class CORE_EXPORT QgsProject : public QObject
//! @note not available in python bindings
void loadEmbeddedNodes( QgsLayerTreeGroup* group );

//! @note not available in python bindings
void updateEmbeddedGroupsProjectPath( QgsLayerTreeGroup* group );

signals:
//! emitted when project is being read
void readProject( const QDomDocument & );
Expand Down

0 comments on commit 092b7fc

Please sign in to comment.