Skip to content

Commit d40f884

Browse files
committedNov 13, 2018
Add map tool rectangle 3 points where 3rd point is projected on segment p1-p2
1 parent 7cce9b3 commit d40f884

File tree

9 files changed

+262
-76
lines changed

9 files changed

+262
-76
lines changed
 

‎images/images.qrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@
671671
<file>themes/default/mActionEllipseCenterPoint.svg</file>
672672
<file>themes/default/mActionEllipseExtent.svg</file>
673673
<file>themes/default/mActionRectangleExtent.svg</file>
674-
<file>themes/default/mActionRectangle3Points.svg</file>
674+
<file>themes/default/mActionRectangle3PointsDistance.svg</file>
675+
<file>themes/default/mActionRectangle3PointsProjected.svg</file>
675676
<file>themes/default/mActionRectangleCenter.svg</file>
676677
<file>themes/default/mActionRegularPolygonCenterCorner.svg</file>
677678
<file>themes/default/mActionRegularPolygon2Points.svg</file>
Lines changed: 122 additions & 0 deletions
Loading

‎src/app/qgisapp.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/***************************************************************************
2-
qgisapp.cpp - description
3-
-------------------
2+
qgisapp.cpp - description
3+
-------------------
44

5-
begin : Sat Jun 22 2002
6-
copyright : (C) 2002 by Gary E.Sherman
7-
email : sherman at mrcc.com
8-
Romans 3:23=>Romans 6:23=>Romans 10:9,10=>Romans 12
9-
***************************************************************************/
5+
begin : Sat Jun 22 2002
6+
copyright : (C) 2002 by Gary E.Sherman
7+
email : sherman at mrcc.com
8+
Romans 3:23=>Romans 6:23=>Romans 10:9,10=>Romans 12
9+
***************************************************************************/
1010

