Skip to content

Commit b5ae888

Browse files
authored
Merge pull request #4478 from nyalldawson/layer_store
Split off map layer storage handling from QgsProject to QgsMapLayerStore
2 parents 27ab5a0 + 32e06f4 commit b5ae888

18 files changed

+2003
-723
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/processing/qgsprocessingcontext.sip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class QgsProcessingContext
7575
Sets the expression ``context``.
7676
%End
7777

78-
QgsProject &temporaryLayerStore();
78+
QgsMapLayerStore *temporaryLayerStore();
7979
%Docstring
80-
Returns a reference to the project used for storing temporary layers during
80+
Returns a reference to the layer store used for storing temporary layers during
8181
algorithm execution.
82-
:rtype: QgsProject
82+
:rtype: QgsMapLayerStore
8383
%End
8484

8585
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const;

python/core/qgsmaplayerstore.sip

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
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+
82+
%Docstring
83+
\brief
84+
Add a list of ``layers`` to the store. Ownership of the layers is transferred
85+
to the store.
86+
87+
The layersAdded() and layerWasAdded() signals will always be emitted.
88+
89+
\param layers A list of layer which should be added to the store.
90+
\param takeOwnership Ownership will be transferred to the layer store.
91+
If you specify false here you have take care of deleting
92+
the layers yourself. Not available in Python.
93+
94+
:return: a list of the map layers that were added
95+
successfully. If a layer is invalid, or already exists in the store,
96+
it will not be part of the returned list.
97+
98+
.. seealso:: addMapLayer()
99+
:rtype: list of QgsMapLayer
100+
%End
101+
102+
QgsMapLayer *addMapLayer( QgsMapLayer *layer /Transfer/);
103+
104+
%Docstring
105+
\brief
106+
Add a ``layer`` to the store. Ownership of the layer is transferred to the
107+
store.
108+
109+
The layersAdded() and layerWasAdded() signals will always be emitted.
110+
If you are adding multiple layers at once, you should use
111+
addMapLayers() instead.
112+
113+
\param layer A layer to add to the store
114+
\param takeOwnership Ownership will be transferred to the layer store.
115+
If you specify false here you have take care of deleting
116+
the layers yourself. Not available in Python.
117+
118+
:return: None if unable to add layer, otherwise pointer to newly added layer
119+
120+
.. seealso:: addMapLayers
121+
122+
.. note::
123+
124+
Use addMapLayers() if adding more than one layer at a time.
125+
.. seealso:: addMapLayers()
126+
:rtype: QgsMapLayer
127+
%End
128+
129+
void removeMapLayers( const QStringList &layerIds ) /PyName=removeMapLayersById/;
130+
%Docstring
131+
\brief
132+
Remove a set of layers from the store by layer ID.
133+
134+
The specified layers will be removed from the store.
135+
These layers will also be deleted.
136+
137+
\param layerIds list of IDs of the layers to remove
138+
139+
.. seealso:: takeMapLayer()
140+
.. seealso:: removeMapLayer()
141+
.. seealso:: removeAllMapLayers()
142+
.. note::
143+
144+
available in Python bindings as removeMapLayersById.
145+
%End
146+
147+
void removeMapLayers( const QList<QgsMapLayer *> &layers );
148+
%Docstring
149+
\brief
150+
Remove a set of ``layers`` from the store.
151+
152+
The specified layers will be removed from the store.
153+
These layers will also be deleted.
154+
155+
\param layers A list of layers to remove. Null pointers are ignored.
156+
157+
.. seealso:: takeMapLayer()
158+
.. seealso:: removeMapLayer()
159+
.. seealso:: removeAllMapLayers()
160+
%End
161+
162+
void removeMapLayer( const QString &id );
163+
%Docstring
164+
\brief
165+
Remove a layer from the store by layer ``id``.
166+
167+
The specified layer will be removed from the store. The layer will also be deleted.
168+
169+
\param id ID of the layer to remove
170+
171+
.. seealso:: takeMapLayer()
172+
.. seealso:: removeMapLayers()
173+
.. seealso:: removeAllMapLayers()
174+
%End
175+
176+
void removeMapLayer( QgsMapLayer *layer );
177+
%Docstring
178+
\brief
179+
Remove a ``layer`` from the store.
180+
181+
The specified layer will be removed from the store. The layer will also be deleted.
182+
183+
\param layer The layer to remove. Null pointers are ignored.
184+
185+
.. seealso:: takeMapLayer()
186+
.. seealso:: removeMapLayers()
187+
.. seealso:: removeAllMapLayers()
188+
%End
189+
190+
QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) /TransferBack/;
191+
%Docstring
192+
Takes a ``layer`` from the store. If the layer was owned by the store, the
193+
layer will be returned without deleting it. The caller takes ownership of
194+
the layer and is responsible for deleting it.
195+
.. seealso:: removeMapLayer()
196+
:rtype: QgsMapLayer
197+
%End
198+
199+
void removeAllMapLayers();
200+
%Docstring
201+
Removes all registered layers. These layers will also be deleted.
202+
203+
.. note::
204+
205+
Calling this method will cause the removeAll() signal to
206+
be emitted.
207+
.. seealso:: removeMapLayer()
208+
.. seealso:: removeMapLayers()
209+
%End
210+
211+
signals:
212+
213+
void layersWillBeRemoved( const QStringList &layerIds );
214+
%Docstring
215+
Emitted when one or more layers are about to be removed from the store.
216+
217+
\param layerIds A list of IDs for the layers which are to be removed.
218+
.. seealso:: layerWillBeRemoved()
219+
.. seealso:: layersRemoved()
220+
%End
221+
222+
void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
223+
%Docstring
224+
Emitted when one or more layers are about to be removed from the store.
225+
226+
\param layers A list of layers which are to be removed.
227+
.. seealso:: layerWillBeRemoved()
228+
.. seealso:: layersRemoved()
229+
%End
230+
231+
void layerWillBeRemoved( const QString &layerId );
232+
%Docstring
233+
Emitted when a layer is about to be removed from the store.
234+
235+
\param layerId The ID of the layer to be removed.
236+
237+
.. note::
238+
239+
Consider using layersWillBeRemoved() instead.
240+
.. seealso:: layersWillBeRemoved()
241+
.. seealso:: layerRemoved()
242+
%End
243+
244+
void layerWillBeRemoved( QgsMapLayer *layer );
245+
%Docstring
246+
Emitted when a layer is about to be removed from the store.
247+
248+
\param layer The layer to be removed.
249+
250+
.. note::
251+
252+
Consider using layersWillBeRemoved() instead.
253+
.. seealso:: layersWillBeRemoved()
254+
.. seealso:: layerRemoved()
255+
%End
256+
257+
void layersRemoved( const QStringList &layerIds );
258+
%Docstring
259+
Emitted after one or more layers were removed from the store.
260+
261+
\param layerIds A list of IDs of the layers which were removed.
262+
.. seealso:: layersWillBeRemoved()
263+
%End
264+
265+
void layerRemoved( const QString &layerId );
266+
%Docstring
267+
Emitted after a layer was removed from the store.
268+
269+
\param layerId The ID of the layer removed.
270+
271+
.. note::
272+
273+
Consider using layersRemoved() instead
274+
.. seealso:: layerWillBeRemoved()
275+
%End
276+
277+
void allLayersRemoved();
278+
%Docstring
279+
Emitted when all layers are removed, before layersWillBeRemoved() and
280+
layerWillBeRemoved() signals are emitted. The layersWillBeRemoved() and
281+
layerWillBeRemoved() signals will still be emitted following this signal.
282+
You can use this signal to do easy (and fast) cleanup.
283+
%End
284+
285+
void layersAdded( const QList<QgsMapLayer *> &layers );
286+
%Docstring
287+
Emitted when one or more layers were added to the store.
288+
289+
\param layers List of layers which have been added.
290+
291+
.. seealso:: legendLayersAdded()
292+
.. seealso:: layerWasAdded()
293+
%End
294+
295+
void layerWasAdded( QgsMapLayer *layer );
296+
%Docstring
297+
Emitted when a ``layer`` was added to the store.
298+
299+
.. note::
300+
301+
Consider using layersAdded() instead
302+
.. seealso:: layersAdded()
303+
%End
304+
305+
};
306+
307+
/************************************************************************
308+
* This file has been generated automatically from *
309+
* *
310+
* src/core/qgsmaplayerstore.h *
311+
* *
312+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
313+
************************************************************************/

