Skip to content
Permalink
Browse files

Paul's review

  • Loading branch information
lbartoletti committed Nov 13, 2018
1 parent a8f8659 commit 8b2331a40df065208b50647c9a67717233ed6378
@@ -1,5 +1,5 @@
/***************************************************************************
qgmaptoolextendfeature.cpp - map tool for extending feature
qgmaptooltrimextendfeature.cpp - map tool to trim or extend feature
---------------------
begin : October 2018
copyright : (C) 2018 by Loïc Bartoletti
@@ -73,19 +73,19 @@ static void getPoints( const QgsPointLocator::Match &match, QgsPoint &p1, QgsPoi

void QgsMapToolTrimExtendFeature::canvasMoveEvent( QgsMapMouseEvent *e )
{
mapPoint = e->mapPoint();
mMapPoint = e->mapPoint();

FeatureFilter filter;
QgsPointLocator::Match match;

switch ( step )
switch ( mStep )
{
case 0:
case StepLimit:

match = mCanvas->snappingUtils()->snapToMap( mapPoint, &filter );
match = mCanvas->snappingUtils()->snapToMap( mMapPoint, &filter );
if ( match.isValid() )
{
is3DLayer = QgsWkbTypes::hasZ( match.layer()->wkbType() );
mIs3DLayer = QgsWkbTypes::hasZ( match.layer()->wkbType() );

QgsPointXY p1, p2;
match.edgePoints( p1, p2 );
@@ -96,31 +96,30 @@ void QgsMapToolTrimExtendFeature::canvasMoveEvent( QgsMapMouseEvent *e )
mRubberBandLimit->show();

}
else
else if ( mRubberBandLimit )
{
if ( mRubberBandLimit )
mRubberBandLimit->hide();
}
break;
case 1:
case StepExtend:

QgsMapLayer *currentLayer = mCanvas->currentLayer();
if ( !currentLayer )
break;

vlayer = qobject_cast<QgsVectorLayer *>( currentLayer );
if ( !vlayer )
mVlayer = qobject_cast<QgsVectorLayer *>( currentLayer );
if ( !mVlayer )
break;

if ( !vlayer->isEditable() )
if ( !mVlayer->isEditable() )
break;

filter.setLayer( vlayer );
match = mCanvas->snappingUtils()->snapToMap( mapPoint, &filter );
filter.setLayer( mVlayer );
match = mCanvas->snappingUtils()->snapToMap( mMapPoint, &filter );

if ( match.isValid() )
{
if ( match.layer() != vlayer )
if ( match.layer() != mVlayer )
break;

QgsPointXY p1, p2;
@@ -132,34 +131,35 @@ void QgsMapToolTrimExtendFeature::canvasMoveEvent( QgsMapMouseEvent *e )
if ( ( ( pLimit1 == pExtend1 ) || ( pLimit1 == pExtend2 ) ) || ( ( pLimit2 == pExtend1 ) || ( pLimit2 == pExtend2 ) ) )
break;

segmentIntersects = QgsGeometryUtils::segmentIntersection( pLimit1, pLimit2, pExtend1, pExtend2, intersection, isIntersection, 1e-8, true );
mSegmentIntersects = QgsGeometryUtils::segmentIntersection( pLimit1, pLimit2, pExtend1, pExtend2, mIntersection, mIsIntersection, 1e-8, true );

if ( is3DLayer && QgsWkbTypes::hasZ( match.layer()->wkbType() ) )
if ( mIs3DLayer && QgsWkbTypes::hasZ( match.layer()->wkbType() ) )
{
/* Z Interpolation */
QgsLineString line( pLimit1, pLimit2 );

intersection = QgsGeometryUtils::closestPoint( line, QgsPoint( intersection ) );
mIntersection = QgsGeometryUtils::closestPoint( line, QgsPoint( mIntersection ) );
}

if ( isIntersection )
if ( mIsIntersection )
{
mRubberBandIntersection.reset( createRubberBand( QgsWkbTypes::PointGeometry ) );
mRubberBandIntersection->addPoint( QgsPointXY( intersection ) );
mRubberBandIntersection->addPoint( QgsPointXY( mIntersection ) );
mRubberBandIntersection->show();

mRubberBandExtend.reset( createRubberBand( match.layer()->geometryType() ) );

geom = match.layer()->getGeometry( match.featureId() );
mGeom = match.layer()->getGeometry( match.featureId() );
int index = match.vertexIndex();

if ( !segmentIntersects )
if ( !mSegmentIntersects )
{
QgsPoint ptInter( intersection.x(), intersection.y() );
QgsPoint ptInter( mIntersection.x(), mIntersection.y() );
if ( pExtend2.distance( ptInter ) < pExtend1.distance( ptInter ) )
index += 1;
}
else // TRIM PART
// TRIM PART
else if ( QgsGeometryUtils::leftOfLine( QgsPoint( mMapPoint ), pLimit1, pLimit2 ) != QgsGeometryUtils::leftOfLine( pExtend1, pLimit1, pLimit2 ) )
{
// Part where the mouse is (+) will be trimed
/* |
@@ -177,15 +177,14 @@ void QgsMapToolTrimExtendFeature::canvasMoveEvent( QgsMapMouseEvent *e )
* +
* |
*/
if ( QgsGeometryUtils::leftOfLine( QgsPoint( mapPoint ), pLimit1, pLimit2 ) != QgsGeometryUtils::leftOfLine( pExtend1, pLimit1, pLimit2 ) )
index += 1;
}

isModified = geom.moveVertex( intersection, index );
mIsModified = mGeom.moveVertex( mIntersection, index );

if ( isModified )
if ( mIsModified )
{
mRubberBandExtend->setToGeometry( geom );
mRubberBandExtend->setToGeometry( mGeom );
mRubberBandExtend->show();
}
}
@@ -210,31 +209,31 @@ void QgsMapToolTrimExtendFeature::canvasMoveEvent( QgsMapMouseEvent *e )

