Skip to content
Permalink
Browse files
Support for custom layer order for legacy projects
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent fd8722a commit 0e7955146a869db1195b018633ac5fe230415256
@@ -44,6 +44,63 @@ bool QgsLayerTreeUtils::readOldLegend( QgsLayerTreeGroup* root, const QDomElemen
return true;
}



static bool _readOldLegendLayerOrderGroup( const QDomElement& groupElem, QMap<int, QString>& layerIndexes )
{
QDomNodeList legendChildren = groupElem.childNodes();

for ( int i = 0; i < legendChildren.size(); ++i )
{
QDomElement currentChildElem = legendChildren.at( i ).toElement();
if ( currentChildElem.tagName() == "legendlayer" )
{
QDomElement layerFileElem = currentChildElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" );

int layerIndex = currentChildElem.attribute( "drawingOrder" ).toInt();
if ( layerIndex == -1 )
return false; // order undefined
layerIndexes.insert( layerIndex, layerFileElem.attribute( "layerid" ) );
}
else if ( currentChildElem.tagName() == "legendgroup" )
{
if ( !_readOldLegendLayerOrderGroup( currentChildElem, layerIndexes ) )
return false;
}
}

return true;
}


bool QgsLayerTreeUtils::readOldLegendLayerOrder( const QDomElement& legendElem, bool& hasCustomOrder, QStringList& order )
{
if ( legendElem.isNull() )
return false;

hasCustomOrder = legendElem.attribute( "updateDrawingOrder" ) == "false";
order.clear();

QMap<int, QString> layerIndexes;

// try to read the order. may be undefined (order = -1) for some or all items
bool res = _readOldLegendLayerOrderGroup( legendElem, layerIndexes );

if ( !res && hasCustomOrder )
return false; // invalid state

foreach ( QString layerId, layerIndexes )
{
QgsDebugMsg( layerId );
order.append( layerId );
}

return true;
}




QString QgsLayerTreeUtils::checkStateToXml( Qt::CheckState state )
{
switch ( state )
@@ -74,6 +131,7 @@ void QgsLayerTreeUtils::addLegendGroupToTreeWidget( const QDomElement& groupElem
parent->addChildNode( groupNode );

groupNode->setVisible( checkStateFromXml( groupElem.attribute( "checked" ) ) );
groupNode->setExpanded( groupElem.attribute( "open" ) == "true" );

if ( groupElem.attribute( "embedded" ) == "1" )
{
@@ -97,15 +155,23 @@ void QgsLayerTreeUtils::addLegendGroupToTreeWidget( const QDomElement& groupElem

void QgsLayerTreeUtils::addLegendLayerToTreeWidget( const QDomElement& layerElem, QgsLayerTreeGroup* parent )
{
QString layerId = layerElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
QDomElement layerFileElem = layerElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" );
QString layerId = layerFileElem.attribute( "layerid" );
QgsLayerTreeLayer* layerNode = new QgsLayerTreeLayer( layerId, layerElem.attribute( "name" ) );

layerNode->setVisible( checkStateFromXml( layerElem.attribute( "checked" ) ) );
layerNode->setExpanded( layerElem.attribute( "open" ) == "true" );

if ( layerFileElem.attribute( "isInOverview" ) == "1" )
layerNode->setCustomProperty( "overview", 1 );

if ( layerElem.attribute( "embedded" ) == "1" )
layerNode->setCustomProperty( "embedded", 1 );

// TODO: is in overview, drawing order, show feature count
if ( layerElem.attribute( "showFeatureCount" ) == "1" )
layerNode->setCustomProperty( "showFeatureCount", 1 );

// drawing order is handled by readOldLegendLayerOrder()

parent->addChildNode( layerNode );
}
@@ -20,6 +20,7 @@
#include <QList>

class QDomElement;
class QStringList;

class QgsLayerTreeGroup;
class QgsLayerTreeLayer;
@@ -31,6 +32,8 @@ class CORE_EXPORT QgsLayerTreeUtils
// return a new instance - or null on error
static bool readOldLegend( QgsLayerTreeGroup* root, const QDomElement& legendElem );

static bool readOldLegendLayerOrder( const QDomElement& legendElem, bool& hasCustomOrder, QStringList& order );

static QString checkStateToXml( Qt::CheckState state );
static Qt::CheckState checkStateFromXml( QString txt );

@@ -16,6 +16,7 @@
#include "qgslayertreemapcanvasbridge.h"

#include "qgslayertree.h"
#include "qgslayertreeutils.h"

#include "qgsmapcanvas.h"

@@ -172,7 +173,16 @@ void QgsLayerTreeMapCanvasBridge::readProject( const QDomDocument& doc )
{
QDomElement elem = doc.documentElement().firstChildElement( "layer-tree-canvas" );
if ( elem.isNull() )
{
bool oldEnabled;
QStringList oldOrder;
if ( QgsLayerTreeUtils::readOldLegendLayerOrder( doc.documentElement().firstChildElement( "legend" ), oldEnabled, oldOrder ) )
{
setHasCustomLayerOrder( oldEnabled );
setCustomLayerOrder( oldOrder );
}
return;
}

QDomElement customOrderElem = elem.firstChildElement( "custom-order" );
if ( !customOrderElem.isNull() )

0 comments on commit 0e79551

Please sign in to comment.