Skip to content

Commit

Permalink
Add ownership management to the MapLayerRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 24, 2013
1 parent b6a6f3b commit 260b617
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
28 changes: 14 additions & 14 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -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 )
Expand All @@ -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 );
}
}
Expand All @@ -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];
}

Expand All @@ -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 ) );
Expand Down
31 changes: 15 additions & 16 deletions src/core/qgsmaplayerregistry.h
Expand Up @@ -20,6 +20,7 @@
#define QGSMAPLAYERREGISTRY_H

#include <QMap>
#include <QSet>
#include <QObject>
#include <QStringList>
class QString;
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -373,7 +373,7 @@ QImage* QgsWMSServer::getLegendGraphics()
currentY += layerSpace;
}

QgsMapLayerRegistry::instance()->clearMapLayers();
QgsMapLayerRegistry::instance()->removeAllMapLayers();
delete theImage;
return paintImage;
}
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 260b617

Please sign in to comment.