Quantum GIS API Documentation
1.8
|
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 : [email protected] 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 00018 #include "qgsmaplayerregistry.h" 00019 #include "qgsmaplayer.h" 00020 #include "qgslogger.h" 00021 00022 // 00023 // Static calls to enforce singleton behaviour 00024 // 00025 QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0; 00026 QgsMapLayerRegistry *QgsMapLayerRegistry::instance() 00027 { 00028 if ( mInstance == 0 ) 00029 { 00030 mInstance = new QgsMapLayerRegistry(); 00031 } 00032 return mInstance; 00033 } 00034 00035 // 00036 // Main class begins now... 00037 // 00038 00039 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent ) 00040 { 00041 // constructor does nothing 00042 } 00043 00044 QgsMapLayerRegistry::~QgsMapLayerRegistry() 00045 { 00046 removeAllMapLayers(); 00047 } 00048 00049 // get the layer count (number of registered layers) 00050 int QgsMapLayerRegistry::count() 00051 { 00052 return mMapLayers.size(); 00053 } 00054 00055 QgsMapLayer * QgsMapLayerRegistry::mapLayer( QString theLayerId ) 00056 { 00057 return mMapLayers.value( theLayerId ); 00058 } 00059 00060 //introduced in 1.8 00061 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers( 00062 QList<QgsMapLayer *> theMapLayers, 00063 bool theEmitSignal ) 00064 { 00065 QList<QgsMapLayer *> myResultList; 00066 for ( int i = 0; i < theMapLayers.size(); ++i ) 00067 { 00068 QgsMapLayer * myLayer = theMapLayers.at( i ); 00069 if ( !myLayer || !myLayer->isValid() ) 00070 { 00071 QgsDebugMsg( "cannot add invalid layers" ); 00072 continue; 00073 } 00074 //check the layer is not already registered! 00075 QMap<QString, QgsMapLayer*>::iterator myIterator = 00076 mMapLayers.find( myLayer->id() ); 00077 //if myIterator returns mMapLayers.end() then it 00078 //does not exist in registry and its safe to add it 00079 if ( myIterator == mMapLayers.end() ) 00080 { 00081 mMapLayers[myLayer->id()] = myLayer; 00082 myResultList << mMapLayers[myLayer->id()]; 00083 if ( theEmitSignal ) 00084 emit layerWasAdded( myLayer ); 00085 } 00086 } 00087 if ( theEmitSignal && myResultList.count() > 0 ) 00088 { 00089 emit layersAdded( myResultList ); 00090 } 00091 return myResultList; 00092 } // QgsMapLayerRegistry::addMapLayers 00093 00094 //this is deprecated by addMapLayers and is just a thin wrapper for that 00095 QgsMapLayer * 00096 QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, 00097 bool theEmitSignal ) 00098 { 00099 QList<QgsMapLayer *> myList; 00100 myList.append( theMapLayer ); 00101 addMapLayers( myList, theEmitSignal ); 00102 return theMapLayer; 00103 } // QgsMapLayerRegistry::addMapLayer 00104 00105 //introduced in 1.8 00106 void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds, 00107 bool theEmitSignal ) 00108 { 00109 if ( theEmitSignal ) 00110 emit layersWillBeRemoved( theLayerIds ); 00111 00112 foreach( const QString &myId, theLayerIds ) 00113 { 00114 if ( theEmitSignal ) 00115 emit layerWillBeRemoved( myId ); 00116 delete mMapLayers[myId]; 00117 mMapLayers.remove( myId ); 00118 } 00119 emit layersWillBeRemoved( theLayerIds ); 00120 } 00121 00122 //deprecated 1.8 use removeMapLayers rather 00123 void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, 00124 bool theEmitSignal ) 00125 { 00126 QStringList myList; 00127 myList << theLayerId; 00128 removeMapLayers( myList, theEmitSignal ); 00129 } 00130 00131 void QgsMapLayerRegistry::removeAllMapLayers() 00132 { 00133 // moved before physically removing the layers 00134 emit removedAll(); 00135 00136 // now let all canvas observers know to clear themselves, 00137 // and then consequently any of their map legends 00138 QStringList myList; 00139 QMap<QString, QgsMapLayer *>::iterator it; 00140 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00141 { 00142 QString id = it.key(); 00143 myList << id; 00144 } 00145 removeMapLayers( myList, false ); 00146 mMapLayers.clear(); 00147 } // QgsMapLayerRegistry::removeAllMapLayers() 00148 00149 //Added in QGIS 1.4 00150 void QgsMapLayerRegistry::clearAllLayerCaches() 00151 { 00152 QMap<QString, QgsMapLayer *>::iterator it; 00153 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00154 { 00155 //the map layer will take care of deleting the QImage 00156 it.value()->setCacheImage( 0 ); 00157 } 00158 } // QgsMapLayerRegistry::clearAllLayerCaches() 00159 00160 void QgsMapLayerRegistry::reloadAllLayers() 00161 { 00162 QMap<QString, QgsMapLayer *>::iterator it; 00163 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00164 { 00165 QgsMapLayer* layer = it.value(); 00166 if ( layer ) 00167 { 00168 layer->reload(); 00169 } 00170 } 00171 } 00172 00173 QMap<QString, QgsMapLayer*> & QgsMapLayerRegistry::mapLayers() 00174 { 00175 return mMapLayers; 00176 } 00177 00178 00179 00180 void QgsMapLayerRegistry::connectNotify( const char * signal ) 00181 { 00182 Q_UNUSED( signal ); 00183 //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal)); 00184 } // QgsMapLayerRegistry::connectNotify