Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit edbb2fe

Browse files
committedJun 6, 2019
Fix reshape for snapped point with Z and add test
1 parent e0d04bb commit edbb2fe

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed
 

‎src/app/qgsmaptoolreshape.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ void QgsMapToolReshape::reshape( QgsVectorLayer *vlayer )
121121
bbox.combineExtentWith( points().at( i ).x(), points().at( i ).y() );
122122
}
123123

124-
QgsLineString reshapeLineString( points() );
125-
if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
126-
reshapeLineString.addZValue( defaultZValue() );
124+
125+
QgsPointSequence pts;
126+
QVector<QgsPoint> points;
127+
captureCurve()->points( pts );
128+
QgsLineString reshapeLineString( pts );
127129

128130
//query all the features that intersect bounding box of capture line
129131
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( bbox ).setNoAttributes() );
@@ -143,7 +145,7 @@ void QgsMapToolReshape::reshape( QgsVectorLayer *vlayer )
143145
{
144146
// in case of a binding line, we just want to update the line from
145147
// the starting point and not both side
146-
if ( isBinding && !geom.asPolyline().contains( points().first() ) )
148+
if ( isBinding && !geom.asPolyline().contains( pts.constFirst() ) )
147149
continue;
148150

149151
reshapeReturn = geom.reshapeGeometry( reshapeLineString );

‎tests/src/app/testqgsmaptoolreshape.cpp

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,138 @@
1414
***************************************************************************/
1515

1616
#include "qgstest.h"
17-
#include "qgsapplication.h"
17+
18+
#include "qgisapp.h"
19+
#include "qgsadvanceddigitizingdockwidget.h"
20+
#include "qgsgeometry.h"
1821
#include "qgsmapcanvas.h"
19-
#include "qgsvectorlayer.h"
20-
#include "qgslinestring.h"
22+
#include "qgsmapcanvassnappingutils.h"
23+
#include "qgssnappingconfig.h"
24+
#include "qgssnappingutils.h"
2125
#include "qgsmaptoolreshape.h"
22-
#include "qgisapp.h"
26+
#include "qgsproject.h"
27+
#include "qgssettings.h"
28+
#include "qgsvectorlayer.h"
29+
#include "qgsmapmouseevent.h"
30+
#include "testqgsmaptoolutils.h"
31+
2332

24-
class TestQgsMapToolReshape : public QObject
33+
/**
34+
* \ingroup UnitTests
35+
* This is a unit test for the vertex tool
36+
*/
37+
class TestQgsMapToolReshape: public QObject
2538
{
2639
Q_OBJECT
2740
public:
28-
TestQgsMapToolReshape() = default;
41+
TestQgsMapToolReshape();
2942

3043
private slots:
31-
void initTestCase(); // will be called before the first testfunction is executed.
32-
void cleanupTestCase(); // will be called after the last testfunction was executed.
33-
void init(); // will be called before each testfunction is executed.
34-
void cleanup(); // will be called after every testfunction.
44+
void initTestCase();// will be called before the first testfunction is executed.
45+
void cleanupTestCase();// will be called after the last testfunction was executed.
3546

47+
void testReshapeZ();
3648
void reshapeWithBindingLine();
3749

3850
private:
3951
QgisApp *mQgisApp = nullptr;
52+
QgsMapCanvas *mCanvas = nullptr;
53+
QgsMapToolReshape *mCaptureTool = nullptr;
54+
QgsVectorLayer *mLayerLineZ = nullptr;
4055
};
4156

57+
TestQgsMapToolReshape::TestQgsMapToolReshape() = default;
58+
59+
60+
//runs before all tests
4261
void TestQgsMapToolReshape::initTestCase()
4362
{
63+
qDebug() << "TestMapToolCapture::initTestCase()";
64+
// init QGIS's paths - true means that all path will be inited from prefix
4465
QgsApplication::init();
4566
QgsApplication::initQgis();
4667

47-
// Set up the QgsSettings environment
68+
// Set up the QSettings environment
4869
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
4970
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
5071
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
5172

52-
QgsApplication::showSettings();
73+
mQgisApp = new QgisApp();
5374

54-
// enforce C locale because the tests expect it
55-
// (decimal separators / thousand separators)
56-
QLocale::setDefault( QLocale::c() );
75+
mCanvas = new QgsMapCanvas();
5776

58-
mQgisApp = new QgisApp();
77+
mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3946" ) ) );
78+
79+
mCanvas->setFrameStyle( QFrame::NoFrame );
80+
mCanvas->resize( 512, 512 );
81+
mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) );
82+
mCanvas->show(); // to make the canvas resize
83+
mCanvas->hide();
84+
85+
// make testing layers
86+
mLayerLineZ = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:3946" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
87+
QVERIFY( mLayerLineZ->isValid() );
88+
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerLineZ );
89+
90+
mLayerLineZ->startEditing();
91+
QString wkt1 = "LineStringZ (0 0 0, 1 1 0, 1 2 0)";
92+
QString wkt2 = "LineStringZ (2 1 5, 3 3 5)";
93+
QgsFeature f1;
94+
f1.setGeometry( QgsGeometry::fromWkt( wkt1 ) );
95+
QgsFeature f2;
96+
f2.setGeometry( QgsGeometry::fromWkt( wkt2 ) );
97+
98+
QgsFeatureList flist;
99+
flist << f1 << f2;
100+
mLayerLineZ->dataProvider()->addFeatures( flist );
101+
QCOMPARE( mLayerLineZ->featureCount(), ( long )2 );
102+
QCOMPARE( mLayerLineZ->getFeature( 1 ).geometry().asWkt(), wkt1 );
103+
QCOMPARE( mLayerLineZ->getFeature( 2 ).geometry().asWkt(), wkt2 );
104+
105+
QgsSnappingConfig cfg = mCanvas->snappingUtils()->config();
106+
cfg.setMode( QgsSnappingConfig::AllLayers );
107+
cfg.setTolerance( 100 );
108+
cfg.setType( QgsSnappingConfig::VertexAndSegment );
109+
cfg.setEnabled( true );
110+
mCanvas->snappingUtils()->setConfig( cfg );
111+
112+
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerLineZ );
113+
mCanvas->setCurrentLayer( mLayerLineZ );
114+
115+
// create the tool
116+
mCaptureTool = new QgsMapToolReshape( mCanvas );
117+
mCanvas->setMapTool( mCaptureTool );
118+
119+
QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) );
120+
QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) );
59121
}
60122

