bool QgsComposerLegend::readXml( const QDomElement &itemElem, const QDomDocument &doc ) { if ( itemElem.isNull() ) { return false; } //read general properties mTitle = itemElem.attribute( QStringLiteral( "title" ) ); mSettings.setTitle( mTitle ); if ( !itemElem.attribute( QStringLiteral( "titleAlignment" ) ).isEmpty() ) { mSettings.setTitleAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "titleAlignment" ) ).toInt() ) ); } int colCount = itemElem.attribute( QStringLiteral( "columnCount" ), QStringLiteral( "1" ) ).toInt(); if ( colCount < 1 ) colCount = 1; mColumnCount = colCount; mSettings.setColumnCount( mColumnCount ); mSettings.setSplitLayer( itemElem.attribute( QStringLiteral( "splitLayer" ), QStringLiteral( "0" ) ).toInt() == 1 ); mSettings.setEqualColumnWidth( itemElem.attribute( QStringLiteral( "equalColumnWidth" ), QStringLiteral( "0" ) ).toInt() == 1 ); QDomNodeList stylesNodeList = itemElem.elementsByTagName( QStringLiteral( "styles" ) ); if ( !stylesNodeList.isEmpty() ) { QDomNode stylesNode = stylesNodeList.at( 0 ); for ( int i = 0; i < stylesNode.childNodes().size(); i++ ) { QDomElement styleElem = stylesNode.childNodes().at( i ).toElement(); QgsLegendStyle style; style.readXml( styleElem, doc ); QString name = styleElem.attribute( QStringLiteral( "name" ) ); QgsLegendStyle::Style s; if ( name == QLatin1String( "title" ) ) s = QgsLegendStyle::Title; else if ( name == QLatin1String( "group" ) ) s = QgsLegendStyle::Group; else if ( name == QLatin1String( "subgroup" ) ) s = QgsLegendStyle::Subgroup; else if ( name == QLatin1String( "symbol" ) ) s = QgsLegendStyle::Symbol; else if ( name == QLatin1String( "symbolLabel" ) ) s = QgsLegendStyle::SymbolLabel; else continue; setStyle( s, style ); } } //font color QColor fontClr; fontClr.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) ); mSettings.setFontColor( fontClr ); //spaces mSettings.setBoxSpace( itemElem.attribute( QStringLiteral( "boxSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); mSettings.setColumnSpace( itemElem.attribute( QStringLiteral( "columnSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); mSettings.setSymbolSize( QSizeF( itemElem.attribute( QStringLiteral( "symbolWidth" ), QStringLiteral( "7.0" ) ).toDouble(), itemElem.attribute( QStringLiteral( "symbolHeight" ), QStringLiteral( "14.0" ) ).toDouble() ) ); mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( QStringLiteral( "wmsLegendWidth" ), QStringLiteral( "50" ) ).toDouble(), itemElem.attribute( QStringLiteral( "wmsLegendHeight" ), QStringLiteral( "25" ) ).toDouble() ) ); mSettings.setLineSpacing( itemElem.attribute( QStringLiteral( "lineSpacing" ), QStringLiteral( "1.0" ) ).toDouble() ); mSettings.setDrawRasterStroke( itemElem.attribute( QStringLiteral( "rasterBorder" ), QStringLiteral( "1" ) ) != QLatin1String( "0" ) ); mSettings.setRasterStrokeColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "rasterBorderColor" ), QStringLiteral( "0,0,0" ) ) ) ); mSettings.setRasterStrokeWidth( itemElem.attribute( QStringLiteral( "rasterBorderWidth" ), QStringLiteral( "0" ) ).toDouble() ); mSettings.setWrapChar( itemElem.attribute( QStringLiteral( "wrapChar" ) ) ); mSizeToContents = itemElem.attribute( QStringLiteral( "resizeToContents" ), QStringLiteral( "1" ) ) != QLatin1String( "0" ); //composer map mLegendFilterByMap = itemElem.attribute( QStringLiteral( "legendFilterByMap" ), QStringLiteral( "0" ) ).toInt(); if ( !itemElem.attribute( QStringLiteral( "map" ) ).isEmpty() ) { setComposerMap( mComposition->getComposerMapById( itemElem.attribute( QStringLiteral( "map" ) ).toInt() ) ); } mFilterOutAtlas = itemElem.attribute( QStringLiteral( "legendFilterByAtlas" ), QStringLiteral( "0" ) ).toInt(); // QGIS >= 2.6 QDomElement layerTreeElem = itemElem.firstChildElement( QStringLiteral( "layer-tree" ) ); if ( layerTreeElem.isNull() ) layerTreeElem = itemElem.firstChildElement( QStringLiteral( "layer-tree-group" ) ); if ( !layerTreeElem.isNull() ) { std::unique_ptr< QgsLayerTree > tree( QgsLayerTree::readXml( layerTreeElem ) ); if ( mComposition ) tree->resolveReferences( mComposition->project(), true ); setCustomLayerTree( tree.release() ); } else setCustomLayerTree( nullptr ); //restore general composer item properties QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) ); if ( !composerItemList.isEmpty() ) { QDomElement composerItemElem = composerItemList.at( 0 ).toElement(); _readXml( composerItemElem, doc ); } // < 2.0 projects backward compatibility >>>>> //title font QString titleFontString = itemElem.attribute( QStringLiteral( "titleFont" ) ); if ( !titleFontString.isEmpty() ) { rstyle( QgsLegendStyle::Title ).rfont().fromString( titleFontString ); } //group font QString groupFontString = itemElem.attribute( QStringLiteral( "groupFont" ) ); if ( !groupFontString.isEmpty() ) { rstyle( QgsLegendStyle::Group ).rfont().fromString( groupFontString ); } //layer font QString layerFontString = itemElem.attribute( QStringLiteral( "layerFont" ) ); if ( !layerFontString.isEmpty() ) { rstyle( QgsLegendStyle::Subgroup ).rfont().fromString( layerFontString ); } //item font QString itemFontString = itemElem.attribute( QStringLiteral( "itemFont" ) ); if ( !itemFontString.isEmpty() ) { rstyle( QgsLegendStyle::SymbolLabel ).rfont().fromString( itemFontString ); } if ( !itemElem.attribute( QStringLiteral( "groupSpace" ) ).isEmpty() ) { rstyle( QgsLegendStyle::Group ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "groupSpace" ), QStringLiteral( "3.0" ) ).toDouble() ); } if ( !itemElem.attribute( QStringLiteral( "layerSpace" ) ).isEmpty() ) { rstyle( QgsLegendStyle::Subgroup ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "layerSpace" ), QStringLiteral( "3.0" ) ).toDouble() ); } if ( !itemElem.attribute( QStringLiteral( "symbolSpace" ) ).isEmpty() ) { rstyle( QgsLegendStyle::Symbol ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "symbolSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); rstyle( QgsLegendStyle::SymbolLabel ).setMargin( QgsLegendStyle::Top, itemElem.attribute( QStringLiteral( "symbolSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); } // <<<<<<< < 2.0 projects backward compatibility emit itemChanged(); return true; }
bool QgsLayoutItemLegend::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) { //read general properties mTitle = itemElem.attribute( QStringLiteral( "title" ) ); mSettings.setTitle( mTitle ); if ( !itemElem.attribute( QStringLiteral( "titleAlignment" ) ).isEmpty() ) { mSettings.setTitleAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "titleAlignment" ) ).toInt() ) ); } int colCount = itemElem.attribute( QStringLiteral( "columnCount" ), QStringLiteral( "1" ) ).toInt(); if ( colCount < 1 ) colCount = 1; mColumnCount = colCount; mSettings.setColumnCount( mColumnCount ); mSettings.setSplitLayer( itemElem.attribute( QStringLiteral( "splitLayer" ), QStringLiteral( "0" ) ).toInt() == 1 ); mSettings.setEqualColumnWidth( itemElem.attribute( QStringLiteral( "equalColumnWidth" ), QStringLiteral( "0" ) ).toInt() == 1 ); QDomNodeList stylesNodeList = itemElem.elementsByTagName( QStringLiteral( "styles" ) ); if ( !stylesNodeList.isEmpty() ) { QDomNode stylesNode = stylesNodeList.at( 0 ); for ( int i = 0; i < stylesNode.childNodes().size(); i++ ) { QDomElement styleElem = stylesNode.childNodes().at( i ).toElement(); QgsLegendStyle style; style.readXml( styleElem, doc ); QString name = styleElem.attribute( QStringLiteral( "name" ) ); QgsLegendStyle::Style s; if ( name == QLatin1String( "title" ) ) s = QgsLegendStyle::Title; else if ( name == QLatin1String( "group" ) ) s = QgsLegendStyle::Group; else if ( name == QLatin1String( "subgroup" ) ) s = QgsLegendStyle::Subgroup; else if ( name == QLatin1String( "symbol" ) ) s = QgsLegendStyle::Symbol; else if ( name == QLatin1String( "symbolLabel" ) ) s = QgsLegendStyle::SymbolLabel; else continue; setStyle( s, style ); } } //font color QColor fontClr; fontClr.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) ); mSettings.setFontColor( fontClr ); //spaces mSettings.setBoxSpace( itemElem.attribute( QStringLiteral( "boxSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); mSettings.setColumnSpace( itemElem.attribute( QStringLiteral( "columnSpace" ), QStringLiteral( "2.0" ) ).toDouble() ); mSettings.setSymbolSize( QSizeF( itemElem.attribute( QStringLiteral( "symbolWidth" ), QStringLiteral( "7.0" ) ).toDouble(), itemElem.attribute( QStringLiteral( "symbolHeight" ), QStringLiteral( "14.0" ) ).toDouble() ) ); mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( QStringLiteral( "wmsLegendWidth" ), QStringLiteral( "50" ) ).toDouble(), itemElem.attribute( QStringLiteral( "wmsLegendHeight" ), QStringLiteral( "25" ) ).toDouble() ) ); mSettings.setLineSpacing( itemElem.attribute( QStringLiteral( "lineSpacing" ), QStringLiteral( "1.0" ) ).toDouble() ); mSettings.setDrawRasterStroke( itemElem.attribute( QStringLiteral( "rasterBorder" ), QStringLiteral( "1" ) ) != QLatin1String( "0" ) ); mSettings.setRasterStrokeColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "rasterBorderColor" ), QStringLiteral( "0,0,0" ) ) ) ); mSettings.setRasterStrokeWidth( itemElem.attribute( QStringLiteral( "rasterBorderWidth" ), QStringLiteral( "0" ) ).toDouble() ); mSettings.setWrapChar( itemElem.attribute( QStringLiteral( "wrapChar" ) ) ); mSizeToContents = itemElem.attribute( QStringLiteral( "resizeToContents" ), QStringLiteral( "1" ) ) != QLatin1String( "0" ); // map mLegendFilterByMap = itemElem.attribute( QStringLiteral( "legendFilterByMap" ), QStringLiteral( "0" ) ).toInt(); mMapUuid.clear(); if ( !itemElem.attribute( QStringLiteral( "map_uuid" ) ).isEmpty() ) { mMapUuid = itemElem.attribute( QStringLiteral( "map_uuid" ) ); } // disconnect current map setupMapConnections( mMap, false ); mMap = nullptr; mFilterOutAtlas = itemElem.attribute( QStringLiteral( "legendFilterByAtlas" ), QStringLiteral( "0" ) ).toInt(); // QGIS >= 2.6 QDomElement layerTreeElem = itemElem.firstChildElement( QStringLiteral( "layer-tree" ) ); if ( layerTreeElem.isNull() ) layerTreeElem = itemElem.firstChildElement( QStringLiteral( "layer-tree-group" ) ); if ( !layerTreeElem.isNull() ) { std::unique_ptr< QgsLayerTree > tree( QgsLayerTree::readXml( layerTreeElem, context ) ); if ( mLayout ) tree->resolveReferences( mLayout->project(), true ); setCustomLayerTree( tree.release() ); } else setCustomLayerTree( nullptr ); return true; }