Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] [Snapping] Remove and delete locator when layer is destroyed #60581

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/qgssnappingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ QgsPointLocator *QgsSnappingUtils::locatorForLayer( QgsVectorLayer *vl )
{
QgsPointLocator *vlpl = new QgsPointLocator( vl, destinationCrs(), mMapSettings.transformContext(), nullptr );
connect( vlpl, &QgsPointLocator::initFinished, this, &QgsSnappingUtils::onInitFinished );
connect( vl, &QObject::destroyed, this, [this, vl]()
{
delete mLocators.take( vl );
} );

mLocators.insert( vl, vlpl );
}
return mLocators.value( vl );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgssnappingutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ class CORE_EXPORT QgsSnappingUtils : public QObject

//! Disable or not the snapping on all features. By default is always TRUE except for non visible features on map canvas.
bool mEnableSnappingForInvisibleFeature = true;

friend class TestQgsSnappingUtils;
};


Expand Down
54 changes: 54 additions & 0 deletions tests/src/core/testqgssnappingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,60 @@ class TestQgsSnappingUtils : public QObject
QVERIFY( !m5.isValid() );
QVERIFY( !m6.isValid() );
}


void testLocatorsCleaning()
{
auto vl = std::make_unique<QgsVectorLayer>( QStringLiteral( "Polygon?field=fld:int" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
const int idx = mVL->fields().indexFromName( QStringLiteral( "fld" ) );
QVERIFY( idx != -1 );
f1.initAttributes( 1 );
f2.initAttributes( 1 );

QgsPolygonXY polygon;
QgsPolylineXY polyline;
polyline << QgsPointXY( 0, 1 ) << QgsPointXY( 1, 0 ) << QgsPointXY( 1, 1 ) << QgsPointXY( 0, 1 );
polygon << polyline;
const QgsGeometry polygonGeom = QgsGeometry::fromPolygonXY( polygon );
f1.setGeometry( polygonGeom );
f1.setAttribute( idx, QVariant( 2 ) );
QgsFeatureList flist;
flist << f1;

vl->dataProvider()->addFeatures( flist );

QgsMapSettings mapSettings;
mapSettings.setOutputSize( QSize( 100, 100 ) );
mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
QVERIFY( mapSettings.hasValidSettings() );

QgsSnappingUtils u;
u.setMapSettings( mapSettings );
u.setCurrentLayer( vl.get() );

// first try with no snapping enabled
QgsSnappingConfig snappingConfig = u.config();
snappingConfig.setEnabled( false );
snappingConfig.setTolerance( 10 );
snappingConfig.setUnits( Qgis::MapToolUnit::Pixels );
snappingConfig.setMode( Qgis::SnappingMode::ActiveLayer );
snappingConfig.setEnabled( true );
snappingConfig.setTypeFlag( Qgis::SnappingType::Vertex );
u.setConfig( snappingConfig );

QCOMPARE( u.mLocators.count(), 0 );

const QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) );
QVERIFY( m.isValid() );
QVERIFY( m.hasVertex() );
QCOMPARE( m.point(), QgsPointXY( 1, 0 ) );

QCOMPARE( u.mLocators.count(), 1 );

vl.reset();

QCOMPARE( u.mLocators.count(), 0 );
}
};

QGSTEST_MAIN( TestQgsSnappingUtils )
Expand Down
Loading