Exemplo n.º 1
0
static void _readOldLegendGroup( const QDomElement &groupElem, QgsLayerTreeGroup *parent )
{
  QDomNodeList groupChildren = groupElem.childNodes();

  QgsLayerTreeGroup *groupNode = new QgsLayerTreeGroup( groupElem.attribute( QStringLiteral( "name" ) ) );

  groupNode->setItemVisibilityChecked( QgsLayerTreeUtils::checkStateFromXml( groupElem.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked );
  groupNode->setExpanded( groupElem.attribute( QStringLiteral( "open" ) ) == QLatin1String( "true" ) );

  if ( groupElem.attribute( QStringLiteral( "embedded" ) ) == QLatin1String( "1" ) )
  {
    groupNode->setCustomProperty( QStringLiteral( "embedded" ), 1 );
    groupNode->setCustomProperty( QStringLiteral( "embedded_project" ), groupElem.attribute( QStringLiteral( "project" ) ) );
  }

  for ( int i = 0; i < groupChildren.size(); ++i )
  {
    QDomElement currentChildElem = groupChildren.at( i ).toElement();
    if ( currentChildElem.tagName() == QLatin1String( "legendlayer" ) )
    {
      _readOldLegendLayer( currentChildElem, groupNode );
    }
    else if ( currentChildElem.tagName() == QLatin1String( "legendgroup" ) )
    {
      _readOldLegendGroup( currentChildElem, groupNode );
    }
  }

  parent->addChildNode( groupNode );
}
Exemplo n.º 2
0
static void _readOldLegendGroup( QDomElement& elem, QgsLayerTreeGroup* parentGroup, QgsProject* project )
{
  QDomElement itemElem = elem.firstChildElement();

  while ( !itemElem.isNull() )
  {

    if ( itemElem.tagName() == QLatin1String( "LayerItem" ) )
    {
      QString layerId = itemElem.attribute( QStringLiteral( "layerId" ) );
      if ( QgsMapLayer* layer = project->mapLayer( layerId ) )
      {
        QgsLayerTreeLayer* nodeLayer = parentGroup->addLayer( layer );
        QString userText = itemElem.attribute( QStringLiteral( "userText" ) );
        if ( !userText.isEmpty() )
          nodeLayer->setCustomProperty( QStringLiteral( "legend/title-label" ), userText );
        QString style = itemElem.attribute( QStringLiteral( "style" ) );
        if ( !style.isEmpty() )
          nodeLayer->setCustomProperty( QStringLiteral( "legend/title-style" ), style );
        QString showFeatureCount = itemElem.attribute( QStringLiteral( "showFeatureCount" ) );
        if ( showFeatureCount.toInt() )
          nodeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), 1 );

        // support for individual legend items (user text, order) not implemented yet
      }
    }
    else if ( itemElem.tagName() == QLatin1String( "GroupItem" ) )
    {
      QgsLayerTreeGroup* nodeGroup = parentGroup->addGroup( itemElem.attribute( QStringLiteral( "userText" ) ) );
      QString style = itemElem.attribute( QStringLiteral( "style" ) );
      if ( !style.isEmpty() )
        nodeGroup->setCustomProperty( QStringLiteral( "legend/title-style" ), style );

      _readOldLegendGroup( itemElem, nodeGroup, project );
    }

    itemElem = itemElem.nextSiblingElement();
  }
}