Skip to content

Commit 193b615

Browse files
committed
QgsMapLayerRegistry add/remove signals reworked
* "theEmitSignal" parameter renamed to addToLegend * layer(s)WillBeRemoved has no more "theEmitSingal" parameter * layer(s)WillBeRemoved and layer(s)(Was)Added emitted in every case * legendLayersAdded( QList<QgsMapLayer*> theMapLayers ) signal added * removedAll renamed to removeAll (is emitted BEFORE layers are removed)
1 parent 835a8aa commit 193b615

File tree

7 files changed

+303
-194
lines changed

7 files changed

+303
-194
lines changed

python/core/qgsmaplayerregistry.sip

+140-80
Original file line numberDiff line numberDiff line change
@@ -28,104 +28,164 @@ class QgsMapLayerRegistry : QObject
2828
//! Retrieve the mapLayers collection (mainly intended for use by projection)
2929
QMap<QString, QgsMapLayer*> & mapLayers();
3030

31-
/** Add a list of layers to the map of loaded layers
32-
@returns QList<QgsMapLayer *> - a list of the map layers that were added
33-
successfully. If a layer is invalid, or already exists in the registry,
34-
it will not be part of the returned QList.
35-
@note added in QGIS 1.8
36-
37-
As a side-effect QgsProject is made dirty.
38-
39-
If theEmitSignal is true (by default), a layersAdded( QList<QgsMapLayer *>)
40-
signal will be emitted indicating that a batch of layers were added.
41-
Not emitting signal is useful when you want to use registry for layers
42-
on a different canvas and don't want them added to the main canvas automatically.
43-
*/
44-
QList<QgsMapLayer *> addMapLayers( QList<QgsMapLayer *> theMapLayers /Transfer/,
45-
bool theEmitSignal = true );
46-
47-
/** Add a layer to the map of loaded layers
48-
@returns NULL if unable to add layer, otherwise pointer to newly added layer
49-
@see addMapLayers
50-
@note Use addMapLayers if adding more than one layer at a time
51-
*/
52-
QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer /Transfer/, bool theEmitSignal = true );
53-
54-
/** Remove a set of layers from qgis
55-
@note As a side-effect QgsProject is made dirty.
56-
Any canvases using the affected layers will need to remove them
57-
58-
If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
59-
signal will be emitted indicating to any listeners that the layers are being removed.
60-
61-
The layer being removed is deleted as well as the registry
62-
table entry.
63-
*/
64-
void removeMapLayers( QStringList theLayerIds, bool theEmitSignal = true );
65-
66-
/** Remove a layer from qgis
67-
@note As a side-effect QgsProject is made dirty.
68-
Any canvases using the affected layers will need to remove them
69-
70-
If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
71-
signal will be emitted indicating to any listeners that the layers are being removed.
72-
73-
The layer being removed is deleted as well as the registry
74-
table entry.
75-
*/
76-
void removeMapLayer( const QString& theLayerId, bool theEmitSignal = true );
77-
78-
/** Remove all registered layers
79-
@note raises removedAll()
80-
As a side-effect QgsProject is made dirty.
81-
@note The layers are deleted as the registry is cleared!
82-
*/
31+
/**
32+
* @brief
33+
* Add a list of layers to the map of loaded layers
34+
*
35+
* The layersAdded() and layersWasAdded() signals will be emitted in any case.
36+
* The legendLayersAdded() signal only if addToLegend is true.
37+
*
38+
* @param theMapLayers A list of layer which should be added to the registry
39+
* @param addToLegend If true (by default), the layers will be added to the
40+
* legend and to the main canvas. If you have a private
41+
* layer, you can set this parameter to false to hide it.
42+
*
43+
* @return QList<QgsMapLayer *> - a list of the map layers that were added
44+
* successfully. If a layer is invalid, or already exists in the registry,
45+
* it will not be part of the returned QList.
46+
*
47+
* @note As a side-effect QgsProject is made dirty.
48+
* @note Added in QGIS 1.8
49+
*/
50+
QList<QgsMapLayer *> addMapLayers(QList<QgsMapLayer *> theMapLayers,
51+
bool addToLegend = true );
52+
53+
/**
54+
* @brief
55+
* Add a layer to the map of loaded layers
56+
*
57+
* The layersAdded() and layersWasAdded() signals will be emitted in any case.
58+
* The legendLayersAdded() signal only if addToLegend is true.
59+
* If you are adding multiple layers at once, you should use
60+
* {@link addMapLayers()} instead.
61+
*
62+
* @param theMapLayer A layer to add to the registry
63+
* @param addToLegend If true (by default), the layer will be added to the
64+
* legend and to the main canvas. If you have a private
65+
* you can set this parameter to false to hide it.
66+
*
67+
* @return NULL if unable to add layer, otherwise pointer to newly added layer
68+
*
69+
* @see addMapLayers
70+
*
71+
* @note As a side-effect QgsProject is made dirty.
72+
* @note Use addMapLayers if adding more than one layer at a time
73+
*/
74+
QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true );
75+
76+
/**
77+
* @brief
78+
* Remove a set of layers from the registry
79+
*
80+
* Any canvases using the affected layers will need to remove them
81+
*
82+
* The layers being removed are deleted as well as the registry
83+
* table entries.
84+
*
85+
* @param theLayerIds The ids of the layers to remove
86+
*
87+
* @note As a side-effect QgsProject is made dirty.
88+
*/
89+
void removeMapLayers(QStringList theLayerIds );
90+
91+
/**
92+
* @brief
93+
* Remove a layer from qgis
94+
*
95+
* Any canvases using the affected layers will need to remove them
96+
*
97+
* The layer being removed is deleted as well as the registry
98+
* table entry.
99+
*
100+
* @param theLayerId The id of the layer to remove
101+
*
102+
* @note As a side-effect QgsProject is made dirty.
103+
*/
104+
void removeMapLayer( const QString& theLayerId );
105+
106+
/**
107+
* Remove all registered layers
108+
*
109+
* @note As a side-effect QgsProject is made dirty.
110+
* @note The layers are deleted as the registry is cleared!
111+
*/
83112
void removeAllMapLayers();
84113