1111
/***************************************************************************
1212
* *
@@ -1530,7 +1530,8 @@ QgisApp::~QgisApp()
15301530
delete mMapTools.mEllipseFoci;
15311531
delete mMapTools.mRectangleCenterPoint;
15321532
delete mMapTools.mRectangleExtent;
1533-
delete mMapTools.mRectangle3Points;
1533+
delete mMapTools.mRectangle3PointsDistance;
1534+
delete mMapTools.mRectangle3PointsProjected;
15341535
delete mMapTools.mRegularPolygon2Points;
15351536
delete mMapTools.mRegularPolygonCenterPoint;
15361537
delete mMapTools.mRegularPolygonCenterCorner;
@@ -2090,7 +2091,8 @@ void QgisApp::createActions()
20902091
connect( mActionEllipseFoci, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mEllipseFoci, true ); } );
20912092
connect( mActionRectangleCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleCenterPoint, true ); } );
20922093
connect( mActionRectangleExtent, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleExtent, true ); } );
2093-
connect( mActionRectangle3Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3Points, true ); } );
2094+
connect( mActionRectangle3PointsDistance, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsDistance, true ); } );
2095+
connect( mActionRectangle3PointsProjected, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsProjected, true ); } );
20942096
connect( mActionRegularPolygon2Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygon2Points, true ); } );
20952097
connect( mActionRegularPolygonCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterPoint, true ); } );
20962098
connect( mActionRegularPolygonCenterCorner, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterCorner, true ); } );
@@ -2375,7 +2377,8 @@ void QgisApp::createActionGroups()
23752377
mMapToolGroup->addAction( mActionEllipseFoci );
23762378
mMapToolGroup->addAction( mActionRectangleCenterPoint );
23772379
mMapToolGroup->addAction( mActionRectangleExtent );
2378-
mMapToolGroup->addAction( mActionRectangle3Points );
2380+
mMapToolGroup->addAction( mActionRectangle3PointsDistance );
2381+
mMapToolGroup->addAction( mActionRectangle3PointsProjected );
23792382
mMapToolGroup->addAction( mActionRegularPolygon2Points );
23802383
mMapToolGroup->addAction( mActionRegularPolygonCenterPoint );
23812384
mMapToolGroup->addAction( mActionRegularPolygonCenterCorner );
@@ -2934,7 +2937,8 @@ void QgisApp::createToolBars()
29342937
tbAddRectangle->setPopupMode( QToolButton::MenuButtonPopup );
29352938
tbAddRectangle->addAction( mActionRectangleCenterPoint );
29362939
tbAddRectangle->addAction( mActionRectangleExtent );
2937-
tbAddRectangle->addAction( mActionRectangle3Points );
2940+
tbAddRectangle->addAction( mActionRectangle3PointsDistance );
2941+
tbAddRectangle->addAction( mActionRectangle3PointsProjected );
29382942
tbAddRectangle->setDefaultAction( mActionRectangleCenterPoint );
29392943
connect( tbAddRectangle, &QToolButton::triggered, this, &QgisApp::toolButtonActionTriggered );
29402944
mShapeDigitizeToolBar->insertWidget( mActionVertexTool, tbAddRectangle );
@@ -3574,8 +3578,10 @@ void QgisApp::createCanvasTools()
35743578
mMapTools.mRectangleCenterPoint->setAction( mActionRectangleCenterPoint );
35753579
mMapTools.mRectangleExtent = new QgsMapToolRectangleExtent( mMapTools.mAddFeature, mMapCanvas );
35763580
mMapTools.mRectangleExtent->setAction( mActionRectangleExtent );
3577-
mMapTools.mRectangle3Points = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas );
3578-
mMapTools.mRectangle3Points->setAction( mActionRectangle3Points );
3581+
mMapTools.mRectangle3PointsDistance = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::DistanceMode );
3582+
mMapTools.mRectangle3PointsDistance->setAction( mActionRectangle3PointsDistance );
3583+
mMapTools.mRectangle3PointsProjected = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::ProjectedMode );
3584+
mMapTools.mRectangle3PointsProjected->setAction( mActionRectangle3PointsProjected );
35793585
mMapTools.mRegularPolygon2Points = new QgsMapToolRegularPolygon2Points( mMapTools.mAddFeature, mMapCanvas );
35803586
mMapTools.mRegularPolygon2Points->setAction( mActionRegularPolygon2Points );
35813587
mMapTools.mRegularPolygonCenterPoint = new QgsMapToolRegularPolygonCenterPoint( mMapTools.mAddFeature, mMapCanvas );
@@ -12532,7 +12538,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
1253212538
mActionEllipseFoci->setEnabled( enableShapeTools );
1253312539
mActionRectangleCenterPoint->setEnabled( enableShapeTools );
1253412540
mActionRectangleExtent->setEnabled( enableShapeTools );
12535-
mActionRectangle3Points->setEnabled( enableShapeTools );
12541+
mActionRectangle3PointsDistance->setEnabled( enableShapeTools );
12542+
mActionRectangle3PointsProjected->setEnabled( enableShapeTools );
1253612543
mActionRegularPolygon2Points->setEnabled( enableShapeTools );
1253712544
mActionRegularPolygonCenterPoint->setEnabled( enableShapeTools );
1253812545
mActionRegularPolygonCenterCorner->setEnabled( enableShapeTools );

‎src/app/qgisapp.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************
2-
qgisapp.h - description
3-
-------------------
4-
begin : Sat Jun 22 2002
5-
copyright : (C) 2002 by Gary E.Sherman
6-
email : sherman at mrcc.com
7-
***************************************************************************/
2+
qgisapp.h - description
3+
-------------------
4+
begin : Sat Jun 22 2002
5+
copyright : (C) 2002 by Gary E.Sherman
6+
email : sherman at mrcc.com
7+
***************************************************************************/
88

99
/***************************************************************************
1010
* *
@@ -2044,7 +2044,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
20442044
QgsMapTool *mEllipseFoci = nullptr;
20452045
QgsMapTool *mRectangleCenterPoint = nullptr;
20462046
QgsMapTool *mRectangleExtent = nullptr;
2047-
QgsMapTool *mRectangle3Points = nullptr;
2047+
QgsMapTool *mRectangle3PointsDistance = nullptr;
2048+
QgsMapTool *mRectangle3PointsProjected = nullptr;
20482049
QgsMapTool *mRegularPolygon2Points = nullptr;
20492050
QgsMapTool *mRegularPolygonCenterPoint = nullptr;
20502051
QgsMapTool *mRegularPolygonCenterCorner = nullptr;

‎src/app/qgsmaptoolrectangle3points.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/***************************************************************************
2-
qgsmaptoolrectangle3points.cpp - map tool for adding rectangle
3-
from 3 points
4-
---------------------
5-
begin : September 2017
6-
copyright : (C) 2017 by Loïc Bartoletti
7-
email : lbartoletti at tuxfamily dot org
8-
***************************************************************************
9-
* *
10-
* This program is free software; you can redistribute it and/or modify *
11-
* it under the terms of the GNU General Public License as published by *
12-
* the Free Software Foundation; either version 3 of the License, or *
13-
* (at your option) any later version. *
14-
* *
15-
***************************************************************************/
2+
qgsmaptoolrectangle3points.cpp - map tool for adding rectangle
3+
from 3 points
4+
---------------------
5+
begin : September 2017
6+
copyright : (C) 2017 by Loïc Bartoletti
7+
email : lbartoletti at tuxfamily dot org
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 3 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
1616

