Skip to content
Permalink
Browse files

[bugfix] Pass QgsReadWriteContext into project XML r/w

Or we miss the path resolver and we do not read/write
relative paths correctly

Fixes #17116
  • Loading branch information
elpaso committed Nov 7, 2017
1 parent 8aa8c30 commit 7aa24f69066ee26dec749940acadc7330511a86c
@@ -8,6 +8,7 @@




class QgsComposerItemGroup: QgsComposerItem
{
%Docstring
@@ -9,6 +9,7 @@




class QgsComposerObject: QObject, QgsExpressionContextGenerator
{
%Docstring
@@ -122,7 +122,7 @@ class QgsLayerTree : QgsLayerTreeGroup
.. versionadded:: 3.0
%End

static QgsLayerTree *readXml( QDomElement &element );
static QgsLayerTree *readXml( QDomElement &element, const QgsReadWriteContext &context );
%Docstring
Load the layer tree from an XML element.
It is not required that layers are loaded at this point.
@@ -141,7 +141,7 @@ class QgsLayerTree : QgsLayerTreeGroup
.. versionadded:: 3.0
%End

virtual void writeXml( QDomElement &parentElement );
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context );

virtual QgsLayerTree *clone() const /Factory/;

@@ -138,27 +138,27 @@ class QgsLayerTreeGroup : QgsLayerTreeNode
:rtype: QgsLayerTreeGroup
%End

static QgsLayerTreeGroup *readXml( QDomElement &element ) /Factory/;
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on error).
Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
:rtype: QgsLayerTreeGroup
%End

static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsProject *project ) /Factory/;
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on error).
Also resolves textual references to layers from the project (calls resolveReferences() internally).
.. versionadded:: 3.0
:rtype: QgsLayerTreeGroup
%End

virtual void writeXml( QDomElement &parentElement );
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context );
%Docstring
Write group (tree) as XML element <layer-tree-group> and add it to the given parent element
%End

void readChildrenFromXml( QDomElement &element );
void readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context );
%Docstring
Read children from XML and append them to the group.
Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
@@ -63,22 +63,22 @@ class QgsLayerTreeLayer : QgsLayerTreeNode
.. versionadded:: 3.0
%End

static QgsLayerTreeLayer *readXml( QDomElement &element ) /Factory/;
static QgsLayerTreeLayer *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Read layer node from XML. Returns new instance.
Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
:rtype: QgsLayerTreeLayer
%End

static QgsLayerTreeLayer *readXml( QDomElement &element, const QgsProject *project ) /Factory/;
static QgsLayerTreeLayer *readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Read layer node from XML. Returns new instance.
Also resolves textual references to layers from the project (calls resolveReferences() internally).
.. versionadded:: 3.0
:rtype: QgsLayerTreeLayer
%End

virtual void writeXml( QDomElement &parentElement );
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context );

virtual QString dump() const;

@@ -111,7 +111,7 @@ Get list of children of the node. Children are owned by the parent
.. versionadded:: 3.0
%End

static QgsLayerTreeNode *readXml( QDomElement &element ) /Factory/;
static QgsLayerTreeNode *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Read layer tree from XML. Returns new instance.
Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
@@ -126,7 +126,7 @@ Get list of children of the node. Children are owned by the parent
:rtype: QgsLayerTreeNode
%End

