Skip to content
Permalink
Browse files

Clear cached map renders if dependent layer is removed

  • Loading branch information
nyalldawson committed Feb 6, 2017
1 parent aa392dc commit c1b5da4726f28ec6d7b9b3bcb0c5247d45bdaaf9
Showing with 31 additions and 2 deletions.
  1. +3 −0 src/core/qgsmaprenderercache.cpp
  2. +28 −2 tests/src/python/test_qgsmaprenderercache.py
@@ -40,6 +40,7 @@ void QgsMapRendererCache::clearInternal()
if ( layer.data() )
{
disconnect( layer.data(), &QgsMapLayer::repaintRequested, this, &QgsMapRendererCache::layerRequestedRepaint );
disconnect( layer.data(), &QgsMapLayer::willBeDeleted, this, &QgsMapRendererCache::layerRequestedRepaint );
}
}
mCachedImages.clear();
@@ -55,6 +56,7 @@ void QgsMapRendererCache::dropUnusedConnections()
if ( layer.data() )
{
disconnect( layer.data(), &QgsMapLayer::repaintRequested, this, &QgsMapRendererCache::layerRequestedRepaint );
disconnect( layer.data(), &QgsMapLayer::willBeDeleted, this, &QgsMapRendererCache::layerRequestedRepaint );
}
}

@@ -110,6 +112,7 @@ void QgsMapRendererCache::setCacheImage( const QString& cacheKey, const QImage&
if ( !mConnectedLayers.contains( QgsWeakMapLayerPointer( layer ) ) )
{
connect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapRendererCache::layerRequestedRepaint );
connect( layer, &QgsMapLayer::willBeDeleted, this, &QgsMapRendererCache::layerRequestedRepaint );
mConnectedLayers << layer;
}
}
@@ -208,9 +208,35 @@ def testDependentLayers(self):
cache.setCacheImage('depends', im, [layer1, layer2])
self.assertEqual(set(cache.dependentLayers('depends')), set([layer1, layer2]))

# try deleting a layer in the meantime..
def testLayerRemoval(self):
"""test that cached image is cleared when a dependent layer is removed"""
cache = QgsMapRendererCache()
layer1 = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
im = QImage(200, 200, QImage.Format_RGB32)
cache.setCacheImage('depends', im, [layer1, layer2])
cache.setCacheImage('depends2', im, [layer1])
cache.setCacheImage('depends3', im, [layer2])
cache.setCacheImage('no depends', im, [])
self.assertTrue(cache.hasCacheImage('depends'))
self.assertTrue(cache.hasCacheImage('depends2'))
self.assertTrue(cache.hasCacheImage('depends3'))
self.assertTrue(cache.hasCacheImage('no depends'))

# try deleting a layer
layer2 = None
self.assertEqual(set(cache.dependentLayers('depends')), set([layer1]))
self.assertFalse(cache.hasCacheImage('depends'))
self.assertTrue(cache.hasCacheImage('depends2'))
self.assertFalse(cache.hasCacheImage('depends3'))
self.assertTrue(cache.hasCacheImage('no depends'))

layer1 = None
self.assertFalse(cache.hasCacheImage('depends'))
self.assertFalse(cache.hasCacheImage('depends2'))
self.assertFalse(cache.hasCacheImage('depends3'))
self.assertTrue(cache.hasCacheImage('no depends'))


if __name__ == '__main__':

0 comments on commit c1b5da4

Please sign in to comment.
You can’t perform that action at this time.