Skip to content

Commit f16b215

Browse files
committedOct 10, 2017
Fix usability issues of tracing with offset
1 parent b140b29 commit f16b215

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
 

‎src/core/qgstracer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,10 @@ QVector<QgsPointXY> QgsTracer::findShortestPath( const QgsPointXY &p1, const Qgs
729729
// sometimes (with negative offset?) the resulting curve is reversed
730730
if ( points.count() >= 2 )
731731
{
732-
double diff = points[0].distance( p1 );
733-
if ( !qgsDoubleNear( diff, mOffset ) )
732+
QgsPointXY res1 = points.first(), res2 = points.last();
733+
double diffNormal = res1.distance( p1 ) + res2.distance( p2 );
734+
double diffReversed = res1.distance( p2 ) + res2.distance( p1 );
735+
if ( diffReversed < diffNormal )
734736
std::reverse( points.begin(), points.end() );
735737
}
736738
}

‎src/gui/qgsmaptoolcapture.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ QgsPointXY QgsMapToolCapture::tracingStartPoint()
142142
QgsMapLayer *layer = mCanvas->currentLayer();
143143
if ( !layer )
144144
return QgsPointXY();
145+
146+
// if we have starting point from previous trace, then preferably use that one
147+
// (useful when tracing with offset)
148+
if ( mTracingStartPoint != QgsPointXY() )
149+
return mTracingStartPoint;
150+
145151
QgsPoint v = mCaptureCurve.endPoint();
146152
return toMapCoordinates( layer, QgsPointXY( v.x(), v.y() ) );
147153
}
@@ -179,6 +185,15 @@ bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e )
179185
if ( mCaptureMode == CapturePolygon )
180186
mTempRubberBand->addPoint( *mRubberBand->getPoint( 0, 0 ), false );
181187

188+
// if there is offset, we need to add the previous point as well otherwise there may be a gap
189+
// between the existing rubber band and temporary rubber band
190+
if ( mRubberBand->numberOfVertices() != 0 )
191+
{
192+
QgsPointXY lastPoint = *mRubberBand->getPoint( 0, mRubberBand->numberOfVertices() - 1 );
193+
if ( points[0] != lastPoint )
194+
mTempRubberBand->addPoint( lastPoint, false );
195+
}
196+
182197
// update rubberband
183198
for ( int i = 0; i < points.count(); ++i )
184199
mTempRubberBand->addPoint( points.at( i ), i == points.count() - 1 );
@@ -237,7 +252,11 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
237252
QgsVertexId::VertexType type;
238253
mCaptureCurve.pointAt( mCaptureCurve.numPoints() - 1, last, type );
239254
if ( last != lp )
255+
{
256+
// last captured point and first point of the trace are not the same (different offset maybe)
257+
// so we need to use also the first point of the trace
240258
indexStart = 0;
259+
}
241260
}
242261

243262
// transform points
@@ -434,6 +453,10 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
434453
traceCreated = tracingAddVertex( point );
435454
}
436455

456+
// keep new tracing start point if we created a trace. This is useful when tracing with
457+
// offset so that the user stays "snapped"
458+
mTracingStartPoint = traceCreated ? point : QgsPointXY();
459+
437460
if ( !traceCreated )
438461
{
439462
// ordinary digitizing
@@ -514,6 +537,8 @@ QList<QgsPointLocator::Match> QgsMapToolCapture::snappingMatches() const
514537

515538
void QgsMapToolCapture::undo()
516539
{
540+
mTracingStartPoint = QgsPointXY();
541+
517542
if ( mRubberBand )
518543
{
519544
int rubberBandSize = mRubberBand->numberOfVertices();
@@ -600,6 +625,8 @@ void QgsMapToolCapture::stopCapturing()
600625

601626
mGeomErrors.clear();
602627

628+
mTracingStartPoint = QgsPointXY();
629+
603630
#ifdef Q_OS_WIN
604631
Q_FOREACH ( QWidget *w, qApp->topLevelWidgets() )
605632
{

‎src/gui/qgsmaptoolcapture.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
240240

241241
QgsVertexMarker *mSnappingMarker = nullptr;
242242

243+
//! Keeps point (in map units) snapped to a segment where we most recently finished tracing,
244+
//! so that we can use as the starting point for further tracing. This is useful mainly when
245+
//! tracing with offset: without knowledge of this point user would need to click a segment
246+
//! again after every time a new trace with offset is created (to get new "anchor" point)
247+
QgsPointXY mTracingStartPoint;
248+
243249
#ifdef Q_OS_WIN
244250
int mSkipNextContextMenuEvent;
245251
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.