Skip to content

Commit

Permalink
Remove another restriction on QgsMapToolCapture preventing non-vector
Browse files Browse the repository at this point in the history
use of this class
  • Loading branch information
nyalldawson committed Sep 8, 2021
1 parent c8ac971 commit e7faf95
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsmaptoolcapture.sip.in
Expand Up @@ -157,7 +157,7 @@ Converts a map point to layer coordinates

:return:
0 in case of success
1 if the current layer is ``None`` or not a vector layer
1 if the current layer is ``None``
2 if the transformation failed
%End

Expand Down
34 changes: 17 additions & 17 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -450,26 +450,26 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )

int QgsMapToolCapture::nextPoint( const QgsPoint &mapPoint, QgsPoint &layerPoint )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( !vlayer )
{
QgsDebugMsg( QStringLiteral( "no vector layer" ) );
return 1;
}
try
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) )
{
const QgsPointXY mapP( mapPoint.x(), mapPoint.y() ); //#spellok
layerPoint = QgsPoint( toLayerCoordinates( vlayer, mapP ) ); //transform snapped point back to layer crs //#spellok
if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
layerPoint.addZValue( defaultZValue() );
if ( QgsWkbTypes::hasM( vlayer->wkbType() ) )
layerPoint.addMValue( defaultMValue() );
try
{
const QgsPointXY mapP( mapPoint.x(), mapPoint.y() ); //#spellok
layerPoint = QgsPoint( toLayerCoordinates( vlayer, mapP ) ); //transform snapped point back to layer crs //#spellok
if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
layerPoint.addZValue( defaultZValue() );
if ( QgsWkbTypes::hasM( vlayer->wkbType() ) )
layerPoint.addMValue( defaultMValue() );
}
catch ( QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "transformation to layer coordinate failed" ) );
return 2;
}
}
catch ( QgsCsException &cse )
else
{
Q_UNUSED( cse )
QgsDebugMsg( QStringLiteral( "transformation to layer coordinate failed" ) );
return 2;
layerPoint = QgsPoint( toLayerCoordinates( mCanvas->currentLayer(), mapPoint ) );
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmaptoolcapture.h
Expand Up @@ -256,7 +256,7 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
* \param[in,out] layerPoint the point in layer coordinates
* \returns
* 0 in case of success
* 1 if the current layer is NULLPTR or not a vector layer
* 1 if the current layer is NULLPTR
* 2 if the transformation failed
*/
int nextPoint( const QgsPoint &mapPoint, QgsPoint &layerPoint );
Expand Down
6 changes: 5 additions & 1 deletion tests/src/gui/testqgsmaptoolcapture.cpp
Expand Up @@ -83,7 +83,11 @@ 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 );


// 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 );
}

QGSTEST_MAIN( TestQgsMapToolCapture )
Expand Down

0 comments on commit e7faf95

Please sign in to comment.