34 return mMapLayers.size();
40 const QList<QgsMapLayer *> cLayers = mMapLayers.values();
41 for (
const auto l : cLayers )
51 return mMapLayers.value( layerId );
56 QList<QgsMapLayer *> myResultList;
59 if ( layer->
name() == layerName )
61 myResultList << layer;
69 QList<QgsMapLayer *> myResultList;
74 QgsDebugMsg( QStringLiteral(
"Cannot add null layers" ) );
78 if ( !mMapLayers.contains( myLayer->
id() ) )
80 mMapLayers[myLayer->
id()] = myLayer;
81 myResultList << mMapLayers[myLayer->
id()];
84 myLayer->setParent(
this );
86 connect( myLayer, &QObject::destroyed,
this, &QgsMapLayerStore::onMapLayerDeleted );
90 if ( !myResultList.isEmpty() )
100 QList<QgsMapLayer *> addedLayers;
101 addedLayers =
addMapLayers( QList<QgsMapLayer *>() << layer, takeOwnership );
102 return addedLayers.isEmpty() ? nullptr : addedLayers[0];
107 QList<QgsMapLayer *>
layers;
108 Q_FOREACH (
const QString &myId, layerIds )
110 layers << mMapLayers.value( myId );
118 if ( layers.isEmpty() )
121 QStringList layerIds;
122 QList<QgsMapLayer *> layerList;
127 if ( layer && mMapLayers.contains( layer->
id() ) )
129 layerIds << layer->
id();
134 if ( layerIds.isEmpty() )
142 QString myId( lyr->
id() );
145 mMapLayers.remove( myId );
146 if ( lyr->parent() == this )
158 removeMapLayers( QList<QgsMapLayer *>() << mMapLayers.value( layerId ) );
172 if ( mMapLayers.contains( layer->
id() ) )
179 mMapLayers.remove( layer->
id() );
180 layer->setParent(
nullptr );
199 if ( !other || other ==
this )
202 Q_ASSERT_X( other->thread() == thread(),
"QgsMapLayerStore::transferLayersFromStore",
"Cannot transfer layers from store with different thread affinity" );
204 QMap<QString, QgsMapLayer *> otherLayers = other->
mapLayers();
205 QMap<QString, QgsMapLayer *>::const_iterator it = otherLayers.constBegin();
206 for ( ; it != otherLayers.constEnd(); ++it )
214 void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
216 QString
id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );
220 QgsDebugMsg( QStringLiteral(
"Map layer deleted without unregistering! %1" ).arg(
id ) );
221 mMapLayers.remove(
id );
232 QMap<QString, QgsMapLayer *> validLayers;
233 for (
const auto &
id : mMapLayers.keys() )
235 if ( mMapLayers[
id]->isValid() )
236 validLayers[
id] = mMapLayers[
id];
QMap< QString, QgsMapLayer * > validMapLayers() const
Returns a map of all valid layers by layer ID.
Base class for all map layer types.
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.
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'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.
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.