Skip to content

Commit

Permalink
Fix QgsMapToolCapture coordinates when associated with a non-vector
Browse files Browse the repository at this point in the history
layer where the layer CRS != canvas CRS
  • Loading branch information
nyalldawson committed Sep 22, 2021
1 parent 2ee90bf commit c0daa50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -488,9 +488,13 @@ int QgsMapToolCapture::fetchLayerPoint( const QgsPointLocator::Match &match, Qgs
QgsVectorLayer *sourceLayer = match.layer();
if ( mCadDockWidget && mCadDockWidget->cadEnabled() )
{
layerPoint = mCadDockWidget->currentPointLayerCoordinates( vlayer );
layerPoint = mCadDockWidget->currentPointLayerCoordinates( layer() );
return 0;
}
else if ( !vlayer )
{
return 1;
}
else
{
if ( match.isValid() && ( match.hasVertex() || match.hasLineEndpoint() || ( QgsProject::instance()->topologicalEditing() && ( match.hasEdge() || match.hasMiddleSegment() ) ) ) && sourceLayer &&
Expand Down Expand Up @@ -566,11 +570,10 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
if ( mCapturing && mStreamingEnabled && !mAllowAddingStreamingPoints )
return 0;

int res = 0;
QgsPoint layerPoint;
if ( layer() && layer()->type() == QgsMapLayerType::VectorLayer )
if ( layer() )
{
res = fetchLayerPoint( match, layerPoint );
int res = fetchLayerPoint( match, layerPoint );
if ( res != 0 )
{
res = nextPoint( QgsPoint( point ), layerPoint );
Expand All @@ -580,10 +583,6 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
}
}
}
else
{
layerPoint = QgsPoint( point );
}
const QgsPoint mapPoint = toMapCoordinates( layer(), layerPoint );

if ( mCaptureMode == CapturePoint )
Expand Down
38 changes: 38 additions & 0 deletions tests/src/gui/testqgsmaptoolcapture.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsMapToolCapture : public QObject
void cleanup(); // will be called after every testfunction.

void addVertexNonVectorLayer();
void addVertexNonVectorLayerTransform();

};

Expand Down Expand Up @@ -83,11 +84,48 @@ void TestQgsMapToolCapture::addVertexNonVectorLayer()
// even though we don't have a vector layer selected, adding vertices should still be allowed
QCOMPARE( tool.addVertex( QgsPoint( 5, 5 ), QgsPointLocator::Match() ), 0 );

QCOMPARE( tool.captureCurve()->asWkt(), QStringLiteral( "CompoundCurve ((5 5))" ) );

// the nextPoint method must also accept non vector layers
QgsPoint layerPoint;
QCOMPARE( tool.nextPoint( QgsPoint( 5, 6 ), layerPoint ), 0 );
QCOMPARE( layerPoint.x(), 5.0 );
QCOMPARE( layerPoint.y(), 6.0 );

}

void TestQgsMapToolCapture::addVertexNonVectorLayerTransform()
{
QgsProject::instance()->clear();
QgsMapCanvas canvas;
canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
canvas.setFrameStyle( QFrame::NoFrame );
canvas.resize( 600, 600 );
canvas.setExtent( QgsRectangle( 0, 0, 10, 10 ) );
canvas.show(); // to make the canvas resize

QgsAnnotationLayer *layer = new QgsAnnotationLayer( QStringLiteral( "test" ), QgsAnnotationLayer::LayerOptions( QgsProject::instance()->transformContext() ) );
layer->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
QVERIFY( layer->isValid() );
QgsProject::instance()->addMapLayers( { layer } );

canvas.setLayers( { layer } );
canvas.setCurrentLayer( layer );

QgsAdvancedDigitizingDockWidget cadDock( &canvas );
QgsMapToolCapture tool( &canvas, &cadDock, QgsMapToolCapture::CaptureLine );
canvas.setMapTool( &tool );

// even though we don't have a vector layer selected, adding vertices should still be allowed
QCOMPARE( tool.addVertex( QgsPoint( 5, 5 ), QgsPointLocator::Match() ), 0 );

QCOMPARE( tool.captureCurve()->asWkt( 0 ), QStringLiteral( "CompoundCurve ((556597 557305))" ) );

// the nextPoint method must also accept non vector layers
QgsPoint layerPoint;
QCOMPARE( tool.nextPoint( QgsPoint( 5, 6 ), layerPoint ), 0 );
QGSCOMPARENEAR( layerPoint.x(), 556597, 10 );
QGSCOMPARENEAR( layerPoint.y(), 669141, 10 );
}

QGSTEST_MAIN( TestQgsMapToolCapture )
Expand Down

0 comments on commit c0daa50

Please sign in to comment.