@@ -611,6 +611,77 @@ Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer )
611611 return ll ? ll->checkState ( 0 ) : Qt::Unchecked;
612612}
613613
614+ void QgsLegend::addEmbeddedGroup ( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent )
615+ {
616+ mEmbeddedGroups .insert ( groupName, projectFilePath );
617+
618+ // open project file, get layer ids in group, add the layers
619+ QFile projectFile ( projectFilePath );
620+ if ( !projectFile.open ( QIODevice::ReadOnly ) )
621+ {
622+ return ;
623+ }
624+
625+ QDomDocument projectDocument;
626+ if ( !projectDocument.setContent ( &projectFile ) )
627+ {
628+ return ;
629+ }
630+
631+ QDomElement legendElem = projectDocument.documentElement ().firstChildElement (" legend" );
632+ if ( legendElem.isNull () )
633+ {
634+ return ;
635+ }
636+
637+ QList<QDomNode> brokenNodes;
638+ QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
639+ QSettings settings;
640+
641+ QDomNodeList legendGroupList = legendElem.elementsByTagName (" legendgroup" );
642+ for ( int i = 0 ; i < legendGroupList.size (); ++i )
643+ {
644+ QDomElement legendElem = legendGroupList.at (i).toElement ();
645+ if ( legendElem.attribute (" name" ) == groupName )
646+ {
647+ QgsLegendGroup* group = 0 ;
648+ if ( parent )
649+ {
650+ group = new QgsLegendGroup ( parent, groupName );
651+ }
652+ else
653+ {
654+ group = new QgsLegendGroup ( this , groupName );
655+ }
656+
657+ QFont groupFont;
658+ groupFont.setItalic ( true );
659+ group->setFont ( 0 , groupFont );
660+ setCurrentItem ( group );
661+
662+ QDomNodeList groupChildren = legendElem.childNodes ();
663+ for ( int j = 0 ; j < groupChildren.size (); ++j )
664+ {
665+ QDomElement childElem = groupChildren.at ( j ).toElement ();
666+ QString tagName = childElem.tagName ();
667+ if ( tagName == " legendlayer" )
668+ {
669+ QString layerId = childElem.firstChildElement (" filegroup" ).firstChildElement (" legendlayerfile" ).attribute (" layerid" );
670+ QgsProject::instance ()->createEmbeddedLayer ( layerId, projectFilePath, brokenNodes, vectorLayerList );
671+ if ( currentItem () )
672+ {
673+ insertItem ( currentItem (), group );
674+ }
675+ }
676+ else if ( tagName == " legendgroup" )
677+ {
678+ addEmbeddedGroup ( childElem.attribute (" name" ), projectFilePath, group );
679+ }
680+ }
681+ }
682+ }
683+ }
684+
614685int QgsLegend::getItemPos ( QTreeWidgetItem* item )
615686{
616687 int counter = 1 ;
0 commit comments