Skip to content

Commit

Permalink
[vertex tool] add new vertex without moving it (shift+double click)
Browse files Browse the repository at this point in the history
Obviously this is something that some people find useful, but because
it is not so common it uses shift modifier.
  • Loading branch information
wonder-sk authored and nirvn committed Feb 15, 2019
1 parent 9195314 commit 64482ae
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/app/vertextool/qgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ void QgsVertexTool::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
// so we need to cancel edge moving before we start dragging new vertex
stopDragging();
startDraggingAddVertex( m );

if ( e->modifiers() & Qt::ShiftModifier )
{
// if this was shift + double click, immediately place the vertex
moveVertex( m.point(), &m );
// force update of rubber bands
mouseMoveNotDragging( e );
}
}
else if ( mSelectionRect )
{
Expand Down
78 changes: 78 additions & 0 deletions tests/src/app/testqgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TestQgsVertexTool : public QObject
void testMoveEdge();
void testAddVertex();
void testAddVertexAtEndpoint();
void testAddVertexDoubleClick();
void testAddVertexDoubleClickWithShift();
void testDeleteVertex();
void testMoveMultipleVertices();
void testMoveMultipleVertices2();
Expand Down Expand Up @@ -102,6 +104,18 @@ class TestQgsVertexTool : public QObject
mouseRelease( mapX, mapY, button, stateKey );
}

void mouseDoubleClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
{
// this is how Qt passes the events: 1. mouse press, 2. mouse release, 3. mouse double-click, 4. mouse release

mouseClick( mapX, mapY, button, stateKey );

QgsMapMouseEvent e( mCanvas, QEvent::MouseButtonDblClick, mapToScreen( mapX, mapY ), button, button, stateKey );
mVertexTool->canvasDoubleClickEvent( &e );

mouseRelease( mapX, mapY, button, stateKey );
}

void keyClick( int key )
{
QKeyEvent e1( QEvent::KeyPress, key, Qt::KeyboardModifiers() );
Expand Down Expand Up @@ -397,6 +411,70 @@ void TestQgsVertexTool::testAddVertexAtEndpoint()
QCOMPARE( mLayerLine->getFeature( mFidLineF1 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(2 1, 1 1, 1 3)" ) );
}

void TestQgsVertexTool::testAddVertexDoubleClick()
{
// add vertex in linestring with double-click and then place the point to the new location

mouseDoubleClick( 1, 1.5, Qt::LeftButton );
mouseClick( 2, 2, Qt::LeftButton );

QCOMPARE( mLayerLine->undoStack()->index(), 2 );
QCOMPARE( mLayerLine->getFeature( mFidLineF1 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(2 1, 1 1, 2 2, 1 3)" ) );

mLayerLine->undoStack()->undo();
QCOMPARE( mLayerLine->undoStack()->index(), 1 );

QCOMPARE( mLayerLine->getFeature( mFidLineF1 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(2 1, 1 1, 1 3)" ) );

// add vertex in polygon
mouseDoubleClick( 4, 2, Qt::LeftButton );
mouseClick( 3, 2.5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 3 2.5, 4 1))" ) );

mLayerPolygon->undoStack()->undo();

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 4 1))" ) );

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );

}

void TestQgsVertexTool::testAddVertexDoubleClickWithShift()
{
// add vertex in linestring with shift + double-click to immediately place the new vertex

mouseDoubleClick( 1, 1.5, Qt::LeftButton, Qt::ShiftModifier );

QCOMPARE( mLayerLine->undoStack()->index(), 2 );
QCOMPARE( mLayerLine->getFeature( mFidLineF1 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(2 1, 1 1, 1 1.5, 1 3)" ) );

mLayerLine->undoStack()->undo();
QCOMPARE( mLayerLine->undoStack()->index(), 1 );

QCOMPARE( mLayerLine->getFeature( mFidLineF1 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(2 1, 1 1, 1 3)" ) );

// add vertex in polygon
mouseDoubleClick( 4, 2, Qt::LeftButton, Qt::ShiftModifier );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 4 2, 4 1))" ) );

mLayerPolygon->undoStack()->undo();

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 4 1))" ) );

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );

}


void TestQgsVertexTool::testDeleteVertex()
{
Expand Down

0 comments on commit 64482ae

Please sign in to comment.