Skip to content
Permalink
Browse files

QgsMapLayerRegistry add/remove signals reworked

* "theEmitSignal" parameter renamed to addToLegend
* layer(s)WillBeRemoved has no more "theEmitSingal" parameter
* layer(s)WillBeRemoved and layer(s)(Was)Added emitted in every case
* legendLayersAdded( QList<QgsMapLayer*> theMapLayers ) signal added
* removedAll renamed to removeAll (is emitted BEFORE layers are removed)
  • Loading branch information
m-kuhn committed Apr 20, 2013
1 parent 835a8aa commit 193b6154d213254672061267bac8e6e8ad5ea1f3
@@ -28,104 +28,164 @@ class QgsMapLayerRegistry : QObject
//! Retrieve the mapLayers collection (mainly intended for use by projection)
QMap<QString, QgsMapLayer*> & mapLayers();

/** Add a list of layers to the map of loaded layers
@returns QList<QgsMapLayer *> - a list of the map layers that were added
successfully. If a layer is invalid, or already exists in the registry,
it will not be part of the returned QList.
@note added in QGIS 1.8

As a side-effect QgsProject is made dirty.

If theEmitSignal is true (by default), a layersAdded( QList<QgsMapLayer *>)
signal will be emitted indicating that a batch of layers were added.
Not emitting signal is useful when you want to use registry for layers
on a different canvas and don't want them added to the main canvas automatically.
*/
QList<QgsMapLayer *> addMapLayers( QList<QgsMapLayer *> theMapLayers /Transfer/,
bool theEmitSignal = true );

/** Add a layer to the map of loaded layers
@returns NULL if unable to add layer, otherwise pointer to newly added layer
@see addMapLayers
@note Use addMapLayers if adding more than one layer at a time
*/
QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer /Transfer/, bool theEmitSignal = true );

/** Remove a set of layers from qgis
@note As a side-effect QgsProject is made dirty.
Any canvases using the affected layers will need to remove them

If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
signal will be emitted indicating to any listeners that the layers are being removed.

The layer being removed is deleted as well as the registry
table entry.
*/
void removeMapLayers( QStringList theLayerIds, bool theEmitSignal = true );

/** Remove a layer from qgis
@note As a side-effect QgsProject is made dirty.
Any canvases using the affected layers will need to remove them

If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
signal will be emitted indicating to any listeners that the layers are being removed.

The layer being removed is deleted as well as the registry
table entry.
*/
void removeMapLayer( const QString& theLayerId, bool theEmitSignal = true );

/** Remove all registered layers
@note raises removedAll()
As a side-effect QgsProject is made dirty.
@note The layers are deleted as the registry is cleared!
*/
/**
* @brief
* Add a list of layers to the map of loaded layers
*
* The layersAdded() and layersWasAdded() signals will be emitted in any case.
* The legendLayersAdded() signal only if addToLegend is true.
*
* @param theMapLayers A list of layer which should be added to the registry
* @param addToLegend If true (by default), the layers will be added to the
* legend and to the main canvas. If you have a private
* layer, you can set this parameter to false to hide it.
*
* @return QList<QgsMapLayer *> - a list of the map layers that were added
* successfully. If a layer is invalid, or already exists in the registry,
* it will not be part of the returned QList.
*
* @note As a side-effect QgsProject is made dirty.
* @note Added in QGIS 1.8
*/
QList<QgsMapLayer *> addMapLayers(QList<QgsMapLayer *> theMapLayers,
bool addToLegend = true );

/**
* @brief
* Add a layer to the map of loaded layers
*
* The layersAdded() and layersWasAdded() signals will be emitted in any case.
* The legendLayersAdded() signal only if addToLegend is true.
* If you are adding multiple layers at once, you should use
* {@link addMapLayers()} instead.
*
* @param theMapLayer A layer to add to the registry
* @param addToLegend If true (by default), the layer will be added to the
* legend and to the main canvas. If you have a private
* you can set this parameter to false to hide it.
*
* @return NULL if unable to add layer, otherwise pointer to newly added layer
*
* @see addMapLayers
*
* @note As a side-effect QgsProject is made dirty.
* @note Use addMapLayers if adding more than one layer at a time
*/
QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true );

