QGIS API Documentation  3.0.2-Girona (307d082)
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 #endif
63 
71  QgsMapLayer *mapLayer( const QString &id ) const;
72 
80  QList<QgsMapLayer *> mapLayersByName( const QString &name ) const;
81 
88  QMap<QString, QgsMapLayer *> mapLayers() const;
89 
90 #ifndef SIP_RUN
91 
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 
137  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &layers SIP_TRANSFER,
138  bool takeOwnership SIP_PYARGREMOVE = true );
139 
161  QgsMapLayer *addMapLayer( QgsMapLayer *layer SIP_TRANSFER,
162  bool takeOwnership SIP_PYARGREMOVE = true );
163 
178  void removeMapLayers( const QStringList &layerIds ) SIP_PYNAME( removeMapLayersById );
179 
193  void removeMapLayers( const QList<QgsMapLayer *> &layers );
194 
207  void removeMapLayer( const QString &id );
208 
221  void removeMapLayer( QgsMapLayer *layer );
222 
229  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
230 
239  void removeAllMapLayers();
240 
246  void transferLayersFromStore( QgsMapLayerStore *other );
247 
248  signals:
249 
257  void layersWillBeRemoved( const QStringList &layerIds );
258 
266  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
267 
277  void layerWillBeRemoved( const QString &layerId );
278 
288  void layerWillBeRemoved( QgsMapLayer *layer );
289 
296  void layersRemoved( const QStringList &layerIds );
297 
306  void layerRemoved( const QString &layerId );
307 
314  void allLayersRemoved();
315 
324  void layersAdded( const QList<QgsMapLayer *> &layers );
325 
332  void layerWasAdded( QgsMapLayer *layer );
333 
334  private slots:
335 
336  void onMapLayerDeleted( QObject *obj );
337 
338  private:
339 
340  QMap<QString, QgsMapLayer *> mMapLayers;
341 
342 };
343 
344 #endif //QGSMAPLAYERSTORE_H
Base class for all map layer types.
Definition: qgsmaplayer.h:56
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#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