Skip to content
Permalink
Browse files

Close vertex for line when editing. fixes #32359 [needs-docs]

  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 3, 2020
1 parent 3d3e1a7 commit 6fe51c42070bdf2c900435a0c7ab637e3bb8a5e4
@@ -32,6 +32,8 @@ QgsMapToolDigitizeFeature is a map tool to digitize a feature geometry
:param mode: type of geometry to capture (point/line/polygon), QgsMapToolCapture.CaptureNone to autodetect geometry
%End

virtual void keyPressEvent( QKeyEvent *e );

virtual void cadCanvasReleaseEvent( QgsMapMouseEvent *e );


@@ -92,6 +92,40 @@ void QgsMapToolDigitizeFeature::setCheckGeometryType( bool checkGeometryType )
mCheckGeometryType = checkGeometryType;
}

void QgsMapToolDigitizeFeature::keyPressEvent( QKeyEvent *e )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
if ( !vlayer )
//if no given layer take the current from canvas
vlayer = currentVectorLayer();

if ( !vlayer )
{
notifyNotVectorLayer();
return;
}

QgsVectorDataProvider *provider = vlayer->dataProvider();

if ( !( provider->capabilities() & QgsVectorDataProvider::AddFeatures ) )
{
emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::Warning );
return;
}

if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
return;
}

if ( e && e->key() == Qt::Key_C && mode() == CaptureLine && vlayer->geometryType() == QgsWkbTypes::LineGeometry && mCheckGeometryType )
{
closePolygon();
QgsMapMouseEvent e2( mCanvas, QEvent::MouseButtonRelease, QPoint( ), Qt::RightButton );
cadCanvasReleaseEvent( &e2 );
}
}
void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
@@ -42,6 +42,7 @@ class GUI_EXPORT QgsMapToolDigitizeFeature : public QgsMapToolCapture
*/
QgsMapToolDigitizeFeature( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode = QgsMapToolCapture::CaptureNone );

void keyPressEvent( QKeyEvent *e ) override;
void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;

/**
@@ -68,6 +68,7 @@ class TestQgsMapToolAddFeatureLine : public QObject
void testZ();
void testZMSnapping();
void testTopologicalEditingZ();
void testCloseLine();

private:
QgisApp *mQgisApp = nullptr;
@@ -80,6 +81,7 @@ class TestQgsMapToolAddFeatureLine : public QObject
QgsVectorLayer *mLayerPointZM = nullptr;
QgsVectorLayer *mLayerTopoZ = nullptr;
QgsVectorLayer *mLayerLine2D = nullptr;
QgsVectorLayer *mLayerCloseLine = nullptr;
QgsFeatureId mFidLineF1 = 0;
};

@@ -137,6 +139,11 @@ void TestQgsMapToolAddFeatureLine::initTestCase()
mLayerLineZ->addFeature( lineF2 );
QCOMPARE( mLayerLineZ->featureCount(), ( long )1 );

mLayerCloseLine = new QgsVectorLayer( QStringLiteral( "LineString?crs=EPSG:27700" ), QStringLiteral( "layer line Closed" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerCloseLine->isValid() );
mLayerCloseLine->startEditing();
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerCloseLine );

mCanvas->setFrameStyle( QFrame::NoFrame );
mCanvas->resize( 512, 512 );
mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) );
@@ -482,5 +489,23 @@ void TestQgsMapToolAddFeatureLine::testTopologicalEditingZ()
mCanvas->snappingUtils()->setConfig( cfg );
cfg.project()->setTopologicalEditing( topologicalEditing );
}

void TestQgsMapToolAddFeatureLine::testCloseLine()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );

mCanvas->setCurrentLayer( mLayerCloseLine );
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
utils.mouseClick( 1, 1, Qt::LeftButton );
utils.mouseClick( 5, 1, Qt::LeftButton );
utils.mouseClick( 5, 5, Qt::LeftButton );
utils.keyClick( Qt::Key_C );
QgsFeatureId newFid = utils.newFeatureId( oldFids );

QString wkt = "LineString (1 1, 5 1, 5 5, 1 1)";
QCOMPARE( mLayerCloseLine->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( wkt ) );

mLayerCloseLine->undoStack()->undo();
}
QGSTEST_MAIN( TestQgsMapToolAddFeatureLine )
#include "testqgsmaptooladdfeatureline.moc"

0 comments on commit 6fe51c4

Please sign in to comment.
You can’t perform that action at this time.