Skip to content

Commit f9ed6d4

Browse files
committedMar 31, 2012
[API] Added registry support for addition and removal of multiple layers in one operation (and notification thereof). Maintains API compatibility with single layer add/remove operations.
1 parent 767439c commit f9ed6d4

File tree

2 files changed

+105
-30
lines changed

2 files changed

+105
-30
lines changed
 

‎src/core/qgsmaplayerregistry.cpp

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,68 @@ QgsMapLayer * QgsMapLayerRegistry::mapLayer( QString theLayerId )
5757
return mMapLayers.value( theLayerId );
5858
}
5959

60-
61-
62-
QgsMapLayer *
63-
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
60+
//introduced in 1.8
61+
QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
62+
QList<QgsMapLayer *> theMapLayers,
63+
bool theEmitSignal )
6464
{
65-
if ( !theMapLayer || !theMapLayer->isValid() )
65+
QList<QgsMapLayer *> myResultList;
66+
for (int i = 0; i < theMapLayers.size(); ++i)
6667
{
67-
QgsDebugMsg( "cannot add invalid layers" );
68-
return 0;
68+
QgsMapLayer * myLayer = theMapLayers.at(i);
69+
if ( !myLayer || !myLayer->isValid() )
70+
{
71+
QgsDebugMsg( "cannot add invalid layers" );
72+
continue;
73+
}
74+
//check the layer is not already registered!
75+
QMap<QString, QgsMapLayer*>::iterator myIterator =
76+
mMapLayers.find( myLayer->id() );
77+
//if myIterator returns mMapLayers.end() then it
78+
//does not exist in registry and its safe to add it
79+
if ( myIterator == mMapLayers.end() )
80+
{
81+
mMapLayers[myLayer->id()] = myLayer;
82+
myResultList << mMapLayers[myLayer->id()];
83+
}
6984
}
85+
if ( theEmitSignal )
86+
emit layersAdded( myResultList );
87+
return myResultList;
88+
} // QgsMapLayerRegistry::addMapLayers
7089

71-
//check the layer is not already registered!
72-
QMap<QString, QgsMapLayer*>::iterator myIterator = mMapLayers.find( theMapLayer->id() );
73-
//if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
74-
if ( myIterator == mMapLayers.end() )
75-
{
76-
mMapLayers[theMapLayer->id()] = theMapLayer;
90+
//this is deprecated by addMapLayers and is just a thin wrapper for that
91+
QgsMapLayer *
92+
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer,
93+
bool theEmitSignal )
94+
{
95+
QList<QgsMapLayer *> myList;
96+
myList.append(theMapLayer);
97+
addMapLayers(myList, theEmitSignal);
7798

78-
if ( theEmitSignal )
79-
emit layerWasAdded( theMapLayer );
99+
if ( theEmitSignal )
100+
emit layerWasAdded( theMapLayer );
80101

81-
return mMapLayers[theMapLayer->id()];
82-
}
83-
else
84-
{
85-
return 0;
86-
}
102+
return theMapLayer;
87103
} // QgsMapLayerRegistry::addMapLayer
88104

105+
//introduced in 1.8
106+
void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds,
107+
bool theEmitSignal )
108+
{
109+
if ( theEmitSignal )
110+
emit layersWillBeRemoved( theLayerIds );
89111

112+
foreach (const QString &myId, theLayerIds) {
113+
delete mMapLayers[myId];
114+
mMapLayers.remove( myId );
115+
}
116+
emit layersWillBeRemoved(theLayerIds);
117+
}
90118

