QGIS API Documentation  3.6.0-Noosa (5873452)
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_sip.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 
57  int validCount() const;
58 
59 #ifdef SIP_RUN
60 
64  int __len__() const;
65  % MethodCode
66  sipRes = sipCpp->count();
67  % End
68 
70  int __bool__() const;
71  % MethodCode
72  sipRes = true;
73  % End
74 #endif
75 
83  QgsMapLayer *mapLayer( const QString &id ) const;
84 
92  QList<QgsMapLayer *> mapLayersByName( const QString &name ) const;
93 
100  QMap<QString, QgsMapLayer *> mapLayers() const;
101 
109  QMap<QString, QgsMapLayer *> validMapLayers() const;
110 
111 #ifndef SIP_RUN
112 
123  template <typename T>
124  QVector<T> layers() const
125  {
126  QVector<T> layers;
127  QMap<QString, QgsMapLayer *>::const_iterator layerIt = mMapLayers.constBegin();
128  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
129  {
130  T tLayer = qobject_cast<T>( layerIt.value() );
131  if ( tLayer )
132  {
133  layers << tLayer;
134  }
135  }
136  return layers;
137  }
138 #endif
139 
158  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &layers SIP_TRANSFER,
159  bool takeOwnership SIP_PYARGREMOVE = true );
160 
182  QgsMapLayer *addMapLayer( QgsMapLayer *layer SIP_TRANSFER,
183  bool takeOwnership SIP_PYARGREMOVE = true );
184 
199  void removeMapLayers( const QStringList &layerIds ) SIP_PYNAME( removeMapLayersById );
200 
214  void removeMapLayers( const QList<QgsMapLayer *> &layers );
215 
228  void removeMapLayer( const QString &id );
229 
242  void removeMapLayer( QgsMapLayer *layer );
243 
250  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
251 
260  void removeAllMapLayers();
261 
267  void transferLayersFromStore( QgsMapLayerStore *other );
268 
269  signals:
270 
278  void layersWillBeRemoved( const QStringList &layerIds );
279 
287  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
288 
298  void layerWillBeRemoved( const QString &layerId );
299 
309  void layerWillBeRemoved( QgsMapLayer *layer );
310 
317  void layersRemoved( const QStringList &layerIds );
318 
327  void layerRemoved( const QString &layerId );
328 
335  void allLayersRemoved();
336 
344  void layersAdded( const QList<QgsMapLayer *> &layers );
345 
352  void layerWasAdded( QgsMapLayer *layer );
353 
354  private slots:
355 
356  void onMapLayerDeleted( QObject *obj );
357 
358  private:
359 
360  QMap<QString, QgsMapLayer *> mMapLayers;
361 
362 };
363 
364 #endif //QGSMAPLAYERSTORE_H
Base class for all map layer types.
Definition: qgsmaplayer.h:64
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