QGIS API Documentation  3.6.0-Noosa (5873452)
qgsmaplayerstore.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerstore.cpp
3  --------------------
4  begin : May 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
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 "qgsmaplayerstore.h"
19 #include "qgsmaplayer.h"
20 #include "qgslogger.h"
21 #include <QList>
22 
24  : QObject( parent )
25 {}
26 
28 {
30 }
31 
33 {
34  return mMapLayers.size();
35 }
36 
38 {
39  int i = 0;
40  const QList<QgsMapLayer *> cLayers = mMapLayers.values();
41  for ( const auto l : cLayers )
42  {
43  if ( l->isValid() )
44  i++;
45  }
46  return i;
47 }
48 
49 QgsMapLayer *QgsMapLayerStore::mapLayer( const QString &layerId ) const
50 {
51  return mMapLayers.value( layerId );
52 }
53 
54 QList<QgsMapLayer *> QgsMapLayerStore::mapLayersByName( const QString &layerName ) const
55 {
56  QList<QgsMapLayer *> myResultList;
57  Q_FOREACH ( QgsMapLayer *layer, mMapLayers )
58  {
59  if ( layer->name() == layerName )
60  {
61  myResultList << layer;
62  }
63  }
64  return myResultList;
65 }
66 
67 QList<QgsMapLayer *> QgsMapLayerStore::addMapLayers( const QList<QgsMapLayer *> &layers, bool takeOwnership )
68 {
69  QList<QgsMapLayer *> myResultList;
70  Q_FOREACH ( QgsMapLayer *myLayer, layers )
71  {
72  if ( !myLayer )
73  {
74  QgsDebugMsg( QStringLiteral( "Cannot add null layers" ) );
75  continue;
76  }
77  //check the layer is not already registered!
78  if ( !mMapLayers.contains( myLayer->id() ) )
79  {
80  mMapLayers[myLayer->id()] = myLayer;
81  myResultList << mMapLayers[myLayer->id()];
82  if ( takeOwnership )
83  {
84  myLayer->setParent( this );
85  }
86  connect( myLayer, &QObject::destroyed, this, &QgsMapLayerStore::onMapLayerDeleted );
87  emit layerWasAdded( myLayer );
88  }
89  }
90  if ( !myResultList.isEmpty() )
91  {
92  emit layersAdded( myResultList );
93  }
94  return myResultList;
95 }
96 
98 QgsMapLayerStore::addMapLayer( QgsMapLayer *layer, bool takeOwnership )
99 {
100  QList<QgsMapLayer *> addedLayers;
101  addedLayers = addMapLayers( QList<QgsMapLayer *>() << layer, takeOwnership );
102  return addedLayers.isEmpty() ? nullptr : addedLayers[0];
103 }
104 
105 void QgsMapLayerStore::removeMapLayers( const QStringList &layerIds )
106 {
107  QList<QgsMapLayer *> layers;
108  Q_FOREACH ( const QString &myId, layerIds )
109  {
110  layers << mMapLayers.value( myId );
111  }
112 
113  removeMapLayers( layers );
114 }
115 
116 void QgsMapLayerStore::removeMapLayers( const QList<QgsMapLayer *> &layers )
117 {
118  if ( layers.isEmpty() )
119  return;
120 
121  QStringList layerIds;
122  QList<QgsMapLayer *> layerList;
123 
124  Q_FOREACH ( QgsMapLayer *layer, layers )
125  {
126  // check layer and the store contains it
127  if ( layer && mMapLayers.contains( layer->id() ) )
128  {
129  layerIds << layer->id();
130  layerList << layer;
131  }
132  }
133 
134  if ( layerIds.isEmpty() )
135  return;
136 
137  emit layersWillBeRemoved( layerIds );
138  emit layersWillBeRemoved( layerList );
139 
140  Q_FOREACH ( QgsMapLayer *lyr, layerList )
141  {
142  QString myId( lyr->id() );
143  emit layerWillBeRemoved( myId );
144  emit layerWillBeRemoved( lyr );
145  mMapLayers.remove( myId );
146  if ( lyr->parent() == this )
147  {
148  delete lyr;
149  }
150  emit layerRemoved( myId );
151  }
152 
153  emit layersRemoved( layerIds );
154 }
155 
156 void QgsMapLayerStore::removeMapLayer( const QString &layerId )
157 {
158  removeMapLayers( QList<QgsMapLayer *>() << mMapLayers.value( layerId ) );
159 }
160 
162 {
163  if ( layer )
164  removeMapLayers( QList<QgsMapLayer *>() << layer );
165 }
166 
168 {
169  if ( !layer )
170  return nullptr;
171 
172  if ( mMapLayers.contains( layer->id() ) )
173  {
174  emit layersWillBeRemoved( QStringList() << layer->id() );
175  emit layersWillBeRemoved( QList<QgsMapLayer *>() << layer );
176  emit layerWillBeRemoved( layer->id() );
177  emit layerWillBeRemoved( layer );
178 
179  mMapLayers.remove( layer->id() );
180  layer->setParent( nullptr );
181  emit layerRemoved( layer->id() );
182  emit layersRemoved( QStringList() << layer->id() );
183  return layer;
184  }
185  return nullptr; //don't return layer - it wasn't owned and accordingly we aren't transferring ownership
186 }
187 
189 {
190  emit allLayersRemoved();
191  // now let all observers know to clear themselves,
192  // and then consequently any of their map legends
193  removeMapLayers( mMapLayers.keys() );
194  mMapLayers.clear();
195 }
196 
198 {
199  if ( !other || other == this )
200  return;
201 
202  Q_ASSERT_X( other->thread() == thread(), "QgsMapLayerStore::transferLayersFromStore", "Cannot transfer layers from store with different thread affinity" );
203 
204  QMap<QString, QgsMapLayer *> otherLayers = other->mapLayers();
205  QMap<QString, QgsMapLayer *>::const_iterator it = otherLayers.constBegin();
206  for ( ; it != otherLayers.constEnd(); ++it )
207  {
208  QgsMapLayer *layer = other->takeMapLayer( it.value() );
209  if ( layer )
210  addMapLayer( layer );
211  }
212 }
213 
214 void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
215 {
216  QString id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );
217 
218  if ( !id.isNull() )
219  {
220  QgsDebugMsg( QStringLiteral( "Map layer deleted without unregistering! %1" ).arg( id ) );
221  mMapLayers.remove( id );
222  }
223 }
224 
225 QMap<QString, QgsMapLayer *> QgsMapLayerStore::mapLayers() const
226 {
227  return mMapLayers;
228 }
229 
230 QMap<QString, QgsMapLayer *> QgsMapLayerStore::validMapLayers() const
231 {
232  QMap<QString, QgsMapLayer *> validLayers;
233  for ( const auto &id : mMapLayers.keys() )
234  {
235  if ( mMapLayers[id]->isValid() )
236  validLayers[id] = mMapLayers[id];
237  }
238  return validLayers;
239 }
QMap< QString, QgsMapLayer * > validMapLayers() const
Returns a map of all valid layers by layer ID.
Base class for all map layer types.
Definition: qgsmaplayer.h:64
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
void layerWasAdded(QgsMapLayer *layer)
Emitted when a layer was added to the store.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void removeMapLayer(const QString &id)
Remove a layer from the store by layer id.
void layersAdded(const QList< QgsMapLayer *> &layers)
Emitted when one or more layers were added to the store.
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the store.
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
void layerWillBeRemoved(const QString &layerId)
Emitted when a layer is about to be removed from the store.
void allLayersRemoved()
Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals ar...
void removeAllMapLayers()
Removes all registered layers.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
~QgsMapLayerStore() override
void layerRemoved(const QString &layerId)
Emitted after a layer was removed from the store.
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the store.
int count() const
Returns the number of layers contained in the store.
int validCount() const
Returns the number of valid layers contained in the store.
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all layers by layer ID.
QgsMapLayer * addMapLayer(QgsMapLayer *layer, bool takeOwnership=true)
Add a layer to the store.
void transferLayersFromStore(QgsMapLayerStore *other)
Transfers all the map layers contained within another map layer store and adds them to this store...
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the store by layer ID.
QString name
Definition: qgsmaplayer.h:68
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
QList< QgsMapLayer * > addMapLayers(const QList< QgsMapLayer *> &layers, bool takeOwnership=true)
Add a list of layers to the store.
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer by layer id.
QList< QgsMapLayer * > mapLayersByName(const QString &name) const
Retrieve a list of matching layers by layer name.
QgsMapLayerStore(QObject *parent=nullptr)
Constructor for QgsMapLayerStore.