Quantum GIS API Documentation  1.7.4
src/core/qgsmaplayerregistry.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  QgsMapLayerRegistry.cpp  -  Singleton class for tracking mMapLayers.
00003  *                         -------------------
00004  * begin                : Sun June 02 2004
00005  * copyright            : (C) 2004 by Tim Sutton
00006  * email                : tim@linfiniti.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 /* $Id$ */
00018 
00019 #include <iostream>
00020 
00021 #include "qgsmaplayerregistry.h"
00022 #include "qgsmaplayer.h"
00023 #include "qgslogger.h"
00024 
00025 //
00026 // Static calls to enforce singleton behaviour
00027 //
00028 QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0;
00029 QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
00030 {
00031   if ( mInstance == 0 )
00032   {
00033     mInstance = new QgsMapLayerRegistry();
00034   }
00035   return mInstance;
00036 }
00037 
00038 //
00039 // Main class begins now...
00040 //
00041 
00042 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
00043 {
00044   // constructor does nothing
00045 }
00046 
00047 QgsMapLayerRegistry::~QgsMapLayerRegistry()
00048 {
00049   removeAllMapLayers();
00050 }
00051 
00052 // get the layer count (number of registered layers)
00053 int QgsMapLayerRegistry::count()
00054 {
00055   return mMapLayers.size();
00056 }
00057 
00058 QgsMapLayer * QgsMapLayerRegistry::mapLayer( QString theLayerId )
00059 {
00060   return mMapLayers.value( theLayerId );
00061 }
00062 
00063 
00064 
00065 QgsMapLayer *
00066 QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
00067 {
00068   if ( !theMapLayer || !theMapLayer->isValid() )
00069   {
00070     QgsDebugMsg( "cannot add invalid layers" );
00071     return 0;
00072   }
00073 
00074   //check the layer is not already registered!
00075   QMap<QString, QgsMapLayer*>::iterator myIterator = mMapLayers.find( theMapLayer->id() );
00076   //if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
00077   if ( myIterator == mMapLayers.end() )
00078   {
00079     mMapLayers[theMapLayer->id()] = theMapLayer;
00080 
00081     if ( theEmitSignal )
00082       emit layerWasAdded( theMapLayer );
00083 
00084     return mMapLayers[theMapLayer->id()];
00085   }
00086   else
00087   {
00088     return 0;
00089   }
00090 } //  QgsMapLayerRegistry::addMapLayer
00091 
00092 
00093 
00094 void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, bool theEmitSignal )
00095 {
00096   if ( theEmitSignal )
00097     emit layerWillBeRemoved( theLayerId );
00098   delete mMapLayers[theLayerId];
00099   mMapLayers.remove( theLayerId );
00100 }
00101 
00102 void QgsMapLayerRegistry::removeAllMapLayers()
00103 {
00104   // moved before physically removing the layers
00105   emit removedAll();
00106 
00107   // now let all canvas observers know to clear themselves,
00108   // and then consequently any of their map legends
00109   while ( mMapLayers.size() > 0 )
00110   {
00111     QString id = mMapLayers.begin().key();
00112     emit layerWillBeRemoved( id );
00113     delete mMapLayers[ id ]; // delete the map layer
00114     mMapLayers.remove( id );
00115   }
00116 
00117   mMapLayers.clear();
00118 } // QgsMapLayerRegistry::removeAllMapLayers()
00119 
00120 //Added in QGIS 1.4
00121 void QgsMapLayerRegistry::clearAllLayerCaches()
00122 {
00123   QMap<QString, QgsMapLayer *>::iterator it;
00124   for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
00125   {
00126     //the map layer will take care of deleting the QImage
00127     it.value()->setCacheImage( 0 );
00128   }
00129 } // QgsMapLayerRegistry::clearAllLayerCaches()
00130 
00131 void QgsMapLayerRegistry::reloadAllLayers()
00132 {
00133   QMap<QString, QgsMapLayer *>::iterator it;
00134   for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
00135   {
00136     QgsMapLayer* layer = it.value();
00137     if ( layer )
00138     {
00139       layer->reload();
00140     }
00141   }
00142 }
00143 
00144 QMap<QString, QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
00145 {
00146   return mMapLayers;
00147 }
00148 
00149 
00150 
00151 void QgsMapLayerRegistry::connectNotify( const char * signal )
00152 {
00153   //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
00154 } //  QgsMapLayerRegistry::connectNotify
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines