|
22 | 22 | #include "qgscomposermap.h"
|
23 | 23 | #include "qgscomposition.h"
|
24 | 24 | #include "qgscomposermodel.h"
|
| 25 | +#include "qgsmaplayerregistry.h" |
25 | 26 | #include "qgslayertree.h"
|
26 | 27 | #include "qgslayertreemodel.h"
|
27 | 28 | #include "qgslegendrenderer.h"
|
@@ -308,6 +309,46 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
|
308 | 309 | return _writeXML( composerLegendElem, doc );
|
309 | 310 | }
|
310 | 311 |
|
| 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 | + |
311 | 352 | bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
|
312 | 353 | {
|
313 | 354 | if ( itemElem.isNull() )
|
@@ -369,8 +410,20 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
|
369 | 410 | setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) );
|
370 | 411 | }
|
371 | 412 |
|
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 | + } |
374 | 427 |
|
375 | 428 | //restore general composer item properties
|
376 | 429 | QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
|
|
0 commit comments