Skip to content

Commit

Permalink
Add possibility to temporarily disable layer tree to registry bridge
Browse files Browse the repository at this point in the history
This is useful during the project load where the updates from
map layer registry would make the layers appear twice in the tree
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 129d295 commit 1906de1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Expand Up @@ -7,6 +7,7 @@
QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, QObject *parent)
: QObject(parent)
, mRoot(root)
, mEnabled(true)
{
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersAdded(QList<QgsMapLayer*>)), this, SLOT(layersAdded(QList<QgsMapLayer*>)));
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersWillBeRemoved(QStringList)), this, SLOT(layersWillBeRemoved(QStringList)));
Expand All @@ -16,6 +17,9 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root,

void QgsLayerTreeRegistryBridge::layersAdded(QList<QgsMapLayer*> layers)
{
if (!mEnabled)
return;

foreach (QgsMapLayer* layer, layers)
{
mRoot->addLayer(layer);
Expand All @@ -24,6 +28,9 @@ void QgsLayerTreeRegistryBridge::layersAdded(QList<QgsMapLayer*> layers)

void QgsLayerTreeRegistryBridge::layersWillBeRemoved(QStringList layerIds)
{
if (!mEnabled)
return;

foreach (QString layerId, layerIds)
{
QgsLayerTreeLayer* nodeLayer = mRoot->findLayer(layerId);
Expand Down
4 changes: 4 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.h
Expand Up @@ -17,6 +17,9 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
public:
explicit QgsLayerTreeRegistryBridge(QgsLayerTreeGroup* root, QObject *parent = 0);

void setEnabled(bool enabled) { mEnabled = enabled; }
bool isEnabled() const { return mEnabled; }

signals:

protected slots:
Expand All @@ -33,6 +36,7 @@ protected slots:
protected:
QgsLayerTreeGroup* mRoot;
QStringList mLayerIdsForRemoval;
bool mEnabled;
};

#endif // QGSLAYERTREEREGISTRYBRIDGE_H
6 changes: 5 additions & 1 deletion src/core/qgsproject.cpp
Expand Up @@ -342,7 +342,7 @@ QgsProject::QgsProject()
// bind the layer tree to the map layer registry.
// whenever layers are added to or removed from the registry,
// layer tree will be updated
new QgsLayerTreeRegistryBridge(mRootGroup, this);
mLayerTreeRegistryBridge = new QgsLayerTreeRegistryBridge(mRootGroup, this);

} // QgsProject ctor

Expand Down Expand Up @@ -915,6 +915,8 @@ bool QgsProject::read()

QgsDebugMsg( "Loaded layer tree:\n " + mRootGroup->dump() );

mLayerTreeRegistryBridge->setEnabled( false );

// get the map layers
QPair< bool, QList<QDomNode> > getMapLayersResults = _getMapLayers( *doc );

Expand All @@ -935,6 +937,8 @@ bool QgsProject::read()
mBadLayerHandler->handleBadLayers( getMapLayersResults.second, *doc );
}

mLayerTreeRegistryBridge->setEnabled( true );

// read the project: used by map canvas and legend
emit readProject( *doc );

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -40,6 +40,7 @@ class QDomElement;
class QDomNode;

class QgsLayerTreeGroup;
class QgsLayerTreeRegistryBridge;
class QgsMapLayer;
class QgsProjectBadLayerHandler;
class QgsRelationManager;
Expand Down Expand Up @@ -417,6 +418,8 @@ class CORE_EXPORT QgsProject : public QObject

QgsLayerTreeGroup* mRootGroup;

QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;

}; // QgsProject


Expand Down

0 comments on commit 1906de1

Please sign in to comment.