QGIS API Documentation  2.8.6-Wien
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 // Main class begins now...
24 //
25 
26 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
27 {
28  // constructor does nothing
29 }
30 
32 {
34 }
35 
36 // get the layer count (number of registered layers)
38 {
39  return mMapLayers.size();
40 }
41 
43 {
44  return mMapLayers.value( theLayerId );
45 }
46 
47 QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
48 {
49  QList<QgsMapLayer *> myResultList;
50  foreach ( QgsMapLayer* layer, mMapLayers )
51  {
52  if ( layer->name() == layerName )
53  {
54  myResultList << layer;
55  }
56  }
57  return myResultList;
58 }
59 
60 //introduced in 1.8
61 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
62  QList<QgsMapLayer *> theMapLayers,
63  bool addToLegend,
64  bool takeOwnership )
65 {
66  QList<QgsMapLayer *> myResultList;
67  for ( int i = 0; i < theMapLayers.size(); ++i )
68  {
69  QgsMapLayer * myLayer = theMapLayers.at( i );
70  if ( !myLayer || !myLayer->isValid() )
71  {
72  QgsDebugMsg( "cannot add invalid layers" );
73  continue;
74  }
75  //check the layer is not already registered!
76  if ( !mMapLayers.contains( myLayer->id() ) )
77  {
78  mMapLayers[myLayer->id()] = myLayer;
79  myResultList << mMapLayers[myLayer->id()];
80  if ( takeOwnership )
81  mOwnedLayers << myLayer;
82  emit layerWasAdded( myLayer );
83  }
84  }
85  if ( myResultList.count() > 0 )
86  {
87  emit layersAdded( myResultList );
88 
89  if ( addToLegend )
90  emit legendLayersAdded( myResultList );
91  }
92  return myResultList;
93 } // QgsMapLayerRegistry::addMapLayers
94 
95 //this is just a thin wrapper for addMapLayers
98  bool addToLegend,
99  bool takeOwnership )
100 {
101  QList<QgsMapLayer *> addedLayers;
102  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
103  return addedLayers.isEmpty() ? 0 : addedLayers[0];
104 }
105 
106 
107 //introduced in 1.8
108 void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )
109 {
110  emit layersWillBeRemoved( theLayerIds );
111 
112  foreach ( const QString &myId, theLayerIds )
113  {
114  QgsMapLayer* lyr = mMapLayers[myId];
115  if ( mOwnedLayers.contains( lyr ) )
116  {
117  emit layerWillBeRemoved( myId );
118  delete lyr;
119  mOwnedLayers.remove( lyr );
120  }
121  mMapLayers.remove( myId );
122  emit layerRemoved( myId );
123  }
124  emit layersRemoved( theLayerIds );
125 }
126 
127 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
128 {
129  removeMapLayers( QStringList( theLayerId ) );
130 }
131 
133 {
134  emit removeAll();
135  // now let all canvas observers know to clear themselves,
136  // and then consequently any of their map legends
137  removeMapLayers( mMapLayers.keys() );
138  mMapLayers.clear();
139 } // QgsMapLayerRegistry::removeAllMapLayers()
140 
142 {
143 }
144 
146 {
147  QMap<QString, QgsMapLayer *>::iterator it;
148  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
149  {
150  QgsMapLayer* layer = it.value();
151  if ( layer )
152  {
153  layer->reload();
154  }
155  }
156 }
157 
158 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
159 {
160  return mMapLayers;
161 }
162 
163 
164 #if 0
165 void QgsMapLayerRegistry::connectNotify( const char * signal )
166 {
167  Q_UNUSED( signal );
168  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
169 } // QgsMapLayerRegistry::connectNotify
170 #endif
void legendLayersAdded(QList< QgsMapLayer * > theMapLayers)
Emitted, when a layer is added to the registry and the legend.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void removeMapLayer(const QString &theLayerId)
Remove a layer from qgis.
void layersAdded(QList< QgsMapLayer * > theMapLayers)
Emitted when one or more layers are added to the registry.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void layersWillBeRemoved(QStringList theLayerIds)
Emitted when one or more layers are removed from the registry.
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:129
void removeAllMapLayers()
Remove all registered layers.
void layersRemoved(QStringList theLayerIds)
Emitted after one or more layers were removed from the registry.
const QString & name() const
Get the display name of the layer.
void layerRemoved(QString theLayerId)
Emitted after a layer was removed from the registry.
void layerWasAdded(QgsMapLayer *theMapLayer)
Emitted when a layer is added to the registry.
QList< QgsMapLayer * > addMapLayers(QList< QgsMapLayer * > theMapLayers, bool addToLegend=true, bool takeOwnership=true)
Add a list of layers to the map of loaded layers.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
bool isValid()
void removeAll()
Emitted, when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals a...
QList< QgsMapLayer * > mapLayersByName(QString layerName)
Retrieve a pointer to a loaded layer by name.
int count()
Return the number of registered layers.
void removeMapLayers(QStringList theLayerIds)
Remove a set of layers from the registry.
const QMap< QString, QgsMapLayer * > & mapLayers()
Retrieve the mapLayers collection (mainly intended for use by projection)
Q_DECL_DEPRECATED void clearAllLayerCaches()
Clears all layer caches, resetting them to zero and freeing up any memory they may have been using...
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
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)
void layerWillBeRemoved(QString theLayerId)
Emitted when a layer is removed from the registry.