1717
#include "qgsmaptoolrectangle3points.h"
1818
#include "qgsgeometryrubberband.h"
@@ -25,8 +25,9 @@
2525
#include "qgssnapindicator.h"
2626

2727
QgsMapToolRectangle3Points::QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool,
28-
QgsMapCanvas *canvas, CaptureMode mode )
29-
: QgsMapToolAddRectangle( parentTool, canvas, mode )
28+
QgsMapCanvas *canvas, CreateMode createMode, CaptureMode mode )
29+
: QgsMapToolAddRectangle( parentTool, canvas, mode ),
30+
mCreateMode( createMode )
3031
{
3132
}
3233

@@ -77,7 +78,11 @@ void QgsMapToolRectangle3Points::cadCanvasMoveEvent( QgsMapMouseEvent *e )
7778
break;
7879
case 2:
7980
{
80-
setDistance2( mPoints.at( 1 ).distance( point ) );
81+
if ( mCreateMode == DistanceMode )
82+
setDistance2( mPoints.at( 1 ).distance( point ) );
83+
else if ( mCreateMode == ProjectedMode )
84+
setDistance2( QgsGeometryUtils::perpendicularSegment( point, mPoints.at( 0 ), mPoints.at( 1 ) ).length() );
85+
8186
int side = QgsGeometryUtils::leftOfLine( point.x(), point.y(),
8287
mPoints.at( 0 ).x(), mPoints.at( 0 ).y(),
8388
mPoints.at( 1 ).x(), mPoints.at( 1 ).y() );

‎src/app/qgsmaptoolrectangle3points.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/***************************************************************************
2-
qgsmaptoolrectangle3points.h - map tool for adding rectangle
3-
from 3 points
4-
---------------------
5-
begin : September 2017
6-
copyright : (C) 2017 by Loïc Bartoletti
7-
email : lbartoletti at tuxfamily dot org
8-
***************************************************************************
9-
* *
10-
* This program is free software; you can redistribute it and/or modify *
11-
* it under the terms of the GNU General Public License as published by *
12-
* the Free Software Foundation; either version 3 of the License, or *
13-
* (at your option) any later version. *
14-
* *
15-
***************************************************************************/
2+
qgsmaptoolrectangle3points.h - map tool for adding rectangle
3+
from 3 points
4+
---------------------
5+
begin : September 2017
6+
copyright : (C) 2017 by Loïc Bartoletti
7+
email : lbartoletti at tuxfamily dot org
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 3 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
1616

1717
#ifndef QGSMAPTOOLRECTANGLE3POINTS_H
1818
#define QGSMAPTOOLRECTANGLE3POINTS_H
@@ -25,11 +25,19 @@ class APP_EXPORT QgsMapToolRectangle3Points: public QgsMapToolAddRectangle
2525
Q_OBJECT
2626

2727
public:
28-
QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CaptureMode mode = CaptureLine );
28+
enum CreateMode
29+
{
30+
DistanceMode,
31+
ProjectedMode,
32+
};
33+
QgsMapToolRectangle3Points( QgsMapToolCapture *parentTool, QgsMapCanvas *canvas, CreateMode createMode, CaptureMode mode = CaptureLine );
2934

3035
void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;
3136
void cadCanvasMoveEvent( QgsMapMouseEvent *e ) override;
3237

38+
private:
39+
CreateMode mCreateMode;
40+
3341
};
3442

3543
#endif // QGSMAPTOOLRECTANGLE3POINTS_H

