void QgsComposerLegend::setAutoUpdateModel( bool autoUpdate ) { if ( autoUpdate == autoUpdateModel() ) return; setCustomLayerTree( autoUpdate ? 0 : QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() ) ); }
void QgsComposerLegend::setAutoUpdateModel( bool autoUpdate ) { if ( autoUpdate == autoUpdateModel() ) return; setCustomLayerTree( autoUpdate ? nullptr : mComposition->project()->layerTreeRoot()->clone() ); adjustBoxSize(); updateItem(); }
void QgsLayoutItemLegend::setAutoUpdateModel( bool autoUpdate ) { if ( autoUpdate == autoUpdateModel() ) return; setCustomLayerTree( autoUpdate ? nullptr : mLayout->project()->layerTreeRoot()->clone() ); adjustBoxSize(); updateFilterByMap( false ); }
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; }
bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc ) { if ( itemElem.isNull() ) { return false; } //read general properties mSettings.setTitle( itemElem.attribute( "title" ) ); if ( !itemElem.attribute( "titleAlignment" ).isEmpty() ) { mSettings.setTitleAlignment(( Qt::AlignmentFlag )itemElem.attribute( "titleAlignment" ).toInt() ); } int colCount = itemElem.attribute( "columnCount", "1" ).toInt(); if ( colCount < 1 ) colCount = 1; mSettings.setColumnCount( colCount ); mSettings.setSplitLayer( itemElem.attribute( "splitLayer", "0" ).toInt() == 1 ); mSettings.setEqualColumnWidth( itemElem.attribute( "equalColumnWidth", "0" ).toInt() == 1 ); QDomNodeList stylesNodeList = itemElem.elementsByTagName( "styles" ); if ( stylesNodeList.size() > 0 ) { QDomNode stylesNode = stylesNodeList.at( 0 ); for ( int i = 0; i < stylesNode.childNodes().size(); i++ ) { QDomElement styleElem = stylesNode.childNodes().at( i ).toElement(); QgsComposerLegendStyle style; style.readXML( styleElem, doc ); QString name = styleElem.attribute( "name" ); QgsComposerLegendStyle::Style s; if ( name == "title" ) s = QgsComposerLegendStyle::Title; else if ( name == "group" ) s = QgsComposerLegendStyle::Group; else if ( name == "subgroup" ) s = QgsComposerLegendStyle::Subgroup; else if ( name == "symbol" ) s = QgsComposerLegendStyle::Symbol; else if ( name == "symbolLabel" ) s = QgsComposerLegendStyle::SymbolLabel; else continue; setStyle( s, style ); } } //font color QColor fontClr; fontClr.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) ); mSettings.setFontColor( fontClr ); //spaces mSettings.setBoxSpace( itemElem.attribute( "boxSpace", "2.0" ).toDouble() ); mSettings.setColumnSpace( itemElem.attribute( "columnSpace", "2.0" ).toDouble() ); mSettings.setSymbolSize( QSizeF( itemElem.attribute( "symbolWidth", "7.0" ).toDouble(), itemElem.attribute( "symbolHeight", "14.0" ).toDouble() ) ); mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( "wmsLegendWidth", "50" ).toDouble(), itemElem.attribute( "wmsLegendHeight", "25" ).toDouble() ) ); mSettings.setDrawRasterBorder( itemElem.attribute( "rasterBorder", "1" ) != "0" ); mSettings.setRasterBorderColor( QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "rasterBorderColor", "0,0,0" ) ) ); mSettings.setRasterBorderWidth( itemElem.attribute( "rasterBorderWidth", "0" ).toDouble() ); mSettings.setWrapChar( itemElem.attribute( "wrapChar" ) ); //composer map mLegendFilterByMap = itemElem.attribute( "legendFilterByMap", "0" ).toInt(); if ( !itemElem.attribute( "map" ).isEmpty() ) { setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) ); } QDomElement oldLegendModelElem = itemElem.firstChildElement( "Model" ); if ( !oldLegendModelElem.isNull() ) { // QGIS <= 2.4 QgsLayerTreeGroup* nodeRoot = new QgsLayerTreeGroup(); _readOldLegendGroup( oldLegendModelElem, nodeRoot ); setCustomLayerTree( nodeRoot ); } else { // QGIS >= 2.6 QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" ); setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem ) ); } //restore general composer item properties QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" ); if ( composerItemList.size() > 0 ) { QDomElement composerItemElem = composerItemList.at( 0 ).toElement(); _readXML( composerItemElem, doc ); } // < 2.0 projects backward compatibility >>>>> //title font QString titleFontString = itemElem.attribute( "titleFont" ); if ( !titleFontString.isEmpty() ) { rstyle( QgsComposerLegendStyle::Title ).rfont().fromString( titleFontString ); } //group font QString groupFontString = itemElem.attribute( "groupFont" ); if ( !groupFontString.isEmpty() ) { rstyle( QgsComposerLegendStyle::Group ).rfont().fromString( groupFontString ); } //layer font QString layerFontString = itemElem.attribute( "layerFont" ); if ( !layerFontString.isEmpty() ) { rstyle( QgsComposerLegendStyle::Subgroup ).rfont().fromString( layerFontString ); } //item font QString itemFontString = itemElem.attribute( "itemFont" ); if ( !itemFontString.isEmpty() ) { rstyle( QgsComposerLegendStyle::SymbolLabel ).rfont().fromString( itemFontString ); } if ( !itemElem.attribute( "groupSpace" ).isEmpty() ) { rstyle( QgsComposerLegendStyle::Group ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "groupSpace", "3.0" ).toDouble() ); } if ( !itemElem.attribute( "layerSpace" ).isEmpty() ) { rstyle( QgsComposerLegendStyle::Subgroup ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "layerSpace", "3.0" ).toDouble() ); } if ( !itemElem.attribute( "symbolSpace" ).isEmpty() ) { rstyle( QgsComposerLegendStyle::Symbol ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "symbolSpace", "2.0" ).toDouble() ); rstyle( QgsComposerLegendStyle::SymbolLabel ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "symbolSpace", "2.0" ).toDouble() ); } // <<<<<<< < 2.0 projects backward compatibility emit itemChanged(); return true; }