void QgsMapToolTrimExtendFeature::canvasReleaseEvent( QgsMapMouseEvent *e )
{
mapPoint = e->mapPoint();
mMapPoint = e->mapPoint();

FeatureFilter filter;
QgsPointLocator::Match match;

if ( e->button() == Qt::LeftButton )
{
switch ( step )
switch ( mStep )
{
case 0:
match = mCanvas->snappingUtils()->snapToMap( mapPoint, &filter );
case StepLimit:
match = mCanvas->snappingUtils()->snapToMap( mMapPoint, &filter );
if ( mRubberBandLimit && mRubberBandLimit->isVisible() )
{
getPoints( match, pLimit1, pLimit2 );
step += 1;
mStep += 1;
}
break;
case 1:
if ( isModified )
case StepExtend:
if ( mIsModified )
{
filter.setLayer( vlayer );
match = mCanvas->snappingUtils()->snapToMap( mapPoint, &filter );
filter.setLayer( mVlayer );
match = mCanvas->snappingUtils()->snapToMap( mMapPoint, &filter );

match.layer()->beginEditCommand( tr( "Trim/Extend feature" ) );
match.layer()->changeGeometry( match.featureId(), geom );
match.layer()->changeGeometry( match.featureId(), mGeom );
match.layer()->endEditCommand();
match.layer()->triggerRepaint();

@@ -270,11 +269,11 @@ void QgsMapToolTrimExtendFeature::keyPressEvent( QKeyEvent *e )

void QgsMapToolTrimExtendFeature::deactivate()
{
step = 0;
isModified = false;
is3DLayer = false;
isIntersection = false;
segmentIntersects = false;
mStep = 0;
mIsModified = false;
mIs3DLayer = false;
mIsIntersection = false;
mSegmentIntersects = false;
mRubberBandLimit.reset();
mRubberBandExtend.reset();
mRubberBandIntersection.reset();
@@ -1,5 +1,5 @@
/***************************************************************************
qgmaptoolextendfeature.h - map tool for extending feature
qgmaptooltrimextendfeature.h - map tool to trim or extend feature
---------------------
begin : October 2018
copyright : (C) 2018 by Loïc Bartoletti
@@ -51,23 +51,28 @@ class APP_EXPORT QgsMapToolTrimExtendFeature : public QgsMapToolEdit
//! Points for extend
QgsPoint pExtend1, pExtend2;
//! intersection point between the projection of [pExtend1 - pExtend2] on [pLimit1 - pLimit2]
QgsPoint intersection;
QgsPoint mIntersection;
//! map point used to determine which edges will be used for trim the feature
QgsPointXY mapPoint;
QgsPointXY mMapPoint;
//! geometry that will be returned
QgsGeometry geom;
QgsGeometry mGeom;
//! Current layer which will be modified
QgsVectorLayer *vlayer = nullptr;
QgsVectorLayer *mVlayer = nullptr;
//! Keep information about the state of the intersection
bool isIntersection = false;
bool mIsIntersection = false;
//! Keep information of the first layer snapped is 3D or not
bool is3DLayer = false;
bool mIs3DLayer = false;
//! if feature is modified
bool isModified = false;
bool mIsModified = false;
//! if the segments are intersected = trim
bool segmentIntersects = false;
bool mSegmentIntersects = false;
enum Step
{
StepLimit,
StepExtend,
};
//! The first step (0): choose the limit. The second step (1): choose the segment to trim/extend
int step = 0;
int mStep = StepLimit;
};

#endif // QGSMAPTOOLTRIMEXTENDFEATURE_H
@@ -529,7 +529,7 @@ QVector<QgsGeometryUtils::SelfIntersection> QgsGeometryUtils::selfIntersections(
return intersections;
}

int QgsGeometryUtils::leftOfLine( const QgsPoint point, const QgsPoint p1, const QgsPoint p2 )
int QgsGeometryUtils::leftOfLine( const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 )
{
return leftOfLine( point.x(), point.y(), p1.x(), p1.y(), p2.x(), p2.y() );
}
@@ -255,7 +255,7 @@ class CORE_EXPORT QgsGeometryUtils
*
* \since QGIS 3.6
*/
static int leftOfLine( const QgsPoint point, const QgsPoint p1, const QgsPoint p2 );
static int leftOfLine(const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 );

/**
* Returns a point a specified \a distance toward a second point.

0 comments on commit 8b2331a

Please sign in to comment.
You can’t perform that action at this time.