python/core/qgsproject.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ class QgsProject : QObject, QgsExpressionContextGenerator
422422
*/
423423
const QgsLabelingEngineSettings &labelingEngineSettings() const;
424424

425+
QgsMapLayerStore *layerStore();
426+
425427
int count() const;
426428

427429
QgsMapLayer *mapLayer( const QString &layerId ) const;

python/plugins/processing/tools/vector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def createVectorWriter(destination, encoding, fields, geometryType, crs, context
481481

482482
layer = QgsVectorLayer(uri, destination, 'memory')
483483
sink = layer.dataProvider()
484-
context.temporaryLayerStore().addMapLayer(layer, False)
484+
context.temporaryLayerStore().addMapLayer(layer)
485485
destination = layer.id()
486486
elif destination.startswith(POSTGIS_LAYER_PREFIX):
487487
uri = QgsDataSourceUri(destination[len(POSTGIS_LAYER_PREFIX):])
@@ -518,7 +518,7 @@ def _runSQL(sql):
518518

519519
layer = QgsVectorLayer(uri.uri(), uri.table(), "postgres")
520520
sink = layer.dataProvider()
521-
context.temporaryLayerStore().addMapLayer(layer, False)
521+
context.temporaryLayerStore().addMapLayer(layer)
522522
elif destination.startswith(SPATIALITE_LAYER_PREFIX):
523523
uri = QgsDataSourceUri(destination[len(SPATIALITE_LAYER_PREFIX):])
524524
try:
@@ -548,7 +548,7 @@ def _runSQL(sql):
548548

549549
layer = QgsVectorLayer(uri.uri(), uri.table(), "spatialite")
550550
sink = layer.dataProvider()
551-
context.temporaryLayerStore().addMapLayer(layer, False)
551+
context.temporaryLayerStore().addMapLayer(layer)
552552
else:
553553
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
554554
OGRCodes = {}

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ SET(QGIS_CORE_SRCS
178178
qgsmaplayerlegend.cpp
179179
qgsmaplayermodel.cpp
180180
qgsmaplayerproxymodel.cpp
181+
qgsmaplayerstore.cpp
181182
qgsmaplayerstylemanager.cpp
182183
qgsmaprenderercache.cpp
183184
qgsmaprenderercustompainterjob.cpp
@@ -527,6 +528,7 @@ SET(QGIS_CORE_MOC_HDRS
527528
qgsmaplayerlegend.h
528529
qgsmaplayermodel.h
529530
qgsmaplayerproxymodel.h
531+
qgsmaplayerstore.h
530532
qgsmaplayerstylemanager.h
531533
qgsmaprenderercache.h
532534
qgsmaprenderercustompainterjob.h

0 commit comments

Comments
 (0)