Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* $Id$ */
18 
19 #include <iostream>
20 
21 #include "qgsmaplayerregistry.h"
22 #include "qgsmaplayer.h"
23 #include "qgslogger.h"
24 
25 //
26 // Static calls to enforce singleton behaviour
27 //
30 {
31  if ( mInstance == 0 )
32  {
34  }
35  return mInstance;
36 }
37 
38 //
39 // Main class begins now...
40 //
41 
42 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
43 {
44  // constructor does nothing
45 }
46 
48 {
50 }
51 
52 // get the layer count (number of registered layers)
54 {
55  return mMapLayers.size();
56 }
57 
59 {
60  return mMapLayers.value( theLayerId );
61 }
62 
63 
64 
66 QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
67 {
68  if ( !theMapLayer || !theMapLayer->isValid() )
69  {
70  QgsDebugMsg( "cannot add invalid layers" );
71  return 0;
72  }
73 
74  //check the layer is not already registered!
75  QMap<QString, QgsMapLayer*>::iterator myIterator = mMapLayers.find( theMapLayer->id() );
76  //if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
77  if ( myIterator == mMapLayers.end() )
78  {
79  mMapLayers[theMapLayer->id()] = theMapLayer;
80 
81  if ( theEmitSignal )
82  emit layerWasAdded( theMapLayer );
83 
84  return mMapLayers[theMapLayer->id()];
85  }
86  else
87  {
88  return 0;
89  }
90 } // QgsMapLayerRegistry::addMapLayer
91 
92 
93 
94 void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, bool theEmitSignal )
95 {
96  if ( theEmitSignal )
97  emit layerWillBeRemoved( theLayerId );
98  delete mMapLayers[theLayerId];
99  mMapLayers.remove( theLayerId );
100 }
101 
103 {
104  // moved before physically removing the layers
105  emit removedAll();
106 
107  // now let all canvas observers know to clear themselves,
108  // and then consequently any of their map legends
109  while ( mMapLayers.size() > 0 )
110  {
111  QString id = mMapLayers.begin().key();
112  emit layerWillBeRemoved( id );
113  delete mMapLayers[ id ]; // delete the map layer
114  mMapLayers.remove( id );
115  }
116 
117  mMapLayers.clear();
118 } // QgsMapLayerRegistry::removeAllMapLayers()
119 
120 //Added in QGIS 1.4
122 {
123  QMap<QString, QgsMapLayer *>::iterator it;
124  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
125  {
126  //the map layer will take care of deleting the QImage
127  it.value()->setCacheImage( 0 );
128  }
129 } // QgsMapLayerRegistry::clearAllLayerCaches()
130 
132 {
133  QMap<QString, QgsMapLayer *>::iterator it;
134  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
135  {
136  QgsMapLayer* layer = it.value();
137  if ( layer )
138  {
139  layer->reload();
140  }
141  }
142 }
143 
144 QMap<QString, QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
145 {
146  return mMapLayers;
147 }
148 
149 
150 
151 void QgsMapLayerRegistry::connectNotify( const char * signal )
152 {
153  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
154 } // QgsMapLayerRegistry::connectNotify