Skip to content

Commit

Permalink
Add storeInvalidLayersProperties to store invalid layers
Browse files Browse the repository at this point in the history
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 Nov 5, 2018
1 parent bcd8581 commit 1b041f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Expand Up @@ -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 );
Expand Down
34 changes: 34 additions & 0 deletions src/core/layertree/qgslayertreeutils.cpp
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/core/layertree/qgslayertreeutils.h
Expand Up @@ -19,6 +19,7 @@
#include <qnamespace.h>
#include <QList>
#include <QPair>
#include <QDomNodeList>
#include "qgis_core.h"

class QDomElement;
Expand Down Expand Up @@ -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 );

Expand Down

0 comments on commit 1b041f4

Please sign in to comment.