123+
//runs after all tests
61124
void TestQgsMapToolReshape::cleanupTestCase()
62125
{
126+
delete mCaptureTool;
127+
delete mCanvas;
63128
QgsApplication::exitQgis();
64129
}
65130

66-
void TestQgsMapToolReshape::init()
131+
void TestQgsMapToolReshape::testReshapeZ()
67132
{
68-
}
133+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
134+
135+
// test with default Z value = 333
136+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
137+
138+
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
139+
140+
utils.mouseClick( 1, 2, Qt::LeftButton, Qt::KeyboardModifiers(), true );
141+
utils.mouseClick( 2, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
142+
utils.mouseClick( 2, 1, Qt::RightButton );
143+
144+
QString wkt = "LineStringZ (0 0 0, 1 1 0, 1 2 0, 2 1 5)";
145+
QCOMPARE( mLayerLineZ->getFeature( 1 ).geometry().asWkt(), wkt );
146+
147+
mLayerLineZ->undoStack()->undo();
69148

70-
void TestQgsMapToolReshape::cleanup()
71-
{
72149
}
73150

74151
void TestQgsMapToolReshape::reshapeWithBindingLine()
@@ -139,5 +216,6 @@ void TestQgsMapToolReshape::reshapeWithBindingLine()
139216
vl->rollBack();
140217
}
141218

219+
142220
QGSTEST_MAIN( TestQgsMapToolReshape )
143221
#include "testqgsmaptoolreshape.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.