Skip to content

Commit

Permalink
fixe some issue with trace
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Jul 29, 2020
1 parent 7b772ea commit f3a8dd7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
97 changes: 52 additions & 45 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -146,19 +146,15 @@ bool QgsMapToolCapture::tracingEnabled()

QgsPointXY QgsMapToolCapture::tracingStartPoint()
{
//There is already a try/catch in the toMapCoordinate() (in lastCaptureMapPoint()) int method if transform fail. Can we remove this try/catch?
//There is already a try/catch in the toLayerCoordinate() method if transform fails. Can we remove this try/catch?
try
{
QgsMapLayer *layer = mCanvas->currentLayer();
if ( !layer )
return QgsPointXY();

// if we have starting point from previous trace, then preferably use that one
// (useful when tracing with offset)
if ( mTracingStartPoint != QgsPointXY() )
return mTracingStartPoint;

return lastCaptureMapPoint();
return mTempRubberBand->pointFromEnd( 1 );
}
catch ( QgsCsException & )
{
Expand All @@ -170,6 +166,13 @@ QgsPointXY QgsMapToolCapture::tracingStartPoint()

bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e )
{
mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, QgsWkbTypes::LineString );
if ( mCaptureMode == CapturePolygon )
{
mTempRubberBand->setFirstPolygonPoint( firstCaptureMapPoint() );
}
mTempRubberBand->addPoint( lastCaptureMapPoint() );

if ( !e->isSnapped() )
return false;

Expand All @@ -181,13 +184,6 @@ bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e )
if ( !tracer )
return false; // this should not happen!

mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, mDigitizingType );
mTempRubberBand->setStringType( QgsWkbTypes::LineString );
if ( mCaptureMode == CapturePolygon )
{
mTempRubberBand->setFirstPolygonPoint( firstCaptureMapPoint() );
}

QgsTracer::PathError err;
QVector<QgsPointXY> points = tracer->findShortestPath( pt0, e->mapPoint(), &err );
if ( points.isEmpty() )
Expand All @@ -213,7 +209,7 @@ bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e )
}
else
{
mTempRubberBand->addPoint( lastPoint, false );
mTempRubberBand->movePoint( lastPoint );
}
}

Expand All @@ -232,7 +228,7 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
if ( !tracer )
return false; // this should not happen!

