QGIS API Documentation  2.99.0-Master (37c43df)
qgsmaplayerregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerregistry.h
3  Singleton class for keeping track of loaded layers
4  -------------------
5  begin : Sun June 04 2004
6  copyright : (C) 2004 by Tim Sutton
7  email : [email protected]
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifndef QGSMAPLAYERREGISTRY_H
20 #define QGSMAPLAYERREGISTRY_H
21 
22 #include <QMap>
23 #include <QObject>
24 #include <QStringList>
25 
26 class QString;
27 class QgsMapLayer;
28 
34 class CORE_EXPORT QgsMapLayerRegistry : public QObject
35 {
36  Q_OBJECT
37 
38  public:
39 
41  static QgsMapLayerRegistry * instance();
42 
44 
46  int count() const;
47 
54  //TODO QGIS 3.0 - rename theLayerId to layerId
55  QgsMapLayer* mapLayer( const QString& theLayerId ) const;
56 
63  QList<QgsMapLayer *> mapLayersByName( const QString& layerName ) const;
64 
70  QMap<QString, QgsMapLayer*> mapLayers() const;
71 
82  template <typename T>
83  QVector<T> layers() const
84  {
85  QVector<T> layers;
86  QMap<QString, QgsMapLayer*>::const_iterator layerIt = mMapLayers.constBegin();
87  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
88  {
89  T tLayer = qobject_cast<T>( layerIt.value() );
90  if ( tLayer )
91  {
92  layers << tLayer;
93  }
94  }
95  return layers;
96  }
97 
123  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer*>& theMapLayers,
124  bool addToLegend = true,
125  bool takeOwnership = true );
126 
154  QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true, bool takeOwnership = true );
155 
170  // TODO QGIS 3.0 - rename theLayerIds to layerIds
171  void removeMapLayers( const QStringList& theLayerIds );
172 
186  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
187  void removeMapLayers( const QList<QgsMapLayer*>& layers );
188 
202  // TODO QGIS 3.0 - rename theLayerId to layerId
203  void removeMapLayer( const QString& theLayerId );
204 
218  void removeMapLayer( QgsMapLayer* layer );
219 
230  void removeAllMapLayers();
231 
237  void reloadAllLayers();
238 
239  signals:
240 
248  // TODO QGIS 3.0 - rename theLayerIds to layerIds
249  void layersWillBeRemoved( const QStringList& theLayerIds );
250 
258  void layersWillBeRemoved( const QList<QgsMapLayer*>& layers );
259 
269  //TODO QGIS 3.0 - rename theLayerId to layerId
270  void layerWillBeRemoved( const QString& theLayerId );
271 
281  void layerWillBeRemoved( QgsMapLayer* layer );
282 
289  //TODO QGIS 3.0 - rename theLayerIds to layerIds
290  void layersRemoved( const QStringList& theLayerIds );
291 
300  //TODO QGIS 3.0 - rename theLayerId to layerId
301  void layerRemoved( const QString& theLayerId );
302 
309  //TODO QGIS 3.0 - rename to past tense
310  void removeAll();
311 
322  //TODO QGIS 3.0 - rename theMapLayers to mapLayers
323  void layersAdded( const QList<QgsMapLayer *>& theMapLayers );
324 
333  // TODO QGIS 3.0 - rename theMapLayer to layer
334  void layerWasAdded( QgsMapLayer* theMapLayer );
335 
344  //TODO QGIS 3.0 rename theMapLayers to mapLayers
345  void legendLayersAdded( const QList<QgsMapLayer*>& theMapLayers );
346 
347  protected:
348 #if 0
349 
353  void connectNotify( const char * signal ) override;
354 #endif
355 
356  private slots:
357  void onMapLayerDeleted( QObject* obj );
358 
359  private:
361  QgsMapLayerRegistry( QObject * parent = nullptr );
362 
363  QMap<QString, QgsMapLayer*> mMapLayers;
364 };
365 
366 #endif //QGSMAPLAYERREGISTRY_H
367 
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
This class tracks map layers that are currently loaded and provides various methods to retrieve match...