QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgslogger.h"
20 
22  : QObject( parent )
23 {}
24 
26 {
28 }
29 
31 {
32  return mMapLayers.size();
33 }
34 
35 QgsMapLayer *QgsMapLayerStore::mapLayer( const QString &layerId ) const
36 {
37  return mMapLayers.value( layerId );
38 }
39 
40 QList<QgsMapLayer *> QgsMapLayerStore::mapLayersByName( const QString &layerName ) const
41 {
42  QList<QgsMapLayer *> myResultList;
43  Q_FOREACH ( QgsMapLayer *layer, mMapLayers )
44  {
45  if ( layer->name() == layerName )
46  {
47  myResultList << layer;
48  }
49  }
50  return myResultList;
51 }
52 
53 QList<QgsMapLayer *> QgsMapLayerStore::addMapLayers( const QList<QgsMapLayer *> &layers, bool takeOwnership )
54 {
55  QList<QgsMapLayer *> myResultList;
56  Q_FOREACH ( QgsMapLayer *myLayer, layers )
57  {
58  if ( !myLayer || !myLayer->isValid() )
59  {
60  QgsDebugMsg( "Cannot add invalid layers" );
61  continue;
62  }
63  //check the layer is not already registered!
64  if ( !mMapLayers.contains( myLayer->id() ) )
65  {
66  mMapLayers[myLayer->id()] = myLayer;
67  myResultList << mMapLayers[myLayer->id()];
68  if ( takeOwnership )
69  {
70  myLayer->setParent( this );
71  }
72  connect( myLayer, &QObject::destroyed, this, &QgsMapLayerStore::onMapLayerDeleted );
73  emit layerWasAdded( myLayer );
74  }
75  }
76  if ( !myResultList.isEmpty() )
77  {
78  emit layersAdded( myResultList );
79  }
80  return myResultList;
81 }
82 
84 QgsMapLayerStore::addMapLayer( QgsMapLayer *layer, bool takeOwnership )
85 {
86  QList<QgsMapLayer *> addedLayers;
87  addedLayers = addMapLayers( QList<QgsMapLayer *>() << layer, takeOwnership );
88  return addedLayers.isEmpty() ? nullptr : addedLayers[0];
89 }
90 
91 void QgsMapLayerStore::removeMapLayers( const QStringList &layerIds )
92 {
93  QList<QgsMapLayer *> layers;
94  Q_FOREACH ( const QString &myId, layerIds )
95  {
96  layers << mMapLayers.value( myId );
97  }
98 
99  removeMapLayers( layers );
100 }
101 
102 void QgsMapLayerStore::removeMapLayers( const QList<QgsMapLayer *> &layers )
103 {
104  if ( layers.isEmpty() )
105  return;
106 
107  QStringList layerIds;
108  QList<QgsMapLayer *> layerList;
109 
110  Q_FOREACH ( QgsMapLayer *layer, layers )
111  {
112  // check layer and the store contains it
113  if ( layer && mMapLayers.contains( layer->id() ) )
114  {
115  layerIds << layer->id();
116  layerList << layer;
117  }
118  }
119 
120  if ( layerIds.isEmpty() )
121  return;
122 
123  emit layersWillBeRemoved( layerIds );
124  emit layersWillBeRemoved( layerList );
125 
126  Q_FOREACH ( QgsMapLayer *lyr, layerList )
127  {
128  QString myId( lyr->id() );
129  emit layerWillBeRemoved( myId );
130  emit layerWillBeRemoved( lyr );
131  mMapLayers.remove( myId );
132  if ( lyr->parent() == this )
133  {
134  delete lyr;
135  }
136  emit layerRemoved( myId );
137  }
138 
139  emit layersRemoved( layerIds );
140 }
141 
142 void QgsMapLayerStore::removeMapLayer( const QString &layerId )
143 {
144  removeMapLayers( QList<QgsMapLayer *>() << mMapLayers.value( layerId ) );
145 }
146 
148 {
149  if ( layer )
150  removeMapLayers( QList<QgsMapLayer *>() << layer );
151 }
152 
154 {
155  if ( !layer )
156  return nullptr;
157 
158  if ( mMapLayers.contains( layer->id() ) )
159  {
160  emit layersWillBeRemoved( QStringList() << layer->id() );
161  emit layersWillBeRemoved( QList<QgsMapLayer *>() << layer );
162  emit layerWillBeRemoved( layer->id() );
163  emit layerWillBeRemoved( layer );
164 
165  mMapLayers.remove( layer->id() );
166  layer->setParent( nullptr );
167  emit layerRemoved( layer->id() );
168  emit layersRemoved( QStringList() << layer->id() );
169  return layer;
170  }
171  return nullptr; //don't return layer - it wasn't owned and accordingly we aren't transferring ownership
172 }
173 
175 {
176  emit allLayersRemoved();
177  // now let all observers know to clear themselves,
178  // and then consequently any of their map legends
179  removeMapLayers( mMapLayers.keys() );
180  mMapLayers.clear();
181 }
182 
184 {
185  if ( !other || other == this )
186  return;
187 
188  Q_ASSERT_X( other->thread() == thread(), "QgsMapLayerStore::transferLayersFromStore", "Cannot transfer layers from store with different thread affinity" );
189 
190  QMap<QString, QgsMapLayer *> otherLayers = other->mapLayers();
191  QMap<QString, QgsMapLayer *>::const_iterator it = otherLayers.constBegin();
192  for ( ; it != otherLayers.constEnd(); ++it )
193  {
194  QgsMapLayer *layer = other->takeMapLayer( it.value() );
195  if ( layer )
196  addMapLayer( layer );
197  }
198 }
199 
200 void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
201 {
202  QString id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );
203 
204  if ( !id.isNull() )
205  {
206  QgsDebugMsg( QString( "Map layer deleted without unregistering! %1" ).arg( id ) );
207  mMapLayers.remove( id );
208  }
209 }
210 
211 QMap<QString, QgsMapLayer *> QgsMapLayerStore::mapLayers() const
212 {
213  return mMapLayers;
214 }
Base class for all map layer types.
Definition: qgsmaplayer.h:61
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.
bool isValid() const
Returns the status of the layer.
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.
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:65
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.