QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
46 {
47  return mMapLayers.value( theLayerId );
48 }
49 
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 
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 
110 {
112  Q_FOREACH ( const QString &myId, theLayerIds )
113  {
114  layers << mMapLayers.value( myId );
115  }
116 
117  removeMapLayers( layers );
118 }
119 
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 
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 
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.
void removeMapLayer(const QString &theLayerId)
Remove a layer from the registry by layer ID.
void layersRemoved(const QStringList &theLayerIds)
Emitted after one or more layers were removed from the registry.
bool contains(const Key &key) const
void legendLayersAdded(const QList< QgsMapLayer *> &theMapLayers)
Emitted, when a layer was added to the registry and the legend.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:250
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all registered layers by layer ID.
void removeMapLayers(const QStringList &theLayerIds)
Remove a set of layers from the registry by layer ID.
void removeAllMapLayers()
Removes all registered layers.
void clear()
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
QList< Key > keys() const
int count() const
Returns the number of registered layers.
void layerWasAdded(QgsMapLayer *theMapLayer)
Emitted when a layer was added to the registry.
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
QList< QgsMapLayer * > addMapLayers(const QList< QgsMapLayer *> &theMapLayers, bool addToLegend=true, bool takeOwnership=true)
Add a list of layers to the map of loaded layers.
bool isEmpty() const
void layerRemoved(const QString &theLayerId)
Emitted after a layer was removed from the registry.
bool isValid()
Return the status of the layer.
This class tracks map layers that are currently loaded and provides various methods to retrieve match...
virtual void connectNotify(const char *signal)
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 setParent(QObject *parent)
const Key key(const T &value) const
QObject(QObject *parent)
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
void layersAdded(const QList< QgsMapLayer *> &theMapLayers)
Emitted when one or more layers were added to the registry.
QgsMapLayer * addMapLayer(QgsMapLayer *theMapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
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)
int size() const
void destroyed(QObject *obj)
const T value(const Key &key) const
int remove(const Key &key)
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.