Skip to content

Commit b089b57

Browse files
authoredNov 19, 2018
Merge pull request #8480 from lbartoletti/rectangle3PointsProjected
[FEATURE][needs-docs]Add map tool: rectangle 3 points (projected)
2 parents 29875d3 + 2c12146 commit b089b57

File tree

9 files changed

+266
-76
lines changed

9 files changed

+266
-76
lines changed
 

‎images/images.qrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@
673673
<file>themes/default/mActionEllipseCenterPoint.svg</file>
674674
<file>themes/default/mActionEllipseExtent.svg</file>
675675
<file>themes/default/mActionRectangleExtent.svg</file>
676-
<file>themes/default/mActionRectangle3Points.svg</file>
676+
<file>themes/default/mActionRectangle3PointsDistance.svg</file>
677+
<file>themes/default/mActionRectangle3PointsProjected.svg</file>
677678
<file>themes/default/mActionRectangleCenter.svg</file>
678679
<file>themes/default/mActionRegularPolygonCenterCorner.svg</file>
679680
<file>themes/default/mActionRegularPolygon2Points.svg</file>
Lines changed: 122 additions & 0 deletions

Error rendering embedded code

Invalid image source.

‎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
* *
@@ -1531,7 +1531,8 @@ QgisApp::~QgisApp()
15311531
delete mMapTools.mEllipseFoci;
15321532
delete mMapTools.mRectangleCenterPoint;
15331533
delete mMapTools.mRectangleExtent;
1534-
delete mMapTools.mRectangle3Points;
1534+
delete mMapTools.mRectangle3PointsDistance;
1535+
delete mMapTools.mRectangle3PointsProjected;
15351536
delete mMapTools.mRegularPolygon2Points;
15361537
delete mMapTools.mRegularPolygonCenterPoint;
15371538
delete mMapTools.mRegularPolygonCenterCorner;
@@ -2091,7 +2092,8 @@ void QgisApp::createActions()
20912092
connect( mActionEllipseFoci, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mEllipseFoci, true ); } );
20922093
connect( mActionRectangleCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleCenterPoint, true ); } );
20932094
connect( mActionRectangleExtent, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangleExtent, true ); } );
2094-
connect( mActionRectangle3Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3Points, true ); } );
2095+
connect( mActionRectangle3PointsDistance, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsDistance, true ); } );
2096+
connect( mActionRectangle3PointsProjected, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRectangle3PointsProjected, true ); } );
20952097
connect( mActionRegularPolygon2Points, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygon2Points, true ); } );
20962098
connect( mActionRegularPolygonCenterPoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterPoint, true ); } );
20972099
connect( mActionRegularPolygonCenterCorner, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mRegularPolygonCenterCorner, true ); } );
@@ -2376,7 +2378,8 @@ void QgisApp::createActionGroups()
23762378
mMapToolGroup->addAction( mActionEllipseFoci );
23772379
mMapToolGroup->addAction( mActionRectangleCenterPoint );
23782380
mMapToolGroup->addAction( mActionRectangleExtent );
2379-
mMapToolGroup->addAction( mActionRectangle3Points );
2381+
mMapToolGroup->addAction( mActionRectangle3PointsDistance );
2382+
mMapToolGroup->addAction( mActionRectangle3PointsProjected );
23802383
mMapToolGroup->addAction( mActionRegularPolygon2Points );
23812384
mMapToolGroup->addAction( mActionRegularPolygonCenterPoint );
23822385
mMapToolGroup->addAction( mActionRegularPolygonCenterCorner );
@@ -2935,7 +2938,8 @@ void QgisApp::createToolBars()
29352938
tbAddRectangle->setPopupMode( QToolButton::MenuButtonPopup );
29362939
tbAddRectangle->addAction( mActionRectangleCenterPoint );
29372940
tbAddRectangle->addAction( mActionRectangleExtent );
2938-
tbAddRectangle->addAction( mActionRectangle3Points );
2941+
tbAddRectangle->addAction( mActionRectangle3PointsDistance );
2942+
tbAddRectangle->addAction( mActionRectangle3PointsProjected );
29392943
tbAddRectangle->setDefaultAction( mActionRectangleCenterPoint );
29402944
connect( tbAddRectangle, &QToolButton::triggered, this, &QgisApp::toolButtonActionTriggered );
29412945
mShapeDigitizeToolBar->insertWidget( mActionVertexTool, tbAddRectangle );
@@ -3576,8 +3580,10 @@ void QgisApp::createCanvasTools()
35763580
mMapTools.mRectangleCenterPoint->setAction( mActionRectangleCenterPoint );
35773581
mMapTools.mRectangleExtent = new QgsMapToolRectangleExtent( mMapTools.mAddFeature, mMapCanvas );
35783582
mMapTools.mRectangleExtent->setAction( mActionRectangleExtent );
3579-
mMapTools.mRectangle3Points = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas );
3580-
mMapTools.mRectangle3Points->setAction( mActionRectangle3Points );
3583+
mMapTools.mRectangle3PointsDistance = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::DistanceMode );
3584+
mMapTools.mRectangle3PointsDistance->setAction( mActionRectangle3PointsDistance );
3585+
mMapTools.mRectangle3PointsProjected = new QgsMapToolRectangle3Points( mMapTools.mAddFeature, mMapCanvas, QgsMapToolRectangle3Points::ProjectedMode );
3586+
mMapTools.mRectangle3PointsProjected->setAction( mActionRectangle3PointsProjected );
35813587
mMapTools.mRegularPolygon2Points = new QgsMapToolRegularPolygon2Points( mMapTools.mAddFeature, mMapCanvas );
35823588
mMapTools.mRegularPolygon2Points->setAction( mActionRegularPolygon2Points );
35833589
mMapTools.mRegularPolygonCenterPoint = new QgsMapToolRegularPolygonCenterPoint( mMapTools.mAddFeature, mMapCanvas );
@@ -12543,7 +12549,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
1254312549
mActionEllipseFoci->setEnabled( enableShapeTools );
1254412550
mActionRectangleCenterPoint->setEnabled( enableShapeTools );
1254512551
mActionRectangleExtent->setEnabled( enableShapeTools );
12546-
mActionRectangle3Points->setEnabled( enableShapeTools );
12552+
mActionRectangle3PointsDistance->setEnabled( enableShapeTools );
12553+
mActionRectangle3PointsProjected->setEnabled( enableShapeTools );
1254712554
mActionRegularPolygon2Points->setEnabled( enableShapeTools );
1254812555
mActionRegularPolygonCenterPoint->setEnabled( enableShapeTools );
1254912556
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: 26 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,15 @@ void QgsMapToolRectangle3Points::cadCanvasMoveEvent( QgsMapMouseEvent *e )
7778
break;
7879
case 2:
7980
{
80-
setDistance2( mPoints.at( 1 ).distance( point ) );
81+
switch ( mCreateMode )
82+
{
83+
case DistanceMode:
84+
setDistance2( mPoints.at( 1 ).distance( point ) );
85+
break;
86+
case ProjectedMode:
87+
setDistance2( QgsGeometryUtils::perpendicularSegment( point, mPoints.at( 0 ), mPoints.at( 1 ) ).length() );
88+
break;
89+
}
8190
int side = QgsGeometryUtils::leftOfLine( point.x(), point.y(),
8291
mPoints.at( 0 ).x(), mPoints.at( 0 ).y(),
8392
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
@@ -316,7 +316,8 @@
316316
</property>
317317
<addaction name="mActionRectangleExtent"/>
318318
<addaction name="mActionRectangleCenterPoint"/>
319-
<addaction name="mActionRectangle3Points"/>
319+
<addaction name="mActionRectangle3PointsDistance"/>
320+
<addaction name="mActionRectangle3PointsProjected"/>
320321
</widget>
321322
<widget class="QMenu" name="mMenuRegularPolygon">
322323
<property name="title">
@@ -2961,19 +2962,19 @@ Acts on currently active editable layer</string>
29612962
<string>Add circle from 3 tangents</string>
29622963
</property>
29632964
</action>
2964-
<action name="mActionRectangle3Points">
2965+
<action name="mActionRectangle3PointsDistance">
29652966
<property name="checkable">
29662967
<bool>true</bool>
29672968
</property>
29682969
<property name="icon">
2969-
<iconset resource="../../images/images.qrc">
2970-
<normaloff>:/images/themes/default/mActionRectangle3Points.svg</normaloff>:/images/themes/default/mActionRectangle3Points.svg</iconset>
2970+
<iconset>
2971+
<normaloff>:/images/themes/default/mActionRectangle3PointsDistance.svg</normaloff>:/images/themes/default/mActionRectangle3PointsDistance.svg</iconset>
29712972
</property>
29722973
<property name="text">
2973-
<string>Add Rectangle &amp;from 3 Points</string>
2974+
<string>Add Rectangle &amp;from 3 Points (Distance from 2nd and 3rd point)</string>
29742975
</property>
29752976
<property name="toolTip">
2976-
<string>Add rectangle from 3 points</string>
2977+
<string>Add rectangle from 3 points (Distance from 2nd and 3rd point)</string>
29772978
</property>
29782979
</action>
29792980
<action name="mActionCircle2TangentsPoint">
@@ -3112,6 +3113,21 @@ Acts on currently active editable layer</string>
31123113
<string>Vertex Tool (Current Layer)</string>
31133114
</property>
31143115
</action>
3116+
<action name="mActionRectangle3PointsProjected">
3117+
<property name="checkable">
3118+
<bool>true</bool>
3119+
</property>
3120+
<property name="icon">
3121+
<iconset>
3122+
<normaloff>:/images/themes/default/mActionRectangle3PointsProjected.svg</normaloff>:/images/themes/default/mActionRectangle3PointsProjected.svg</iconset>
3123+
</property>
3124+
<property name="text">
3125+
<string>Add Rectangle &amp;from 3 Points (Distance from projected point on segment p1 and p2)</string>
3126+
</property>
3127+
<property name="toolTip">
3128+
<string>Add rectangle from 3 points (Distance from projected point on segment p1 and p2)</string>
3129+
</property>
3130+
</action>
31153131
</widget>
31163132
<resources>
31173133
<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.