Skip to content
Permalink
Browse files

Add method to retrieve dependent layers for map renderer cached images

  • Loading branch information
nyalldawson committed Feb 6, 2017
1 parent 748acc6 commit aa392dc23a39d50880cf87a73c4b1d6ce4aaccb6
@@ -18,6 +18,8 @@ class QgsMapRendererCache : QObject

QImage cacheImage( const QString& cacheKey ) const;

QList< QgsMapLayer* > dependentLayers( const QString& cacheKey ) const;

void clearCacheImage( const QString& cacheKey );

};
@@ -129,6 +129,15 @@ QImage QgsMapRendererCache::cacheImage( const QString& cacheKey ) const
return mCachedImages.value( cacheKey ).cachedImage;
}

QList< QgsMapLayer* > QgsMapRendererCache::dependentLayers( const QString& cacheKey ) const
{
if ( mCachedImages.contains( cacheKey ) )
{
return _qgis_listQPointerToRaw( mCachedImages.value( cacheKey ).dependentLayers );
}
return QList< QgsMapLayer* >();
}

void QgsMapRendererCache::layerRequestedRepaint()
{
QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( sender() );
@@ -84,6 +84,12 @@ class CORE_EXPORT QgsMapRendererCache : public QObject
*/
QImage cacheImage( const QString& cacheKey ) const;

/**
* Returns a list of map layers on which an image in the cache depends.
* @note added in QGIS 3.0
*/
QList< QgsMapLayer* > dependentLayers( const QString& cacheKey ) const;

/**
* Removes an image from the cache with matching \a cacheKey.
* @see clear()
@@ -192,6 +192,26 @@ def testRequestRepaintMultiple(self):
self.assertFalse(cache.cacheImage('nolayer').isNull())
self.assertEqual(cache.cacheImage('nolayer'), im1)

def testDependentLayers(self):
# bad layer tests
cache = QgsMapRendererCache()
self.assertEqual(cache.dependentLayers('not a layer'), [])

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('no depends', im, [])
self.assertEqual(cache.dependentLayers('no depends'), [])
cache.setCacheImage('depends', im, [layer1, layer2])
self.assertEqual(set(cache.dependentLayers('depends')), set([layer1, layer2]))

# try deleting a layer in the meantime..
layer2 = None
self.assertEqual(set(cache.dependentLayers('depends')), set([layer1]))


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

0 comments on commit aa392dc

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