Skip to content

Commit ba02a5e

Browse files
authoredApr 26, 2018
Merge pull request #6851 from pblottiere/bugfix_segfault_z
[bugfix] Fixes Add Feature map tool when snapped layer has ZM support
2 parents f816588 + 07b07ab commit ba02a5e

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed
 

‎src/gui/qgsmaptoolcapture.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,20 @@ int QgsMapToolCapture::fetchLayerPoint( const QgsPointLocator::Match &match, Qgs
399399
QgsVertexId vId;
400400
if ( !f.geometry().vertexIdFromVertexNr( match.vertexIndex(), vId ) )
401401
return 2;
402+
402403
layerPoint = f.geometry().constGet()->vertexAt( vId );
404+
405+
// ZM support depends on the target layer
406+
if ( !QgsWkbTypes::hasZ( vlayer->wkbType() ) )
407+
{
408+
layerPoint.dropZValue();
409+
}
410+
411+
if ( !QgsWkbTypes::hasM( vlayer->wkbType() ) )
412+
{
413+
layerPoint.dropMValue();
414+
}
415+
403416
return 0;
404417
}
405418
else

‎tests/src/app/testqgsmaptooladdfeature.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsgeometry.h"
2121
#include "qgsmapcanvas.h"
2222
#include "qgsmapcanvassnappingutils.h"
23+
#include "qgssnappingconfig.h"
2324
#include "qgsmaptooladdfeature.h"
2425
#include "qgsmapcanvastracer.h"
2526
#include "qgsproject.h"
@@ -65,6 +66,7 @@ class TestQgsMapToolAddFeature : public QObject
6566
void testTracing();
6667
void testTracingWithOffset();
6768
void testZ();
69+
void testZMSnapping();
6870

6971
private:
7072
QgisApp *mQgisApp = nullptr;
@@ -74,6 +76,7 @@ class TestQgsMapToolAddFeature : public QObject
7476
QgsMapToolAddFeature *mCaptureTool = nullptr;
7577
QgsVectorLayer *mLayerLine = nullptr;
7678
QgsVectorLayer *mLayerLineZ = nullptr;
79+
QgsVectorLayer *mLayerPointZM = nullptr;
7780
QgsFeatureId mFidLineF1 = 0;
7881
};
7982

@@ -139,7 +142,20 @@ void TestQgsMapToolAddFeature::initTestCase()
139142
QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) );
140143
QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) );
141144

142-
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerLine << mLayerLineZ ); //<< mLayerPolygon << mLayerPoint );
145+
// make layer for snapping
146+
mLayerPointZM = new QgsVectorLayer( QStringLiteral( "PointZM?crs=EPSG:27700" ), QStringLiteral( "layer point ZM" ), QStringLiteral( "memory" ) );
147+
QVERIFY( mLayerPointZM->isValid() );
148+
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPointZM );
149+
150+
mLayerPointZM->startEditing();
151+
QgsFeature pointF;
152+
QString pointWktZM = "PointZM(6 6 3 4)";
153+
pointF.setGeometry( QgsGeometry::fromWkt( pointWktZM ) );
154+
155+
mLayerPointZM->addFeature( pointF );
156+
QCOMPARE( mLayerPointZM->featureCount(), ( long )1 );
157+
158+
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerLine << mLayerLineZ << mLayerPointZM ); //<< mLayerPolygon << mLayerPoint );
143159

144160
mCanvas->setSnappingUtils( new QgsMapCanvasSnappingUtils( mCanvas, this ) );
145161

@@ -352,6 +368,33 @@ void TestQgsMapToolAddFeature::testZ()
352368
mCanvas->setCurrentLayer( mLayerLine );
353369
}
354370

371+
void TestQgsMapToolAddFeature::testZMSnapping()
372+
{
373+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
374+
375+
mCanvas->setCurrentLayer( mLayerLine );
376+
377+
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
378+
379+
QgsSnappingConfig cfg = mCanvas->snappingUtils()->config();
380+
cfg.setMode( QgsSnappingConfig::AllLayers );
381+
cfg.setEnabled( true );
382+
mCanvas->snappingUtils()->setConfig( cfg );
383+
384+
// snap a on a layer with ZM support
385+
utils.mouseClick( 6, 6, Qt::LeftButton, Qt::KeyboardModifiers(), true );
386+
utils.mouseClick( 5, 0, Qt::LeftButton );
387+
utils.mouseClick( 5, 0, Qt::RightButton );
388+
QgsFeatureId newFid = utils.newFeatureId( oldFids );
389+
390+
QCOMPARE( mLayerLine->getFeature( newFid ).geometry().get()->is3D(), false );
391+
QCOMPARE( mLayerLine->getFeature( newFid ).geometry().get()->isMeasure(), false );
392+
393+
mLayerLine->undoStack()->undo();
394+
395+
cfg.setEnabled( false );
396+
mCanvas->snappingUtils()->setConfig( cfg );
397+
}
355398

356399
QGSTEST_MAIN( TestQgsMapToolAddFeature )
357400
#include "testqgsmaptooladdfeature.moc"

‎tests/src/app/testqgsmaptoolutils.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,30 @@ class TestQgsMapToolAdvancedDigitizingUtils
6969
mMapTool->cadCanvasMoveEvent( &e );
7070
}
7171

72-
void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
72+
void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
7373
{
7474
QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );
75+
76+
if ( snap )
77+
e1.snapPoint();
78+
7579
mMapTool->cadCanvasPressEvent( &e1 );
7680
}
7781

78-
void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
82+
void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
7983
{
8084
QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );
85+
86+
if ( snap )
87+
e2.snapPoint();
88+
8189
mMapTool->cadCanvasReleaseEvent( &e2 );
8290
}
8391

84-
void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
92+
void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
8593
{
86-
mousePress( mapX, mapY, button, stateKey );
87-
mouseRelease( mapX, mapY, button, stateKey );
94+
mousePress( mapX, mapY, button, stateKey, snap );
95+
mouseRelease( mapX, mapY, button, stateKey, snap );
8896
}
8997

9098
void keyClick( int key )

0 commit comments

Comments
 (0)
Please sign in to comment.