QGIS API Documentation  2.99.0-Master (c558d51)
qgsmaplayerregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * QgsMapLayerRegistry.cpp - Singleton class for tracking mMapLayers.
3  * -------------------
4  * begin : Sun June 02 2004
5  * copyright : (C) 2004 by Tim Sutton
6  * email : [email protected]
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 #include "qgsmaplayerregistry.h"
19 #include "qgsmaplayer.h"
20 #include "qgslogger.h"
21 
22 //
23 // Static calls to enforce singleton behaviour
24 //
26 {
27  static QgsMapLayerRegistry sInstance;
28  return &sInstance;
29 }
30 
31 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent )
32  : QObject( parent )
33 {}
34 
36 {
38 }
39 
41 {
42  return mMapLayers.size();
43 }
44 
45 QgsMapLayer * QgsMapLayerRegistry::mapLayer( const QString& theLayerId ) const
46 {
47  return mMapLayers.value( theLayerId );
48 }
49 
50 QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( const QString& layerName ) const
51 {
52  QList<QgsMapLayer *> myResultList;
53  Q_FOREACH ( QgsMapLayer* layer, mMapLayers )
54  {
55  if ( layer->name() == layerName )
56  {
57  myResultList << layer;
58  }
59  }
60  return myResultList;
61 }
62 
63 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
64  const QList<QgsMapLayer *>& theMapLayers,
65  bool addToLegend,
66  bool takeOwnership )
67 {
68  QList<QgsMapLayer *> myResultList;
69  Q_FOREACH ( QgsMapLayer* myLayer, theMapLayers )
70  {
71  if ( !myLayer || !myLayer->isValid() )
72  {
73  QgsDebugMsg( "Cannot add invalid layers" );
74  continue;
75  }
76  //check the layer is not already registered!
77  if ( !mMapLayers.contains( myLayer->id() ) )
78  {
79  mMapLayers[myLayer->id()] = myLayer;
80  myResultList << mMapLayers[myLayer->id()];
81  if ( takeOwnership )
82  {
83  myLayer->setParent( this );
84  }
85  connect( myLayer, SIGNAL( destroyed( QObject* ) ), this, SLOT( onMapLayerDeleted( QObject* ) ) );
86  emit layerWasAdded( myLayer );
87  }
88  }
89  if ( !myResultList.isEmpty() )
90  {
91  emit layersAdded( myResultList );
92 
93  if ( addToLegend )
94  emit legendLayersAdded( myResultList );
95  }
96  return myResultList;
97 }
98 
101  bool addToLegend,
102  bool takeOwnership )
103 {
104  QList<QgsMapLayer *> addedLayers;
105  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
106  return addedLayers.isEmpty() ? nullptr : addedLayers[0];
107 }
108 
109 void QgsMapLayerRegistry::removeMapLayers( const QStringList& theLayerIds )
110 {
111  QList<QgsMapLayer*> layers;
112  Q_FOREACH ( const QString &myId, theLayerIds )
113  {
114  layers << mMapLayers.value( myId );
115  }
116 
117  removeMapLayers( layers );
118 }
119 
120 void QgsMapLayerRegistry::removeMapLayers( const QList<QgsMapLayer*>& layers )
121 {
122  if ( layers.isEmpty() )
123  return;
124 
125  QStringList layerIds;
126  QList<QgsMapLayer*> layerList;
127 
128  Q_FOREACH ( QgsMapLayer* layer, layers )
129  {
130  // check layer and the registry contains it
131  if ( layer && mMapLayers.contains( layer->id() ) )
132  {
133  layerIds << layer->id();
134  layerList << layer;
135  }
136  }
137 
138  if ( layerIds.isEmpty() )
139  return;
140 
141  emit layersWillBeRemoved( layerIds );
142  emit layersWillBeRemoved( layerList );
143 
144  Q_FOREACH ( QgsMapLayer* lyr, layerList )
145  {
146  QString myId( lyr->id() );
147  emit layerWillBeRemoved( myId );
148  emit layerWillBeRemoved( lyr );
149  mMapLayers.remove( myId );
150  if ( lyr->parent() == this )
151  {
152  delete lyr;
153  }
154  emit layerRemoved( myId );
155  }
156 
157  emit layersRemoved( layerIds );
158 }
159 
160 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
161 {
162  removeMapLayers( QList<QgsMapLayer*>() << mMapLayers.value( theLayerId ) );
163 }
164 
166 {
167  if ( layer )
168  removeMapLayers( QList<QgsMapLayer*>() << layer );
169 }
170 
172 {
173  emit removeAll();
174  // now let all observers know to clear themselves,
175  // and then consequently any of their map legends
176  removeMapLayers( mMapLayers.keys() );
177  mMapLayers.clear();
178 }
179 
181 {
182  Q_FOREACH ( QgsMapLayer* layer, mMapLayers )
183  {
184  layer->reload();
185  }
186 }
187 
188 void QgsMapLayerRegistry::onMapLayerDeleted( QObject* obj )
189 {
190  QString id = mMapLayers.key( static_cast<QgsMapLayer*>( obj ) );
191 
192  if ( !id.isNull() )
193  {
194  QgsDebugMsg( QString( "Map layer deleted without unregistering! %1" ).arg( id ) );
195  mMapLayers.remove( id );
196  }
197 }
198 
199 QMap<QString, QgsMapLayer*> QgsMapLayerRegistry::mapLayers() const
200 {
201  return mMapLayers;
202 }
203 
204 
205 #if 0
206 void QgsMapLayerRegistry::connectNotify( const char * signal )
207 {
208  Q_UNUSED( signal );
209  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
210 } // QgsMapLayerRegistry::connectNotify
211 #endif
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void layerWillBeRemoved(const QString &theLayerId)
Emitted when a layer is about to be removed from the registry.
bool isValid() const
Return the status of the layer.
void removeMapLayer(const QString &theLayerId)
Remove a layer from the registry by layer ID.
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
void layersRemoved(const QStringList &theLayerIds)
Emitted after one or more layers were removed from the registry.
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
QList< QgsMapLayer * > addMapLayers(const QList< QgsMapLayer * > &theMapLayers, bool addToLegend=true, bool takeOwnership=true)
Add a list of layers to the map of loaded layers.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:290
void removeMapLayers(const QStringList &theLayerIds)
Remove a set of layers from the registry by layer ID.
void removeAllMapLayers()
Removes all registered layers.
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
void layerWasAdded(QgsMapLayer *theMapLayer)
Emitted when a layer was added to the registry.
void layersAdded(const QList< QgsMapLayer * > &theMapLayers)
Emitted when one or more layers were added to the registry.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsMapLayerRegistry.
void layerRemoved(const QString &theLayerId)
Emitted after a layer was removed from the registry.
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all registered layers by layer ID.
This class tracks map layers that are currently loaded and provides various methods to retrieve match...
void removeAll()
Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals ar...
void layersWillBeRemoved(const QStringList &theLayerIds)
Emitted when one or more layers are about to be removed from the registry.
void legendLayersAdded(const QList< QgsMapLayer * > &theMapLayers)
Emitted, when a layer was added to the registry and the legend.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
QgsMapLayer * addMapLayer(QgsMapLayer *theMapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
int count() const
Returns the number of registered layers.
void reloadAllLayers()
Reload all registered layer&#39;s provider data caches, synchronising the layer with any changes in the d...
bool isNull(const QVariant &v)