Skip to content

Commit 1dce459

Browse files
committedJul 6, 2017
Add method to transfer all layers from one map store to another
With a note and assert that both stores must have the same thread affinity
1 parent 2543e07 commit 1dce459

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
 

‎python/core/qgsmaplayerstore.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ class QgsMapLayerStore : QObject
208208
.. seealso:: removeMapLayers()
209209
%End
210210

211+
void transferLayersFromStore( QgsMapLayerStore *other );
212+
%Docstring
213+
Transfers all the map layers contained within another map layer store and adds
214+
them to this store.
215+
Note that ``other`` and this store must have the same thread affinity.
216+
%End
217+
211218
signals:
212219

213220
void layersWillBeRemoved( const QStringList &layerIds );

‎src/core/qgsmaplayerstore.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ void QgsMapLayerStore::removeAllMapLayers()
180180
mMapLayers.clear();
181181
}
182182

183+
void QgsMapLayerStore::transferLayersFromStore( QgsMapLayerStore *other )
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+
183200
void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
184201
{
185202
QString id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );

‎src/core/qgsmaplayerstore.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ class CORE_EXPORT QgsMapLayerStore : public QObject
238238
*/
239239
void removeAllMapLayers();
240240

241+
/**
242+
* Transfers all the map layers contained within another map layer store and adds
243+
* them to this store.
244+
* Note that \a other and this store must have the same thread affinity.
245+
*/
246+
void transferLayersFromStore( QgsMapLayerStore *other );
247+
241248
signals:
242249

243250
/**

‎tests/src/python/test_qgsmaplayerstore.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,36 @@ def testTakeLayer(self):
495495
store = None
496496
self.assertTrue(l1.isValid())
497497

498+
def testTransferLayers(self):
499+
# test transferring all layers from another store
500+
store1 = QgsMapLayerStore()
501+
store2 = QgsMapLayerStore()
502+
503+
# empty stores
504+
store1.transferLayersFromStore(store2)
505+
506+
# silly behavior checks
507+
store1.transferLayersFromStore(None)
508+
store1.transferLayersFromStore(store1)
509+
510+
l1 = createLayer('l1')
511+
l2 = createLayer('l2')
512+
store1.addMapLayer(l1)
513+
store1.addMapLayer(l2)
514+
515+
l3 = createLayer('l3')
516+
store2.addMapLayer(l3)
517+
518+
store2.transferLayersFromStore(store1)
519+
self.assertFalse(store1.mapLayers()) # no layers left
520+
self.assertEqual(len(store2.mapLayers()), 3)
521+
self.assertEqual(store2.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})
522+
523+
store1.transferLayersFromStore(store2)
524+
self.assertFalse(store2.mapLayers()) # no layers left
525+
self.assertEqual(len(store1.mapLayers()), 3)
526+
self.assertEqual(store1.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})
527+
498528

499529
if __name__ == '__main__':
500530
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.