@@ -48,6 +48,7 @@ class TestQgsMapToolEditAnnotation : public QObject
48
48
void testSelectItem ();
49
49
void testDeleteItem ();
50
50
void testMoveItem ();
51
+ void testMoveNode ();
51
52
52
53
};
53
54
@@ -314,6 +315,109 @@ void TestQgsMapToolEditAnnotation::testMoveItem()
314
315
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))" ) );
315
316
}
316
317
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
+
317
421
318
422
QGSTEST_MAIN ( TestQgsMapToolEditAnnotation )
319
423
#include " testqgsmaptooleditannotation.moc"
0 commit comments