Skip to content

Commit

Permalink
Add method to transfer all layers from one map store to another
Browse files Browse the repository at this point in the history
With a note and assert that both stores must have the same
thread affinity
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 2543e07 commit 1dce459
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsmaplayerstore.sip
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ class QgsMapLayerStore : QObject
.. seealso:: removeMapLayers()
%End

void transferLayersFromStore( QgsMapLayerStore *other );
%Docstring
Transfers all the map layers contained within another map layer store and adds
them to this store.
Note that ``other`` and this store must have the same thread affinity.
%End

signals:

void layersWillBeRemoved( const QStringList &layerIds );
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsmaplayerstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ void QgsMapLayerStore::removeAllMapLayers()
mMapLayers.clear();
}

void QgsMapLayerStore::transferLayersFromStore( QgsMapLayerStore *other )
{
if ( !other || other == this )
return;

Q_ASSERT_X( other->thread() == thread(), "QgsMapLayerStore::transferLayersFromStore", "Cannot transfer layers from store with different thread affinity" );

QMap<QString, QgsMapLayer *> otherLayers = other->mapLayers();
QMap<QString, QgsMapLayer *>::const_iterator it = otherLayers.constBegin();
for ( ; it != otherLayers.constEnd(); ++it )
{
QgsMapLayer *layer = other->takeMapLayer( it.value() );
if ( layer )
addMapLayer( layer );
}
}

void QgsMapLayerStore::onMapLayerDeleted( QObject *obj )
{
QString id = mMapLayers.key( static_cast<QgsMapLayer *>( obj ) );
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsmaplayerstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ class CORE_EXPORT QgsMapLayerStore : public QObject
*/
void removeAllMapLayers();

/**
* Transfers all the map layers contained within another map layer store and adds
* them to this store.
* Note that \a other and this store must have the same thread affinity.
*/
void transferLayersFromStore( QgsMapLayerStore *other );

signals:

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/src/python/test_qgsmaplayerstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,36 @@ def testTakeLayer(self):
store = None
self.assertTrue(l1.isValid())

def testTransferLayers(self):
# test transferring all layers from another store
store1 = QgsMapLayerStore()
store2 = QgsMapLayerStore()

# empty stores
store1.transferLayersFromStore(store2)

# silly behavior checks
store1.transferLayersFromStore(None)
store1.transferLayersFromStore(store1)

l1 = createLayer('l1')
l2 = createLayer('l2')
store1.addMapLayer(l1)
store1.addMapLayer(l2)

l3 = createLayer('l3')
store2.addMapLayer(l3)

store2.transferLayersFromStore(store1)
self.assertFalse(store1.mapLayers()) # no layers left
self.assertEqual(len(store2.mapLayers()), 3)
self.assertEqual(store2.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})

store1.transferLayersFromStore(store2)
self.assertFalse(store2.mapLayers()) # no layers left
self.assertEqual(len(store1.mapLayers()), 3)
self.assertEqual(store1.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})


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

0 comments on commit 1dce459

Please sign in to comment.