Skip to content

Commit c1260f2

Browse files
committedSep 10, 2021
Add unit tests for moving annotation item nodes
1 parent a85c8cb commit c1260f2

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
 

‎tests/src/app/testqgsmaptooleditannotation.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class TestQgsMapToolEditAnnotation : public QObject
4848
void testSelectItem();
4949
void testDeleteItem();
5050
void testMoveItem();
51+
void testMoveNode();
5152

5253
};
5354

@@ -314,6 +315,109 @@ void TestQgsMapToolEditAnnotation::testMoveItem()
314315
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt( 1 ), QStringLiteral( "Polygon ((0.9 1, 4.9 1, 4.9 5, 0.9 5, 0.9 1))" ) );
315316
}
316317

318+
void TestQgsMapToolEditAnnotation::testMoveNode()
319+
{
320+
QgsProject::instance()->clear();
321+
QgsMapCanvas canvas;
322+
canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
323+
canvas.setFrameStyle( QFrame::NoFrame );
324+
canvas.resize( 600, 600 );
325+
canvas.setExtent( QgsRectangle( 0, 0, 10, 10 ) );
326+
canvas.show(); // to make the canvas resize
327+
328+
QgsAnnotationLayer *layer = new QgsAnnotationLayer( QStringLiteral( "test" ), QgsAnnotationLayer::LayerOptions( QgsProject::instance()->transformContext() ) );
329+
QVERIFY( layer->isValid() );
330+
QgsProject::instance()->addMapLayers( { layer } );
331+
332+
QgsAnnotationPolygonItem *item1 = new QgsAnnotationPolygonItem( new QgsPolygon( new QgsLineString( QVector<QgsPoint> { QgsPoint( 1, 1 ), QgsPoint( 5, 1 ), QgsPoint( 5, 5 ), QgsPoint( 1, 5 ), QgsPoint( 1, 1 ) } ) ) );
333+
item1->setZIndex( 1 );
334+
const QString i1id = layer->addItem( item1 );
335+
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt(), QStringLiteral( "Polygon ((1 1, 5 1, 5 5, 1 5, 1 1))" ) );
336+
337+
layer->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
338+
339+
canvas.setLayers( { layer } );
340+
while ( !canvas.isDrawing() )
341+
{
342+
QgsApplication::processEvents();
343+
}
344+
canvas.waitWhileRendering();
345+
QCOMPARE( canvas.renderedItemResults()->renderedItems().size(), 1 );
346+
347+
QgsAdvancedDigitizingDockWidget cadDock( &canvas );
348+
QgsMapToolModifyAnnotation tool( &canvas, &cadDock );
349+
canvas.setMapTool( &tool );
350+
351+
QSignalSpy spy( &tool, &QgsMapToolModifyAnnotation::itemSelected );
352+
TestQgsMapToolUtils utils( &tool );
353+
354+
// click on item
355+
utils.mouseMove( 1.5, 1.5 );
356+
utils.mouseClick( 1.5, 1.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
357+
QCOMPARE( spy.count(), 1 );
358+
QCOMPARE( spy.at( 0 ).at( 1 ).toString(), i1id );
359+
360+
// click on a node
361+
utils.mouseMove( 5, 5 );
362+
utils.mouseClick( 5, 5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
363+
// second click isn't selecting an item, so no new signals should be emitted
364+
QCOMPARE( spy.count(), 1 );
365+
366+
// move mouse and click to end node move
367+
utils.mouseMove( 4.5, 4.5 );
368+
utils.mouseClick( 4.5, 4.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
369+
while ( !canvas.isDrawing() )
370+
{
371+
QgsApplication::processEvents();
372+
}
373+
canvas.waitWhileRendering();
374+
QCOMPARE( canvas.renderedItemResults()->renderedItems().size(), 1 );
375+
376+
// check that item was moved
377+
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt(), QStringLiteral( "Polygon ((1 1, 5 1, 4.5 4.5, 1 5, 1 1))" ) );
378+
379+
// start a new move node
380+
// click on item -- it should already be selected, so this will start a new move, not emit the itemSelected signal
381+
utils.mouseMove( 5, 1 );
382+
utils.mouseClick( 5, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
383+
QCOMPARE( spy.count(), 1 );
384+
385+
utils.mouseMove( 5.5, 1.5 );
386+
utils.mouseClick( 5.5, 1.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
387+
while ( !canvas.isDrawing() )
388+
{
389+
QgsApplication::processEvents();
390+
}
391+
canvas.waitWhileRendering();
392+
QCOMPARE( canvas.renderedItemResults()->renderedItems().size(), 1 );
393+
394+
// check that item was moved
395+
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt( 1 ), QStringLiteral( "Polygon ((1 1, 5.5 1.5, 4.5 4.5, 1 5, 1 1))" ) );
396+
397+
// start a move then cancel it via right click
398+
utils.mouseMove( 4.5, 4.5 );
399+
utils.mouseClick( 4.5, 4.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
400+
QCOMPARE( spy.count(), 1 );
401+
402+
utils.mouseMove( 4.9, 4.9 );
403+
utils.mouseClick( 4.9, 4.9, Qt::RightButton, Qt::KeyboardModifiers(), true );
404+
// check that node was NOT moved
405+
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt( 1 ), QStringLiteral( "Polygon ((1 1, 5.5 1.5, 4.5 4.5, 1 5, 1 1))" ) );
406+
407+
// cancel a move via escape key
408+
utils.mouseMove( 4.5, 4.5 );
409+
utils.mouseClick( 4.5, 4.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
410+
QCOMPARE( spy.count(), 1 );
411+
412+
utils.mouseMove( 4.9, 4.9 );
413+
// escape should cancel
414+
utils.keyClick( Qt::Key_Escape );
415+
//... so next click is not "finish move", but "clear selection"
416+
utils.mouseClick( 6.5, 6.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
417+
// check that node was NOT moved
418+
QCOMPARE( qgis::down_cast< QgsAnnotationPolygonItem * >( layer->item( i1id ) )->geometry()->asWkt( 1 ), QStringLiteral( "Polygon ((1 1, 5.5 1.5, 4.5 4.5, 1 5, 1 1))" ) );
419+
}
420+
317421

318422
QGSTEST_MAIN( TestQgsMapToolEditAnnotation )
319423
#include "testqgsmaptooleditannotation.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.