mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
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
This commit is contained in:
parent
2543e079a5
commit
1dce459610
@ -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 );
|
||||
|
@ -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 ) );
|
||||
|
@ -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:
|
||||
|
||||
/**
|
||||
|
@ -495,6 +495,36 @@ class TestQgsMapLayerStore(unittest.TestCase):
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user