Skip to content
Permalink
Browse files
[API] Added registry support for addition and removal of multiple lay…
…ers in one operation (and notification thereof). Maintains API compatibility with single layer add/remove operations.
  • Loading branch information
timlinux committed Mar 31, 2012
1 parent 767439c commit f9ed6d4dd0d23be9d31f6635023b56d812847fb8
Showing with 105 additions and 30 deletions.
  1. +52 −22 src/core/qgsmaplayerregistry.cpp
  2. +53 −8 src/core/qgsmaplayerregistry.h
@@ -57,38 +57,68 @@ QgsMapLayer * QgsMapLayerRegistry::mapLayer( QString theLayerId )
return mMapLayers.value( theLayerId );
}



QgsMapLayer *
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
//introduced in 1.8
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
QList<QgsMapLayer *> theMapLayers,
bool theEmitSignal )
{
if ( !theMapLayer || !theMapLayer->isValid() )
QList<QgsMapLayer *> myResultList;
for (int i = 0; i < theMapLayers.size(); ++i)
{
QgsDebugMsg( "cannot add invalid layers" );
return 0;
QgsMapLayer * myLayer = theMapLayers.at(i);
if ( !myLayer || !myLayer->isValid() )
{
QgsDebugMsg( "cannot add invalid layers" );
continue;
}
//check the layer is not already registered!
QMap<QString, QgsMapLayer*>::iterator myIterator =
mMapLayers.find( myLayer->id() );
//if myIterator returns mMapLayers.end() then it
//does not exist in registry and its safe to add it
if ( myIterator == mMapLayers.end() )
{
mMapLayers[myLayer->id()] = myLayer;
myResultList << mMapLayers[myLayer->id()];
}
}
if ( theEmitSignal )
emit layersAdded( myResultList );
return myResultList;
} // QgsMapLayerRegistry::addMapLayers

//check the layer is not already registered!
QMap<QString, QgsMapLayer*>::iterator myIterator = mMapLayers.find( theMapLayer->id() );
//if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
if ( myIterator == mMapLayers.end() )
{
mMapLayers[theMapLayer->id()] = theMapLayer;
//this is deprecated by addMapLayers and is just a thin wrapper for that
QgsMapLayer *
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer,
bool theEmitSignal )
{
QList<QgsMapLayer *> myList;
myList.append(theMapLayer);
addMapLayers(myList, theEmitSignal);

if ( theEmitSignal )
emit layerWasAdded( theMapLayer );
if ( theEmitSignal )
emit layerWasAdded( theMapLayer );

return mMapLayers[theMapLayer->id()];
}
else
{
return 0;
}
return theMapLayer;
} // QgsMapLayerRegistry::addMapLayer

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

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

void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, bool theEmitSignal )
//deprecated 1.8 use removeMapLayers rather
void QgsMapLayerRegistry::removeMapLayer( QString theLayerId,
bool theEmitSignal )
{
if ( theEmitSignal )
emit layerWillBeRemoved( theLayerId );
@@ -21,10 +21,8 @@

#include <QMap>
#include <QObject>

#include <QStringList>
class QString;
class QStringList;

class QgsMapLayer;

/** \ingroup core
@@ -59,20 +57,56 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
As a side-effect QgsProject is made dirty.
Emits signal that layer has been added only if theEmitSignal is true (by default).
Not emitting signal is useful when you want to use registry also for layers
which won't be used in main map canvas but will be used in a special one
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.
@note This method is deprecated since QGIS 1.8, you should use addMapLayers rather.
*/
QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal = true );

/** Add a layer 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,
bool theEmitSignal = true );


/** Remove a layer from qgis
@note As a side-effect QgsProject is made dirty. Any canvases using that layer will need to remove it
If theEmitSignal is true (by default), a layerWillBeRemoved( QString theId )
signal will be emitted indicating to any listeners that the layer is being removed.
The layer being removed is deleted as well as the registry
table entry.
@note This method is deprecated since QGIS 1.8, you should use removeMapLayers rather.
*/
void removeMapLayer( QString theLayerId, bool theEmitSignal = true );

/** Remove a set of layers from qgis
@note
As a side-effect QgsProject is made dirty.
Any canvases using that layer will need to remove it
theEmitSignal - see addMapLayer()
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( QString theLayerId, bool theEmitSignal = true );
void removeMapLayers( QStringList theLayerIds, bool theEmitSignal = true );


/** Remove all registered layers
@note raises removedAll()
@@ -95,13 +129,24 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject

signals:

/** Emitted when one or more layers are removed from the registry
@note intended to replace layerWillBeRemoved in QGIS 1.8
*/
void layersWillBeRemoved( QStringList theLayerIds );

/** emitted when a layer is removed from the registry
connected to main map canvas and overview map canvas remove()
@note Deprecated in 1.8 - see layersWillBeRemoved
*/
void layerWillBeRemoved( QString theLayerId );

/** Emitted when one or more layers are added to the registry
@note intended to replace layerWasAdded in QGIS 1.8
*/
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 Deprecated in 1.8 - see layersAdded
*/
void layerWasAdded( QgsMapLayer * theMapLayer );

0 comments on commit f9ed6d4

Please sign in to comment.