QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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>
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 
238  Q_DECL_DEPRECATED void clearAllLayerCaches() {}
240 
246  void reloadAllLayers();
247 
248  signals:
249 
257  // TODO QGIS 3.0 - rename theLayerIds to layerIds
258  void layersWillBeRemoved( const QStringList& theLayerIds );
259 
267  void layersWillBeRemoved( const QList<QgsMapLayer*>& layers );
268 
278  //TODO QGIS 3.0 - rename theLayerId to layerId
279  void layerWillBeRemoved( const QString& theLayerId );
280 
290  void layerWillBeRemoved( QgsMapLayer* layer );
291 
298  //TODO QGIS 3.0 - rename theLayerIds to layerIds
299  void layersRemoved( const QStringList& theLayerIds );
300 
309  //TODO QGIS 3.0 - rename theLayerId to layerId
310  void layerRemoved( const QString& theLayerId );
311 
318  //TODO QGIS 3.0 - rename to past tense
319  void removeAll();
320 
331  //TODO QGIS 3.0 - rename theMapLayers to mapLayers
332  void layersAdded( const QList<QgsMapLayer *>& theMapLayers );
333 
342  // TODO QGIS 3.0 - rename theMapLayer to layer
343  void layerWasAdded( QgsMapLayer* theMapLayer );
344 
353  //TODO QGIS 3.0 rename theMapLayers to mapLayers
354  void legendLayersAdded( const QList<QgsMapLayer*>& theMapLayers );
355 
356  protected:
357 #if 0
358 
361  void connectNotify( const char * signal ) override;
362 #endif
363 
364  private slots:
365  void onMapLayerDeleted( QObject* obj );
366 
367  private:
369  QgsMapLayerRegistry( QObject * parent = nullptr );
370 
371  QMap<QString, QgsMapLayer*> mMapLayers;
372 };
373 
374 #endif //QGSMAPLAYERREGISTRY_H
375 
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.
const T & value() const
This class tracks map layers that are currently loaded and provides various methods to retrieve match...
virtual void connectNotify(const char *signal)