QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsmaplayerregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * QgsMapLayerRegistry.cpp - Singleton class for tracking mMapLayers.
3  * -------------------
4  * begin : Sun June 02 2004
5  * copyright : (C) 2004 by Tim Sutton
6  * email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsmaplayerregistry.h"
19 #include "qgsmaplayer.h"
20 #include "qgslogger.h"
21 
22 //
23 // Main class begins now...
24 //
25 
26 QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
27 {
28  // constructor does nothing
29 }
30 
32 {
34 }
35 
36 // get the layer count (number of registered layers)
38 {
39  return mMapLayers.size();
40 }
41 
43 {
44  return mMapLayers.value( theLayerId );
45 }
46 
47 QList<QgsMapLayer *> QgsMapLayerRegistry::mapLayersByName( QString layerName )
48 {
49  QList<QgsMapLayer *> myResultList;
50  foreach ( QgsMapLayer* layer, mMapLayers )
51  {
52  if ( layer->name() == layerName )
53  {
54  myResultList << layer;
55  }
56  }
57  return myResultList;
58 }
59 
60 //introduced in 1.8
61 QList<QgsMapLayer *> QgsMapLayerRegistry::addMapLayers(
62  QList<QgsMapLayer *> theMapLayers,
63  bool addToLegend,
64  bool takeOwnership )
65 {
66  QList<QgsMapLayer *> myResultList;
67  for ( int i = 0; i < theMapLayers.size(); ++i )
68  {
69  QgsMapLayer * myLayer = theMapLayers.at( i );
70  if ( !myLayer || !myLayer->isValid() )
71  {
72  QgsDebugMsg( "cannot add invalid layers" );
73  continue;
74  }
75  //check the layer is not already registered!
76  if ( !mMapLayers.contains( myLayer->id() ) )
77  {
78  mMapLayers[myLayer->id()] = myLayer;
79  myResultList << mMapLayers[myLayer->id()];
80  if ( takeOwnership )
81  mOwnedLayers << myLayer;
82  emit layerWasAdded( myLayer );
83  }
84  }
85  if ( myResultList.count() > 0 )
86  {
87  emit layersAdded( myResultList );
88 
89  if ( addToLegend )
90  emit legendLayersAdded( myResultList );
91  }
92  return myResultList;
93 } // QgsMapLayerRegistry::addMapLayers
94 
95 //this is just a thin wrapper for addMapLayers
98  bool addToLegend,
99  bool takeOwnership )
100 {
101  QList<QgsMapLayer *> addedLayers;
102  addedLayers = addMapLayers( QList<QgsMapLayer*>() << theMapLayer, addToLegend, takeOwnership );
103  return addedLayers.isEmpty() ? 0 : addedLayers[0];
104 }
105 
106 
107 //introduced in 1.8
108 void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds )
109 {
110  emit layersWillBeRemoved( theLayerIds );
111 
112  foreach ( const QString &myId, theLayerIds )
113  {
114  QgsMapLayer* lyr = mMapLayers[myId];
115  if ( mOwnedLayers.contains( lyr ) )
116  {
117  emit layerWillBeRemoved( myId );
118  delete lyr;
119  mOwnedLayers.remove( lyr );
120  }
121  mMapLayers.remove( myId );
122  emit layerRemoved( myId );
123  }
124  emit layersRemoved( theLayerIds );
125 }
126 
127 void QgsMapLayerRegistry::removeMapLayer( const QString& theLayerId )
128 {
129  removeMapLayers( QStringList( theLayerId ) );
130 }
131 
133 {
134  emit removeAll();
135  // now let all canvas observers know to clear themselves,
136  // and then consequently any of their map legends
137  removeMapLayers( mMapLayers.keys() );
138  mMapLayers.clear();
139 } // QgsMapLayerRegistry::removeAllMapLayers()
140 
142 {
143 }
144 
146 {
147  QMap<QString, QgsMapLayer *>::iterator it;
148  for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
149  {
150  QgsMapLayer* layer = it.value();
151  if ( layer )
152  {
153  layer->reload();
154  }
155  }
156 }
157 
158 const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
159 {
160  return mMapLayers;
161 }
162 
163 
164 #if 0
165 void QgsMapLayerRegistry::connectNotify( const char * signal )
166 {
167  Q_UNUSED( signal );
168  //QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
169 } // QgsMapLayerRegistry::connectNotify
170 #endif