Fix vertextool's geometry cache invalidation

This a forward port from d79c212e7ba1e5 in PR #8724

Another fix is added here (which will be backported to 3.4): layer'
signal connections must be destroyed when the cache for this layer is
cleared (otherwise I got an "assertion failed" in onCacheGeometryXXXX)
This commit is contained in:
Hugo Mercier 2019-01-14 14:53:38 +01:00
parent c263750930
commit a1970104d0
2 changed files with 18 additions and 3 deletions

View File

@ -980,14 +980,17 @@ void QgsVertexTool::keyPressEvent( QKeyEvent *e )
QgsGeometry QgsVertexTool::cachedGeometry( const QgsVectorLayer *layer, QgsFeatureId fid )
{
if ( !mCache.contains( layer ) )
const bool layerWasNotInCache = !mCache.contains( layer );
// insert if it was not in cache
QHash<QgsFeatureId, QgsGeometry> &layerCache = mCache[layer];
if ( layerWasNotInCache )
{
connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVertexTool::onCachedGeometryChanged );
connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::onCachedGeometryDeleted );
// TODO: also clear cache when layer is deleted
connect( layer, &QgsVectorLayer::willBeDeleted, this, &QgsVertexTool::clearGeometryCache );
connect( layer, &QgsVectorLayer::dataChanged, this, &QgsVertexTool::clearGeometryCache );
}
QHash<QgsFeatureId, QgsGeometry> &layerCache = mCache[layer];
if ( !layerCache.contains( fid ) )
{
QgsFeature f;
@ -1003,6 +1006,16 @@ QgsGeometry QgsVertexTool::cachedGeometryForVertex( const Vertex &vertex )
return cachedGeometry( vertex.layer, vertex.fid );
}
void QgsVertexTool::clearGeometryCache()
{
const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( sender() );
mCache.remove( layer );
disconnect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVertexTool::onCachedGeometryChanged );
disconnect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::onCachedGeometryDeleted );
disconnect( layer, &QgsVectorLayer::willBeDeleted, this, &QgsVertexTool::clearGeometryCache );
disconnect( layer, &QgsVectorLayer::dataChanged, this, &QgsVertexTool::clearGeometryCache );
}
void QgsVertexTool::onCachedGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( sender() );

View File

@ -97,6 +97,8 @@ class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing
void onCachedGeometryDeleted( QgsFeatureId fid );
void clearGeometryCache();
void showVertexEditor(); //#spellok
void deleteVertexEditorSelection();