Skip to content

Commit

Permalink
Merge pull request #5558 from elpaso/project_use_rw_context
Browse files Browse the repository at this point in the history
[bugfix] Project use rw context
  • Loading branch information
elpaso authored Nov 8, 2017
2 parents 8453100 + d11ea07 commit e781ad2
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 59 deletions.
1 change: 1 addition & 0 deletions python/core/composer/qgscomposeritemgroup.sip
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@




class QgsComposerItemGroup: QgsComposerItem
{
%Docstring
Expand Down
1 change: 1 addition & 0 deletions python/core/composer/qgscomposerobject.sip
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@




class QgsComposerObject: QObject, QgsExpressionContextGenerator
{
%Docstring
Expand Down
4 changes: 2 additions & 2 deletions python/core/layertree/qgslayertree.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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/;

Expand Down
8 changes: 4 additions & 4 deletions python/core/layertree/qgslayertreegroup.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions python/core/layertree/qgslayertreelayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions python/core/layertree/qgslayertreenode.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsprojectlayergroupdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/core/composer/qgscomposerarrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposeritemgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgscomposeritem.h"
#include "qgsreadwritecontext.h"

#include <QSet>

/**
Expand Down
16 changes: 14 additions & 2 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 )
Expand All @@ -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 );
Expand Down Expand Up @@ -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() );
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposernodesitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposerobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "qgsobjectcustomproperties.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgspropertycollection.h"
#include "qgsreadwritecontext.h"

#include <QObject>
#include <QDomNode>
#include <QMap>
Expand Down
8 changes: 4 additions & 4 deletions src/core/layertree/qgslayertree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions src/core/layertree/qgslayertree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down
16 changes: 8 additions & 8 deletions src/core/layertree/qgslayertreegroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" ) );
Expand All @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/core/layertree/qgslayertreegroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions src/core/layertree/qgslayertreelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void QgsLayerTreeLayer::setName( const QString &n )
}
}

QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element )
QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
if ( element.tagName() != QLatin1String( "layer-tree-layer" ) )
return nullptr;
Expand All @@ -106,7 +106,7 @@ QgsLayerTreeLayer *QgsLayerTreeLayer::readXml( QDomElement &element )
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" ) );
Expand All @@ -121,15 +121,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" ) );
Expand All @@ -138,7 +138,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() );
}

Expand Down
Loading

0 comments on commit e781ad2

Please sign in to comment.