1
/***************************************************************************
2
* QgsMapLayerRegistry.cpp - Singleton class for tracking mMapLayers.
4
* begin : Sun June 02 2004
5
* copyright : (C) 2004 by Tim Sutton
6
* email : tim@linfiniti.com
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
/* $Id: qgsmaplayerregistry.cpp 5177 2006-04-05 13:01:52Z mhugent $ */
21
#include "qgsmaplayerregistry.h"
22
#include "qgslogger.h"
23
#include "qgsproject.h"
27
// Static calls to enforce singleton behaviour
29
QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0;
30
QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
34
mInstance = new QgsMapLayerRegistry();
40
// Main class begins now...
43
QgsMapLayerRegistry::QgsMapLayerRegistry(QObject *parent, const char *name) : QObject(parent,name)
45
QgsDebugMsg("QgsMapLayerRegistry created!");
46
// constructor does nothing
48
// get the layer count (number of registered layers)
49
const int QgsMapLayerRegistry::count()
51
return mMapLayers.size();
53
//! Get a vector layer from the registry - the the requested key does not exist or
54
//does not correspond to a vector layer, null returned!
55
QgsVectorLayer * QgsMapLayerRegistry::getVectorLayer(QString theLayerId)
57
QgsVectorLayer * myVectorLayer = (QgsVectorLayer*) mMapLayers[theLayerId];
60
if (myVectorLayer->type() == QgsMapLayer::VECTOR)
77
QgsMapLayer * QgsMapLayerRegistry::mapLayer(QString theLayerId)
79
QgsMapLayer * myMapLayer = mMapLayers[theLayerId];
93
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
95
QgsDebugMsg("QgsMapLayerRegistry::addMaplayer - '" + theMapLayer->name());
96
//check the layer is not already registered!
97
std::map<QString,QgsMapLayer*>::iterator myIterator = mMapLayers.find(theMapLayer->getLayerID());
98
//if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
99
if (myIterator == mMapLayers.end())
101
mMapLayers[theMapLayer->getLayerID()] = theMapLayer;
104
emit layerWasAdded(theMapLayer);
106
// notify the project we've made a change
107
QgsProject::instance()->dirty(true);
109
return mMapLayers[theMapLayer->getLayerID()];
113
QgsDebugMsg("addMaplayer - " + theMapLayer->name() + " already registered");
116
} // QgsMapLayerRegistry::addMapLayer
120
void QgsMapLayerRegistry::removeMapLayer(QString theLayerId, bool theEmitSignal)
122
QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - emitting signal to notify all users of this layer to release it.");
124
emit layerWillBeRemoved(theLayerId);
125
QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - deleting map layer.");
126
delete mMapLayers[theLayerId];
127
QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - unregistering map layer.");
128
mMapLayers.erase(theLayerId);
129
QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - operation complete.");
130
// notify the project we've made a change
131
QgsProject::instance()->dirty(true);
134
void QgsMapLayerRegistry::removeAllMapLayers()
136
QgsDebugMsg("QgsMapLayerRegistry::removeAllMapLayers");
138
// moved before physically removing the layers
139
emit removedAll(); // now let all canvas Observers know to clear
140
// themselves, and then consequently any of
143
std::map<QString, QgsMapLayer *>::iterator myMapIterator = mMapLayers.begin();
144
while ( myMapIterator != mMapLayers.end() )
146
delete myMapIterator->second; // delete the map layer
148
mMapLayers.erase( myMapIterator );
150
myMapIterator = mMapLayers.begin(); // since iterator invalidated due to
151
// erase(), reset to new first element
154
// notify the project we've made a change
155
QgsProject::instance()->dirty(true);
157
} // QgsMapLayerRegistry::removeAllMapLayers()
160
std::map<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
162
QgsDebugMsg("QgsMapLayerRegistry::mapLayers");
168
void QgsMapLayerRegistry::connectNotify( const char * signal )
170
QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
171
} // QgsMapLayerRegistry::connectNotify