@@ -682,54 +682,66 @@ QPair< bool, QList<QDomNode> > QgsProject::_getMapLayers( QDomDocument const &do
682
682
QDomNode node = nl.item ( i );
683
683
QDomElement element = node.toElement ();
684
684
685
- QString type = element.attribute ( " type" );
686
-
687
- QgsDebugMsg ( " Layer type is " + type );
688
-
689
- QgsMapLayer *mapLayer = NULL ;
690
-
691
- if ( type == " vector" )
685
+ if ( element.attribute (" embedded" ) == " 1" )
692
686
{
693
- mapLayer = new QgsVectorLayer;
687
+ createEmbeddedLayer ( element.attribute ( " id" ), readPath ( element.attribute ( " project" ) ) );
688
+ continue ;
694
689
}
695
- else if ( type == " raster" )
696
- {
697
- mapLayer = new QgsRasterLayer;
698
- }
699
- else if ( type == " plugin" )
690
+ else
700
691
{
701
- QString typeName = element.attribute ( " name" );
702
- mapLayer = QgsPluginLayerRegistry::instance ()->createLayer ( typeName );
703
- }
692
+ #if 0
693
+ QString type = element.attribute( "type" );
694
+ QgsDebugMsg( "Layer type is " + type );
695
+ QgsMapLayer *mapLayer = NULL;
696
+
697
+ if ( type == "vector" )
698
+ {
699
+ mapLayer = new QgsVectorLayer;
700
+ }
701
+ else if ( type == "raster" )
702
+ {
703
+ mapLayer = new QgsRasterLayer;
704
+ }
705
+ else if ( type == "plugin" )
706
+ {
707
+ QString typeName = element.attribute( "name" );
708
+ mapLayer = QgsPluginLayerRegistry::instance()->createLayer( typeName );
709
+ }
704
710
705
- Q_CHECK_PTR ( mapLayer );
711
+ Q_CHECK_PTR( mapLayer );
706
712
707
- if ( !mapLayer )
708
- {
709
- QgsDebugMsg ( " Unable to create layer" );
713
+ if ( !mapLayer )
714
+ {
715
+ QgsDebugMsg( "Unable to create layer" );
710
716
711
- return qMakePair ( false , brokenNodes );
712
- }
717
+ return qMakePair( false, brokenNodes );
718
+ }
713
719
714
- // have the layer restore state that is stored in Dom node
715
- if ( mapLayer->readXML ( node ) && mapLayer->isValid () )
716
- {
717
- mapLayer = QgsMapLayerRegistry::instance ()->addMapLayer ( mapLayer );
718
- QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
719
- if ( vLayer && vLayer->vectorJoins ().size () > 0 )
720
+ // have the layer restore state that is stored in Dom node
721
+ if ( mapLayer->readXML( node ) && mapLayer->isValid() )
720
722
{
721
- vLayerList.push_back ( qMakePair ( vLayer, element ) );
723
+ mapLayer = QgsMapLayerRegistry::instance()->addMapLayer( mapLayer );
724
+ QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
725
+ if ( vLayer && vLayer->vectorJoins().size() > 0 )
726
+ {
727
+ vLayerList.push_back( qMakePair( vLayer, element ) );
728
+ }
722
729
}
723
- }
724
- else
725
- {
726
- delete mapLayer;
730
+ else
731
+ {
732
+ delete mapLayer;
727
733
728
- QgsDebugMsg ( " Unable to load " + type + " layer" );
734
+ QgsDebugMsg( "Unable to load " + type + " layer" );
729
735
730
- returnStatus = false ; // flag that we had problems loading layers
736
+ returnStatus = false; // flag that we had problems loading layers
731
737
732
- brokenNodes.push_back ( node );
738
+ brokenNodes.push_back( node );
739
+ }
740
+ #endif // 0
741
+ if ( !addLayer ( element, brokenNodes, vLayerList ) )
742
+ {
743
+ returnStatus = false ;
744
+ }
733
745
}
734
746
emit layerLoaded ( i + 1 , nl.count () );
735
747
}
@@ -754,6 +766,55 @@ QPair< bool, QList<QDomNode> > QgsProject::_getMapLayers( QDomDocument const &do
754
766
} // _getMapLayers
755
767
756
768
769
+ bool QgsProject::addLayer ( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList )
770
+ {
771
+ QString type = layerElem.attribute ( " type" );
772
+ QgsDebugMsg ( " Layer type is " + type );
773
+ QgsMapLayer *mapLayer = NULL ;
774
+
775
+ if ( type == " vector" )
776
+ {
777
+ mapLayer = new QgsVectorLayer;
778
+ }
779
+ else if ( type == " raster" )
780
+ {
781
+ mapLayer = new QgsRasterLayer;
782
+ }
783
+ else if ( type == " plugin" )
784
+ {
785
+ QString typeName = layerElem.attribute ( " name" );
786
+ mapLayer = QgsPluginLayerRegistry::instance ()->createLayer ( typeName );
787
+ }
788
+
789
+ Q_CHECK_PTR ( mapLayer );
790
+
791
+ if ( !mapLayer )
792
+ {
793
+ QgsDebugMsg ( " Unable to create layer" );
794
+
795
+ return false ;
796
+ }
797
+
798
+ // have the layer restore state that is stored in Dom node
799
+ if ( mapLayer->readXML ( layerElem ) && mapLayer->isValid () )
800
+ {
801
+ mapLayer = QgsMapLayerRegistry::instance ()->addMapLayer ( mapLayer );
802
+ QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
803
+ if ( vLayer && vLayer->vectorJoins ().size () > 0 )
804
+ {
805
+ vectorLayerList.push_back ( qMakePair ( vLayer, layerElem ) );
806
+ }
807
+ return true ;
808
+ }
809
+ else
810
+ {
811
+ delete mapLayer;
812
+
813
+ QgsDebugMsg ( " Unable to load " + type + " layer" );
814
+ brokenNodes.push_back ( layerElem );
815
+ return false ;
816
+ }
817
+ }
757
818
758
819
759
820
/* *
@@ -1022,7 +1083,19 @@ bool QgsProject::write()
1022
1083
1023
1084
if ( ml )
1024
1085
{
1025
- ml->writeXML ( projectLayersNode, *doc );
1086
+ QString externalProjectFile = layerIsEmbedded ( ml->id () );
1087
+ if ( externalProjectFile.isEmpty () )
1088
+ {
1089
+ ml->writeXML ( projectLayersNode, *doc );
1090
+ }
1091
+ else // layer defined in an external project file
1092
+ {
1093
+ QDomElement mapLayerElem = doc->createElement (" maplayer" );
1094
+ mapLayerElem.setAttribute (" embedded" , 1 );
1095
+ mapLayerElem.setAttribute (" project" , writePath ( externalProjectFile ) );
1096
+ mapLayerElem.setAttribute (" id" , ml->id () );
1097
+ projectLayersNode.appendChild ( mapLayerElem );
1098
+ }
1026
1099
}
1027
1100
li++;
1028
1101
}
@@ -1529,6 +1602,94 @@ void QgsProject::setBadLayerHandler( QgsProjectBadLayerHandler* handler )
1529
1602
mBadLayerHandler = handler;
1530
1603
}
1531
1604
1605
+ void QgsProject::addEmbeddedLayer ( const QString& layerId, const QString& projectFilePath )
1606
+ {
1607
+ mEmbeddedLayers .insert ( layerId, projectFilePath );
1608
+ }
1609
+
1610
+ QString QgsProject::layerIsEmbedded ( const QString& id ) const
1611
+ {
1612
+ QHash< QString, QString >::const_iterator it = mEmbeddedLayers .find ( id );
1613
+ if ( it == mEmbeddedLayers .constEnd () )
1614
+ {
1615
+ return QString ();
1616
+ }
1617
+ return it.value ();
1618
+ };
1619
+
1620
+ QgsMapLayer* QgsProject::createEmbeddedLayer ( const QString& layerId, const QString& projectFilePath )
1621
+ {
1622
+ QFile projectFile ( projectFilePath );
1623
+ if ( !projectFile.open ( QIODevice::ReadOnly ) )
1624
+ {
1625
+ return 0 ;
1626
+ }
1627
+
1628
+ QDomDocument projectDocument;
1629
+ if ( !projectDocument.setContent ( &projectFile ) )
1630
+ {
1631
+ return 0 ;
1632
+ }
1633
+
1634
+ QDomElement projectLayersElem = projectDocument.documentElement ().firstChildElement (" projectlayers" );
1635
+ if ( projectLayersElem.isNull () )
1636
+ {
1637
+ return 0 ;
1638
+ }
1639
+
1640
+ QDomNodeList mapLayerNodes = projectLayersElem.elementsByTagName (" maplayer" );
1641
+ for ( int i = 0 ; i < mapLayerNodes.size (); ++i )
1642
+ {
1643
+ // get layer id
1644
+ QDomElement mapLayerElem = mapLayerNodes.at (i).toElement ();
1645
+ QString id = mapLayerElem.firstChildElement (" id" ).text ();
1646
+ if ( id == layerId )
1647
+ {
1648
+ #if 0
1649
+ if( !addLayer( element, brokenNodes, vLayerList ) )
1650
+ {
1651
+ returnStatus = false;
1652
+ }
1653
+ #endif // 0
1654
+ QString type = mapLayerElem.attribute (" type" );
1655
+ QgsMapLayer* layer = 0 ;
1656
+ if ( type == " vector" )
1657
+ {
1658
+ layer = new QgsVectorLayer ();
1659
+ }
1660
+ else if ( type == " raster" )
1661
+ {
1662
+ layer = new QgsRasterLayer ();
1663
+ }
1664
+ else if ( type == " plugin" )
1665
+ {
1666
+ QString typeName = mapLayerElem.attribute ( " name" );
1667
+ layer = QgsPluginLayerRegistry::instance ()->createLayer ( typeName );
1668
+ }
1669
+ else
1670
+ {
1671
+ return 0 ;
1672
+ }
1673
+
1674
+ // have the layer restore state that is stored in Dom node
1675
+ if ( layer->readXML ( mapLayerElem ) )
1676
+ {
1677
+ QgsMapLayerRegistry::instance ()->addMapLayer ( layer );
1678
+ QgsProject::instance ()->addEmbeddedLayer ( layerId, projectFilePath );
1679
+ }
1680
+ else
1681
+ {
1682
+ delete layer;
1683
+ QgsDebugMsg ( " unable to load " + type + " layer" );
1684
+ return 0 ;
1685
+ }
1686
+ return layer;
1687
+ }
1688
+ }
1689
+
1690
+ return 0 ;
1691
+ }
1692
+
1532
1693
void QgsProjectBadLayerDefaultHandler::handleBadLayers ( QList<QDomNode> /* layers*/ , QDomDocument /* projectDom*/ )
1533
1694
{
1534
1695
// just ignore any bad layers
0 commit comments