|
QGIS API Documentation
master-59fd5e0
|
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 bool takeOwnership ) 00078 { 00079 QList<QgsMapLayer *> myResultList; 00080 for ( int i = 0; i < theMapLayers.size(); ++i ) 00081 { 00082 QgsMapLayer * myLayer = theMapLayers.at( i ); 00083 if ( !myLayer || !myLayer->isValid() ) 00084 { 00085 QgsDebugMsg( "cannot add invalid layers" ); 00086 continue; 00087 } 00088 //check the layer is not already registered! 00089 if ( !mMapLayers.contains( myLayer->id() ) ) 00090 { 00091 mMapLayers[myLayer->id()] = myLayer; 00092 myResultList << mMapLayers[myLayer->id()]; 00093 if ( takeOwnership ) 00094 mOwnedLayers << myLayer; 00095 emit layerWasAdded( myLayer ); 00096 } 00097 } 00098 if ( myResultList.count() > 0 ) 00099 { 00100 emit layersAdded( myResultList ); 00101 00102 if ( addToLegend ) 00103 emit legendLayersAdded( myResultList ); 00104 } 00105 return myResultList; 00106 } // QgsMapLayerRegistry::addMapLayers 00107 00108 //this is just a thin wrapper for addMapLayers 00109 QgsMapLayer * 00110 QgsMapLayerRegistry::addMapLayer( QgsMapLayer* theMapLayer, 00111 bool addToLegend, 00112 bool takeOwnership ) 00113 { 00114 QList<QgsMapLayer *> addedLayers; 00115 addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership ); 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 QgsMapLayer* lyr = mMapLayers[myId]; 00128 emit layerWillBeRemoved( myId ); 00129 if ( mOwnedLayers.contains( lyr ) ) 00130 { 00131 delete lyr; 00132 mOwnedLayers.remove( lyr ); 00133 } 00134 mMapLayers.remove( myId ); 00135 emit layerRemoved( myId ); 00136 } 00137 emit layersRemoved( theLayerIds ); 00138 } 00139 00140 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId ) 00141 { 00142 removeMapLayers( QStringList( theLayerId ) ); 00143 } 00144 00145 void QgsMapLayerRegistry::removeAllMapLayers() 00146 { 00147 emit removeAll(); 00148 // now let all canvas observers know to clear themselves, 00149 // and then consequently any of their map legends 00150 removeMapLayers( mMapLayers.keys() ); 00151 mMapLayers.clear(); 00152 } // QgsMapLayerRegistry::removeAllMapLayers() 00153 00154 //Added in QGIS 1.4 00155 void QgsMapLayerRegistry::clearAllLayerCaches() 00156 { 00157 QMap<QString, QgsMapLayer *>::iterator it; 00158 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00159 { 00160 //the map layer will take care of deleting the QImage 00161 it.value()->setCacheImage( 0 ); 00162 } 00163 } // QgsMapLayerRegistry::clearAllLayerCaches() 00164 00165 void QgsMapLayerRegistry::reloadAllLayers() 00166 { 00167 QMap<QString, QgsMapLayer *>::iterator it; 00168 for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it ) 00169 { 00170 QgsMapLayer* layer = it.value(); 00171 if ( layer ) 00172 { 00173 layer->reload(); 00174 } 00175 } 00176 } 00177 00178 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers() 00179 { 00180 return mMapLayers; 00181 } 00182 00183 00184 00185 void QgsMapLayerRegistry::connectNotify( const char * signal ) 00186 { 00187 Q_UNUSED( signal ); 00188 //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal)); 00189 } // QgsMapLayerRegistry::connectNotify