QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 //
27 {
28  if ( mInstance == 0 )
29  {
31  }
32  return mInstance;
33 }
34 
35 //
36 // Main class begins now...
37 //
38 
39 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
40 {
41  // constructor does nothing
42 }
43 
45 {
47 }
48 
49 // get the layer count (number of registered layers)
51 {
52  return mMapLayers.size();
53 }
54 
56 {
57  return mMapLayers.value( theLayerId );
58 }
59 
60 QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
61 {
62  QList<QgsMapLayer *> myResultList;
63  foreach ( QgsMapLayer* layer, mMapLayers )
64  {
65  if ( layer->name() == layerName )
66  {
67  myResultList << layer;
68  }
69  }
70  return myResultList;
71 }
72 
73 //introduced in 1.8
74 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
75  QList<QgsMapLayer *> theMapLayers,
76  bool addToLegend,
77  bool takeOwnership )
78 {
79  QList<QgsMapLayer *> myResultList;
80  for ( int i = 0; i < theMapLayers.size(); ++i )
81  {
82  QgsMapLayer * myLayer = theMapLayers.at( i );
83  if ( !myLayer || !myLayer->isValid() )
84  {
85  QgsDebugMsg( "cannot add invalid layers" );
86  continue;
87  }
88  //check the layer is not already registered!
89  if ( !mMapLayers.contains( myLayer->id() ) )
90  {
91  mMapLayers[myLayer->id()] = myLayer;
92  myResultList << mMapLayers[myLayer->id()];
93  if ( takeOwnership )
94  mOwnedLayers << myLayer;
95  emit layerWasAdded( myLayer );
96  }
97  }
98  if ( myResultList.count() > 0 )
99  {
100  emit layersAdded( myResultList );
101 
102  if ( addToLegend )
103  emit legendLayersAdded( myResultList );
104  }
105  return myResultList;
106 } // QgsMapLayerRegistry::addMapLayers
107 
108 //this is just a thin wrapper for addMapLayers
109 QgsMapLayer *
111  bool addToLegend,
112  bool takeOwnership )
113 {
114  QList<QgsMapLayer *> addedLayers;
115  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
116  return addedLayers.isEmpty() ? 0 : addedLayers[0];
117 }
118 
119 
120 //introduced in 1.8
121 void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )
122 {
123  emit layersWillBeRemoved( theLayerIds );
124 
125  foreach ( const QString &myId, theLayerIds )
126  {
127  QgsMapLayer* lyr = mMapLayers[myId];
128  emit layerWillBeRemoved( myId );
129  if ( mOwnedLayers.contains( lyr ) )
130  {
131  delete lyr;
132  mOwnedLayers.remove( lyr );
133  }
134  mMapLayers.remove( myId );
135  emit layerRemoved( myId );
136  }
137  emit layersRemoved( theLayerIds );
138 }
139 
140 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
141 {
142  removeMapLayers( QStringList( theLayerId ) );
143 }
144 
146 {
147  emit removeAll();
148  // now let all canvas observers know to clear themselves,
149  // and then consequently any of their map legends
150  removeMapLayers( mMapLayers.keys() );
151  mMapLayers.clear();
152 } // QgsMapLayerRegistry::removeAllMapLayers()
153 
154 //Added in QGIS 1.4
156 {
157  QMap<QString, QgsMapLayer *>::iterator it;
158  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
159  {
160  //the map layer will take care of deleting the QImage
161  it.value()->setCacheImage( 0 );
162  }
163 } // QgsMapLayerRegistry::clearAllLayerCaches()
164 
166 {
167  QMap<QString, QgsMapLayer *>::iterator it;
168  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
169  {
170  QgsMapLayer* layer = it.value();
171  if ( layer )
172  {
173  layer->reload();
174  }
175  }
176 }
177 
178 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
179 {
180  return mMapLayers;
181 }
182 
183 
184 
185 void QgsMapLayerRegistry::connectNotify( const char * signal )
186 {
187  Q_UNUSED( signal );
188  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
189 } // QgsMapLayerRegistry::connectNotify
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:45
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:36
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 connectNotify(const char *signal)
debugging member invoked when a connect() is made to this object
void removeAllMapLayers()
Remove all registered layers.
static QgsMapLayerRegistry * mInstance
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.
Definition: qgsmaplayer.cpp:94
bool isValid()
QgsMapLayerRegistry(QObject *parent=0)
protected constructor
This class tracks map layers that are currently loaded and provides a means to fetch a pointer to a m...
QMap< QString, QgsMapLayer * > mMapLayers
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.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QSet< QgsMapLayer * > mOwnedLayers
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)
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.