/**
* @brief
* Remove a set of layers from the registry
*
* Any canvases using the affected layers will need to remove them
*
* The layers being removed are deleted as well as the registry
* table entries.
*
* @param theLayerIds The ids of the layers to remove
*
* @note As a side-effect QgsProject is made dirty.
*/
void removeMapLayers(QStringList theLayerIds );

/**
* @brief
* Remove a layer from qgis
*
* Any canvases using the affected layers will need to remove them
*
* The layer being removed is deleted as well as the registry
* table entry.
*
* @param theLayerId The id of the layer to remove
*
* @note As a side-effect QgsProject is made dirty.
*/
void removeMapLayer( const QString& theLayerId );

/**
* Remove all registered layers
*
* @note As a side-effect QgsProject is made dirty.
* @note The layers are deleted as the registry is cleared!
*/
void removeAllMapLayers();

/* Clears all layer caches, resetting them to zero and
/**
* Clears all layer caches, resetting them to zero and
* freeing up any memory they may have been using. Layer
* caches are used to speed up rendering in certain situations
* see ticket #1974 for more details.
* @note this method was added in QGIS 1.4
*
* @note Added in QGIS 1.4
*/
void clearAllLayerCaches();

/**Reload all provider data caches (currently used for WFS and WMS providers)
@note: this method was added in QGIS 1.6*/
/**
* Reload all provider data caches (currently used for WFS and WMS providers)
*
* @note Added in QGIS 1.6
*/
void reloadAllLayers();

signals:
/** Emitted when one or more layers are removed from the registry
@note intended to replace layerWillBeRemoved in QGIS 1.8
*/
/**
* Emitted when one or more layers are removed from the registry
*
* @param theLayerIds A list of ids of the layers which are removed.
*/
void layersWillBeRemoved( QStringList theLayerIds );

/** emitted when a layer is removed from the registry
connected to main map canvas and overview map canvas remove()
@note we should deprecate this at some stage
*/
/**
* Emitted when a layer is removed from the registry
*
* @param theLayerId The id of the layer being removed
*
* @note Consider using {@link layersWillBeRemoved()} instead
*/
void layerWillBeRemoved( QString theLayerId );

/** Emitted when one or more layers are added to the registry
@note intended to replace layerWasAdded in QGIS 1.8
*/
/**
* Emitted, when all layers are removed, before {@link layersWillBeRemoved()} and
* {@link layerWillBeRemoved()} signals are emitted. You will still get these signals
* in any case.
* You can use this signal to do easy (and fast) cleanup.
*
* @note Added in 2.0
*/
void removeAll();

/**
* Emitted when one or more layers are added to the registry.
* This signal is also emitted for layers added to the registry,
* but not to the legend and canvas.
*
* @param theMapLayers The layers which have been added
*
* @see legendLayersAdded()
*/
void layersAdded( QList<QgsMapLayer *> theMapLayers );

/** emitted when a layer is added to the registry
connected to main map canvas and overview map canvas addLayer()
@note we should deprecate this at some stage
*/
void layerWasAdded( QgsMapLayer * theMapLayer );

/** emitted when ALL layers are removed at once
This could have been implemented by iteratively signalling
layerWillBeRemoved() for each layer as it is removed. However, this
generally causes a cascade of effects that are unnecessary if we're
ultimately removing all layers. E.g., removing the legend item
corresponding to the layer. Why bother doing that when you're just going
to clear everything anyway?
/**
* Emitted when a layer is added to the registry.
*
* @param theMapLayer The id of the layer which has been added
*
* @note Consider using {@link layersAdded()} instead
*/
void layerWasAdded( QgsMapLayer* theMapLayer );