85-
/* Clears all layer caches, resetting them to zero and
114+
/**
115+
* Clears all layer caches, resetting them to zero and
86116
* freeing up any memory they may have been using. Layer
87117
* caches are used to speed up rendering in certain situations
88118
* see ticket #1974 for more details.
89-
* @note this method was added in QGIS 1.4
119+
*
120+
* @note Added in QGIS 1.4
90121
*/
91122
void clearAllLayerCaches();
92123

93-
/**Reload all provider data caches (currently used for WFS and WMS providers)
94-
@note: this method was added in QGIS 1.6*/
124+
/**
125+
* Reload all provider data caches (currently used for WFS and WMS providers)
126+
*
127+
* @note Added in QGIS 1.6
128+
*/
95129
void reloadAllLayers();
96130

97131
signals:
98-
/** Emitted when one or more layers are removed from the registry
99-
@note intended to replace layerWillBeRemoved in QGIS 1.8
100-
*/
132+
/**
133+
* Emitted when one or more layers are removed from the registry
134+
*
135+
* @param theLayerIds A list of ids of the layers which are removed.
136+
*/
101137
void layersWillBeRemoved( QStringList theLayerIds );
102138

103-
/** emitted when a layer is removed from the registry
104-
connected to main map canvas and overview map canvas remove()
105-
@note we should deprecate this at some stage
106-
*/
139+
/**
140+
* Emitted when a layer is removed from the registry
141+
*
142+
* @param theLayerId The id of the layer being removed
143+
*
144+
* @note Consider using {@link layersWillBeRemoved()} instead
145+
*/
107146
void layerWillBeRemoved( QString theLayerId );
108147

109-
/** Emitted when one or more layers are added to the registry
110-
@note intended to replace layerWasAdded in QGIS 1.8
111-
*/
148+
/**
149+
* Emitted, when all layers are removed, before {@link layersWillBeRemoved()} and
150+
* {@link layerWillBeRemoved()} signals are emitted. You will still get these signals
151+
* in any case.
152+
* You can use this signal to do easy (and fast) cleanup.
153+
*
154+
* @note Added in 2.0
155+
*/
156+
void removeAll();
157+
158+
/**
159+
* Emitted when one or more layers are added to the registry.
160+
* This signal is also emitted for layers added to the registry,
161+
* but not to the legend and canvas.
162+
*
163+
* @param theMapLayers The layers which have been added
164+
*
165+
* @see legendLayersAdded()
166+
*/
112167
void layersAdded( QList<QgsMapLayer *> theMapLayers );
113168

114-
/** emitted when a layer is added to the registry
115-
connected to main map canvas and overview map canvas addLayer()
116-
@note we should deprecate this at some stage
117-
*/
118-
void layerWasAdded( QgsMapLayer * theMapLayer );
119-
120-
/** emitted when ALL layers are removed at once
121-
This could have been implemented by iteratively signalling
122-
layerWillBeRemoved() for each layer as it is removed. However, this
123-
generally causes a cascade of effects that are unnecessary if we're
124-
ultimately removing all layers. E.g., removing the legend item
125-
corresponding to the layer. Why bother doing that when you're just going
126-
to clear everything anyway?
169+
/**
170+
* Emitted when a layer is added to the registry.
171+
*
172+
* @param theMapLayer The id of the layer which has been added
173+
*
174+
* @note Consider using {@link layersAdded()} instead
175+
*/
176+
void layerWasAdded( QgsMapLayer* theMapLayer );
177+
178+
/**
179+
* Emitted, when a layer is added to the registry and the legend.
180+
* Plugins are allowed to have private layers, which are signalled by
181+
* {@link layersAdded()} and {@link layerWasAdded()} but will not be
182+
* advertised by this signal.
183+
*
184+
* @param theMapLayers The {@link QgsMapLayer}s which are added to the legend.
185+
*
186+
* @note Added in 2.0
127187
*/
128-
void removedAll();
188+
void legendLayersAdded( QList<QgsMapLayer*> theMapLayers );
129189

