Skip to content

Commit

Permalink
Clear cached map renders if dependent layer is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 6, 2017
1 parent aa392dc commit c1b5da4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/qgsmaprenderercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
30 changes: 28 additions & 2 deletions tests/src/python/test_qgsmaprenderercache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit c1b5da4

Please sign in to comment.