/**
* Emitted, when a layer is added to the registry and the legend.
* Plugins are allowed to have private layers, which are signalled by
* {@link layersAdded()} and {@link layerWasAdded()} but will not be
* advertised by this signal.
*
* @param theMapLayers The {@link QgsMapLayer}s which are added to the legend.
*
* @note Added in 2.0
*/
void removedAll();
void legendLayersAdded( QList<QgsMapLayer*> theMapLayers );

protected:
//! protected constructor
@@ -149,7 +149,7 @@ def __init__(self):

LayerRegistry.layers = self.getAllLayers()
LayerRegistry._instance = self
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("removedAll()"), self.removeAllLayers)
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("removeAll()"), self.removeAllLayers)
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWasAdded(QgsMapLayer *)"), self.layerAdded)
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWillBeRemoved(QString)"), self.removeLayer)

@@ -104,10 +104,10 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
connect( QgsMapLayerRegistry::instance(),
SIGNAL( layersWillBeRemoved( QStringList ) ),
this, SLOT( removeLayers( QStringList ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( removedAll() ),
connect( QgsMapLayerRegistry::instance(), SIGNAL( removeAll() ),
this, SLOT( removeAll() ) );
connect( QgsMapLayerRegistry::instance(),
SIGNAL( layersAdded( QList<QgsMapLayer*> ) ),
SIGNAL( legendLayersAdded( QList<QgsMapLayer*> ) ),
this, SLOT( addLayers( QList<QgsMapLayer *> ) ) );

connect( mMapCanvas, SIGNAL( layersChanged() ),
@@ -73,7 +73,7 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
//introduced in 1.8
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
QList<QgsMapLayer *> theMapLayers,
bool theEmitSignal )
bool addToLegend )
{
QList<QgsMapLayer *> myResultList;
for ( int i = 0; i < theMapLayers.size(); ++i )
@@ -93,67 +93,55 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
{
mMapLayers[myLayer->id()] = myLayer;
myResultList << mMapLayers[myLayer->id()];
if ( theEmitSignal )
emit layerWasAdded( myLayer );
emit layerWasAdded( myLayer );
}
}
if ( theEmitSignal && myResultList.count() > 0 )
if ( myResultList.count() > 0 )
{
emit layersAdded( myResultList );

if ( addToLegend )
emit legendLayersAdded( myResultList );
}
return myResultList;
} // QgsMapLayerRegistry::addMapLayers

//this is just a thin wrapper for addMapLayers
QgsMapLayer *
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer,
bool theEmitSignal )
QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer,
bool addToLegend )
{
QList<QgsMapLayer *> myList, myList2;
myList.append( theMapLayer );
myList2 = addMapLayers( myList, theEmitSignal );
return myList2.isEmpty() ? 0 : myList2[0];
QList<QgsMapLayer *> addedLayers;
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend );
return addedLayers.isEmpty() ? 0 : addedLayers[0];
}


//introduced in 1.8
void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds,
bool theEmitSignal )
void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )
{
if ( theEmitSignal )
emit layersWillBeRemoved( theLayerIds );
emit layersWillBeRemoved( theLayerIds );

foreach ( const QString &myId, theLayerIds )
{
if ( theEmitSignal )
emit layerWillBeRemoved( myId );
emit layerWillBeRemoved( myId );
delete mMapLayers[myId];
mMapLayers.remove( myId );
}
emit layersWillBeRemoved( theLayerIds );
}

void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId, bool theEmitSignal )
void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
{
removeMapLayers( QStringList( theLayerId ), theEmitSignal );
removeMapLayers( QStringList( theLayerId ) );
}


void QgsMapLayerRegistry::removeAllMapLayers()
{
// moved before physically removing the layers
emit removedAll();

emit removeAll();
// now let all canvas observers know to clear themselves,
// and then consequently any of their map legends
QStringList myList;
QMap<QString, QgsMapLayer *>::iterator it;
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
{
QString id = it.key();
myList << id;
}
removeMapLayers( myList, false );
removeMapLayers( mMapLayers.keys() );
mMapLayers.clear();
} // QgsMapLayerRegistry::removeAllMapLayers()

0 comments on commit 193b615

Please sign in to comment.
You can’t perform that action at this time.