‎src/ui/qgisapp.ui

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@
315315
</property>
316316
<addaction name="mActionRectangleExtent"/>
317317
<addaction name="mActionRectangleCenterPoint"/>
318-
<addaction name="mActionRectangle3Points"/>
318+
<addaction name="mActionRectangle3PointsDistance"/>
319+
<addaction name="mActionRectangle3PointsProjected"/>
319320
</widget>
320321
<widget class="QMenu" name="mMenuRegularPolygon">
321322
<property name="title">
@@ -2945,19 +2946,19 @@ Acts on currently active editable layer</string>
29452946
<string>Add circle from 3 tangents</string>
29462947
</property>
29472948
</action>
2948-
<action name="mActionRectangle3Points">
2949+
<action name="mActionRectangle3PointsDistance">
29492950
<property name="checkable">
29502951
<bool>true</bool>
29512952
</property>
29522953
<property name="icon">
2953-
<iconset resource="../../images/images.qrc">
2954-
<normaloff>:/images/themes/default/mActionRectangle3Points.svg</normaloff>:/images/themes/default/mActionRectangle3Points.svg</iconset>
2954+
<iconset>
2955+
<normaloff>:/images/themes/default/mActionRectangle3PointsDistance.svg</normaloff>:/images/themes/default/mActionRectangle3PointsDistance.svg</iconset>
29552956
</property>
29562957
<property name="text">
2957-
<string>Add Rectangle &amp;from 3 Points</string>
2958+
<string>Add Rectangle &amp;from 3 Points (Distance from 2nd and 3rd point)</string>
29582959
</property>
29592960
<property name="toolTip">
2960-
<string>Add rectangle from 3 points</string>
2961+
<string>Add rectangle from 3 points (Distance from 2nd and 3rd point)</string>
29612962
</property>
29622963
</action>
29632964
<action name="mActionCircle2TangentsPoint">
@@ -3096,6 +3097,21 @@ Acts on currently active editable layer</string>
30963097
<string>Vertex Tool (Current Layer)</string>
30973098
</property>
30983099
</action>
3100+
<action name="mActionRectangle3PointsProjected">
3101+
<property name="checkable">
3102+
<bool>true</bool>
3103+
</property>
3104+
<property name="icon">
3105+
<iconset>
3106+
<normaloff>:/images/themes/default/mActionRectangle3PointsProjected.svg</normaloff>:/images/themes/default/mActionRectangle3PointsProjected.svg</iconset>
3107+
</property>
3108+
<property name="text">
3109+
<string>Add Rectangle &amp;from 3 Points (Distance from projected point on segment p1 and p2)</string>
3110+
</property>
3111+
<property name="toolTip">
3112+
<string>Add rectangle from 3 points (Distance from projected point on segment p1 and p2)</string>
3113+
</property>
3114+
</action>
30993115
</widget>
31003116
<resources>
31013117
<include location="../../images/images.qrc"/>

‎tests/src/app/testqgsmaptoolrectangle.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/***************************************************************************
2-
testqgsmaptoolrectangle.cpp
3-
---------------------------
4-
Date : January 2018
5-
Copyright : (C) 2018 by Paul Blottiere
6-
Email : paul.blottiere@oslandia.com
7-
***************************************************************************
8-
* *
9-
* This program is free software; you can redistribute it and/or modify *
10-
* it under the terms of the GNU General Public License as published by *
11-
* the Free Software Foundation; either version 2 of the License, or *
12-
* (at your option) any later version. *
13-
* *
14-
***************************************************************************/
2+
testqgsmaptoolrectangle.cpp
3+
---------------------------
4+
Date : January 2018
5+
Copyright : (C) 2018 by Paul Blottiere
6+
Email : paul.blottiere@oslandia.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
1515

1616
#include "qgstest.h"
1717

@@ -42,7 +42,8 @@ class TestQgsMapToolRectangle : public QObject
4242

4343
void testRectangleFromCenter();
4444
void testRectangleFromExtent();
45-
void testRectangleFrom3Points();
45+
void testRectangleFrom3PointsDistance();
46+
void testRectangleFrom3PointsProjected();
4647

4748
private:
4849
QgisApp *mQgisApp = nullptr;
@@ -130,12 +131,12 @@ void TestQgsMapToolRectangle::testRectangleFromExtent()
130131
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
131132
}
132133

133-
void TestQgsMapToolRectangle::testRectangleFrom3Points()
134+
void TestQgsMapToolRectangle::testRectangleFrom3PointsDistance()
134135
{
135136
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
136137
mLayer->startEditing();
137138

138-
QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas );
139+
QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::DistanceMode );
139140
mCanvas->setMapTool( &mapTool );
140141

141142
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
@@ -156,5 +157,30 @@ void TestQgsMapToolRectangle::testRectangleFrom3Points()
156157
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
157158
}
158159

160+
void TestQgsMapToolRectangle::testRectangleFrom3PointsProjected()
161+
{
162+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
163+
mLayer->startEditing();
164+
165+
QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::ProjectedMode );
166+
mCanvas->setMapTool( &mapTool );
167+
168+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
169+
utils.mouseClick( 0, 0, Qt::LeftButton );
170+
utils.mouseMove( 2, 0 );
171+
utils.mouseClick( 2, 0, Qt::LeftButton );
172+
utils.mouseMove( 2, 1 );
173+
utils.mouseClick( 2, 1, Qt::RightButton );
174+
QgsFeatureId newFid = utils.newFeatureId();
175+
176+
QCOMPARE( mLayer->featureCount(), ( long )1 );
177+
QgsFeature f = mLayer->getFeature( newFid );
178+
179+
QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
180+
QCOMPARE( f.geometry().asWkt( 0 ), wkt );
181+
182+
mLayer->rollBack();
183+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
184+
}
159185
QGSTEST_MAIN( TestQgsMapToolRectangle )
160186
#include "testqgsmaptoolrectangle.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.