Skip to content
Permalink
Browse files

More robust approach to updating rubber band -- use actual transforme…

…d geometry results instead of transforming rubber band
  • Loading branch information
nyalldawson committed Jan 12, 2021
1 parent 934fa1d commit 0dfaa78545a1edca30e15ecd7706af0c9ff38882
Showing with 21 additions and 17 deletions.
  1. +19 −13 src/app/qgsmaptoolscalefeature.cpp
  2. +2 −4 src/app/qgsmaptoolscalefeature.h
@@ -185,8 +185,6 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )

deleteRubberband();

mInitialCanvasPos = e->pos();

if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
@@ -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 );
@@ -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;

@@ -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 );
}
}
}
@@ -396,6 +400,8 @@ void QgsMapToolScaleFeature::deleteRubberband()
{
delete mRubberBand;
mRubberBand = nullptr;

mOriginalGeometries.clear();
}

void QgsMapToolScaleFeature::deactivate()
@@ -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;

0 comments on commit 0dfaa78

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