if ( mCaptureCurve.numPoints() == 0 )
if ( mTempRubberBand->pointsCount() == 0 )
{
if ( !tracer->init() )
{
Expand All @@ -244,14 +240,9 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
bool res = tracer->isPointSnapped( point );
if ( res )
{
QgsPoint layerPoint;
nextPoint( QgsPoint( point ), layerPoint ); // assuming the transform went fine earlier

mRubberBand->addPoint( point );
mCaptureCurve.addVertex( layerPoint );
mSnappingMatches.append( QgsPointLocator::Match() );
mTracingStartPoint = point;
}
return res;
return false;
}

QgsPointXY pt0 = tracingStartPoint();
Expand Down Expand Up @@ -385,8 +376,7 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
QgsMapToolAdvancedDigitizing::cadCanvasMoveEvent( e );
QgsPointXY point = e->mapPoint();

const QgsPointLocator::Match &match = e->mapPointMatch();
mSnapIndicator->setMatch( match );
mSnapIndicator->setMatch( e->mapPointMatch() );

QgsPoint mapPoint = QgsPoint( point );

Expand Down Expand Up @@ -552,7 +542,9 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
bool traceCreated = false;
if ( tracingEnabled() )
{
traceCreated = tracingAddVertex( point );
mDigitizingType = QgsWkbTypes::LineString;
mTempRubberBand->setStringType( QgsWkbTypes::LineString );
traceCreated = tracingAddVertex( mapPoint );
}

// keep new tracing start point if we created a trace. This is useful when tracing with
Expand All @@ -563,27 +555,18 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
{
// ordinary digitizing

if ( mRubberBand->size() == 0 )
{
mRubberBand->addPoint( point );
mCaptureCurve.addVertex( layerPoint );
mSnappingMatches.append( match );
if ( mCaptureMode == CapturePolygon )
{
mRubberBand->addPoint( point );
mTempRubberBand->setFirstPolygonPoint( mapPoint );
}
}
if ( mCaptureCurve.vertexCount() == 0 && mCaptureMode == CapturePolygon )
mTempRubberBand->setFirstPolygonPoint( mapPoint );

mTempRubberBand->movePoint( mapPoint );
if ( mTempRubberBand->curveIsComplete() )
mTempRubberBand->movePoint( mapPoint ); //move the last point of the temp rubberband before operating with it
if ( mTempRubberBand->curveIsComplete() ) //2 points for line and 3 points for circular
{
const QgsCurve *curve = mTempRubberBand->curve();
if ( curve )
{
addCurve( curve->clone() );
// add curve append only invalid match to mSnappingMatches,
// so we need to remove one and add the one from here if it is valid
// so we need to remove them and add the one from here if it is valid
if ( match.isValid() && mSnappingMatches.count() > 0 && !mSnappingMatches.last().isValid() )
{
mSnappingMatches.removeLast();
Expand All @@ -602,6 +585,7 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
}
else
{

mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, mDigitizingType );
if ( mCaptureMode == CapturePolygon )
{
Expand Down Expand Up @@ -697,6 +681,17 @@ void QgsMapToolCapture::undo()
vertexToRemove.part = 0;
vertexToRemove.ring = 0;
vertexToRemove.vertex = size() - 1;
if ( mCaptureCurve.numPoints() == 2 && mCaptureCurve.nCurves() == 1 )
{
// store the first vertex to restore if after deleting the curve
// because when only two vertices, removing a point remove all the curve
QgsPoint fp = mCaptureCurve.startPoint();
mCaptureCurve.deleteVertex( vertexToRemove );
mCaptureCurve.addVertex( fp );
}
else
mCaptureCurve.deleteVertex( vertexToRemove );

mCaptureCurve.deleteVertex( vertexToRemove );
mSnappingMatches.removeAt( vertexToRemove.vertex );
updateExtraSnapLayer();
Expand All @@ -707,16 +702,19 @@ void QgsMapToolCapture::undo()
mRubberBand->addGeometry( QgsGeometry( mCaptureCurve.clone() ), vlayer );

mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, mDigitizingType );
if ( mCaptureMode == CapturePolygon )

if ( mCaptureCurve.numPoints() > 0 )
{
mTempRubberBand->setFirstPolygonPoint( firstCaptureMapPoint() );
}
if ( mCaptureMode == CapturePolygon )
{
mTempRubberBand->setFirstPolygonPoint( firstCaptureMapPoint() );
}

mTempRubberBand->addPoint( lastCaptureMapPoint() );
mTempRubberBand->movePoint( lastPoint );
mTempRubberBand->addPoint( lastCaptureMapPoint() );
mTempRubberBand->movePoint( lastPoint );
}

mCadDockWidget->removePreviousPoint();

validateGeometry();
}
}
Expand Down Expand Up @@ -1023,6 +1021,7 @@ void QgsMapToolCaptureRubberband::reset( QgsWkbTypes::GeometryType geomType, Qgs

mStringType = stringType;
mPoints.clear();
mFirstPolygonPoint = QgsPoint();
updateCurve();
}

Expand Down Expand Up @@ -1091,6 +1090,14 @@ QgsPoint QgsMapToolCaptureRubberband::lastPoint() const
return mPoints.last();
}

QgsPoint QgsMapToolCaptureRubberband::pointFromEnd( int posFromEnd ) const
{
if ( posFromEnd < mPoints.size() )
return mPoints.at( mPoints.size() - 1 - posFromEnd );
else
return QgsPoint();
}

void QgsMapToolCaptureRubberband::removeLastPoint()
{
if ( mPoints.count() > 1 )
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsmaptoolcapture.h
Expand Up @@ -93,6 +93,9 @@ class QgsMapToolCaptureRubberband: public QgsGeometryRubberBand
//! Returns the last point of the rubber band
QgsPoint lastPoint() const;

//! Returns the point of the rubber band at position from end
QgsPoint pointFromEnd( int posFromEnd ) const;

//! Removes the last point of the rrubber band
void removeLastPoint();

Expand Down

0 comments on commit f3a8dd7

Please sign in to comment.