QGIS API Documentation  2.0.1-Dufour
 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 //
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