91-
void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, bool theEmitSignal )
119+
//deprecated 1.8 use removeMapLayers rather
120+
void QgsMapLayerRegistry::removeMapLayer( QString theLayerId,
121+
bool theEmitSignal )
92122
{
93123
if ( theEmitSignal )
94124
emit layerWillBeRemoved( theLayerId );

‎src/core/qgsmaplayerregistry.h

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121

2222
#include <QMap>
2323
#include <QObject>
24-
24+
#include <QStringList>
2525
class QString;
26-
class QStringList;
27-
2826
class QgsMapLayer;
2927

3028
/** \ingroup core
@@ -59,20 +57,56 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
5957
As a side-effect QgsProject is made dirty.
6058
6159
Emits signal that layer has been added only if theEmitSignal is true (by default).
62-
Not emitting signal is useful when you want to use registry also for layers
63-
which won't be used in main map canvas but will be used in a special one
60+
61+
Not emitting signal is useful when you want to use registry for layers
62+
on a different canvas and don't want them added to the main canvas automatically.
63+
64+
@note This method is deprecated since QGIS 1.8, you should use addMapLayers rather.
6465
*/
6566
QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal = true );
6667

68+
/** Add a layer to the map of loaded layers
69+
@returns QList<QgsMapLayer *> - a list of the map layers that were added
70+
successfully. If a layer is invalid, or already exists in the registry,
71+
it will not be part of the returned QList.
72+
@note added in QGIS 1.8
73+
74+
As a side-effect QgsProject is made dirty.
75+
76+
If theEmitSignal is true (by default), a layersAdded( QList<QgsMapLayer *>)
77+
signal will be emitted indicating that a batch of layers were added.
78+
Not emitting signal is useful when you want to use registry for layers
79+
on a different canvas and don't want them added to the main canvas automatically.
80+
*/
81+
QList<QgsMapLayer *> addMapLayers( QList<QgsMapLayer *> theMapLayers,
82+
bool theEmitSignal = true );
83+
84+
6785
/** Remove a layer from qgis
86+
@note As a side-effect QgsProject is made dirty. Any canvases using that layer will need to remove it
87+
88+
If theEmitSignal is true (by default), a layerWillBeRemoved( QString theId )
89+
signal will be emitted indicating to any listeners that the layer is being removed.
90+
91+
The layer being removed is deleted as well as the registry
92+
table entry.
93+
@note This method is deprecated since QGIS 1.8, you should use removeMapLayers rather.
94+
*/
95+
void removeMapLayer( QString theLayerId, bool theEmitSignal = true );
96+
97+
/** Remove a set of layers from qgis
6898
@note
6999
As a side-effect QgsProject is made dirty.
70-
Any canvases using that layer will need to remove it
71-
theEmitSignal - see addMapLayer()
100+
Any canvases using the affected layers will need to remove them
101+
102+
If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
103+
signal will be emitted indicating to any listeners that the layers are being removed.
104+
72105
The layer being removed is deleted as well as the registry
73106
table entry.
74107
*/
75-
void removeMapLayer( QString theLayerId, bool theEmitSignal = true );
108+
void removeMapLayers( QStringList theLayerIds, bool theEmitSignal = true );
109+
76110

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

96130
signals:
97131

132+
/** Emitted when one or more layers are removed from the registry
133+
@note intended to replace layerWillBeRemoved in QGIS 1.8
134+
*/
135+
void layersWillBeRemoved( QStringList theLayerIds );
136+
98137
/** emitted when a layer is removed from the registry
99138
connected to main map canvas and overview map canvas remove()
139+
@note Deprecated in 1.8 - see layersWillBeRemoved
100140
*/
101141
void layerWillBeRemoved( QString theLayerId );
102142

143+
/** Emitted when one or more layers are added to the registry
144+
@note intended to replace layerWasAdded in QGIS 1.8
145+
*/
146+
void layersAdded( QList<QgsMapLayer *> theMapLayers );
103147
/** emitted when a layer is added to the registry
104148
connected to main map canvas and overview map canvas addLayer()
149+
@note Deprecated in 1.8 - see layersAdded
105150
*/
106151
void layerWasAdded( QgsMapLayer * theMapLayer );
107152

0 commit comments

Comments
 (0)
Please sign in to comment.