19#include "moc_qgsmaplayerstore.cpp"
38 return mMapLayers.size();
46 const QList<QgsMapLayer *> cLayers = mMapLayers.values();
47 for (
const auto l : cLayers )
60 return mMapLayers.value( layerId );
67 QList<QgsMapLayer *> myResultList;
68 const auto constMMapLayers = mMapLayers;
71 if ( layer->name() == layerName )
73 myResultList << layer;
83 QList<QgsMapLayer *> myResultList;
84 const auto constLayers =
layers;
96 if ( mMapLayers.contains( myLayer->id() ) && ! mMapLayers[myLayer->id()]->isValid() && myLayer->isValid() && myLayer->dataProvider() )
98 mMapLayers[myLayer->id()]->setDataSource( myLayer->dataProvider()->dataSourceUri(), myLayer->name(), myLayer->providerType(),
QgsDataProvider::ProviderOptions() );
101 if ( !mMapLayers.contains( myLayer->id() ) )
103 mMapLayers[myLayer->id()] = myLayer;
104 myResultList << mMapLayers[myLayer->id()];
107 myLayer->setParent(
this );
109 connect( myLayer, &QObject::destroyed,
this, &QgsMapLayerStore::onMapLayerDeleted );
113 if ( !myResultList.isEmpty() )
125 QList<QgsMapLayer *> addedLayers;
126 addedLayers =
addMapLayers( QList<QgsMapLayer *>() << layer, takeOwnership );
127 return addedLayers.isEmpty() ? nullptr : addedLayers[0];
134 QList<QgsMapLayer *>
layers;
135 const auto constLayerIds = layerIds;
136 for (
const QString &myId : constLayerIds )
138 layers << mMapLayers.value( myId );
151 QStringList layerIds;
152 QList<QgsMapLayer *> layerList;
154 const auto constLayers =
layers;
158 if ( layer && mMapLayers.contains( layer->id() ) )
160 layerIds << layer->id();
165 if ( layerIds.isEmpty() )
171 const auto constLayerList = layerList;
174 const QString myId( lyr->id() );
177 mMapLayers.remove( myId );
178 if ( lyr->parent() ==
this )
192 removeMapLayers( QList<QgsMapLayer *>() << mMapLayers.value( layerId ) );
210 if ( mMapLayers.contains( layer->
id() ) )
217 mMapLayers.remove( layer->
id() );
218 layer->setParent(
nullptr );
241 if ( !other || other ==
this )
244 Q_ASSERT_X( other->thread() == thread(),
"QgsMapLayerStore::transferLayersFromStore",
"Cannot transfer layers from store with different thread affinity" );
246 const QMap<QString, QgsMapLayer *> otherLayers = other->
mapLayers();
247 QMap<QString, QgsMapLayer *>::const_iterator it = otherLayers.constBegin();
248 for ( ; it != otherLayers.constEnd(); ++it )
256void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
260 const QString
id = mMapLayers.key(
static_cast<QgsMapLayer *
>( obj ) );
264 QgsDebugError( QStringLiteral(
"Map layer deleted without unregistering! %1" ).arg(
id ) );
265 mMapLayers.remove(
id );
282 QMap<QString, QgsMapLayer *> validLayers;
283 for (
auto it = mMapLayers.constBegin(); it != mMapLayers.constEnd(); it++ )
285 if ( it.value()->isValid() )
286 validLayers[it.key()] = it.value();
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
int count() const
Returns the number of layers contained in the store.
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all layers by layer ID.
void removeAllMapLayers()
Removes all registered layers.
~QgsMapLayerStore() override
QgsMapLayerStore(QObject *parent=nullptr)
Constructor for QgsMapLayerStore.
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the store.
int validCount() const
Returns the number of valid layers contained in the store.
void removeMapLayer(const QString &id)
Remove a layer from the store by layer id.
void layerWillBeRemoved(const QString &layerId)
Emitted when a layer is about to be removed from the store.
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the store.
void allLayersRemoved()
Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals ar...
void layerRemoved(const QString &layerId)
Emitted after a layer was removed from the store.
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
void layerWasAdded(QgsMapLayer *layer)
Emitted when a layer was added to the store.
QList< QgsMapLayer * > mapLayersByName(const QString &name) const
Retrieve a list of matching layers by layer name.
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the store by layer ID.
QMap< QString, QgsMapLayer * > validMapLayers() const
Returns a map of all valid layers by layer ID.
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer 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 layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the store.
QList< QgsMapLayer * > addMapLayers(const QList< QgsMapLayer * > &layers, bool takeOwnership=true)
Add a list of layers to the store.
Base class for all map layer types.
#define QgsDebugError(str)
#define QGIS_CHECK_QOBJECT_THREAD_EQUALITY(other)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.