130190
protected:
131191
//! protected constructor

python/plugins/GdalTools/tools/GdalTools_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(self):
149149

150150
LayerRegistry.layers = self.getAllLayers()
151151
LayerRegistry._instance = self
152-
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("removedAll()"), self.removeAllLayers)
152+
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("removeAll()"), self.removeAllLayers)
153153
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWasAdded(QgsMapLayer *)"), self.layerAdded)
154154
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWillBeRemoved(QString)"), self.removeLayer)
155155

src/app/legend/qgslegend.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
104104
connect( QgsMapLayerRegistry::instance(),
105105
SIGNAL( layersWillBeRemoved( QStringList ) ),
106106
this, SLOT( removeLayers( QStringList ) ) );
107-
connect( QgsMapLayerRegistry::instance(), SIGNAL( removedAll() ),
107+
connect( QgsMapLayerRegistry::instance(), SIGNAL( removeAll() ),
108108
this, SLOT( removeAll() ) );
109109
connect( QgsMapLayerRegistry::instance(),
110-
SIGNAL( layersAdded( QList<QgsMapLayer*> ) ),
110+
SIGNAL( legendLayersAdded( QList<QgsMapLayer*> ) ),
111111
this, SLOT( addLayers( QList<QgsMapLayer *> ) ) );
112112

113113
connect( mMapCanvas, SIGNAL( layersChanged() ),

src/core/qgsmaplayerregistry.cpp

+18-30
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
7373
//introduced in 1.8
7474
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
7575
QList<QgsMapLayer *> theMapLayers,
76-
bool theEmitSignal )
76+
bool addToLegend )
7777
{
7878
QList<QgsMapLayer *> myResultList;
7979
for ( int i = 0; i < theMapLayers.size(); ++i )
@@ -93,67 +93,55 @@ QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
9393
{
9494
mMapLayers[myLayer->id()] = myLayer;
9595
myResultList << mMapLayers[myLayer->id()];
96-
if ( theEmitSignal )
97-
emit layerWasAdded( myLayer );
96+
emit layerWasAdded( myLayer );
9897
}
9998
}
100-
if ( theEmitSignal && myResultList.count() > 0 )
99+
if ( myResultList.count() > 0 )
101100
{
102101
emit layersAdded( myResultList );
102+
103+
if ( addToLegend )
104+
emit legendLayersAdded( myResultList );
103105
}
104106
return myResultList;
105107
} // QgsMapLayerRegistry::addMapLayers
106108

107109
//this is just a thin wrapper for addMapLayers
108110
QgsMapLayer *
109-
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer,
110-
bool theEmitSignal )
111+
QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer,
112+
bool addToLegend )
111113
{
112-
QList<QgsMapLayer *> myList, myList2;
113-
myList.append( theMapLayer );
114-
myList2 = addMapLayers( myList, theEmitSignal );
115-
return myList2.isEmpty() ? 0 : myList2[0];
114+
QList<QgsMapLayer *> addedLayers;
115+
addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend );
116+
return addedLayers.isEmpty() ? 0 : addedLayers[0];
116117
}
117118

118119

119120
//introduced in 1.8
120-
void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds,
121-
bool theEmitSignal )
121+
void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )
122122
{
123-
if ( theEmitSignal )
124-
emit layersWillBeRemoved( theLayerIds );
123+
emit layersWillBeRemoved( theLayerIds );
125124

126125
foreach ( const QString &myId, theLayerIds )
127126
{
128-
if ( theEmitSignal )
129-
emit layerWillBeRemoved( myId );
127+
emit layerWillBeRemoved( myId );
130128
delete mMapLayers[myId];
131129
mMapLayers.remove( myId );
132130
}
133-
emit layersWillBeRemoved( theLayerIds );
134131
}
135132

136-
void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId, bool theEmitSignal )
133+
void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
137134
{
138-
removeMapLayers( QStringList( theLayerId ), theEmitSignal );
135+
removeMapLayers( QStringList( theLayerId ) );
139136
}
140137

141138

142139
void QgsMapLayerRegistry::removeAllMapLayers()
143140
{
144-
// moved before physically removing the layers
145-
emit removedAll();
146-
141+
emit removeAll();
147142
// now let all canvas observers know to clear themselves,
148143
// and then consequently any of their map legends
149-
QStringList myList;
150-
QMap<QString, QgsMapLayer *>::iterator it;
151-
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
152-
{
153-
QString id = it.key();
154-
myList << id;
155-
}
156-
removeMapLayers( myList, false );
144+
removeMapLayers( mMapLayers.keys() );
157145
mMapLayers.clear();
158146
} // QgsMapLayerRegistry::removeAllMapLayers()
159147

0 commit comments

Comments
 (0)