Skip to content

Commit

Permalink
Merge pull request #8851 from mhugo/fix_vertex_tool_cache
Browse files Browse the repository at this point in the history
Fix vertextool's geometry cache invalidation
  • Loading branch information
Hugo Mercier authored Jan 16, 2019
2 parents a6f2618 + a197010 commit 21a7e15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/app/vertextool/qgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() );
Expand Down
2 changes: 2 additions & 0 deletions src/app/vertextool/qgsvertextool.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing

void onCachedGeometryDeleted( QgsFeatureId fid );

void clearGeometryCache();

void showVertexEditor(); //#spellok

void deleteVertexEditorSelection();
Expand Down

0 comments on commit 21a7e15

Please sign in to comment.