|
QGIS API Documentation
master-6227475
|
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 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 QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName ) 00061 { 00062 QList<QgsMapLayer *> myResultList; 00063 foreach ( QgsMapLayer* layer, mMapLayers ) 00064 { 00065 if ( layer->name() == layerName ) 00066 { 00067 myResultList << layer; 00068 } 00069 } 00070 return myResultList; 00071 } 00072 00073 //introduced in 1.8 00074 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers( 00075 QList<QgsMapLayer *> theMapLayers, 00076 bool addToLegend ) 00077 { 00078 QList<QgsMapLayer *> myResultList; 00079 for ( int i = 0; i < theMapLayers.size(); ++i ) 00080 { 00081 QgsMapLayer * myLayer = theMapLayers.at( i ); 00082 if ( !myLayer || !myLayer->isValid() ) 00083 { 00084 QgsDebugMsg( "cannot add invalid layers" ); 00085 continue; 00086 } 00087 //check the layer is not already registered! 00088 QMap<QString, QgsMapLayer*>::iterator myIterator = 00089 mMapLayers.find( myLayer->id() ); 00090 //if myIterator returns mMapLayers.end() then it 00091 //does not exist in registry and its safe to add it 00092 if ( myIterator == mMapLayers.end() ) 00093 { 00094 mMapLayers[myLayer->id()] = myLayer; 00095 myResultList << mMapLayers[myLayer->id()]; 00096 emit layerWasAdded( myLayer ); 00097 } 00098 } 00099 if ( myResultList.count() > 0 ) 00100 { 00101 emit layersAdded( myResultList ); 00102 00103 if ( addToLegend ) 00104 emit legendLayersAdded( myResultList ); 00105 } 00106 return myResultList; 00107 } // QgsMapLayerRegistry::addMapLayers 00108 00109 //this is just a thin wrapper for addMapLayers 00110 QgsMapLayer * 00111 QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer, 00112 bool addToLegend ) 00113 { 00114 QList<QgsMapLayer *> addedLayers; 00115 addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend ); 00116 return addedLayers.isEmpty() ? 0 : addedLayers[0]; 00117 } 00118 00119 00120 //introduced in 1.8 00121 void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds ) 00122 { 00123 emit layersWillBeRemoved( theLayerIds ); 00124 00125 foreach ( const QString &myId, theLayerIds ) 00126 { 00127 emit layerWillBeRemoved( myId ); 00128 delete mMapLayers[myId]; 00129 mMapLayers.remove( myId ); 00130 } 00131 } 00132 00133 void QgsMapLayerRegistry::clearMapLayers() 00134 { 00135 mMapLayers.clear(); 00136 } // QgsMapLayerRegistry::clearMapLayers() 00137 00138 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId ) 00139 { 00140 removeMapLayers( QStringList( theLayerId ) ); 00141 } 00142 00143 void QgsMapLayerRegistry::removeAllMapLayers() 00144 { 00145 emit removeAll(); 00146 // now let all canvas observers know to clear themselves, 00147 // and then consequently any of their map legends 00148 removeMapLayers( mMapLayers.keys() ); 00149 mMapLayers.clear(); 00150 } // QgsMapLayerRegistry::removeAllMapLayers() 00151 00152 //Added in QGIS 1.4 00153 void QgsMapLayerRegistry::clearAllLayerCaches() 00154 { 00155 QMap<QString, QgsMapLayer *>::iterator it; 00156 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00157 { 00158 //the map layer will take care of deleting the QImage 00159 it.value()->setCacheImage( 0 ); 00160 } 00161 } // QgsMapLayerRegistry::clearAllLayerCaches() 00162 00163 void QgsMapLayerRegistry::reloadAllLayers() 00164 { 00165 QMap<QString, QgsMapLayer *>::iterator it; 00166 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00167 { 00168 QgsMapLayer* layer = it.value(); 00169 if ( layer ) 00170 { 00171 layer->reload(); 00172 } 00173 } 00174 } 00175 00176 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers() 00177 { 00178 return mMapLayers; 00179 } 00180 00181 00182 00183 void QgsMapLayerRegistry::connectNotify( const char * signal ) 00184 { 00185 Q_UNUSED( signal ); 00186 //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal)); 00187 } // QgsMapLayerRegistry::connectNotify