virtual void writeXml( QDomElement &parentElement ) = 0;
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) = 0;
%Docstring
Write layer tree to XML
%End
@@ -170,7 +170,7 @@ void QgsProjectLayerGroupDialog::changeProjectFile()
QDomElement layerTreeElem = projectDom.documentElement().firstChildElement( QStringLiteral( "layer-tree-group" ) );
if ( !layerTreeElem.isNull() )
{
mRootGroup->readChildrenFromXml( layerTreeElem );
mRootGroup->readChildrenFromXml( layerTreeElem, QgsReadWriteContext() );
}
else
{
@@ -429,7 +429,6 @@ bool QgsComposerArrow::writeXml( QDomElement &elem, QDomDocument &doc ) const

QgsReadWriteContext context;
context.setPathResolver( pathResolver );

// absolute paths to relative
QString startMarkerPath = QgsSymbolLayerUtils::svgSymbolPathToName( mStartMarkerFile, pathResolver );
QString endMarkerPath = QgsSymbolLayerUtils::svgSymbolPathToName( mEndMarkerFile, pathResolver );
@@ -143,6 +143,12 @@ void QgsComposerItem::setSelected( bool s )

bool QgsComposerItem::_writeXml( QDomElement &itemElem, QDomDocument &doc ) const
{
QgsPathResolver pathResolver;
if ( composition() )
pathResolver = composition()->project()->pathResolver();
QgsReadWriteContext context;
context.setPathResolver( pathResolver );

if ( itemElem.isNull() )
{
return false;
@@ -20,6 +20,8 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgscomposeritem.h"
#include "qgsreadwritecontext.h"

#include <QSet>

/**
@@ -344,6 +344,12 @@ bool QgsComposerLegend::writeXml( QDomElement &elem, QDomDocument &doc ) const
return false;
}

QgsPathResolver pathResolver;
if ( mComposition )
pathResolver = mComposition->project()->pathResolver();
QgsReadWriteContext context;
context.setPathResolver( pathResolver );

QDomElement composerLegendElem = doc.createElement( QStringLiteral( "ComposerLegend" ) );
elem.appendChild( composerLegendElem );

@@ -389,7 +395,7 @@ bool QgsComposerLegend::writeXml( QDomElement &elem, QDomDocument &doc ) const
if ( mCustomLayerTree )
{
// if not using auto-update - store the custom layer tree
mCustomLayerTree->writeXml( composerLegendElem );
mCustomLayerTree->writeXml( composerLegendElem, context );
}

if ( mLegendFilterByMap )
@@ -408,6 +414,12 @@ bool QgsComposerLegend::readXml( const QDomElement &itemElem, const QDomDocument
return false;
}

QgsPathResolver pathResolver;
if ( mComposition )
pathResolver = mComposition->project()->pathResolver();
QgsReadWriteContext context;
context.setPathResolver( pathResolver );

//read general properties
mTitle = itemElem.attribute( QStringLiteral( "title" ) );
mSettings.setTitle( mTitle );
@@ -479,7 +491,7 @@ bool QgsComposerLegend::readXml( const QDomElement &itemElem, const QDomDocument

if ( !layerTreeElem.isNull() )
{
std::unique_ptr< QgsLayerTree > tree( QgsLayerTree::readXml( layerTreeElem ) );
std::unique_ptr< QgsLayerTree > tree( QgsLayerTree::readXml( layerTreeElem, context ) );
if ( mComposition )
tree->resolveReferences( mComposition->project(), true );
setCustomLayerTree( tree.release() );
@@ -272,6 +272,7 @@ bool QgsComposerNodesItem::moveNode( const int index, QPointF pt )
bool QgsComposerNodesItem::readXml( const QDomElement &itemElem,
const QDomDocument &doc )
{

// restore general composer item properties
const QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
if ( !composerItemList.isEmpty() )
@@ -22,6 +22,8 @@
#include "qgsobjectcustomproperties.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgspropertycollection.h"
#include "qgsreadwritecontext.h"

#include <QObject>
#include <QDomNode>
#include <QMap>
@@ -99,26 +99,26 @@ void QgsLayerTree::setHasCustomLayerOrder( bool hasCustomLayerOrder )
emit layerOrderChanged();
}

QgsLayerTree *QgsLayerTree::readXml( QDomElement &element )
QgsLayerTree *QgsLayerTree::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
QgsLayerTree *tree = new QgsLayerTree();

tree->readCommonXml( element );

tree->readChildrenFromXml( element );
tree->readChildrenFromXml( element, context );

return tree;
}

void QgsLayerTree::writeXml( QDomElement &parentElement )
void QgsLayerTree::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
{
QDomDocument doc = parentElement.ownerDocument();
QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );

writeCommonXml( elem );

Q_FOREACH ( QgsLayerTreeNode *node, mChildren )
node->writeXml( elem );
node->writeXml( elem, context );

QDomElement customOrderElem = doc.createElement( QStringLiteral( "custom-order" ) );
customOrderElem.setAttribute( QStringLiteral( "enabled" ), mHasCustomLayerOrder ? 1 : 0 );
@@ -172,7 +172,7 @@ class CORE_EXPORT QgsLayerTree : public QgsLayerTreeGroup
*
* \since QGIS 3.0
*/
static QgsLayerTree *readXml( QDomElement &element );
static QgsLayerTree *readXml( QDomElement &element, const QgsReadWriteContext &context );

/**
* Load the layer order from an XML element.
@@ -182,7 +182,7 @@ class CORE_EXPORT QgsLayerTree : public QgsLayerTreeGroup
*/
void readLayerOrderFromXml( const QDomElement &doc );

virtual void writeXml( QDomElement &parentElement ) override;
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) override;

virtual QgsLayerTree *clone() const override SIP_FACTORY;

@@ -249,7 +249,7 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
return nullptr;
}

QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element )
QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
return nullptr;
@@ -265,22 +265,22 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element )

