Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1566 from mdouchin/debug_project_path_embedded_items
Fix #11109 (Write relative path in project XML for embedded groups and layers)
  • Loading branch information
wonder-sk committed Aug 29, 2014
2 parents 99ff44c + 092b7fc commit 4a67573
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
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
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
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
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
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 4a67573

Please sign in to comment.