QGIS API Documentation  2.14.11-Essen
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 : tim@linfiniti.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 #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 //
32 // Main class begins now...
33 //
34 
35 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
36 {
37  // constructor does nothing
38 }
39 
41 {
43 }
44 
45 // get the layer count (number of registered layers)
47 {
48  return mMapLayers.size();
49 }
50 
52 {
53  return mMapLayers.value( theLayerId );
54 }
55 
57 {
58  QList<QgsMapLayer *> myResultList;
59  Q_FOREACH ( QgsMapLayer* layer, mMapLayers )
60  {
61  if ( layer->name() == layerName )
62  {
63  myResultList << layer;
64  }
65  }
66  return myResultList;
67 }
68 
69 //introduced in 1.8
71  const QList<QgsMapLayer *>& theMapLayers,
72  bool addToLegend,
73  bool takeOwnership )
74 {
75  QList<QgsMapLayer *> myResultList;
76  Q_FOREACH ( QgsMapLayer* myLayer, theMapLayers )
77  {
78  if ( !myLayer || !myLayer->isValid() )
79  {
80  QgsDebugMsg( "Cannot add invalid layers" );
81  continue;
82  }
83  //check the layer is not already registered!
84  if ( !mMapLayers.contains( myLayer->id() ) )
85  {
86  mMapLayers[myLayer->id()] = myLayer;
87  myResultList << mMapLayers[myLayer->id()];
88  if ( takeOwnership )
89  {
90  myLayer->setParent( this );
91  }
92  connect( myLayer, SIGNAL( destroyed( QObject* ) ), this, SLOT( onMapLayerDeleted( QObject* ) ) );
93  emit layerWasAdded( myLayer );
94  }
95  }
96  if ( !myResultList.isEmpty() )
97  {
98  emit layersAdded( myResultList );
99 
100  if ( addToLegend )
101  emit legendLayersAdded( myResultList );
102  }
103  return myResultList;
104 } // QgsMapLayerRegistry::addMapLayers
105 
106 //this is just a thin wrapper for addMapLayers
107 QgsMapLayer *
109  bool addToLegend,
110  bool takeOwnership )
111 {
112  QList<QgsMapLayer *> addedLayers;
113  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
114  return addedLayers.isEmpty() ? nullptr : addedLayers[0];
115 }
116 
117 
118 //introduced in 1.8
120 {
121  QList<QgsMapLayer*> layers;
122  Q_FOREACH ( const QString &myId, theLayerIds )
123  {
124  layers << mMapLayers.value( myId );
125  }
126 
127  removeMapLayers( layers );
128 }
129 
131 {
132  if ( layers.isEmpty() )
133  return;
134 
135  QStringList layerIds;
136  QList<QgsMapLayer*> layerList;
137 
138  Q_FOREACH ( QgsMapLayer* layer, layers )
139  {
140  // check layer and the registry contains it
141  if ( layer && mMapLayers.contains( layer->id() ) )
142  {
143  layerIds << layer->id();
144  layerList << layer;
145  }
146  }
147 
148  emit layersWillBeRemoved( layerIds );
149  emit layersWillBeRemoved( layerList );
150 
151  Q_FOREACH ( QgsMapLayer* lyr, layerList )
152  {
153  QString myId( lyr->id() );
154  emit layerWillBeRemoved( myId );
155  emit layerWillBeRemoved( lyr );
156  mMapLayers.remove( myId );
157  if ( lyr->parent() == this )
158  {
159  delete lyr;
160  }
161  emit layerRemoved( myId );
162  }
163 
164  emit layersRemoved( layerIds );
165 }
166 
168 {
169  removeMapLayers( QList<QgsMapLayer*>() << mMapLayers.value( theLayerId ) );
170 }
171 
173 {
174  if ( layer )
175  removeMapLayers( QList<QgsMapLayer*>() << layer );
176 }
177 
179 {
180  emit removeAll();
181  // now let all canvas observers know to clear themselves,
182  // and then consequently any of their map legends
183  removeMapLayers( mMapLayers.keys() );
184  mMapLayers.clear();
185 } // QgsMapLayerRegistry::removeAllMapLayers()
186 
188 {
189  Q_FOREACH ( QgsMapLayer* layer, mMapLayers )
190  {
191  layer->reload();
192  }
193 }
194 
195 void QgsMapLayerRegistry::onMapLayerDeleted( QObject* obj )
196 {
197  QString id = mMapLayers.key( static_cast<QgsMapLayer*>( obj ) );
198 
199  if ( !id.isNull() )
200  {
201  QgsDebugMsg( QString( "Map layer deleted without unregistering! %1" ).arg( id ) );
202  mMapLayers.remove( id );
203  }
204 }
205 
207 {
208  return mMapLayers;
209 }
210 
211 
212 #if 0
213 void QgsMapLayerRegistry::connectNotify( const char * signal )
214 {
215  Q_UNUSED( signal );
216  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
217 } // QgsMapLayerRegistry::connectNotify
218 #endif
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void layerWillBeRemoved(const QString &theLayerId)
Emitted when an owned layer is removed from the registry.
void removeMapLayer(const QString &theLayerId)
Remove a layer from qgis.
void layersRemoved(const QStringList &theLayerIds)
Emitted after one or more layers were removed from the registry.
bool contains(const Key &key) const
QString name() const
Get the display name of the layer.
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:236
void removeMapLayers(const QStringList &theLayerIds)
Remove a set of layers from the registry.
void removeAllMapLayers()
Remove all registered layers.
void clear()
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
QList< Key > keys() const
void layerWasAdded(QgsMapLayer *theMapLayer)
Emitted when a layer is added to the registry.
bool isEmpty() const
void layersAdded(const QList< QgsMapLayer * > &theMapLayers)
Emitted when one or more layers are added to the registry.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
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 a means to fetch a pointer to a m...
QList< QgsMapLayer * > mapLayersByName(const QString &layerName)
Retrieve a pointer to a loaded layer by name.
virtual void connectNotify(const char *signal)
void removeAll()
Emitted, when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals a...
void layersWillBeRemoved(const QStringList &theLayerIds)
Emitted when one or more layers are removed from the registry.
void setParent(QObject *parent)
const Key key(const T &value) const
void legendLayersAdded(const QList< QgsMapLayer * > &theMapLayers)
Emitted, when a layer is added to the registry and the legend.
int count()
Return the number of registered layers.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
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 provider data caches (currently used for WFS and WMS providers)
bool isNull(const QVariant &v)
int size() const
void destroyed(QObject *obj)
const T value(const Key &key) const
int remove(const Key &key)