Skip to content

Commit a197010

Browse files
author
Hugo Mercier
committedJan 14, 2019
Fix vertextool's geometry cache invalidation
This a forward port from d79c212 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)
1 parent c263750 commit a197010

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
 

‎src/app/vertextool/qgsvertextool.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,17 @@ void QgsVertexTool::keyPressEvent( QKeyEvent *e )
980980

981981
QgsGeometry QgsVertexTool::cachedGeometry( const QgsVectorLayer *layer, QgsFeatureId fid )
982982
{
983-
if ( !mCache.contains( layer ) )
983+
const bool layerWasNotInCache = !mCache.contains( layer );
984+
// insert if it was not in cache
985+
QHash<QgsFeatureId, QgsGeometry> &layerCache = mCache[layer];
986+
if ( layerWasNotInCache )
984987
{
985988
connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVertexTool::onCachedGeometryChanged );
986989
connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::onCachedGeometryDeleted );
987-
// TODO: also clear cache when layer is deleted
990+
connect( layer, &QgsVectorLayer::willBeDeleted, this, &QgsVertexTool::clearGeometryCache );
991+
connect( layer, &QgsVectorLayer::dataChanged, this, &QgsVertexTool::clearGeometryCache );
988992
}
989993

990-
QHash<QgsFeatureId, QgsGeometry> &layerCache = mCache[layer];
991994
if ( !layerCache.contains( fid ) )
992995
{
993996
QgsFeature f;
@@ -1003,6 +1006,16 @@ QgsGeometry QgsVertexTool::cachedGeometryForVertex( const Vertex &vertex )
10031006
return cachedGeometry( vertex.layer, vertex.fid );
10041007
}
10051008

1009+
void QgsVertexTool::clearGeometryCache()
1010+
{
1011+
const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( sender() );
1012+
mCache.remove( layer );
1013+
disconnect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVertexTool::onCachedGeometryChanged );
1014+
disconnect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::onCachedGeometryDeleted );
1015+
disconnect( layer, &QgsVectorLayer::willBeDeleted, this, &QgsVertexTool::clearGeometryCache );
1016+
disconnect( layer, &QgsVectorLayer::dataChanged, this, &QgsVertexTool::clearGeometryCache );
1017+
}
1018+
10061019
void QgsVertexTool::onCachedGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom )
10071020
{
10081021
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( sender() );

‎src/app/vertextool/qgsvertextool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing
9797

9898
void onCachedGeometryDeleted( QgsFeatureId fid );
9999

100+
void clearGeometryCache();
101+
100102
void showVertexEditor(); //#spellok
101103

102104
void deleteVertexEditorSelection();

0 commit comments

Comments
 (0)
Please sign in to comment.