Skip to content

Commit

Permalink
Add method to retrieve dependent layers for map renderer cached images
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 6, 2017
1 parent 748acc6 commit aa392dc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/qgsmaprenderercache.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

};
9 changes: 9 additions & 0 deletions src/core/qgsmaprenderercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaprenderercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsmaprenderercache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.