Skip to content

Commit

Permalink
add test for QgsMapToolCapture without active layer with snapping
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 16, 2025
1 parent 1904341 commit f69e767
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

Expand Down
1 change: 1 addition & 0 deletions tests/src/app/testqgsadvanceddigitizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* *
***************************************************************************/

#include "qgisapp.h"
#include "qgstest.h"

#include "qgsadvanceddigitizingdockwidget.h"
Expand Down
3 changes: 1 addition & 2 deletions tests/src/app/testqgsmaptoolsplitfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 1 addition & 3 deletions tests/src/app/testqgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@
***************************************************************************/

#include "qgstest.h"
#include "qgisapp.h"

#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsgeometry.h"
#include "qgsgeometryoptions.h"
#include "qgsgeos.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgsproject.h"
#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"
Expand Down
63 changes: 62 additions & 1 deletion tests/src/gui/testqgsmaptoolcapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
***************************************************************************/
#include <QCoreApplication>

#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
{
Expand All @@ -36,6 +38,7 @@ class TestQgsMapToolCapture : public QObject
void cleanup(); // will be called after every testfunction.

void addVertexNoLayer();
void addPointNoLayerSnapping();
void addVertexNonVectorLayer();
void addVertexNonVectorLayerTransform();
};
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/**
Expand Down

0 comments on commit f69e767

Please sign in to comment.