Skip to content

Commit e7ce050

Browse files
committedSep 8, 2021
Allow QgsMapToolCapture subclasses to create tools which are associated
with a specific map layer, instead of always following the current canvas layer
1 parent f2776d9 commit e7ce050

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed
 

‎python/gui/auto_generated/qgsmaptoolcapture.sip.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ Set the points on which to work
301301
void closePolygon();
302302
%Docstring
303303
Close an open polygon
304+
%End
305+
306+
virtual QgsMapLayer *layer() const;
307+
%Docstring
308+
Returns the map layer associated with the geometry being captured.
309+
310+
The base class implementation always returns the current map canvas layer, but
311+
subclasses can override this if they need to be associated with a specific layer.
312+
313+
.. versionadded:: 3.22
304314
%End
305315

306316
protected slots:

‎src/gui/qgsmaptoolcapture.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
287287
if ( QgsSettingsRegistryCore::settingsDigitizingConvertToCurve.value() )
288288
{
289289
// If the tool and the layer support curves
290-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
290+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
291291
if ( vlayer && capabilities().testFlag( QgsMapToolCapture::Capability::SupportsCurves ) && vlayer->dataProvider()->capabilities().testFlag( QgsVectorDataProvider::Capability::CircularGeometries ) )
292292
{
293293
const QgsGeometry linear = QgsGeometry( mCaptureCurve.segmentize() );
@@ -308,7 +308,7 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
308308

309309
// adjust last captured point
310310
const QgsPoint lastPt = mCaptureCurve.endPoint();
311-
mCaptureLastPoint = toMapCoordinates( mCanvas->currentLayer(), lastPt );
311+
mCaptureLastPoint = toMapCoordinates( layer(), lastPt );
312312

313313
return true;
314314
}
@@ -346,7 +346,7 @@ void QgsMapToolCapture::resetRubberBand()
346346
return;
347347
QgsLineString *lineString = mCaptureCurve.curveToLine();
348348
mRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry );
349-
mRubberBand->addGeometry( QgsGeometry( lineString ), mCanvas->currentLayer() );
349+
mRubberBand->addGeometry( QgsGeometry( lineString ), layer() );
350350
}
351351

352352
QgsRubberBand *QgsMapToolCapture::takeRubberBand()
@@ -389,7 +389,7 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
389389
if ( !mCaptureCurve.isEmpty() )
390390
{
391391
const QgsPoint prevPoint = mCaptureCurve.curveAt( mCaptureCurve.nCurves() - 1 )->endPoint();
392-
if ( QgsPointXY( toCanvasCoordinates( toMapCoordinates( mCanvas->currentLayer(), prevPoint ) ) ).distance( toCanvasCoordinates( point ) ) < mStreamingToleranceInPixels )
392+
if ( QgsPointXY( toCanvasCoordinates( toMapCoordinates( layer(), prevPoint ) ) ).distance( toCanvasCoordinates( point ) ) < mStreamingToleranceInPixels )
393393
return;
394394
}
395395

@@ -450,7 +450,7 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
450450

