Skip to content

Commit f9bd83c

Browse files
committedMay 2, 2017
Split off map layer storage handling from QgsProject to QgsMapLayerStore
Turns out that there's valid use cases for having layer stores outside of the cost of using a whole QgsProject. Partially reverts the merger of QgsMapLayerRegistry into QgsProject, except that there's no singletons here.
1 parent 8e70aa8 commit f9bd83c

File tree

7 files changed

+1316
-0
lines changed

7 files changed

+1316
-0
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
%Include qgsmaplayermodel.sip
9393
%Include qgsmaplayerproxymodel.sip
9494
%Include qgsmaplayerrenderer.sip
95+
%Include qgsmaplayerstore.sip
9596
%Include qgsmaplayerstylemanager.sip
9697
%Include qgsmaprenderercache.sip
9798
%Include qgsmaprenderercustompainterjob.sip

‎python/core/qgsmaplayerstore.sip

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsmaplayerstore.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
14+
class QgsMapLayerStore : QObject
15+
{
16+
%Docstring
17+
A storage object for map layers, in which the layers are owned by the
18+
store and have their lifetime bound to the store.
19+
.. versionadded:: 3.0
20+
%End
21+
22+
%TypeHeaderCode
23+
#include "qgsmaplayerstore.h"
24+
%End
25+
public:
26+
27+
explicit QgsMapLayerStore( QObject *parent /TransferThis/ = 0 );
28+
%Docstring
29+
Constructor for QgsMapLayerStore.
30+
%End
31+
32+
~QgsMapLayerStore();
33+
34+
int count() const;
35+
%Docstring
36+
Returns the number of layers contained in the store.
37+
:rtype: int
38+
%End
39+
40+
41+
int __len__() const;
42+
%Docstring
43+
Returns the number of layers contained in the store.
44+
:rtype: int
45+
%End
46+
%MethodCode
47+
sipRes = sipCpp->count();
48+
%End
49+
50+
QgsMapLayer *mapLayer( const QString &id ) const;
51+
%Docstring
52+
Retrieve a pointer to a layer by layer ``id``.
53+
\param id ID of layer to retrieve
54+
:return: matching layer, or None if no matching layer found
55+
.. seealso:: mapLayersByName()
56+
.. seealso:: mapLayers()
57+
:rtype: QgsMapLayer
58+
%End
59+
60+
QList<QgsMapLayer *> mapLayersByName( const QString &name ) const;
61+
%Docstring
62+
Retrieve a list of matching layers by layer ``name``.
63+
\param name name of layers to match
64+
:return: list of matching layers
65+
.. seealso:: mapLayer()
66+
.. seealso:: mapLayers()
67+
:rtype: list of QgsMapLayer
68+
%End
69+
70+
QMap<QString, QgsMapLayer *> mapLayers() const;
71+
%Docstring
72+
Returns a map of all layers by layer ID.
73+
.. seealso:: mapLayer()
74+
.. seealso:: mapLayersByName()
75+
.. seealso:: layers()
76+
:rtype: QMap<str, QgsMapLayer *>
77+
%End
78+
79+
80+
QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &layers /Transfer/ );
81+
%Docstring
82+
\brief
83+
Add a list of ``layers`` to the store. Ownership of the layers is transferred
84+
to the store.
85+
86+
The layersAdded() and layerWasAdded() signals will always be emitted.
87+
88+
\param layers A list of layer which should be added to the store.
89+
90+
:return: a list of the map layers that were added
91+
successfully. If a layer is invalid, or already exists in the store,
92+
it will not be part of the returned list.
93+
94+
.. seealso:: addMapLayer()
95+
:rtype: list of QgsMapLayer
96+
%End
97+
98+
QgsMapLayer *addMapLayer( QgsMapLayer *layer /Transfer/ );
99+
%Docstring
100+
\brief
101+
Add a ``layer`` to the store. Ownership of the layer is transferred to the
102+
store.
103+
104+
The layersAdded() and layerWasAdded() signals will always be emitted.
105+
If you are adding multiple layers at once, you should use
106+
addMapLayers() instead.
107+
108+
\param layer A layer to add to the store
109+
110+
:return: None if unable to add layer, otherwise pointer to newly added layer
111+
112+
.. seealso:: addMapLayers
113+
114+
.. note::
115+
116+
Use addMapLayers() if adding more than one layer at a time.
117+
.. seealso:: addMapLayers()
118+
:rtype: QgsMapLayer
119+
%End
120+
121+
void removeMapLayers( const QStringList &layerIds ) /PyName=removeMapLayersById/;
122+
%Docstring
123+
\brief
124+
Remove a set of layers from the store by layer ID.
125+
126+
The specified layers will be removed from the store.
127+
These layers will also be deleted.
128+
129+
\param layerIds list of IDs of the layers to remove
130+
131+
.. seealso:: takeMapLayer()
132+
.. seealso:: removeMapLayer()
133+
.. seealso:: removeAllMapLayers()
134+
.. note::
135+
136+
available in Python bindings as removeMapLayersById.
137+
%End
138+
139+
void removeMapLayers( const QList<QgsMapLayer *> &layers );
140+
141+
void removeMapLayer( const QString &id );
142+
%Docstring
143+
\brief
144+
Remove a layer from the store by layer ``id``.
145+
146+
The specified layer will be removed from the store. The layer will also be deleted.
147+
148+
\param id ID of the layer to remove
149+
150+
.. seealso:: takeMapLayer()
151+
.. seealso:: removeMapLayers()
152+
.. seealso:: removeAllMapLayers()
153+
%End
154+
155+
void removeMapLayer( QgsMapLayer *layer );
156+
%Docstring
157+
\brief
158+
Remove a ``layer`` from the store.
159+
160+
The specified layer will be removed from the store. The layer will also be deleted.
161+
162+
\param layer The layer to remove. Null pointers are ignored.
163+
164+
.. seealso:: takeMapLayer()
165+
.. seealso:: removeMapLayers()
166+
.. seealso:: removeAllMapLayers()
167+
%End
168+
169+
QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) /TransferBack/;
170+
%Docstring
171+
Takes a ``layer`` from the store. If the layer was owned by the store, the
172+
layer will be returned without deleting it. The caller takes ownership of
173+
the layer and is responsible for deleting it.
174+
.. seealso:: removeMapLayer()
175+
:rtype: QgsMapLayer
176+
%End
177+
178+
void removeAllMapLayers();
179+
%Docstring
180+
Removes all registered layers. These layers will also be deleted.
181+
182+
.. note::
183+
184+
Calling this method will cause the removeAll() signal to
185+
be emitted.
186+
.. seealso:: removeMapLayer()
187+
.. seealso:: removeMapLayers()
188+
%End
189+
190+
signals:
191+
192+
void layersWillBeRemoved( const QStringList &layerIds );
193+
%Docstring
194+
Emitted when one or more layers are about to be removed from the store.
195+
196+
\param layerIds A list of IDs for the layers which are to be removed.
197+
.. seealso:: layerWillBeRemoved()
198+
.. seealso:: layersRemoved()
199+
%End
200+
201+
void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
202+
%Docstring
203+
Emitted when one or more layers are about to be removed from the store.
204+
205+
\param layers A list of layers which are to be removed.
206+
.. seealso:: layerWillBeRemoved()
207+
.. seealso:: layersRemoved()
208+
%End
209+
210+
void layerWillBeRemoved( const QString &layerId );
211+
%Docstring
212+
Emitted when a layer is about to be removed from the store.
213+
214+
\param layerId The ID of the layer to be removed.
215+
216+
.. note::
217+
218+
Consider using layersWillBeRemoved() instead.
219+
.. seealso:: layersWillBeRemoved()
220+
.. seealso:: layerRemoved()
221+
%End
222+
223+
void layerWillBeRemoved( QgsMapLayer *layer );
224+
%Docstring
225+
Emitted when a layer is about to be removed from the store.
226+
227+
\param layer The layer to be removed.
228+
229+
.. note::
230+
231+
Consider using layersWillBeRemoved() instead.
232+
.. seealso:: layersWillBeRemoved()
233+
.. seealso:: layerRemoved()
234+
%End
235+
236+
void layersRemoved( const QStringList &layerIds );
237+
%Docstring
238+
Emitted after one or more layers were removed from the store.
239+
240+
\param layerIds A list of IDs of the layers which were removed.
241+
.. seealso:: layersWillBeRemoved()
242+
%End
243+
244+
void layerRemoved( const QString &layerId );
245+
%Docstring
246+
Emitted after a layer was removed from the store.
247+
248+
\param layerId The ID of the layer removed.
249+
250+
.. note::
251+
252+
Consider using layersRemoved() instead
253+
.. seealso:: layerWillBeRemoved()
254+
%End
255+
256+
void allLayersRemoved();
257+
%Docstring
258+
Emitted when all layers are removed, before layersWillBeRemoved() and
259+
layerWillBeRemoved() signals are emitted. The layersWillBeRemoved() and
260+
layerWillBeRemoved() signals will still be emitted following this signal.
261+
You can use this signal to do easy (and fast) cleanup.
262+
%End
263+
264+
void layersAdded( const QList<QgsMapLayer *> &layers );
265+
%Docstring
266+
Emitted when one or more layers were added to the store.
267+
268+
\param layers List of layers which have been added.
269+
270+
.. seealso:: legendLayersAdded()
271+
.. seealso:: layerWasAdded()
272+
%End
273+
274+
void layerWasAdded( QgsMapLayer *layer );
275+
%Docstring
276+
Emitted when a ``layer`` was added to the store.
277+
278+
.. note::
279+
280+
Consider using layersAdded() instead
281+
.. seealso:: layersAdded()
282+
%End
283+
284+
};
285+
286+
/************************************************************************
287+
* This file has been generated automatically from *
288+
* *
289+
* src/core/qgsmaplayerstore.h *
290+
* *
291+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
292+
************************************************************************/

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ SET(QGIS_CORE_SRCS
176176
qgsmaplayerlegend.cpp
177177
qgsmaplayermodel.cpp
178178
qgsmaplayerproxymodel.cpp
179+
qgsmaplayerstore.cpp
179180
qgsmaplayerstylemanager.cpp
180181
qgsmaprenderercache.cpp
181182
qgsmaprenderercustompainterjob.cpp
@@ -525,6 +526,7 @@ SET(QGIS_CORE_MOC_HDRS
525526
qgsmaplayerlegend.h
526527
qgsmaplayermodel.h
527528
qgsmaplayerproxymodel.h
529+
qgsmaplayerstore.h
528530
qgsmaplayerstylemanager.h
529531
qgsmaprenderercache.h
530532
qgsmaprenderercustompainterjob.h

‎src/core/qgsmaplayerstore.cpp

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/***************************************************************************
2+
qgsmaplayerstore.cpp
3+
--------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsmaplayerstore.h"
19+
#include "qgslogger.h"
20+
21+
QgsMapLayerStore::QgsMapLayerStore( QObject *parent )
22+
: QObject( parent )
23+
{}
24+
25+
QgsMapLayerStore::~QgsMapLayerStore()
26+
{
27+
removeAllMapLayers();
28+
}
29+
30+
int QgsMapLayerStore::count() const
31+
{
32+
return mMapLayers.size();
33+
}
34+
35+
QgsMapLayer *QgsMapLayerStore::mapLayer( const QString &layerId ) const
36+
{
37+
return mMapLayers.value( layerId );
38+
}
39+
40+
QList<QgsMapLayer *> QgsMapLayerStore::mapLayersByName( const QString &layerName ) const
41+
{
42+
QList<QgsMapLayer *> myResultList;
43+
Q_FOREACH ( QgsMapLayer *layer, mMapLayers )
44+
{
45+
if ( layer->name() == layerName )
46+
{
47+
myResultList << layer;
48+
}
49+
}
50+
return myResultList;
51+
}
52+
53+
QList<QgsMapLayer *> QgsMapLayerStore::addMapLayers(
54+
const QList<QgsMapLayer *> &layers )
55+
{
56+
QList<QgsMapLayer *> myResultList;
57+
Q_FOREACH ( QgsMapLayer *myLayer, layers )
58+
{
59+
if ( !myLayer || !myLayer->isValid() )
60+
{
61+
QgsDebugMsg( "Cannot add invalid layers" );
62+
continue;
63+
}
64+
//check the layer is not already registered!
65+
if ( !mMapLayers.contains( myLayer->id() ) )
66+
{
67+
mMapLayers[myLayer->id()] = myLayer;
68+
myResultList << mMapLayers[myLayer->id()];
69+
myLayer->setParent( this );
70+
connect( myLayer, &QObject::destroyed, this, &QgsMapLayerStore::onMapLayerDeleted );
71+
emit layerWasAdded( myLayer );
72+
}
73+
}
74+
if ( !myResultList.isEmpty() )
75+
{
76+
emit layersAdded( myResultList );
77+
}
78+
return myResultList;
79+
}
80+
81+
QgsMapLayer *
82+
QgsMapLayerStore::addMapLayer( QgsMapLayer *layer )
83+
{
84+
QList<QgsMapLayer *> addedLayers;
85+
addedLayers = addMapLayers( QList<QgsMapLayer *>() << layer );
86+
return addedLayers.isEmpty() ? nullptr : addedLayers[0];
87+
}
88+
89+
void QgsMapLayerStore::removeMapLayers( const QStringList &layerIds )
90+
{
91+
QList<QgsMapLayer *> layers;
92+
Q_FOREACH ( const QString &myId, layerIds )
93+
{
94+
layers << mMapLayers.value( myId );
95+
}
96+
97+
removeMapLayers( layers );
98+
}
99+
100+
void QgsMapLayerStore::removeMapLayers( const QList<QgsMapLayer *> &layers )
101+
{
102+
if ( layers.isEmpty() )
103+
return;
104+
105+
QStringList layerIds;
106+
QList<QgsMapLayer *> layerList;
107+
108+
Q_FOREACH ( QgsMapLayer *layer, layers )
109+
{
110+
// check layer and the store contains it
111+
if ( layer && mMapLayers.contains( layer->id() ) )
112+
{
113+
layerIds << layer->id();
114+
layerList << layer;
115+
}
116+
}
117+
118+
if ( layerIds.isEmpty() )
119+
return;
120+
121+
emit layersWillBeRemoved( layerIds );
122+
emit layersWillBeRemoved( layerList );
123+
124+
Q_FOREACH ( QgsMapLayer *lyr, layerList )
125+
{
126+
QString myId( lyr->id() );
127+
emit layerWillBeRemoved( myId );
128+
emit layerWillBeRemoved( lyr );
129+
mMapLayers.remove( myId );
130+
if ( lyr->parent() == this )
131+
{
132+
delete lyr;
133+
}
134+
emit layerRemoved( myId );
135+
}
136+
137+
emit layersRemoved( layerIds );
138+
}
139+
140+
void QgsMapLayerStore::removeMapLayer( const QString &layerId )
141+
{
142+
removeMapLayers( QList<QgsMapLayer *>() << mMapLayers.value( layerId ) );
143+
}
144+
145+
void QgsMapLayerStore::removeMapLayer( QgsMapLayer *layer )
146+
{
147+
if ( layer )
148+
removeMapLayers( QList<QgsMapLayer *>() << layer );
149+
}
150+
151+
QgsMapLayer *QgsMapLayerStore::takeMapLayer( QgsMapLayer *layer )
152+
{
153+
if ( !layer )
154+
return nullptr;
155+
156+
if ( mMapLayers.contains( layer->id() ) )
157+
{
158+
emit layersWillBeRemoved( QStringList() << layer->id() );
159+
emit layersWillBeRemoved( QList<QgsMapLayer *>() << layer );
160+
emit layerWillBeRemoved( layer->id() );
161+
emit layerWillBeRemoved( layer );
162+
163+
mMapLayers.remove( layer->id() );
164+
layer->setParent( nullptr );
165+
emit layerRemoved( layer->id() );
166+
emit layersRemoved( QStringList() << layer->id() );
167+
return layer;
168+
}
169+
return nullptr; //don't return layer - it wasn't owned and accordingly we aren't transferring ownership
170+
}
171+
172+
void QgsMapLayerStore::removeAllMapLayers()
173+
{
174+
emit allLayersRemoved();
175+
// now let all observers know to clear themselves,
176+
// and then consequently any of their map legends
177+
removeMapLayers( mMapLayers.keys() );
178+
mMapLayers.clear();
179+
}
180+
181+
void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
182+
{
183+
QString id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );
184+
185+
if ( !id.isNull() )
186+
{
187+
QgsDebugMsg( QString( "Map layer deleted without unregistering! %1" ).arg( id ) );
188+
mMapLayers.remove( id );
189+
}
190+
}
191+
192+
QMap<QString, QgsMapLayer *> QgsMapLayerStore::mapLayers() const
193+
{
194+
return mMapLayers;
195+
}

‎src/core/qgsmaplayerstore.h

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
/***************************************************************************
2+
qgsmaplayerstore.h
3+
------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
19+
#ifndef QGSMAPLAYERSTORE_H
20+
#define QGSMAPLAYERSTORE_H
21+
22+
#include "qgis_core.h"
23+
#include "qgis.h"
24+
#include "qgsmaplayer.h"
25+
#include <QObject>
26+
27+
/**
28+
* \class QgsMapLayerStore
29+
* \ingroup core
30+
* A storage object for map layers, in which the layers are owned by the
31+
* store and have their lifetime bound to the store.
32+
* \since QGIS 3.0
33+
*/
34+
35+
class CORE_EXPORT QgsMapLayerStore : public QObject
36+
{
37+
Q_OBJECT
38+
39+
public:
40+
41+
/**
42+
* Constructor for QgsMapLayerStore.
43+
*/
44+
explicit QgsMapLayerStore( QObject *parent SIP_TRANSFERTHIS = nullptr );
45+
46+
~QgsMapLayerStore();
47+
48+
/**
49+
* Returns the number of layers contained in the store.
50+
*/
51+
int count() const;
52+
53+
#ifdef SIP_RUN
54+
55+
/**
56+
* Returns the number of layers contained in the store.
57+
*/
58+
int __len__() const;
59+
% MethodCode
60+
sipRes = sipCpp->count();
61+
% End
62+
#endif
63+
64+
/**
65+
* Retrieve a pointer to a layer by layer \a id.
66+
* \param id ID of layer to retrieve
67+
* \returns matching layer, or nullptr if no matching layer found
68+
* \see mapLayersByName()
69+
* \see mapLayers()
70+
*/
71+
QgsMapLayer *mapLayer( const QString &id ) const;
72+
73+
/**
74+
* Retrieve a list of matching layers by layer \a name.
75+
* \param name name of layers to match
76+
* \returns list of matching layers
77+
* \see mapLayer()
78+
* \see mapLayers()
79+
*/
80+
QList<QgsMapLayer *> mapLayersByName( const QString &name ) const;
81+
82+
/**
83+
* Returns a map of all layers by layer ID.
84+
* \see mapLayer()
85+
* \see mapLayersByName()
86+
* \see layers()
87+
*/
88+
QMap<QString, QgsMapLayer *> mapLayers() const;
89+
90+
#ifndef SIP_RUN
91+
92+
/**
93+
* Returns a list of registered map layers with a specified layer type.
94+
*
95+
* Example:
96+
*
97+
* QVector<QgsVectorLayer*> vectorLayers = store->layers<QgsVectorLayer*>();
98+
*
99+
* \note not available in Python bindings
100+
* \see mapLayers()
101+
*/
102+
template <typename T>
103+
QVector<T> layers() const
104+
{
105+
QVector<T> layers;
106+
QMap<QString, QgsMapLayer *>::const_iterator layerIt = mMapLayers.constBegin();
107+
for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
108+
{
109+
T tLayer = qobject_cast<T>( layerIt.value() );
110+
if ( tLayer )
111+
{
112+
layers << tLayer;
113+
}
114+
}
115+
return layers;
116+
}
117+
#endif
118+
119+
/**
120+
* \brief
121+
* Add a list of \a layers to the store. Ownership of the layers is transferred
122+
* to the store.
123+
*
124+
* The layersAdded() and layerWasAdded() signals will always be emitted.
125+
*
126+
* \param layers A list of layer which should be added to the store.
127+
*
128+
* \returns a list of the map layers that were added
129+
* successfully. If a layer is invalid, or already exists in the store,
130+
* it will not be part of the returned list.
131+
*
132+
* \see addMapLayer()
133+
*/
134+
QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &layers SIP_TRANSFER );
135+
136+
/**
137+
* \brief
138+
* Add a \a layer to the store. Ownership of the layer is transferred to the
139+
* store.
140+
*
141+
* The layersAdded() and layerWasAdded() signals will always be emitted.
142+
* If you are adding multiple layers at once, you should use
143+
* addMapLayers() instead.
144+
*
145+
* \param layer A layer to add to the store
146+
*
147+
* \returns nullptr if unable to add layer, otherwise pointer to newly added layer
148+
*
149+
* \see addMapLayers
150+
*
151+
* \note Use addMapLayers() if adding more than one layer at a time.
152+
* \see addMapLayers()
153+
*/
154+
QgsMapLayer *addMapLayer( QgsMapLayer *layer SIP_TRANSFER );
155+
156+
/**
157+
* \brief
158+
* Remove a set of layers from the store by layer ID.
159+
*
160+
* The specified layers will be removed from the store.
161+
* These layers will also be deleted.
162+
*
163+
* \param layerIds list of IDs of the layers to remove
164+
*
165+
* \see takeMapLayer()
166+
* \see removeMapLayer()
167+
* \see removeAllMapLayers()
168+
* \note available in Python bindings as removeMapLayersById.
169+
*/
170+
void removeMapLayers( const QStringList &layerIds ) SIP_PYNAME( removeMapLayersById );
171+
172+
/**
173+
* \brief
174+
* Remove a set of \a layers from the store.
175+
*
176+
* The specified layers will be removed from the store.
177+
* These layers will also be deleted.
178+
*
179+
* \param layers A list of layers to remove. Null pointers are ignored.
180+
*
181+
* \see takeMapLayer()
182+
* \see removeMapLayer()
183+
* \see removeAllMapLayers()
184+
*/
185+
//TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
186+
void removeMapLayers( const QList<QgsMapLayer *> &layers );
187+
188+
/**
189+
* \brief
190+
* Remove a layer from the store by layer \a id.
191+
*
192+
* The specified layer will be removed from the store. The layer will also be deleted.
193+
*
194+
* \param id ID of the layer to remove
195+
*
196+
* \see takeMapLayer()
197+
* \see removeMapLayers()
198+
* \see removeAllMapLayers()
199+
*/
200+
void removeMapLayer( const QString &id );
201+
202+
/**
203+
* \brief
204+
* Remove a \a layer from the store.
205+
*
206+
* The specified layer will be removed from the store. The layer will also be deleted.
207+
*
208+
* \param layer The layer to remove. Null pointers are ignored.
209+
*
210+
* \see takeMapLayer()
211+
* \see removeMapLayers()
212+
* \see removeAllMapLayers()
213+
*/
214+
void removeMapLayer( QgsMapLayer *layer );
215+
216+
/**
217+
* Takes a \a layer from the store. If the layer was owned by the store, the
218+
* layer will be returned without deleting it. The caller takes ownership of
219+
* the layer and is responsible for deleting it.
220+
* \see removeMapLayer()
221+
*/
222+
QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
223+
224+
/**
225+
* Removes all registered layers. These layers will also be deleted.
226+
*
227+
* \note Calling this method will cause the removeAll() signal to
228+
* be emitted.
229+
* \see removeMapLayer()
230+
* \see removeMapLayers()
231+
*/
232+
void removeAllMapLayers();
233+
234+
signals:
235+
236+
/**
237+
* Emitted when one or more layers are about to be removed from the store.
238+
*
239+
* \param layerIds A list of IDs for the layers which are to be removed.
240+
* \see layerWillBeRemoved()
241+
* \see layersRemoved()
242+
*/
243+
void layersWillBeRemoved( const QStringList &layerIds );
244+
245+
/**
246+
* Emitted when one or more layers are about to be removed from the store.
247+
*
248+
* \param layers A list of layers which are to be removed.
249+
* \see layerWillBeRemoved()
250+
* \see layersRemoved()
251+
*/
252+
void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
253+
254+
/**
255+
* Emitted when a layer is about to be removed from the store.
256+
*
257+
* \param layerId The ID of the layer to be removed.
258+
*
259+
* \note Consider using layersWillBeRemoved() instead.
260+
* \see layersWillBeRemoved()
261+
* \see layerRemoved()
262+
*/
263+
void layerWillBeRemoved( const QString &layerId );
264+
265+
/**
266+
* Emitted when a layer is about to be removed from the store.
267+
*
268+
* \param layer The layer to be removed.
269+
*
270+
* \note Consider using layersWillBeRemoved() instead.
271+
* \see layersWillBeRemoved()
272+
* \see layerRemoved()
273+
*/
274+
void layerWillBeRemoved( QgsMapLayer *layer );
275+
276+
/**
277+
* Emitted after one or more layers were removed from the store.
278+
*
279+
* \param layerIds A list of IDs of the layers which were removed.
280+
* \see layersWillBeRemoved()
281+
*/
282+
void layersRemoved( const QStringList &layerIds );
283+
284+
/**
285+
* Emitted after a layer was removed from the store.
286+
*
287+
* \param layerId The ID of the layer removed.
288+
*
289+
* \note Consider using layersRemoved() instead
290+
* \see layerWillBeRemoved()
291+
*/
292+
void layerRemoved( const QString &layerId );
293+
294+
/**
295+
* Emitted when all layers are removed, before layersWillBeRemoved() and
296+
* layerWillBeRemoved() signals are emitted. The layersWillBeRemoved() and
297+
* layerWillBeRemoved() signals will still be emitted following this signal.
298+
* You can use this signal to do easy (and fast) cleanup.
299+
*/
300+
void allLayersRemoved();
301+
302+
/**
303+
* Emitted when one or more layers were added to the store.
304+
*
305+
* \param layers List of layers which have been added.
306+
*
307+
* \see legendLayersAdded()
308+
* \see layerWasAdded()
309+
*/
310+
void layersAdded( const QList<QgsMapLayer *> &layers );
311+
312+
/**
313+
* Emitted when a \a layer was added to the store.
314+
*
315+
* \note Consider using layersAdded() instead
316+
* \see layersAdded()
317+
*/
318+
void layerWasAdded( QgsMapLayer *layer );
319+
320+
private slots:
321+
322+
void onMapLayerDeleted( QObject *obj );
323+
324+
private:
325+
326+
QMap<QString, QgsMapLayer *> mMapLayers;
327+
328+
};
329+
330+
#endif //QGSMAPLAYERSTORE_H

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ADD_PYTHON_TEST(PyQgsMapCanvas test_qgsmapcanvas.py)
7575
ADD_PYTHON_TEST(PyQgsMapCanvasAnnotationItem test_qgsmapcanvasannotationitem.py)
7676
ADD_PYTHON_TEST(PyQgsMapLayer test_qgsmaplayer.py)
7777
ADD_PYTHON_TEST(PyQgsMapLayerModel test_qgsmaplayermodel.py)
78+
ADD_PYTHON_TEST(PyQgsMapLayerStore test_qgsmaplayerstore.py)
7879
ADD_PYTHON_TEST(PyQgsMapRenderer test_qgsmaprenderer.py)
7980
ADD_PYTHON_TEST(PyQgsMapRendererCache test_qgsmaprenderercache.py)
8081
ADD_PYTHON_TEST(PyQgsMapThemeCollection test_qgsmapthemecollection.py)

‎tests/src/python/test_qgsmaplayerstore.py

Lines changed: 495 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.