From 324a79ed96964a401915c4dbc7472c36d314bd8b Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Wed, 15 Jan 2025 14:39:45 +0100 Subject: [PATCH] add test for QgsMapToolCapture without active layer with snapping --- tests/src/app/CMakeLists.txt | 1 + tests/src/app/testqgsadvanceddigitizing.cpp | 1 + tests/src/app/testqgsmaptoolsplitfeatures.cpp | 3 +- tests/src/app/testqgsvertextool.cpp | 4 +- tests/src/gui/testqgsmaptoolcapture.cpp | 63 ++++++++++++++++++- tests/src/{app => gui}/testqgsmaptoolutils.h | 5 +- 6 files changed, 67 insertions(+), 10 deletions(-) rename tests/src/{app => gui}/testqgsmaptoolutils.h (99%) diff --git a/tests/src/app/CMakeLists.txt b/tests/src/app/CMakeLists.txt index 8c20fdddc9ed..43eb43b5c544 100644 --- a/tests/src/app/CMakeLists.txt +++ b/tests/src/app/CMakeLists.txt @@ -3,6 +3,7 @@ # the UI file won't be wrapped! include_directories( ${CMAKE_SOURCE_DIR}/src/test + ${CMAKE_SOURCE_DIR}/tests/src/gui ${CMAKE_CURRENT_BINARY_DIR} ) diff --git a/tests/src/app/testqgsadvanceddigitizing.cpp b/tests/src/app/testqgsadvanceddigitizing.cpp index 5df779a679d8..2b851e0941ff 100644 --- a/tests/src/app/testqgsadvanceddigitizing.cpp +++ b/tests/src/app/testqgsadvanceddigitizing.cpp @@ -13,6 +13,7 @@ * * ***************************************************************************/ +#include "qgisapp.h" #include "qgstest.h" #include "qgsadvanceddigitizingdockwidget.h" diff --git a/tests/src/app/testqgsmaptoolsplitfeatures.cpp b/tests/src/app/testqgsmaptoolsplitfeatures.cpp index e3119ec22a56..394eda766dcd 100644 --- a/tests/src/app/testqgsmaptoolsplitfeatures.cpp +++ b/tests/src/app/testqgsmaptoolsplitfeatures.cpp @@ -13,15 +13,14 @@ * * ***************************************************************************/ +#include "qgssnappingutils.h" #include "qgstest.h" #include "qgisapp.h" #include "qgsgeometry.h" #include "qgsmapcanvas.h" -#include "qgssettings.h" #include "qgsvectorlayer.h" #include "qgsmaptoolsplitfeatures.h" -#include "qgsgeometryutils.h" #include "testqgsmaptoolutils.h" diff --git a/tests/src/app/testqgsvertextool.cpp b/tests/src/app/testqgsvertextool.cpp index 66cafc89bb11..02b20b43948b 100644 --- a/tests/src/app/testqgsvertextool.cpp +++ b/tests/src/app/testqgsvertextool.cpp @@ -14,10 +14,10 @@ ***************************************************************************/ #include "qgstest.h" +#include "qgisapp.h" #include "qgsadvanceddigitizingdockwidget.h" #include "qgsgeometry.h" -#include "qgsgeometryoptions.h" #include "qgsgeos.h" #include "qgsmapcanvas.h" #include "qgsmapcanvassnappingutils.h" @@ -25,8 +25,6 @@ #include "qgsvectorlayer.h" #include "qgsmapmouseevent.h" #include "vertextool/qgsvertextool.h" -#include "qgslinestring.h" -#include "qgscircularstring.h" #include "qgssnappingconfig.h" #include "qgssettingsregistrycore.h" #include "testqgsmaptoolutils.h" diff --git a/tests/src/gui/testqgsmaptoolcapture.cpp b/tests/src/gui/testqgsmaptoolcapture.cpp index 0695dacd2a29..d45272909ae0 100644 --- a/tests/src/gui/testqgsmaptoolcapture.cpp +++ b/tests/src/gui/testqgsmaptoolcapture.cpp @@ -14,14 +14,16 @@ ***************************************************************************/ #include +#include "qgsmapcanvassnappingutils.h" #include "qgstest.h" -#include "qgsguiutils.h" #include "qgsmaptoolcapture.h" #include "qgsapplication.h" #include "qgsmapcanvas.h" #include "qgslogger.h" #include "qgsannotationlayer.h" #include "qgsadvanceddigitizingdockwidget.h" +#include "testqgsmaptoolutils.h" + class TestQgsMapToolCapture : public QObject { @@ -36,6 +38,7 @@ class TestQgsMapToolCapture : public QObject void cleanup(); // will be called after every testfunction. void addVertexNoLayer(); + void addPointNoLayerSnapping(); void addVertexNonVectorLayer(); void addVertexNonVectorLayerTransform(); }; @@ -88,6 +91,64 @@ void TestQgsMapToolCapture::addVertexNoLayer() QCOMPARE( layerPoint.y(), 6.0 ); } +void TestQgsMapToolCapture::addPointNoLayerSnapping() +{ + // checks that snapping works even if no layer is set as current layer + + QgsPoint p = QgsPoint( 2556607, 1115175 ); + + QgsProject::instance()->clear(); + + QgsMapCanvas canvas; + canvas.resize( 600, 600 ); + canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:2056" ) ) ); + canvas.setFrameStyle( QFrame::NoFrame ); + canvas.setExtent( QgsRectangle( p.x() - 50, p.y() - 50, p.x() + 50, p.y() + 50 ) ); + canvas.show(); // to make the canvas resize + + QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:2056" ), QStringLiteral( "point" ), QStringLiteral( "memory" ) ); + QgsProject::instance()->addMapLayer( layer ); + QgsFeature f( layer->fields() ); + f.setGeometry( QgsGeometry( p.clone() ) ); + QgsFeatureList flist { f }; + layer->dataProvider()->addFeatures( flist ); + canvas.setLayers( { layer } ); + + QgsMapSettings mapSettings = canvas.mapSettings(); + QVERIFY( mapSettings.hasValidSettings() ); + + QgsSnappingUtils u; + u.setMapSettings( mapSettings ); + + QgsSnappingConfig snappingConfig = u.config(); + snappingConfig.setEnabled( true ); + snappingConfig.setTolerance( 10 ); + snappingConfig.setUnits( Qgis::MapToolUnit::Pixels ); + snappingConfig.setMode( Qgis::SnappingMode::AllLayers ); + snappingConfig.setTypeFlag( Qgis::SnappingType::Vertex ); + u.setConfig( snappingConfig ); + + QgsMapCanvasSnappingUtils *snappingUtils = new QgsMapCanvasSnappingUtils( &canvas, this ); + snappingUtils->setConfig( snappingConfig ); + snappingUtils->setMapSettings( mapSettings ); + snappingUtils->locatorForLayer( layer )->init(); + + canvas.setSnappingUtils( snappingUtils ); + + canvas.setCurrentLayer( nullptr ); + + QgsAdvancedDigitizingDockWidget cadDock( &canvas ); + + QgsMapToolCapture tool( &canvas, &cadDock, QgsMapToolCapture::CaptureLine ); + canvas.setMapTool( &tool ); + + TestQgsMapToolAdvancedDigitizingUtils utils( &tool ); + utils.mouseClick( p.x() + .5, p.y() + .5, Qt::LeftButton, Qt::KeyboardModifiers(), true ); + + QgsPoint toolPoint = tool.captureCurve()->vertexAt( QgsVertexId( 0, 0, 0 ) ); + QCOMPARE( toolPoint, p ); +} + void TestQgsMapToolCapture::addVertexNonVectorLayer() { QgsProject::instance()->clear(); diff --git a/tests/src/app/testqgsmaptoolutils.h b/tests/src/gui/testqgsmaptoolutils.h similarity index 99% rename from tests/src/app/testqgsmaptoolutils.h rename to tests/src/gui/testqgsmaptoolutils.h index c5cc31f691fb..2d765ff54d9d 100644 --- a/tests/src/app/testqgsmaptoolutils.h +++ b/tests/src/gui/testqgsmaptoolutils.h @@ -15,13 +15,10 @@ * * ***************************************************************************/ -#include "qgstest.h" - -#include "qgisapp.h" +//#include "qgisapp.h" #include "qgsgeometry.h" #include "qgsmapcanvas.h" #include "qgsmapmouseevent.h" -#include "qgssnappingutils.h" #include "qgsmaptooladvanceddigitizing.h" /**