Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add API to QgsMapLayerRegistry to rely less on ids
  • Loading branch information
m-kuhn committed Dec 1, 2015
1 parent 17bd46e commit 4d67951
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 12 deletions.
48 changes: 47 additions & 1 deletion python/core/qgsmaplayerregistry.sip
Expand Up @@ -95,6 +95,21 @@ class QgsMapLayerRegistry : QObject
*/
void removeMapLayers( const QStringList& theLayerIds );

/**
* @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 layers The layers to remove
*
* @note As a side-effect QgsProject is made dirty.

This comment has been minimized.

Copy link
@elpaso

elpaso Dec 5, 2015

Contributor

I'm afraid this will never be accessible from python unless you give it another name.

This comment has been minimized.

Copy link
@elpaso

elpaso Dec 5, 2015

Contributor

Perhaps worth renaming theLayerIds to theLayers (in the C side too)

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Dec 5, 2015

Author Member

Hmm... both work here:

QgsMapLayerRegistry.instance().removeMapLayers([iface.activeLayer()])
QgsMapLayerRegistry.instance().removeMapLayers([iface.activeLayer().id()])

The var name is only wrong in sip, will take care of it as soon as above issue is sorted out

This comment has been minimized.

Copy link
@elpaso

elpaso Dec 5, 2015

Contributor

You're right, It's working here too. I probably tested it on an older build. Sorry.

*/
void removeMapLayers( const QList<QgsMapLayer*>& theLayerIds );

/**
* @brief
* Remove a layer from qgis
Expand All @@ -110,6 +125,21 @@ class QgsMapLayerRegistry : QObject
*/
void removeMapLayer( const QString& theLayerId );

/**
* @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 layer The layer to remove
*
* @note As a side-effect QgsProject is made dirty.
*/
void removeMapLayer( QgsMapLayer* layer );

/**
* Remove all registered layers
*
Expand Down Expand Up @@ -141,14 +171,30 @@ class QgsMapLayerRegistry : QObject
void layersWillBeRemoved( const QStringList& theLayerIds );

/**
* Emitted when a layer is removed from the registry
* Emitted when one or more layers are removed from the registry
*
* @param theLayerIds A list layers which are removed.
*/
void layersWillBeRemoved( const QList<QgsMapLayer*>& layer );

/**
* Emitted when an owned layer is removed from the registry
*
* @param theLayerId The id of the layer being removed
*
* @note Consider using {@link layersWillBeRemoved()} instead
*/
void layerWillBeRemoved( const QString& theLayerId );

/**
* Emitted when an owned layer is removed from the registry
*
* @param layer The layer being removed
*
* @note Consider using {@link layersWillBeRemoved()} instead
*/
void layerWillBeRemoved( QgsMapLayer* layer );

/**
* Emitted after one or more layers were removed from the registry
*
Expand Down
40 changes: 31 additions & 9 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -116,26 +116,52 @@ QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer,
//introduced in 1.8
void QgsMapLayerRegistry::removeMapLayers( const QStringList& theLayerIds )
{
emit layersWillBeRemoved( theLayerIds );

QList<QgsMapLayer*> layers;
Q_FOREACH ( const QString &myId, theLayerIds )
{
QgsMapLayer* lyr = mMapLayers[myId];
layers << mMapLayers[myId];
}

removeMapLayers( layers );
}

void QgsMapLayerRegistry::removeMapLayers( const QList<QgsMapLayer*>& layers )
{
QStringList layerIds;

Q_FOREACH ( QgsMapLayer* layer, layers )
{
layerIds << layer->id();
}

emit layersWillBeRemoved( layerIds );
emit layersWillBeRemoved( layers );

Q_FOREACH ( QgsMapLayer* lyr, layers )
{
QString myId( lyr->id() );
if ( mOwnedLayers.contains( lyr ) )
{
emit layerWillBeRemoved( myId );
emit layerWillBeRemoved( lyr );
delete lyr;
mOwnedLayers.remove( lyr );
}
mMapLayers.remove( myId );
emit layerRemoved( myId );
}
emit layersRemoved( theLayerIds );

emit layersRemoved( layerIds );
}

void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
{
removeMapLayers( QStringList( theLayerId ) );
removeMapLayers( QList<QgsMapLayer*>() << mMapLayers[theLayerId] );
}

void QgsMapLayerRegistry::removeMapLayer( QgsMapLayer* layer )
{
removeMapLayers( QList<QgsMapLayer*>() << layer );
}

void QgsMapLayerRegistry::removeAllMapLayers()
Expand All @@ -147,10 +173,6 @@ void QgsMapLayerRegistry::removeAllMapLayers()
mMapLayers.clear();
} // QgsMapLayerRegistry::removeAllMapLayers()

void QgsMapLayerRegistry::clearAllLayerCaches()
{
}

void QgsMapLayerRegistry::reloadAllLayers()
{
QMap<QString, QgsMapLayer *>::iterator it;
Expand Down
50 changes: 48 additions & 2 deletions src/core/qgsmaplayerregistry.h
Expand Up @@ -120,6 +120,21 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
*/
void removeMapLayers( const QStringList& theLayerIds );

/**
* @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 layers The layers to remove
*
* @note As a side-effect QgsProject is made dirty.
*/
void removeMapLayers( const QList<QgsMapLayer*>& theLayerIds );

/**
* @brief
* Remove a layer from qgis
Expand All @@ -135,6 +150,21 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
*/
void removeMapLayer( const QString& theLayerId );

/**
* @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 layer The layer to remove
*
* @note As a side-effect QgsProject is made dirty.
*/
void removeMapLayer( QgsMapLayer* layer );

/**
* Remove all registered layers
*
Expand All @@ -150,7 +180,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
* see ticket #1974 for more details.
*/
//! @deprecated since 2.4 - does nothing
Q_DECL_DEPRECATED void clearAllLayerCaches();
Q_DECL_DEPRECATED void clearAllLayerCaches() {}

/**
* Reload all provider data caches (currently used for WFS and WMS providers)
Expand All @@ -166,14 +196,30 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
void layersWillBeRemoved( const QStringList& theLayerIds );

/**
* Emitted when a layer is removed from the registry
* Emitted when one or more layers are removed from the registry
*
* @param theLayerIds A list layers which are removed.
*/
void layersWillBeRemoved( const QList<QgsMapLayer*>& layer );

/**
* Emitted when an owned layer is removed from the registry
*
* @param theLayerId The id of the layer being removed
*
* @note Consider using {@link layersWillBeRemoved()} instead
*/
void layerWillBeRemoved( const QString& theLayerId );

/**
* Emitted when an owned layer is removed from the registry
*
* @param layer The layer being removed
*
* @note Consider using {@link layersWillBeRemoved()} instead
*/
void layerWillBeRemoved( QgsMapLayer* layer );

/**
* Emitted after one or more layers were removed from the registry
*
Expand Down

0 comments on commit 4d67951

Please sign in to comment.