|
24 | 24 | #include "qgsexception.h"
|
25 | 25 | #include "qgslayertreenode.h"
|
26 | 26 | #include "qgslayertreeutils.h"
|
| 27 | +#include "qgslayertreeregistrybridge.h" |
27 | 28 | #include "qgslogger.h"
|
28 | 29 | #include "qgsmaplayerregistry.h"
|
29 | 30 | #include "qgspluginlayer.h"
|
@@ -334,10 +335,15 @@ QgsProject::QgsProject()
|
334 | 335 | : imp_( new QgsProject::Imp )
|
335 | 336 | , mBadLayerHandler( new QgsProjectBadLayerDefaultHandler() )
|
336 | 337 | , mRelationManager( new QgsRelationManager( this ) )
|
337 |
| - , mRootGroup( 0 ) |
| 338 | + , mRootGroup( new QgsLayerTreeGroup ) |
338 | 339 | {
|
339 | 340 | clear();
|
340 | 341 |
|
| 342 | + // bind the layer tree to the map layer registry. |
| 343 | + // whenever layers are added to or removed from the registry, |
| 344 | + // layer tree will be updated |
| 345 | + new QgsLayerTreeRegistryBridge(mRootGroup, this); |
| 346 | + |
341 | 347 | } // QgsProject ctor
|
342 | 348 |
|
343 | 349 |
|
@@ -419,8 +425,7 @@ void QgsProject::clear()
|
419 | 425 | mEmbeddedLayers.clear();
|
420 | 426 | mRelationManager->clear();
|
421 | 427 |
|
422 |
| - delete mRootGroup; |
423 |
| - mRootGroup = new QgsLayerTreeGroup; |
| 428 | + mRootGroup->removeChildren(0, mRootGroup->children().count()); |
424 | 429 |
|
425 | 430 | // reset some default project properties
|
426 | 431 | // XXX THESE SHOULD BE MOVED TO STATUSBAR RELATED SOURCE
|
@@ -898,23 +903,17 @@ bool QgsProject::read()
|
898 | 903 |
|
899 | 904 | // read the layer tree from project file
|
900 | 905 |
|
901 |
| - QgsLayerTreeGroup* newRoot = 0; |
902 | 906 | QDomElement layerTreeElem = doc->documentElement().firstChildElement( "layer-tree-group" );
|
903 | 907 | if ( !layerTreeElem.isNull() )
|
904 | 908 | {
|
905 |
| - newRoot = QgsLayerTreeGroup::readXML( layerTreeElem ); |
| 909 | + mRootGroup->readChildrenFromXML( layerTreeElem ); |
906 | 910 | }
|
907 | 911 | else
|
908 | 912 | {
|
909 |
| - newRoot = QgsLayerTreeUtils::readOldLegend( doc->documentElement().firstChildElement( "legend" ) ); |
| 913 | + QgsLayerTreeUtils::readOldLegend( mRootGroup, doc->documentElement().firstChildElement( "legend" ) ); |
910 | 914 | }
|
911 | 915 |
|
912 |
| - if ( newRoot ) |
913 |
| - { |
914 |
| - delete mRootGroup; |
915 |
| - mRootGroup = newRoot; |
916 |
| - QgsDebugMsg( "Loaded layer tree:\n " + mRootGroup->dump() ); |
917 |
| - } |
| 916 | + QgsDebugMsg( "Loaded layer tree:\n " + mRootGroup->dump() ); |
918 | 917 |
|
919 | 918 | // get the map layers
|
920 | 919 | QPair< bool, QList<QDomNode> > getMapLayersResults = _getMapLayers( *doc );
|
|
0 commit comments