QGIS API Documentation  2.10.1-Pisa
 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 : [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  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  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.count() > 0 )
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() ? 0 : addedLayers[0];
113 }
114 
115 
116 //introduced in 1.8
118 {
119  emit layersWillBeRemoved( theLayerIds );
120 
121  foreach ( const QString &myId, theLayerIds )
122  {
123  QgsMapLayer* lyr = mMapLayers[myId];
124  if ( mOwnedLayers.contains( lyr ) )
125  {
126  emit layerWillBeRemoved( myId );
127  delete lyr;
128  mOwnedLayers.remove( lyr );
129  }
130  mMapLayers.remove( myId );
131  emit layerRemoved( myId );
132  }
133  emit layersRemoved( theLayerIds );
134 }
135 
137 {
138  removeMapLayers( QStringList( theLayerId ) );
139 }
140 
142 {
143  emit removeAll();
144  // now let all canvas observers know to clear themselves,
145  // and then consequently any of their map legends
146  removeMapLayers( mMapLayers.keys() );
147  mMapLayers.clear();
148 } // QgsMapLayerRegistry::removeAllMapLayers()
149 
151 {
152 }
153 
155 {
157  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
158  {
159  QgsMapLayer* layer = it.value();
160  if ( layer )
161  {
162  layer->reload();
163  }
164  }
165 }
166 
168 {
169  return mMapLayers;
170 }
171 
172 
173 #if 0
174 void QgsMapLayerRegistry::connectNotify( const char * signal )
175 {
176  Q_UNUSED( signal );
177  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
178 } // QgsMapLayerRegistry::connectNotify
179 #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.
bool contains(const Key &key) const
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.
const T & at(int i) const
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:129
void removeAllMapLayers()
Remove all registered layers.
void clear()
void layersRemoved(QStringList theLayerIds)
Emitted after one or more layers were removed from the registry.
int size() const
QList< Key > keys() const
const QString & name() const
Get the display name of the layer.
int count(const T &value) const
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.
bool isEmpty() const
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:99
iterator end()
bool isValid()
iterator begin()
This class tracks map layers that are currently loaded and provides a means to fetch a pointer to a m...
virtual void connectNotify(const char *signal)
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.
bool contains(const T &value) const
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)
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)
int size() const
const T value(const Key &key) const
void layerWillBeRemoved(QString theLayerId)
Emitted when a layer is removed from the registry.
int remove(const Key &key)