Skip to content
Permalink
Browse files

Add storeInvalidLayersProperties to store invalid layers

This can be used to restore initial layer status or to
apply styles and other layer properties if the data
source is changed.

The idea is that the user can fix bad layers at any time,
and by setting a new datasource she probably wants
to keep the original layer properties.
  • Loading branch information
elpaso committed Oct 20, 2018
1 parent bcd8581 commit 1b041f4f4feba05189c6992d7fe412789942aa1c
@@ -56,7 +56,14 @@ Returns true if any of the layers is modified

static void removeInvalidLayers( QgsLayerTreeGroup *group );
%Docstring
Remove layer nodes that refer to invalid layers
Removes layer nodes that refer to invalid layers
%End

static void storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );
%Docstring
Stores in a custom layer node property the layer properties XML information for an invalid layer

.. versionadded:: 3.6
%End

static void replaceChildrenOfEmbeddedGroups( QgsLayerTreeGroup *group );
@@ -306,6 +306,40 @@ void QgsLayerTreeUtils::removeInvalidLayers( QgsLayerTreeGroup *group )
group->removeChildNode( node );
}

void QgsLayerTreeUtils::storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc )
{
const QDomNodeList mlNodeList( doc->documentElement()
.firstChildElement( QStringLiteral( "projectlayers" ) )
.elementsByTagName( QStringLiteral( "maplayer" ) ) );
for ( QgsLayerTreeNode *node : group->children() )
{
if ( QgsLayerTree::isLayer( node ) )
{
QgsMapLayer *l( QgsLayerTree::toLayer( node )->layer() );
if ( l && ! l->isValid( ) )
{
for ( int i = 0; i < mlNodeList.count(); i++ )
{
QDomNode mlNode( mlNodeList.at( i ) );
QString id( mlNode.firstChildElement( QStringLiteral( "id" ) ).firstChild().nodeValue() );
if ( id == l->id() )
{
QDomImplementation DomImplementation;
QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
QDomDocument document( documentType );
QDomElement element = mlNode.toElement();
document.appendChild( element );
QString str;
QTextStream stream( &str );
document.save( stream, 4 /*indent*/ );
l->setCustomProperty( QStringLiteral( "invalidLayerProperties" ), str );
}
}
}
}
}
}

QStringList QgsLayerTreeUtils::invisibleLayerList( QgsLayerTreeNode *node )
{
QStringList list;
@@ -19,6 +19,7 @@
#include <qnamespace.h>
#include <QList>
#include <QPair>
#include <QDomNodeList>
#include "qgis_core.h"

class QDomElement;
@@ -58,9 +59,15 @@ class CORE_EXPORT QgsLayerTreeUtils
//! Returns true if any of the layers is modified
static bool layersModified( const QList<QgsLayerTreeLayer *> &layerNodes );

//! Remove layer nodes that refer to invalid layers
//! Removes layer nodes that refer to invalid layers
static void removeInvalidLayers( QgsLayerTreeGroup *group );

/**
* Stores in a custom layer node property the layer properties XML information for an invalid layer
* \since 3.6
*/
static void storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );

//! Remove subtree of embedded groups and replaces it with a custom property embedded-visible-layers
static void replaceChildrenOfEmbeddedGroups( QgsLayerTreeGroup *group );

0 comments on commit 1b041f4

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