Skip to content

Commit e02e7c9

Browse files
committed
Support for reading composer legend configuration for projects <= 2.4
The list of layers/groups is now restored (previously the configuration would be omitted).
1 parent 4ac94f2 commit e02e7c9

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/core/composer/qgscomposerlegend.cpp

+55-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgscomposermap.h"
2323
#include "qgscomposition.h"
2424
#include "qgscomposermodel.h"
25+
#include "qgsmaplayerregistry.h"
2526
#include "qgslayertree.h"
2627
#include "qgslayertreemodel.h"
2728
#include "qgslegendrenderer.h"
@@ -308,6 +309,46 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
308309
return _writeXML( composerLegendElem, doc );
309310
}
310311

312+
static void _readOldLegendGroup( QDomElement& elem, QgsLayerTreeGroup* parentGroup )
313+
{
314+
QDomElement itemElem = elem.firstChildElement();
315+
316+
while ( !itemElem.isNull() )
317+
{
318+
319+
if ( itemElem.tagName() == "LayerItem" )
320+
{
321+
QString layerId = itemElem.attribute( "layerId" );
322+
if ( QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId ) )
323+
{
324+
QgsLayerTreeLayer* nodeLayer = parentGroup->addLayer( layer );
325+
QString userText = itemElem.attribute( "userText" );
326+
if ( !userText.isEmpty() )
327+
nodeLayer->setCustomProperty( "legend/title-label", userText );
328+
QString style = itemElem.attribute( "style" );
329+
if ( !style.isEmpty() )
330+
nodeLayer->setCustomProperty( "legend/title-style", style );
331+
QString showFeatureCount = itemElem.attribute( "showFeatureCount" );
332+
if ( showFeatureCount.toInt() )
333+
nodeLayer->setCustomProperty( "showFeatureCount", 1 );
334+
335+
// support for individual legend items (user text, order) not implemented yet
336+
}
337+
}
338+
else if ( itemElem.tagName() == "GroupItem" )
339+
{
340+
QgsLayerTreeGroup* nodeGroup = parentGroup->addGroup( itemElem.attribute( "userText" ) );
341+
QString style = itemElem.attribute( "style" );
342+
if ( !style.isEmpty() )
343+
nodeGroup->setCustomProperty( "legend/title-style", style );
344+
345+
_readOldLegendGroup( itemElem, nodeGroup );
346+
}
347+
348+
itemElem = itemElem.nextSiblingElement();
349+
}
350+
}
351+
311352
bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
312353
{
313354
if ( itemElem.isNull() )
@@ -369,8 +410,20 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
369410
setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) );
370411
}
371412

372-
QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" );
373-
setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem ) );
413+
QDomElement oldLegendModelElem = itemElem.firstChildElement( "Model" );
414+
if ( !oldLegendModelElem.isNull() )
415+
{
416+
// QGIS <= 2.4
417+
QgsLayerTreeGroup* nodeRoot = new QgsLayerTreeGroup();
418+
_readOldLegendGroup( oldLegendModelElem, nodeRoot );
419+
setCustomLayerTree( nodeRoot );
420+
}
421+
else
422+
{
423+
// QGIS >= 2.6
424+
QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" );
425+
setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem ) );
426+
}
374427

375428
//restore general composer item properties
376429
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );

0 commit comments

Comments
 (0)