Skip to content

Commit

Permalink
Fix usability issues of tracing with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Oct 10, 2017
1 parent b140b29 commit f16b215
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/qgstracer.cpp
Expand Up @@ -729,8 +729,10 @@ QVector<QgsPointXY> QgsTracer::findShortestPath( const QgsPointXY &p1, const Qgs
// sometimes (with negative offset?) the resulting curve is reversed
if ( points.count() >= 2 )
{
double diff = points[0].distance( p1 );
if ( !qgsDoubleNear( diff, mOffset ) )
QgsPointXY res1 = points.first(), res2 = points.last();
double diffNormal = res1.distance( p1 ) + res2.distance( p2 );
double diffReversed = res1.distance( p2 ) + res2.distance( p1 );
if ( diffReversed < diffNormal )
std::reverse( points.begin(), points.end() );
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -142,6 +142,12 @@ QgsPointXY QgsMapToolCapture::tracingStartPoint()
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;

QgsPoint v = mCaptureCurve.endPoint();
return toMapCoordinates( layer, QgsPointXY( v.x(), v.y() ) );
}
Expand Down Expand Up @@ -179,6 +185,15 @@ bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e )
if ( mCaptureMode == CapturePolygon )
mTempRubberBand->addPoint( *mRubberBand->getPoint( 0, 0 ), false );

// if there is offset, we need to add the previous point as well otherwise there may be a gap
// between the existing rubber band and temporary rubber band
if ( mRubberBand->numberOfVertices() != 0 )
{
QgsPointXY lastPoint = *mRubberBand->getPoint( 0, mRubberBand->numberOfVertices() - 1 );
if ( points[0] != lastPoint )
mTempRubberBand->addPoint( lastPoint, false );
}

// update rubberband
for ( int i = 0; i < points.count(); ++i )
mTempRubberBand->addPoint( points.at( i ), i == points.count() - 1 );
Expand Down Expand Up @@ -237,7 +252,11 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
QgsVertexId::VertexType type;
mCaptureCurve.pointAt( mCaptureCurve.numPoints() - 1, last, type );
if ( last != lp )
{
// last captured point and first point of the trace are not the same (different offset maybe)
// so we need to use also the first point of the trace
indexStart = 0;
}
}

// transform points
Expand Down Expand Up @@ -434,6 +453,10 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
traceCreated = tracingAddVertex( point );
}

// keep new tracing start point if we created a trace. This is useful when tracing with
// offset so that the user stays "snapped"
mTracingStartPoint = traceCreated ? point : QgsPointXY();

if ( !traceCreated )
{
// ordinary digitizing
Expand Down Expand Up @@ -514,6 +537,8 @@ QList<QgsPointLocator::Match> QgsMapToolCapture::snappingMatches() const

void QgsMapToolCapture::undo()
{
mTracingStartPoint = QgsPointXY();

if ( mRubberBand )
{
int rubberBandSize = mRubberBand->numberOfVertices();
Expand Down Expand Up @@ -600,6 +625,8 @@ void QgsMapToolCapture::stopCapturing()

mGeomErrors.clear();

mTracingStartPoint = QgsPointXY();

#ifdef Q_OS_WIN
Q_FOREACH ( QWidget *w, qApp->topLevelWidgets() )
{
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsmaptoolcapture.h
Expand Up @@ -240,6 +240,12 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing

QgsVertexMarker *mSnappingMarker = nullptr;

//! Keeps point (in map units) snapped to a segment where we most recently finished tracing,
//! so that we can use as the starting point for further tracing. This is useful mainly when
//! tracing with offset: without knowledge of this point user would need to click a segment
//! again after every time a new trace with offset is created (to get new "anchor" point)
QgsPointXY mTracingStartPoint;

#ifdef Q_OS_WIN
int mSkipNextContextMenuEvent;
#endif
Expand Down

0 comments on commit f16b215

Please sign in to comment.