22
22
#include " qgstest.h"
23
23
#include " qgsproject.h"
24
24
#include " qgsreadwritecontext.h"
25
+ #include " qgslayoutitemundocommand.h"
26
+ #include " qgslayoutitemmap.h"
27
+ #include " qgslayoutitemshape.h"
25
28
#include < QObject>
26
29
#include < QPainter>
27
30
#include < QImage>
@@ -144,6 +147,7 @@ class TestQgsLayoutItem: public QObject
144
147
void writeXml ();
145
148
void readXml ();
146
149
void writeReadXmlProperties ();
150
+ void undoRedo ();
147
151
148
152
private:
149
153
@@ -1324,6 +1328,12 @@ void TestQgsLayoutItem::writeReadXmlProperties()
1324
1328
original->setLocked ( true );
1325
1329
original->setZValue ( 55 );
1326
1330
original->setVisible ( false );
1331
+ original->setFrameEnabled ( true );
1332
+ original->setFrameStrokeColor ( QColor ( 100 , 150 , 200 ) );
1333
+ original->setFrameStrokeWidth ( QgsLayoutMeasurement ( 5 , QgsUnitTypes::LayoutCentimeters ) );
1334
+ original->setFrameJoinStyle ( Qt::MiterJoin );
1335
+ original->setBackgroundEnabled ( false );
1336
+ original->setBackgroundColor ( QColor ( 200 , 150 , 100 ) );
1327
1337
1328
1338
QgsLayoutItem *copy = createCopyViaXml ( &l, original );
1329
1339
@@ -1340,11 +1350,111 @@ void TestQgsLayoutItem::writeReadXmlProperties()
1340
1350
QVERIFY ( copy->isLocked () );
1341
1351
QCOMPARE ( copy->zValue (), 55.0 );
1342
1352
QVERIFY ( !copy->isVisible () );
1353
+ QVERIFY ( copy->hasFrame () );
1354
+ QCOMPARE ( copy->frameStrokeColor (), QColor ( 100 , 150 , 200 ) );
1355
+ QCOMPARE ( copy->frameStrokeWidth (), QgsLayoutMeasurement ( 5 , QgsUnitTypes::LayoutCentimeters ) );
1356
+ QCOMPARE ( copy->frameJoinStyle (), Qt::MiterJoin );
1357
+ QVERIFY ( !copy->hasBackground () );
1358
+ QCOMPARE ( copy->backgroundColor (), QColor ( 200 , 150 , 100 ) );
1343
1359
1344
1360
delete copy;
1345
1361
delete original;
1346
1362
}
1347
1363
1364
+ void TestQgsLayoutItem::undoRedo ()
1365
+ {
1366
+ QgsProject proj;
1367
+ QgsLayout l ( &proj );
1368
+
1369
+ QgsLayoutItemRectangularShape *item = new QgsLayoutItemRectangularShape ( &l );
1370
+ QString uuid = item->uuid ();
1371
+ QPointer< QgsLayoutItemRectangularShape > pItem ( item ); // for testing deletion
1372
+ item->setFrameStrokeColor ( QColor ( 255 , 100 , 200 ) );
1373
+ l.addLayoutItem ( item );
1374
+
1375
+ l.undoStack ()->stack ()->push ( new QgsLayoutItemAddItemCommand ( item, QString () ) );
1376
+ QVERIFY ( pItem );
1377
+ QVERIFY ( l.items ().contains ( item ) );
1378
+ QCOMPARE ( l.itemByUuid ( uuid ), item );
1379
+
1380
+ // undo should delete item
1381
+ l.undoStack ()->stack ()->undo ();
1382
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1383
+ QVERIFY ( !pItem );
1384
+ QVERIFY ( !l.items ().contains ( item ) );
1385
+ QVERIFY ( !l.itemByUuid ( uuid ) );
1386
+
1387
+ // redo should restore
1388
+ l.undoStack ()->stack ()->redo ();
1389
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1390
+ item = dynamic_cast < QgsLayoutItemRectangularShape * >( l.itemByUuid ( uuid ) );
1391
+ QVERIFY ( item );
1392
+ QVERIFY ( l.items ().contains ( item ) );
1393
+ pItem = item;
1394
+ QCOMPARE ( item->frameStrokeColor ().name (), QColor ( 255 , 100 , 200 ).name () );
1395
+
1396
+ // ... and repeat!
1397
+
1398
+ // undo should delete item
1399
+ l.undoStack ()->stack ()->undo ();
1400
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1401
+ QVERIFY ( !pItem );
1402
+ QVERIFY ( !l.items ().contains ( item ) );
1403
+ QVERIFY ( !l.itemByUuid ( uuid ) );
1404
+
1405
+ // redo should restore
1406
+ l.undoStack ()->stack ()->redo ();
1407
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1408
+ item = dynamic_cast < QgsLayoutItemRectangularShape * >( l.itemByUuid ( uuid ) );
1409
+ QVERIFY ( item );
1410
+ QVERIFY ( l.items ().contains ( item ) );
1411
+ pItem = item;
1412
+ QCOMPARE ( item->frameStrokeColor ().name (), QColor ( 255 , 100 , 200 ).name () );
1413
+
1414
+ // delete item
1415
+ QgsLayoutItemDeleteUndoCommand *deleteCommand = new QgsLayoutItemDeleteUndoCommand ( item, QString () );
1416
+ l.removeLayoutItem ( item );
1417
+ l.undoStack ()->stack ()->push ( deleteCommand );
1418
+
1419
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1420
+ QVERIFY ( !pItem );
1421
+ QVERIFY ( !l.items ().contains ( item ) );
1422
+ QVERIFY ( !l.itemByUuid ( uuid ) );
1423
+
1424
+ // undo should restore
1425
+ l.undoStack ()->stack ()->undo ();
1426
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1427
+ item = dynamic_cast < QgsLayoutItemRectangularShape * >( l.itemByUuid ( uuid ) );
1428
+ QVERIFY ( item );
1429
+ QVERIFY ( l.items ().contains ( item ) );
1430
+ pItem = item;
1431
+ QCOMPARE ( item->frameStrokeColor ().name (), QColor ( 255 , 100 , 200 ).name () );
1432
+
1433
+ // another undo should delete item
1434
+ l.undoStack ()->stack ()->undo ();
1435
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1436
+ QVERIFY ( !pItem );
1437
+ QVERIFY ( !l.items ().contains ( item ) );
1438
+ QVERIFY ( !l.itemByUuid ( uuid ) );
1439
+
1440
+ // redo should restore
1441
+ l.undoStack ()->stack ()->redo ();
1442
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1443
+ item = dynamic_cast < QgsLayoutItemRectangularShape * >( l.itemByUuid ( uuid ) );
1444
+ QVERIFY ( item );
1445
+ QVERIFY ( l.items ().contains ( item ) );
1446
+ pItem = item;
1447
+ QCOMPARE ( item->frameStrokeColor ().name (), QColor ( 255 , 100 , 200 ).name () );
1448
+
1449
+ // another redo should delete item
1450
+ l.undoStack ()->stack ()->redo ();
1451
+ QgsApplication::sendPostedEvents ( nullptr , QEvent::DeferredDelete );
1452
+ QVERIFY ( !pItem );
1453
+ QVERIFY ( !l.items ().contains ( item ) );
1454
+ QVERIFY ( !l.itemByUuid ( uuid ) );
1455
+
1456
+ }
1457
+
1348
1458
QgsLayoutItem *TestQgsLayoutItem::createCopyViaXml ( QgsLayout *layout, QgsLayoutItem *original )
1349
1459
{
1350
1460
// save original item to xml
0 commit comments