Skip to content
Permalink
Browse files

split generating XML and saving to QLR-file into separate functions

This increases flexibility and is consistent with the loading of a QLR-file.
  • Loading branch information
SebDieBln committed Jan 1, 2016
1 parent ff3200f commit b2b44ccca9f7474389485693a2ffb7b147145959
Showing with 24 additions and 13 deletions.
  1. +2 −0 python/core/qgslayerdefinition.sip
  2. +20 −13 src/core/qgslayerdefinition.cpp
  3. +2 −0 src/core/qgslayerdefinition.h
@@ -17,5 +17,7 @@ class QgsLayerDefinition
static bool loadLayerDefinition( QDomDocument doc, QgsLayerTreeGroup* rootGroup, QString &errorMessage /Out/ );
/** Export the selected layer tree nodes to a QLR file */
static bool exportLayerDefinition( QString path, const QList<QgsLayerTreeNode*>& selectedTreeNodes, QString &errorMessage /Out/ );
/** Export the selected layer tree nodes to a QLR-XML document */
static bool exportLayerDefinition( QDomDocument doc, const QList<QgsLayerTreeNode*>& selectedTreeNodes, QString &errorMessage, const QString& relativeBasePath = QString::null );
};

@@ -119,6 +119,24 @@ bool QgsLayerDefinition::exportLayerDefinition( QString path, const QList<QgsLay
QFileInfo fileinfo( file );

QDomDocument doc( "qgis-layer-definition" );
if ( !exportLayerDefinition( doc, selectedTreeNodes, errorMessage, fileinfo.canonicalFilePath() ) )
return false;
if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
{
QTextStream qlayerstream( &file );
doc.save( qlayerstream, 2 );
return true;
}
else
{
errorMessage = file.errorString();
return false;
}
}

bool QgsLayerDefinition::exportLayerDefinition( QDomDocument doc, const QList<QgsLayerTreeNode*>& selectedTreeNodes, QString &errorMessage, const QString& relativeBasePath )
{
Q_UNUSED( errorMessage );
QDomElement qgiselm = doc.createElement( "qlr" );
doc.appendChild( qgiselm );
QList<QgsLayerTreeNode*> nodes = selectedTreeNodes;
@@ -135,20 +153,9 @@ bool QgsLayerDefinition::exportLayerDefinition( QString path, const QList<QgsLay
Q_FOREACH ( QgsLayerTreeLayer* layer, layers )
{
QDomElement layerelm = doc.createElement( "maplayer" );
layer->layer()->writeLayerXML( layerelm, doc, fileinfo.canonicalFilePath() );
layer->layer()->writeLayerXML( layerelm, doc, relativeBasePath );
layerselm.appendChild( layerelm );
}
qgiselm.appendChild( layerselm );

if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
{
QTextStream qlayerstream( &file );
doc.save( qlayerstream, 2 );
return true;
}
else
{
errorMessage = file.errorString();
return false;
}
return true;
}
@@ -19,6 +19,8 @@ class CORE_EXPORT QgsLayerDefinition
static bool loadLayerDefinition( QDomDocument doc, QgsLayerTreeGroup* rootGroup, QString &errorMessage );
/** Export the selected layer tree nodes to a QLR file */
static bool exportLayerDefinition( QString path, const QList<QgsLayerTreeNode*>& selectedTreeNodes, QString &errorMessage );
/** Export the selected layer tree nodes to a QLR-XML document */
static bool exportLayerDefinition( QDomDocument doc, const QList<QgsLayerTreeNode*>& selectedTreeNodes, QString &errorMessage, const QString& relativeBasePath = QString::null );
};

#endif // QGSLAYERDEFINITION_H

0 comments on commit b2b44cc

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