Skip to content

Commit 260b617

Browse files
committed
Add ownership management to the MapLayerRegistry
1 parent b6a6f3b commit 260b617

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

src/core/qgsmaplayerregistry.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
7373
//introduced in 1.8
7474
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
7575
QList<QgsMapLayer *> theMapLayers,
76-
bool addToLegend )
76+
bool addToLegend,
77+
bool takeOwnership )
7778
{
7879
QList<QgsMapLayer *> myResultList;
7980
for ( int i = 0; i < theMapLayers.size(); ++i )
@@ -85,14 +86,12 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
8586
continue;
8687
}
8788
//check the layer is not already registered!
88-
QMap<QString, QgsMapLayer*>::iterator myIterator =
89-
mMapLayers.find( myLayer->id() );
90-
//if myIterator returns mMapLayers.end() then it
91-
//does not exist in registry and its safe to add it
92-
if ( myIterator == mMapLayers.end() )
89+
if ( !mMapLayers.contains( myLayer->id() ) )
9390
{
9491
mMapLayers[myLayer->id()] = myLayer;
9592
myResultList << mMapLayers[myLayer->id()];
93+
if ( takeOwnership )
94+
mOwnedLayers << myLayer;
9695
emit layerWasAdded( myLayer );
9796
}
9897
}
@@ -109,10 +108,11 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
109108
//this is just a thin wrapper for addMapLayers
110109
QgsMapLayer *
111110
QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer,
112-
bool addToLegend )
111+
bool addToLegend,
112+
bool takeOwnership )
113113
{
114114
QList<QgsMapLayer *> addedLayers;
115-
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend );
115+
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
116116
return addedLayers.isEmpty() ? 0 : addedLayers[0];
117117
}
118118

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

125125
foreach ( const QString &myId, theLayerIds )
126126
{
127+
QgsMapLayer* lyr = mMapLayers[myId];
127128
emit layerWillBeRemoved( myId );
128-
delete mMapLayers[myId];
129+
if ( mOwnedLayers.contains( lyr ) )
130+
{
131+
delete lyr;
132+
mOwnedLayers.remove( lyr );
133+
}
129134
mMapLayers.remove( myId );
130135
}
131136
}
132137

133-
void QgsMapLayerRegistry::clearMapLayers()
134-
{
135-
mMapLayers.clear();
136-
} // QgsMapLayerRegistry::clearMapLayers()
137-
138138
void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
139139
{
140140
removeMapLayers( QStringList( theLayerId ) );

src/core/qgsmaplayerregistry.h

+15-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define QGSMAPLAYERREGISTRY_H
2121

2222
#include <QMap>
23+
#include <QSet>
2324
#include <QObject>
2425
#include <QStringList>
2526
class QString;
@@ -58,10 +59,13 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
5859
* The layersAdded() and layersWasAdded() signals will be emitted in any case.
5960
* The legendLayersAdded() signal only if addToLegend is true.
6061
*
61-
* @param theMapLayers A list of layer which should be added to the registry
62-
* @param addToLegend If true (by default), the layers will be added to the
63-
* legend and to the main canvas. If you have a private
64-
* layer, you can set this parameter to false to hide it.
62+
* @param theMapLayers A list of layer which should be added to the registry
63+
* @param addToLegend If true (by default), the layers will be added to the
64+
* legend and to the main canvas. If you have a private
65+
* layer, you can set this parameter to false to hide it.
66+
* @param takeOwnership Ownership will be transferred to the layer registry.
67+
* If you specify false here, you have take care of deleting
68+
* the layers yourself. Not available in python.
6569
*
6670
* @return QList<QgsMapLayer *> - a list of the map layers that were added
6771
* successfully. If a layer is invalid, or already exists in the registry,
@@ -71,7 +75,8 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
7175
* @note Added in QGIS 1.8
7276
*/
7377
QList<QgsMapLayer *> addMapLayers( QList<QgsMapLayer *> theMapLayers,
74-
bool addToLegend = true );
78+
bool addToLegend = true,
79+
bool takeOwnership = true );
7580

7681
/**
7782
* @brief
@@ -86,6 +91,9 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
8691
* @param addToLegend If true (by default), the layer will be added to the
8792
* legend and to the main canvas. If you have a private
8893
* you can set this parameter to false to hide it.
94+
* @param takeOwnership Ownership will be transferred to the layer registry.
95+
* If you specify false here, you have take care of deleting
96+
* the layer yourself. Not available in python.
8997
*
9098
* @return NULL if unable to add layer, otherwise pointer to newly added layer
9199
*
@@ -94,17 +102,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
94102
* @note As a side-effect QgsProject is made dirty.
95103
* @note Use addMapLayers if adding more than one layer at a time
96104
*/
97-
QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true );
98-
99-
/**
100-
* @brief
101-
* Clears the map layer registry silently. No signals are emitted,
102-
* no layer is deleted. Whatever this is suitable for... The WMS
103-
* server makes use of this.
104-
*
105-
* Not available in python
106-
*/
107-
void clearMapLayers();
105+
QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true, bool takeOwnership = true );
108106

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

232230
QMap<QString, QgsMapLayer*> mMapLayers;
231+
QSet<QgsMapLayer*> mOwnedLayers;
233232

234233
/** debugging member
235234
invoked when a connect() is made to this object

src/mapserver/qgswmsserver.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ QImage* QgsWMSServer::getLegendGraphics()
373373
currentY += layerSpace;
374374
}
375375

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

651651
QgsDebugMsg( "clearing filters" );
652-
QgsMapLayerRegistry::instance()->clearMapLayers();
652+
QgsMapLayerRegistry::instance()->removeAllMapLayers();
653653

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

0 commit comments

Comments
 (0)