QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmaplayerstore.h
Go to the documentation of this file.
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 
35 class CORE_EXPORT QgsMapLayerStore : public QObject
36 {
37  Q_OBJECT
38 
39  public:
40 
44  explicit QgsMapLayerStore( QObject *parent SIP_TRANSFERTHIS = nullptr );
45 
46  ~QgsMapLayerStore() override;
47 
51  int count() const;
52 
53 #ifdef SIP_RUN
54 
58  int __len__() const;
59  % MethodCode
60  sipRes = sipCpp->count();
61  % End
62 
64  int __bool__() const;
65  % MethodCode
66  sipRes = true;
67  % End
68 #endif
69 
77  QgsMapLayer *mapLayer( const QString &id ) const;
78 
86  QList<QgsMapLayer *> mapLayersByName( const QString &name ) const;
87 
94  QMap<QString, QgsMapLayer *> mapLayers() const;
95 
96 #ifndef SIP_RUN
97 
108  template <typename T>
109  QVector<T> layers() const
110  {
111  QVector<T> layers;
112  QMap<QString, QgsMapLayer *>::const_iterator layerIt = mMapLayers.constBegin();
113  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
114  {
115  T tLayer = qobject_cast<T>( layerIt.value() );
116  if ( tLayer )
117  {
118  layers << tLayer;
119  }
120  }
121  return layers;
122  }
123 #endif
124 
143  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &layers SIP_TRANSFER,
144  bool takeOwnership SIP_PYARGREMOVE = true );
145 
167  QgsMapLayer *addMapLayer( QgsMapLayer *layer SIP_TRANSFER,
168  bool takeOwnership SIP_PYARGREMOVE = true );
169 
184  void removeMapLayers( const QStringList &layerIds ) SIP_PYNAME( removeMapLayersById );
185 
199  void removeMapLayers( const QList<QgsMapLayer *> &layers );
200 
213  void removeMapLayer( const QString &id );
214 
227  void removeMapLayer( QgsMapLayer *layer );
228 
235  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
236 
245  void removeAllMapLayers();
246 
252  void transferLayersFromStore( QgsMapLayerStore *other );
253 
254  signals:
255 
263  void layersWillBeRemoved( const QStringList &layerIds );
264 
272  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
273 
283  void layerWillBeRemoved( const QString &layerId );
284 
294  void layerWillBeRemoved( QgsMapLayer *layer );
295 
302  void layersRemoved( const QStringList &layerIds );
303 
312  void layerRemoved( const QString &layerId );
313 
320  void allLayersRemoved();
321 
329  void layersAdded( const QList<QgsMapLayer *> &layers );
330 
337  void layerWasAdded( QgsMapLayer *layer );
338 
339  private slots:
340 
341  void onMapLayerDeleted( QObject *obj );
342 
343  private:
344 
345  QMap<QString, QgsMapLayer *> mMapLayers;
346 
347 };
348 
349 #endif //QGSMAPLAYERSTORE_H
Base class for all map layer types.
Definition: qgsmaplayer.h:63
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:139
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74