451451
int QgsMapToolCapture::nextPoint( const QgsPoint &mapPoint, QgsPoint &layerPoint )
452452
{
453-
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) )
453+
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() ) )
454454
{
455455
try
456456
{
@@ -469,7 +469,7 @@ int QgsMapToolCapture::nextPoint( const QgsPoint &mapPoint, QgsPoint &layerPoint
469469
}
470470
else
471471
{
472-
layerPoint = QgsPoint( toLayerCoordinates( mCanvas->currentLayer(), mapPoint ) );
472+
layerPoint = QgsPoint( toLayerCoordinates( layer(), mapPoint ) );
473473
}
474474

475475
return 0;
@@ -483,7 +483,7 @@ int QgsMapToolCapture::nextPoint( QPoint p, QgsPoint &layerPoint, QgsPoint &mapP
483483

484484
int QgsMapToolCapture::fetchLayerPoint( const QgsPointLocator::Match &match, QgsPoint &layerPoint )
485485
{
486-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
486+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
487487
QgsVectorLayer *sourceLayer = match.layer();
488488
if ( match.isValid() && ( match.hasVertex() || match.hasLineEndpoint() || ( QgsProject::instance()->topologicalEditing() && ( match.hasEdge() || match.hasMiddleSegment() ) ) ) && sourceLayer &&
489489
( sourceLayer->crs() == vlayer->crs() ) )
@@ -559,7 +559,7 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
559559

560560
int res = 0;
561561
QgsPoint layerPoint;
562-
if ( mCanvas->currentLayer() && mCanvas->currentLayer()->type() == QgsMapLayerType::VectorLayer )
562+
if ( layer() && layer()->type() == QgsMapLayerType::VectorLayer )
563563
{
564564
res = fetchLayerPoint( match, layerPoint );
565565
if ( res != 0 )
@@ -575,7 +575,7 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
575575
{
576576
layerPoint = QgsPoint( point );
577577
}
578-
const QgsPoint mapPoint = toMapCoordinates( canvas()->currentLayer(), layerPoint );
578+
const QgsPoint mapPoint = toMapCoordinates( layer(), layerPoint );
579579

580580
if ( mCaptureMode == CapturePoint )
581581
{
@@ -685,7 +685,7 @@ int QgsMapToolCapture::addCurve( QgsCurve *c )
685685
}
686686

687687
//transform back to layer CRS in case map CRS and layer CRS are different
688-
const QgsCoordinateTransform ct = mCanvas->mapSettings().layerTransform( mCanvas->currentLayer() );
688+
const QgsCoordinateTransform ct = mCanvas->mapSettings().layerTransform( layer() );
689689
if ( ct.isValid() )
690690
{
691691
c->transform( ct, QgsCoordinateTransform::ReverseTransform );
@@ -787,7 +787,7 @@ void QgsMapToolCapture::undo( bool isAutoRepeat )
787787
if ( mCaptureCurve.numPoints() > 0 )
788788
{
789789
const QgsPoint lastPt = mCaptureCurve.endPoint();
790-
mCaptureLastPoint = toMapCoordinates( mCanvas->currentLayer(), lastPt );
790+
mCaptureLastPoint = toMapCoordinates( layer(), lastPt );
791791
mTempRubberBand->addPoint( lastCapturedMapPoint() );
792792
mTempRubberBand->movePoint( lastPoint );
793793
}
@@ -865,6 +865,11 @@ void QgsMapToolCapture::closePolygon()
865865
updateExtraSnapLayer();
866866
}
867867

868+
QgsMapLayer *QgsMapToolCapture::layer() const
869+
{
870+
return canvas()->currentLayer();
871+
}
872+
868873
void QgsMapToolCapture::validateGeometry()
869874
{
870875
if ( QgsSettingsRegistryCore::settingsDigitizingValidateGeometries.value() == 0 )
@@ -920,7 +925,7 @@ void QgsMapToolCapture::validateGeometry()
920925
void QgsMapToolCapture::addError( const QgsGeometry::Error &e )
921926
{
922927
mGeomErrors << e;
923-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
928+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
924929
if ( !vlayer )
925930
return;
926931

@@ -986,7 +991,7 @@ QgsPoint QgsMapToolCapture::mapPoint( const QgsPointXY &point ) const
986991
QgsPoint newPoint( QgsWkbTypes::Point, point.x(), point.y() );
987992

988993
// get current layer
989-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
994+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
990995
if ( !vlayer )
991996
{
992997
return newPoint;
@@ -1057,10 +1062,10 @@ void QgsMapToolCapture::updateExtraSnapLayer()
10571062
if ( !mExtraSnapLayer )
10581063
return;
10591064

1060-
if ( canvas()->snappingUtils()->config().selfSnapping() && mCanvas->currentLayer() && mCaptureCurve.numPoints() >= 2 )
1065+
if ( canvas()->snappingUtils()->config().selfSnapping() && layer() && mCaptureCurve.numPoints() >= 2 )
10611066
{
10621067
// the current layer may have changed
1063-
mExtraSnapLayer->setCrs( mCanvas->currentLayer()->crs() );
1068+
mExtraSnapLayer->setCrs( layer()->crs() );
10641069
QgsGeometry geom = QgsGeometry( mCaptureCurve.clone() );
10651070
// we close the curve to allow snapping on last segment
10661071
if ( mCaptureMode == CapturePolygon && mCaptureCurve.numPoints() >= 3 )

‎src/gui/qgsmaptoolcapture.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
390390
*/
391391
void closePolygon();
392392

393+
/**
394+
* Returns the map layer associated with the geometry being captured.
395+
*
396+
* The base class implementation always returns the current map canvas layer, but
397+
* subclasses can override this if they need to be associated with a specific layer.
398+
*
399+
* \since QGIS 3.22
400+
*/
401+
virtual QgsMapLayer *layer() const;
402+
393403
protected slots:
394404

395405
/**

0 commit comments

Comments
 (0)
Please sign in to comment.