diff --git a/images/images.qrc b/images/images.qrc index f86f30924fe..7c8ca5a30ef 100644 --- a/images/images.qrc +++ b/images/images.qrc @@ -673,7 +673,8 @@ themes/default/mActionEllipseCenterPoint.svg themes/default/mActionEllipseExtent.svg themes/default/mActionRectangleExtent.svg - themes/default/mActionRectangle3Points.svg + themes/default/mActionRectangle3PointsDistance.svg + themes/default/mActionRectangle3PointsProjected.svg themes/default/mActionRectangleCenter.svg themes/default/mActionRegularPolygonCenterCorner.svg themes/default/mActionRegularPolygon2Points.svg diff --git a/images/themes/default/mActionRectangle3Points.svg b/images/themes/default/mActionRectangle3PointsDistance.svg similarity index 100% rename from images/themes/default/mActionRectangle3Points.svg rename to images/themes/default/mActionRectangle3PointsDistance.svg diff --git a/images/themes/default/mActionRectangle3PointsProjected.svg b/images/themes/default/mActionRectangle3PointsProjected.svg new file mode 100644 index 00000000000..a07b35c760c --- /dev/null +++ b/images/themes/default/mActionRectangle3PointsProjected.svg @@ -0,0 +1,122 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index f00e048044d..acc958f4bd8 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -1,12 +1,12 @@ /*************************************************************************** - qgisapp.cpp - description - ------------------- + qgisapp.cpp - description + ------------------- - begin : Sat Jun 22 2002 - copyright : (C) 2002 by Gary E.Sherman - email : sherman at mrcc.com - Romans 3:23=>Romans 6:23=>Romans 10:9,10=>Romans 12 - ***************************************************************************/ + begin : Sat Jun 22 2002 + copyright : (C) 2002 by Gary E.Sherman + email : sherman at mrcc.com + Romans 3:23=>Romans 6:23=>Romans 10:9,10=>Romans 12 +***************************************************************************/ /*************************************************************************** * * @@ -1531,7 +1531,8 @@ QgisApp::~QgisApp() delete mMapTools.mEllipseFoci; delete mMapTools.mRectangleCenterPoint; delete mMapTools.mRectangleExtent; - delete mMapTools.mRectangle3Points; + delete mMapTools.mRectangle3PointsDistance; + delete mMapTools.mRectangle3PointsProjected; delete mMapTools.mRegularPolygon2Points; delete mMapTools.mRegularPolygonCenterPoint; delete mMapTools.mRegularPolygonCenterCorner; @@ -2091,7 +2092,8 @@ void QgisApp::createActions() connect( mActionEllipseFoci, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mEllipseFoci, true ); } ); connect( mActionRectangleCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleCenterPoint, true ); } ); connect( mActionRectangleExtent, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleExtent, true ); } ); - connect( mActionRectangle3Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3Points, true ); } ); + connect( mActionRectangle3PointsDistance, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsDistance, true ); } ); + connect( mActionRectangle3PointsProjected, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsProjected, true ); } ); connect( mActionRegularPolygon2Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygon2Points, true ); } ); connect( mActionRegularPolygonCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterPoint, true ); } ); connect( mActionRegularPolygonCenterCorner, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterCorner, true ); } ); @@ -2376,7 +2378,8 @@ void QgisApp::createActionGroups() mMapToolGroup->addAction( mActionEllipseFoci ); mMapToolGroup->addAction( mActionRectangleCenterPoint ); mMapToolGroup->addAction( mActionRectangleExtent ); - mMapToolGroup->addAction( mActionRectangle3Points ); + mMapToolGroup->addAction( mActionRectangle3PointsDistance ); + mMapToolGroup->addAction( mActionRectangle3PointsProjected ); mMapToolGroup->addAction( mActionRegularPolygon2Points ); mMapToolGroup->addAction( mActionRegularPolygonCenterPoint ); mMapToolGroup->addAction( mActionRegularPolygonCenterCorner ); @@ -2935,7 +2938,8 @@ void QgisApp::createToolBars() tbAddRectangle->setPopupMode( QToolButton::MenuButtonPopup ); tbAddRectangle->addAction( mActionRectangleCenterPoint ); tbAddRectangle->addAction( mActionRectangleExtent ); - tbAddRectangle->addAction( mActionRectangle3Points ); + tbAddRectangle->addAction( mActionRectangle3PointsDistance ); + tbAddRectangle->addAction( mActionRectangle3PointsProjected ); tbAddRectangle->setDefaultAction( mActionRectangleCenterPoint ); connect( tbAddRectangle, &QToolButton::triggered, this, &QgisApp::toolButtonActionTriggered ); mShapeDigitizeToolBar->insertWidget( mActionVertexTool, tbAddRectangle ); @@ -3576,8 +3580,10 @@ void QgisApp::createCanvasTools() mMapTools.mRectangleCenterPoint->setAction( mActionRectangleCenterPoint ); mMapTools.mRectangleExtent = new QgsMapToolRectangleExtent( mMapTools.mAddFeature, mMapCanvas ); mMapTools.mRectangleExtent->setAction( mActionRectangleExtent ); - mMapTools.mRectangle3Points = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas ); - mMapTools.mRectangle3Points->setAction( mActionRectangle3Points ); + mMapTools.mRectangle3PointsDistance = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::DistanceMode ); + mMapTools.mRectangle3PointsDistance->setAction( mActionRectangle3PointsDistance ); + mMapTools.mRectangle3PointsProjected = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::ProjectedMode ); + mMapTools.mRectangle3PointsProjected->setAction( mActionRectangle3PointsProjected ); mMapTools.mRegularPolygon2Points = new QgsMapToolRegularPolygon2Points( mMapTools.mAddFeature, mMapCanvas ); mMapTools.mRegularPolygon2Points->setAction( mActionRegularPolygon2Points ); mMapTools.mRegularPolygonCenterPoint = new QgsMapToolRegularPolygonCenterPoint( mMapTools.mAddFeature, mMapCanvas ); @@ -12543,7 +12549,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer ) mActionEllipseFoci->setEnabled( enableShapeTools ); mActionRectangleCenterPoint->setEnabled( enableShapeTools ); mActionRectangleExtent->setEnabled( enableShapeTools ); - mActionRectangle3Points->setEnabled( enableShapeTools ); + mActionRectangle3PointsDistance->setEnabled( enableShapeTools ); + mActionRectangle3PointsProjected->setEnabled( enableShapeTools ); mActionRegularPolygon2Points->setEnabled( enableShapeTools ); mActionRegularPolygonCenterPoint->setEnabled( enableShapeTools ); mActionRegularPolygonCenterCorner->setEnabled( enableShapeTools ); diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h index 131f301a3fc..c2ae030a1fb 100644 --- a/src/app/qgisapp.h +++ b/src/app/qgisapp.h @@ -1,10 +1,10 @@ /*************************************************************************** - qgisapp.h - description - ------------------- - begin : Sat Jun 22 2002 - copyright : (C) 2002 by Gary E.Sherman - email : sherman at mrcc.com - ***************************************************************************/ + qgisapp.h - description + ------------------- + begin : Sat Jun 22 2002 + copyright : (C) 2002 by Gary E.Sherman + email : sherman at mrcc.com +***************************************************************************/ /*************************************************************************** * * @@ -2044,7 +2044,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow QgsMapTool *mEllipseFoci = nullptr; QgsMapTool *mRectangleCenterPoint = nullptr; QgsMapTool *mRectangleExtent = nullptr; - QgsMapTool *mRectangle3Points = nullptr; + QgsMapTool *mRectangle3PointsDistance = nullptr; + QgsMapTool *mRectangle3PointsProjected = nullptr; QgsMapTool *mRegularPolygon2Points = nullptr; QgsMapTool *mRegularPolygonCenterPoint = nullptr; QgsMapTool *mRegularPolygonCenterCorner = nullptr; diff --git a/src/app/qgsmaptoolrectangle3points.cpp b/src/app/qgsmaptoolrectangle3points.cpp index df07ac948c2..3608a5d2ce9 100644 --- a/src/app/qgsmaptoolrectangle3points.cpp +++ b/src/app/qgsmaptoolrectangle3points.cpp @@ -1,18 +1,18 @@ /*************************************************************************** - qgsmaptoolrectangle3points.cpp - map tool for adding rectangle - from 3 points - --------------------- - begin : September 2017 - copyright : (C) 2017 by Loïc Bartoletti - email : lbartoletti at tuxfamily dot org - *************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ + qgsmaptoolrectangle3points.cpp - map tool for adding rectangle + from 3 points + --------------------- + begin : September 2017 + copyright : (C) 2017 by Loïc Bartoletti + email : lbartoletti at tuxfamily dot org +*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 3 of the License, or * +* (at your option) any later version. * +* * +***************************************************************************/ #include "qgsmaptoolrectangle3points.h" #include "qgsgeometryrubberband.h" @@ -25,8 +25,9 @@ #include "qgssnapindicator.h" QgsMapToolRectangle3Points::QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool, - QgsMapCanvas *canvas, CaptureMode mode ) - : QgsMapToolAddRectangle( parentTool, canvas, mode ) + QgsMapCanvas *canvas, CreateMode createMode, CaptureMode mode ) + : QgsMapToolAddRectangle( parentTool, canvas, mode ), + mCreateMode( createMode ) { } @@ -77,7 +78,15 @@ void QgsMapToolRectangle3Points::cadCanvasMoveEvent( QgsMapMouseEvent *e ) break; case 2: { - setDistance2( mPoints.at( 1 ).distance( point ) ); + switch ( mCreateMode ) + { + case DistanceMode: + setDistance2( mPoints.at( 1 ).distance( point ) ); + break; + case ProjectedMode: + setDistance2( QgsGeometryUtils::perpendicularSegment( point, mPoints.at( 0 ), mPoints.at( 1 ) ).length() ); + break; + } int side = QgsGeometryUtils::leftOfLine( point.x(), point.y(), mPoints.at( 0 ).x(), mPoints.at( 0 ).y(), mPoints.at( 1 ).x(), mPoints.at( 1 ).y() ); diff --git a/src/app/qgsmaptoolrectangle3points.h b/src/app/qgsmaptoolrectangle3points.h index b993d62dfab..189c1bacef3 100644 --- a/src/app/qgsmaptoolrectangle3points.h +++ b/src/app/qgsmaptoolrectangle3points.h @@ -1,18 +1,18 @@ /*************************************************************************** - qgsmaptoolrectangle3points.h - map tool for adding rectangle - from 3 points - --------------------- - begin : September 2017 - copyright : (C) 2017 by Loïc Bartoletti - email : lbartoletti at tuxfamily dot org - *************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ + qgsmaptoolrectangle3points.h - map tool for adding rectangle + from 3 points + --------------------- + begin : September 2017 + copyright : (C) 2017 by Loïc Bartoletti + email : lbartoletti at tuxfamily dot org +*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 3 of the License, or * +* (at your option) any later version. * +* * +***************************************************************************/ #ifndef QGSMAPTOOLRECTANGLE3POINTS_H #define QGSMAPTOOLRECTANGLE3POINTS_H @@ -25,11 +25,19 @@ class APP_EXPORT QgsMapToolRectangle3Points: public QgsMapToolAddRectangle Q_OBJECT public: - QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode = CaptureLine ); + enum CreateMode + { + DistanceMode, + ProjectedMode, + }; + QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CreateMode createMode, CaptureMode mode = CaptureLine ); void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override; void cadCanvasMoveEvent( QgsMapMouseEvent *e ) override; + private: + CreateMode mCreateMode; + }; #endif // QGSMAPTOOLRECTANGLE3POINTS_H diff --git a/src/ui/qgisapp.ui b/src/ui/qgisapp.ui index e3ab406f118..72d67de51b4 100644 --- a/src/ui/qgisapp.ui +++ b/src/ui/qgisapp.ui @@ -316,7 +316,8 @@ - + + @@ -2961,19 +2962,19 @@ Acts on currently active editable layer Add circle from 3 tangents - + true - - :/images/themes/default/mActionRectangle3Points.svg:/images/themes/default/mActionRectangle3Points.svg + + :/images/themes/default/mActionRectangle3PointsDistance.svg:/images/themes/default/mActionRectangle3PointsDistance.svg - Add Rectangle &from 3 Points + Add Rectangle &from 3 Points (Distance from 2nd and 3rd point) - Add rectangle from 3 points + Add rectangle from 3 points (Distance from 2nd and 3rd point) @@ -3112,6 +3113,21 @@ Acts on currently active editable layer Vertex Tool (Current Layer) + + + true + + + + :/images/themes/default/mActionRectangle3PointsProjected.svg:/images/themes/default/mActionRectangle3PointsProjected.svg + + + Add Rectangle &from 3 Points (Distance from projected point on segment p1 and p2) + + + Add rectangle from 3 points (Distance from projected point on segment p1 and p2) + + diff --git a/tests/src/app/testqgsmaptoolrectangle.cpp b/tests/src/app/testqgsmaptoolrectangle.cpp index 6ec71730a98..3e7830e8a77 100644 --- a/tests/src/app/testqgsmaptoolrectangle.cpp +++ b/tests/src/app/testqgsmaptoolrectangle.cpp @@ -1,17 +1,17 @@ /*************************************************************************** - testqgsmaptoolrectangle.cpp - --------------------------- - Date : January 2018 - Copyright : (C) 2018 by Paul Blottiere - Email : paul.blottiere@oslandia.com - *************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ + testqgsmaptoolrectangle.cpp + --------------------------- + Date : January 2018 + Copyright : (C) 2018 by Paul Blottiere + Email : paul.blottiere@oslandia.com +*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +***************************************************************************/ #include "qgstest.h" @@ -42,7 +42,8 @@ class TestQgsMapToolRectangle : public QObject void testRectangleFromCenter(); void testRectangleFromExtent(); - void testRectangleFrom3Points(); + void testRectangleFrom3PointsDistance(); + void testRectangleFrom3PointsProjected(); private: QgisApp *mQgisApp = nullptr; @@ -130,12 +131,12 @@ void TestQgsMapToolRectangle::testRectangleFromExtent() QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); } -void TestQgsMapToolRectangle::testRectangleFrom3Points() +void TestQgsMapToolRectangle::testRectangleFrom3PointsDistance() { QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 ); mLayer->startEditing(); - QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas ); + QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::DistanceMode ); mCanvas->setMapTool( &mapTool ); TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool ); @@ -156,5 +157,30 @@ void TestQgsMapToolRectangle::testRectangleFrom3Points() QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); } +void TestQgsMapToolRectangle::testRectangleFrom3PointsProjected() +{ + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 ); + mLayer->startEditing(); + + QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::ProjectedMode ); + mCanvas->setMapTool( &mapTool ); + + TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool ); + utils.mouseClick( 0, 0, Qt::LeftButton ); + utils.mouseMove( 2, 0 ); + utils.mouseClick( 2, 0, Qt::LeftButton ); + utils.mouseMove( 2, 1 ); + utils.mouseClick( 2, 1, Qt::RightButton ); + QgsFeatureId newFid = utils.newFeatureId(); + + QCOMPARE( mLayer->featureCount(), ( long )1 ); + QgsFeature f = mLayer->getFeature( newFid ); + + QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)"; + QCOMPARE( f.geometry().asWkt( 0 ), wkt ); + + mLayer->rollBack(); + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); +} QGSTEST_MAIN( TestQgsMapToolRectangle ) #include "testqgsmaptoolrectangle.moc"