groupNode->readCommonXml( element );

groupNode->readChildrenFromXml( element );
groupNode->readChildrenFromXml( element, context );

groupNode->setIsMutuallyExclusive( isMutuallyExclusive, mutuallyExclusiveChildIndex );

return groupNode;
}

QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsProject *project )
QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
{
QgsLayerTreeGroup *node = readXml( element );
QgsLayerTreeGroup *node = readXml( element, context );
if ( node )
node->resolveReferences( project );
return node;
}

void QgsLayerTreeGroup::writeXml( QDomElement &parentElement )
void QgsLayerTreeGroup::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
{
QDomDocument doc = parentElement.ownerDocument();
QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-group" ) );
@@ -296,18 +296,18 @@ void QgsLayerTreeGroup::writeXml( QDomElement &parentElement )
writeCommonXml( elem );

Q_FOREACH ( QgsLayerTreeNode *node, mChildren )
node->writeXml( elem );
node->writeXml( elem, context );

parentElement.appendChild( elem );
}

void QgsLayerTreeGroup::readChildrenFromXml( QDomElement &element )
void QgsLayerTreeGroup::readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context )
{
QList<QgsLayerTreeNode *> nodes;
QDomElement childElem = element.firstChildElement();
while ( !childElem.isNull() )
{
QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem );
QgsLayerTreeNode *newNode = QgsLayerTreeNode::readXml( childElem, context );
if ( newNode )
nodes << newNode;

@@ -145,25 +145,25 @@ class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode
* Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on error).
* Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
*/
static QgsLayerTreeGroup *readXml( QDomElement &element ) SIP_FACTORY;
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;

/**
* Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on error).
* Also resolves textual references to layers from the project (calls resolveReferences() internally).
* \since QGIS 3.0
*/
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsProject *project ) SIP_FACTORY;
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context ) SIP_FACTORY;

/**
* Write group (tree) as XML element <layer-tree-group> and add it to the given parent element
*/
virtual void writeXml( QDomElement &parentElement ) override;
virtual void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) override;

/**
* Read children from XML and append them to the group.
* Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
*/
void readChildrenFromXml( QDomElement &element );
void readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context );

/**
* Return text representation of the tree. For debugging purposes only.
@@ -97,16 +97,17 @@ void QgsLayerTreeLayer::setName( const QString &n )
}
}

QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element )
QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
Q_UNUSED( context );
if ( element.tagName() != QLatin1String( "layer-tree-layer" ) )
return nullptr;

QString layerID = element.attribute( QStringLiteral( "id" ) );
QString layerName = element.attribute( QStringLiteral( "name" ) );

QString providerKey = element.attribute( QStringLiteral( "providerKey" ) );
QString source = element.attribute( QStringLiteral( "source" ) );
QString source = context.pathResolver().readPath( element.attribute( QStringLiteral( "source" ) ) );

Qt::CheckState checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) );
bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
@@ -121,15 +122,15 @@ QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element )
return nodeLayer;
}

QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsProject *project )
QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context )
{
QgsLayerTreeLayer *node = readXml( element );
QgsLayerTreeLayer *node = readXml( element, context );
if ( node )
node->resolveReferences( project );
return node;
}

void QgsLayerTreeLayer::writeXml( QDomElement &parentElement )
void QgsLayerTreeLayer::writeXml( QDomElement &parentElement, const QgsReadWriteContext &context )
{
QDomDocument doc = parentElement.ownerDocument();
QDomElement elem = doc.createElement( QStringLiteral( "layer-tree-layer" ) );
@@ -138,7 +139,7 @@ void QgsLayerTreeLayer::writeXml( QDomElement &parentElement )

if ( mRef )
{
elem.setAttribute( QStringLiteral( "source" ), mRef->publicSource() );
elem.setAttribute( QStringLiteral( "source" ), context.pathResolver().writePath( mRef->publicSource() ) );
elem.setAttribute( QStringLiteral( "providerKey" ), mRef->dataProvider() ? mRef->dataProvider()->name() : QString() );
}

0 comments on commit 7aa24f6

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