Skip to content
Permalink
Browse files

Add ownership management to the MapLayerRegistry

  • Loading branch information
m-kuhn committed May 24, 2013
1 parent b6a6f3b commit 260b617e420f77d7f7af0f7a45261df4ff0b4837
Showing with 32 additions and 33 deletions.
  1. +14 −14 src/core/qgsmaplayerregistry.cpp
  2. +15 −16 src/core/qgsmaplayerregistry.h
  3. +3 −3 src/mapserver/qgswmsserver.cpp
@@ -73,7 +73,8 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
//introduced in 1.8
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
QList<QgsMapLayer *> theMapLayers,
bool addToLegend )
bool addToLegend,
bool takeOwnership )
{
QList<QgsMapLayer *> myResultList;
for ( int i = 0; i < theMapLayers.size(); ++i )
@@ -85,14 +86,12 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
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() )
if ( !mMapLayers.contains( myLayer->id() ) )
{
mMapLayers[myLayer->id()] = myLayer;
myResultList << mMapLayers[myLayer->id()];
if ( takeOwnership )
mOwnedLayers << myLayer;
emit layerWasAdded( myLayer );
}
}
@@ -109,10 +108,11 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
//this is just a thin wrapper for addMapLayers
QgsMapLayer *
QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer,
bool addToLegend )
bool addToLegend,
bool takeOwnership )
{
QList<QgsMapLayer *> addedLayers;
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend );
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
return addedLayers.isEmpty() ? 0 : addedLayers[0];
}

@@ -124,17 +124,17 @@ void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )

foreach ( const QString &myId, theLayerIds )
{
QgsMapLayer* lyr = mMapLayers[myId];
emit layerWillBeRemoved( myId );
delete mMapLayers[myId];
if ( mOwnedLayers.contains( lyr ) )
{
delete lyr;
mOwnedLayers.remove( lyr );
}
mMapLayers.remove( myId );
}
}

void QgsMapLayerRegistry::clearMapLayers()
{
mMapLayers.clear();
} // QgsMapLayerRegistry::clearMapLayers()

void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
{
removeMapLayers( QStringList( theLayerId ) );
@@ -20,6 +20,7 @@
#define QGSMAPLAYERREGISTRY_H

#include <QMap>
#include <QSet>
#include <QObject>
#include <QStringList>
class QString;
@@ -58,10 +59,13 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
* 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.
* @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.
* @param takeOwnership Ownership will be transferred to the layer registry.
* If you specify false here, you have take care of deleting
* the layers yourself. Not available in python.
*
* @return QList<QgsMapLayer *> - a list of the map layers that were added
* successfully. If a layer is invalid, or already exists in the registry,
@@ -71,7 +75,8 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
* @note Added in QGIS 1.8
*/
QList<QgsMapLayer *> addMapLayers( QList<QgsMapLayer *> theMapLayers,
bool addToLegend = true );
bool addToLegend = true,
bool takeOwnership = true );

/**
* @brief
@@ -86,6 +91,9 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
* @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.
* @param takeOwnership Ownership will be transferred to the layer registry.
* If you specify false here, you have take care of deleting
* the layer yourself. Not available in python.
*
* @return NULL if unable to add layer, otherwise pointer to newly added layer
*
@@ -94,17 +102,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
* @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
* Clears the map layer registry silently. No signals are emitted,
* no layer is deleted. Whatever this is suitable for... The WMS
* server makes use of this.
*
* Not available in python
*/
void clearMapLayers();
QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true, bool takeOwnership = true );

/**
* @brief
@@ -230,6 +228,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
static QgsMapLayerRegistry* mInstance;

QMap<QString, QgsMapLayer*> mMapLayers;
QSet<QgsMapLayer*> mOwnedLayers;

/** debugging member
invoked when a connect() is made to this object
@@ -373,7 +373,7 @@ QImage* QgsWMSServer::getLegendGraphics()
currentY += layerSpace;
}

QgsMapLayerRegistry::instance()->clearMapLayers();
QgsMapLayerRegistry::instance()->removeAllMapLayers();
delete theImage;
return paintImage;
}
@@ -649,7 +649,7 @@ QImage* QgsWMSServer::getMap()
clearFeatureSelections( selectedLayerIdList );

QgsDebugMsg( "clearing filters" );
QgsMapLayerRegistry::instance()->clearMapLayers();
QgsMapLayerRegistry::instance()->removeAllMapLayers();

#ifdef QGISDEBUG
theImage->save( QDir::tempPath() + QDir::separator() + "lastrender.png" );
@@ -1447,7 +1447,7 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
{
layerKeys.push_front( theMapLayer->id() );
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << theMapLayer, false );
QList<QgsMapLayer *>() << theMapLayer, false, false );
}
}
else

0 comments on commit 260b617

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