QGIS API Documentation  master-3f58142
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 
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::removeMapLayer( const QString& theLayerId )
00134 {
00135   removeMapLayers( QStringList( theLayerId ) );
00136 }
00137 
00138 
00139 void QgsMapLayerRegistry::removeAllMapLayers()
00140 {
00141   emit removeAll();
00142   // now let all canvas observers know to clear themselves,
00143   // and then consequently any of their map legends
00144   removeMapLayers( mMapLayers.keys() );
00145   mMapLayers.clear();
00146 } // QgsMapLayerRegistry::removeAllMapLayers()
00147 
00148 //Added in QGIS 1.4
00149 void QgsMapLayerRegistry::clearAllLayerCaches()
00150 {
00151   QMap<QString, QgsMapLayer *>::iterator it;
00152   for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
00153   {
00154     //the map layer will take care of deleting the QImage
00155     it.value()->setCacheImage( 0 );
00156   }
00157 } // QgsMapLayerRegistry::clearAllLayerCaches()
00158 
00159 void QgsMapLayerRegistry::reloadAllLayers()
00160 {
00161   QMap<QString, QgsMapLayer *>::iterator it;
00162   for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
00163   {
00164     QgsMapLayer* layer = it.value();
00165     if ( layer )
00166     {
00167       layer->reload();
00168     }
00169   }
00170 }
00171 
00172 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
00173 {
00174   return mMapLayers;
00175 }
00176 
00177 
00178 
00179 void QgsMapLayerRegistry::connectNotify( const char * signal )
00180 {
00181   Q_UNUSED( signal );
00182   //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
00183 } //  QgsMapLayerRegistry::connectNotify
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines