Skip to content

Commit

Permalink
Add snapping support to scale feature tool
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and github-actions[bot] committed Apr 27, 2021
1 parent eed3818 commit 889d7ec
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
28 changes: 22 additions & 6 deletions src/app/qgsmaptoolscalefeature.cpp
Expand Up @@ -22,6 +22,7 @@
#include <limits>
#include <cmath>

#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsmaptoolscalefeature.h"
#include "qgsfeatureiterator.h"
#include "qgsgeometry.h"
Expand All @@ -33,6 +34,7 @@
#include "qgisapp.h"
#include "qgsspinbox.h"
#include "qgsdoublespinbox.h"
#include "qgssnapindicator.h"
#include "qgsmapmouseevent.h"


Expand Down Expand Up @@ -107,19 +109,23 @@ void QgsScaleMagnetWidget::scaleSpinBoxValueChanged( double scale )
//

QgsMapToolScaleFeature::QgsMapToolScaleFeature( QgsMapCanvas *canvas )
: QgsMapToolEdit( canvas )
: QgsMapToolAdvancedDigitizing( canvas, QgisApp::instance()->cadDockWidget() )
, mSnapIndicator( std::make_unique< QgsSnapIndicator>( canvas ) )
{
mToolName = tr( "Scale feature" );
}

QgsMapToolScaleFeature::~QgsMapToolScaleFeature()
{
deleteScalingWidget();
mAnchorPoint.reset();
deleteRubberband();
mSnapIndicator->setMatch( QgsPointLocator::Match() );
}

void QgsMapToolScaleFeature::canvasMoveEvent( QgsMapMouseEvent *e )
void QgsMapToolScaleFeature::cadCanvasMoveEvent( QgsMapMouseEvent *e )
{
mSnapIndicator->setMatch( e->mapPointMatch() );
if ( mBaseDistance == 0 )
{
return;
Expand All @@ -141,7 +147,7 @@ void QgsMapToolScaleFeature::canvasMoveEvent( QgsMapMouseEvent *e )
}
}

void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )
void QgsMapToolScaleFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( !mCanvas )
{
Expand All @@ -154,6 +160,8 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )
deleteScalingWidget();
deleteRubberband();
notifyNotVectorLayer();
mSnapIndicator->setMatch( QgsPointLocator::Match() );
mCadDockWidget->clear();
return;
}

Expand All @@ -173,6 +181,7 @@ void QgsMapToolScaleFeature::canvasReleaseEvent( QgsMapMouseEvent *e )
}
mAnchorPoint->setCenter( e->mapPoint() );
mFeatureCenterMapCoords = e->mapPoint();
cadDockWidget()->clear();
return;
}

Expand Down Expand Up @@ -298,6 +307,8 @@ void QgsMapToolScaleFeature::cancel()
mAnchorPoint.reset();
}
mScalingActive = false;
mSnapIndicator->setMatch( QgsPointLocator::Match() );
mCadDockWidget->clear();
}

void QgsMapToolScaleFeature::updateRubberband( double scale )
Expand Down Expand Up @@ -335,6 +346,8 @@ void QgsMapToolScaleFeature::applyScaling( double scale )
{
deleteRubberband();
notifyNotVectorLayer();
mSnapIndicator->setMatch( QgsPointLocator::Match() );
mCadDockWidget->clear();
return;
}

Expand All @@ -358,6 +371,8 @@ void QgsMapToolScaleFeature::applyScaling( double scale )

deleteScalingWidget();
deleteRubberband();
mSnapIndicator->setMatch( QgsPointLocator::Match() );
mCadDockWidget->clear();

if ( mAutoSetAnchorPoint )
mAnchorPoint.reset();
Expand All @@ -373,7 +388,7 @@ void QgsMapToolScaleFeature::keyReleaseEvent( QKeyEvent *e )
cancel();
return;
}
QgsMapTool::keyReleaseEvent( e );
QgsMapToolAdvancedDigitizing::keyReleaseEvent( e );
}

void QgsMapToolScaleFeature::activate()
Expand All @@ -398,7 +413,7 @@ void QgsMapToolScaleFeature::activate()
mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS );
mAnchorPoint->setCenter( mFeatureCenterMapCoords );
}
QgsMapTool::activate();
QgsMapToolAdvancedDigitizing::activate();
}

void QgsMapToolScaleFeature::deleteRubberband()
Expand All @@ -415,7 +430,8 @@ void QgsMapToolScaleFeature::deactivate()
mScalingActive = false;
mAnchorPoint.reset();
deleteRubberband();
QgsMapTool::deactivate();
mSnapIndicator->setMatch( QgsPointLocator::Match() );
QgsMapToolAdvancedDigitizing::deactivate();
}

void QgsMapToolScaleFeature::createScalingWidget()
Expand Down
18 changes: 11 additions & 7 deletions src/app/qgsmaptoolscalefeature.h
Expand Up @@ -18,7 +18,7 @@

#include <QWidget>

#include "qgsmaptooledit.h"
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsvertexmarker.h"
#include "qgis_app.h"
#include "qgsgeometry.h"
Expand All @@ -27,6 +27,7 @@
class QgsDoubleSpinBox;
class QHBoxLayout;
class QgsSpinBox;
class QgsSnapIndicator;

class APP_EXPORT QgsScaleMagnetWidget : public QWidget
{
Expand Down Expand Up @@ -62,16 +63,16 @@ class APP_EXPORT QgsScaleMagnetWidget : public QWidget


//! Map tool to scale features
class APP_EXPORT QgsMapToolScaleFeature: public QgsMapToolEdit
class APP_EXPORT QgsMapToolScaleFeature: public QgsMapToolAdvancedDigitizing
{
Q_OBJECT
public:
QgsMapToolScaleFeature( QgsMapCanvas *canvas );
~QgsMapToolScaleFeature() override;

void canvasMoveEvent( QgsMapMouseEvent *e ) override;
void cadCanvasMoveEvent( QgsMapMouseEvent *e ) override;

void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;

//! called when map tool is being deactivated
void deactivate() override;
Expand All @@ -95,15 +96,18 @@ class APP_EXPORT QgsMapToolScaleFeature: public QgsMapToolEdit
void createScalingWidget();
void deleteScalingWidget();

//! Start point of the move in map coordinates
//! Start point of the scaling in map coordinates
QgsPointXY mFeatureCenterMapCoords;
//! Rubberband that shows the feature being moved
//! Rubberband that shows the feature being scaled
QgsRubberBand *mRubberBand = nullptr;

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

//! Snapping indicators
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;

double mScaling = 0;
double mBaseDistance = 1;
QgsRectangle mExtent;
Expand Down
33 changes: 33 additions & 0 deletions tests/src/app/testqgsmaptoolscalefeature.cpp
Expand Up @@ -48,6 +48,7 @@ class TestQgsMapToolScaleFeature: public QObject
void testCancelManualAnchor();
void testScaleFeatureWithAnchorSetAfterStart();
void testScaleSelectedFeatures();
void testScaleFeatureManualAnchorSnapping();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -113,6 +114,7 @@ void TestQgsMapToolScaleFeature::initTestCase()

mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerBase );
mCanvas->setCurrentLayer( mLayerBase );
mCanvas->snappingUtils()->locatorForLayer( mLayerBase )->init();

// create the tool
mScaleTool = new QgsMapToolScaleFeature( mCanvas );
Expand Down Expand Up @@ -229,6 +231,37 @@ void TestQgsMapToolScaleFeature::testScaleSelectedFeatures()
mLayerBase->undoStack()->undo();
}

void TestQgsMapToolScaleFeature::testScaleFeatureManualAnchorSnapping()
{
TestQgsMapToolUtils utils( mScaleTool );

QgsSnappingConfig cfg = mCanvas->snappingUtils()->config();
const double tolerance = cfg.tolerance();
const QgsTolerance::UnitType units = cfg.units();
cfg.setTolerance( 0.5 );
cfg.setUnits( QgsTolerance::LayerUnits );
mCanvas->snappingUtils()->setConfig( cfg );

//set manual anchor point, should snap to (-2, -2)
utils.mouseClick( -1.9, -1.9, Qt::LeftButton, Qt::ControlModifier, true );

// resize, source point should snap to (-1, -1)
utils.mouseMove( -0.9, -0.9 );
utils.mouseClick( -0.9, -0.9, Qt::LeftButton, Qt::KeyboardModifiers(), true );
// target point should snap to (1.1, 0.8)
utils.mouseMove( 1.2, 0.9 );
utils.mouseClick( 1.2, 0.9, Qt::LeftButton, Qt::KeyboardModifiers(), true );

QCOMPARE( mLayerBase->getFeature( 1 ).geometry().asWkt( 2 ), QStringLiteral( "Polygon ((-2 -2, -2 0.95, 0.95 0.95, 0.95 -2, -2 -2))" ) );
QCOMPARE( mLayerBase->getFeature( 2 ).geometry().asWkt( 2 ), QStringLiteral( "Polygon ((1.1 0.8, 1.1 5, 2.1 5, 2.1 0.8, 1.1 0.8))" ) );

mLayerBase->undoStack()->undo();

// restore tolerance setting
cfg.setTolerance( tolerance );
cfg.setUnits( units );
mCanvas->snappingUtils()->setConfig( cfg );
}

QGSTEST_MAIN( TestQgsMapToolScaleFeature )
#include "testqgsmaptoolscalefeature.moc"

0 comments on commit 889d7ec

Please sign in to comment.