Skip to content

Commit

Permalink
More robust approach to updating rubber band -- use actual transforme…
Browse files Browse the repository at this point in the history
…d geometry results instead of transforming rubber band
  • Loading branch information
nyalldawson committed Jan 12, 2021
1 parent 934fa1d commit 0dfaa78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
32 changes: 19 additions & 13 deletions src/app/qgsmaptoolscalefeature.cpp
Expand Up @@ -185,8 +185,6 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )

deleteRubberband();

mInitialCanvasPos = e->pos();

if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
Expand Down Expand Up @@ -253,6 +251,7 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )

mScaledFeatures.clear();
mScaledFeatures << cf.id(); //todo: take the closest feature, not the first one...
mOriginalGeometries << cf.geometry();

mRubberBand = createRubberBand( vlayer->geometryType() );
mRubberBand->setToGeometry( cf.geometry(), vlayer );
Expand All @@ -268,15 +267,12 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )
while ( it.nextFeature( feat ) )
{
mRubberBand->addGeometry( feat.geometry(), vlayer );
mOriginalGeometries << feat.geometry();
}
}

QgsPointXY mapAnchor = toMapCoordinates( vlayer, mFeatureCenterMapCoords );
QPoint rubberAnchor = toCanvasCoordinates( mapAnchor );
mScalingActive = true;

mRubberScale = QPointF( rubberAnchor.x() - mRubberBand->x(), rubberAnchor.y() - mRubberBand->y() );
mRubberBand->setTransformOriginPoint( rubberAnchor );
mRubberBand->show();
mBaseDistance = e->mapPoint().distance( mFeatureCenterMapCoords );
mScaling = 1.0;

Expand Down Expand Up @@ -304,17 +300,25 @@ void QgsMapToolScaleFeature::cancel()

void QgsMapToolScaleFeature::updateRubberband( double scale )
{
if ( mScalingActive )
if ( mScalingActive && mRubberBand )
{
mScaling = scale;

double offsetx = ( 1 - mScaling ) * mRubberScale.x();
double offsety = ( 1 - mScaling ) * mRubberScale.y();
QTransform t;
t.translate( mFeatureCenterMapCoords.x(), mFeatureCenterMapCoords.y() );
t.scale( mScaling, mScaling );
t.translate( -mFeatureCenterMapCoords.x(), -mFeatureCenterMapCoords.y() );

if ( mRubberBand )
QgsVectorLayer *vlayer = currentVectorLayer();
if ( !vlayer )
return;

mRubberBand->reset( vlayer->geometryType() );
for ( const QgsGeometry &originalGeometry : mOriginalGeometries )
{
mRubberBand->setTransform( QTransform( mScaling, 0, 0, mScaling, offsetx, offsety ) );
mRubberBand->update();
QgsGeometry geom = originalGeometry;
geom.transform( t );
mRubberBand->addGeometry( geom, vlayer );
}
}
}
Expand Down Expand Up @@ -396,6 +400,8 @@ void QgsMapToolScaleFeature::deleteRubberband()
{
delete mRubberBand;
mRubberBand = nullptr;

mOriginalGeometries.clear();
}

void QgsMapToolScaleFeature::deactivate()
Expand Down
6 changes: 2 additions & 4 deletions src/app/qgsmaptoolscalefeature.h
Expand Up @@ -97,19 +97,17 @@ class APP_EXPORT QgsMapToolScaleFeature: public QgsMapToolEdit

//! Start point of the move in map coordinates
QgsPointXY mFeatureCenterMapCoords;
QPointF mRubberScale;
QPointF mInitialCanvasPos;

//! Rubberband that shows the feature being moved
QgsRubberBand *mRubberBand = nullptr;

//! Id of moved feature
QgsFeatureIds mScaledFeatures;
QVector< QgsGeometry > mOriginalGeometries;

double mScaling = 0;
double mBaseDistance = 1;
QgsRectangle mExtent;

QgsPointXY mCenterPoint;
std::unique_ptr<QgsVertexMarker> mAnchorPoint = nullptr;

bool mScalingActive = false;
Expand Down

0 comments on commit 0dfaa78

Please sign in to comment.