QGIS API Documentation  2.13.0-Master
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 //
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  for ( int i = 0; i < theMapLayers.size(); ++i )
77  {
78  QgsMapLayer * myLayer = theMapLayers.at( i );
79  if ( !myLayer || !myLayer->isValid() )
80  {
81  QgsDebugMsg( "cannot add invalid layers" );
82  continue;
83  }
84  //check the layer is not already registered!
85  if ( !mMapLayers.contains( myLayer->id() ) )
86  {
87  mMapLayers[myLayer->id()] = myLayer;
88  myResultList << mMapLayers[myLayer->id()];
89  if ( takeOwnership )
90  mOwnedLayers << myLayer;
91  emit layerWasAdded( myLayer );
92  }
93  }
94  if ( !myResultList.isEmpty() )
95  {
96  emit layersAdded( myResultList );
97 
98  if ( addToLegend )
99  emit legendLayersAdded( myResultList );
100  }
101  return myResultList;
102 } // QgsMapLayerRegistry::addMapLayers
103 
104 //this is just a thin wrapper for addMapLayers
105 QgsMapLayer *
107  bool addToLegend,
108  bool takeOwnership )
109 {
110  QList<QgsMapLayer *> addedLayers;
111  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
112  return addedLayers.isEmpty() ? nullptr : addedLayers[0];
113 }
114 
115 
116 //introduced in 1.8
118 {
119  QList<QgsMapLayer*> layers;
120  Q_FOREACH ( const QString &myId, theLayerIds )
121  {
122  layers << mMapLayers.value( myId );
123  }
124 
125  removeMapLayers( layers );
126 }
127 
129 {
130  QStringList layerIds;
131 
132  Q_FOREACH ( QgsMapLayer* layer, layers )
133  {
134  if ( layer )
135  layerIds << layer->id();
136  }
137 
138  emit layersWillBeRemoved( layerIds );
139  emit layersWillBeRemoved( layers );
140 
141  Q_FOREACH ( QgsMapLayer* lyr, layers )
142  {
143  if ( !lyr )
144  continue;
145 
146  QString myId( lyr->id() );
147  if ( mOwnedLayers.contains( lyr ) )
148  {
149  emit layerWillBeRemoved( myId );
150  emit layerWillBeRemoved( lyr );
151  delete lyr;
152  mOwnedLayers.remove( lyr );
153  }
154  mMapLayers.remove( myId );
155  emit layerRemoved( myId );
156  }
157 
158  emit layersRemoved( layerIds );
159 }
160 
162 {
163  removeMapLayers( QList<QgsMapLayer*>() << mMapLayers.value( theLayerId ) );
164 }
165 
167 {
168  if ( layer )
169  removeMapLayers( QList<QgsMapLayer*>() << layer );
170 }
171 
173 {
174  emit removeAll();
175  // now let all canvas observers know to clear themselves,
176  // and then consequently any of their map legends
177  removeMapLayers( mMapLayers.keys() );
178  mMapLayers.clear();
179 } // QgsMapLayerRegistry::removeAllMapLayers()
180 
182 {
184  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
185  {
186  QgsMapLayer* layer = it.value();
187  if ( layer )
188  {
189  layer->reload();
190  }
191  }
192 }
193 
195 {
196  return mMapLayers;
197 }
198 
199 
200 #if 0
201 void QgsMapLayerRegistry::connectNotify( const char * signal )
202 {
203  Q_UNUSED( signal );
204  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
205 } // QgsMapLayerRegistry::connectNotify
206 #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
const T & at(int i) const
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()
int size() const
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.
iterator end()
void layerRemoved(const QString &theLayerId)
Emitted after a layer was removed from the registry.
bool isValid()
Return the status of the layer.
iterator begin()
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.
bool contains(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.
bool remove(const T &value)
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.
void reloadAllLayers()
Reload all provider data caches (currently used for WFS and WMS providers)
int size() const
const T value(const Key &key) const
int remove(const Key &key)