@@ -50,6 +50,7 @@ class TestQgsLayoutModel : public QObject
50
50
void reorderDown (); // test reordering an item down
51
51
void reorderTop (); // test reordering an item to top
52
52
void reorderBottom (); // test reordering an item to bottom
53
+ void moveItem (); // test move an item in the item tree
53
54
void findItemAbove (); // test getting composer item above
54
55
void findItemBelow (); // test getting composer item below
55
56
void setItemRemoved (); // test setting an item as removed
@@ -560,6 +561,60 @@ void TestQgsLayoutModel::reorderBottom()
560
561
delete label;
561
562
}
562
563
564
+ void TestQgsLayoutModel::moveItem ()
565
+ {
566
+ QgsLayout layout ( QgsProject::instance () );
567
+
568
+ // some items in layout
569
+ QgsLayoutItem *item1 = new QgsLayoutItemMap ( &layout );
570
+ layout.addLayoutItem ( item1 );
571
+ item1->setId ( QStringLiteral ( " i1" ) );
572
+ QgsLayoutItem *item2 = new QgsLayoutItemMap ( &layout );
573
+ layout.addLayoutItem ( item2 );
574
+ item2->setId ( QStringLiteral ( " i2" ) );
575
+ QgsLayoutItem *item3 = new QgsLayoutItemMap ( &layout );
576
+ layout.addLayoutItem ( item3 );
577
+ item3->setId ( QStringLiteral ( " i3" ) );
578
+
579
+ // start with an empty model
580
+ layout.itemsModel ()->clear ();
581
+
582
+ layout.itemsModel ()->rebuildZList ();
583
+
584
+ // check z list
585
+ QCOMPARE ( layout.itemsModel ()->zOrderListSize (), 3 );
586
+ QCOMPARE ( layout.itemsModel ()->zOrderList ().at ( 0 ), item3 );
587
+ QCOMPARE ( layout.itemsModel ()->zOrderList ().at ( 1 ), item2 );
588
+ QCOMPARE ( layout.itemsModel ()->zOrderList ().at ( 2 ), item1 );
589
+
590
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 0 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QString () );
591
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 1 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i3" ) );
592
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 2 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i2" ) );
593
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 3 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i1" ) );
594
+
595
+ QgsLayoutModel *model = layout.itemsModel ();
596
+ QMimeData *mimedata = model->mimeData ( QModelIndexList () << model->index ( 2 , 2 ) ); // get i2
597
+ model->dropMimeData ( mimedata, Qt::MoveAction, 1 , 2 , QModelIndex () ); // move i2 at the top
598
+
599
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 1 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i2" ) );
600
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 2 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i3" ) );
601
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 3 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i1" ) );
602
+
603
+ mimedata = model->mimeData ( QModelIndexList () << model->index ( 1 , 2 ) ); // get i2
604
+ model->dropMimeData ( mimedata, Qt::MoveAction, -1 , -1 , QModelIndex () ); // move i2 at the bottom
605
+
606
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 1 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i3" ) );
607
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 2 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i1" ) );
608
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 3 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i2" ) );
609
+
610
+ mimedata = model->mimeData ( QModelIndexList () << model->index ( 3 , 2 ) ); // get i2
611
+ model->dropMimeData ( mimedata, Qt::MoveAction, 2 , 2 , QModelIndex () ); // move i2 between i3 and i1
612
+
613
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 1 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i3" ) );
614
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 2 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i2" ) );
615
+ QCOMPARE ( layout.itemsModel ()->data ( layout.itemsModel ()->index ( 3 , 2 , QModelIndex () ), Qt::DisplayRole ).toString (), QStringLiteral ( " i1" ) );
616
+ }
617
+
563
618
void TestQgsLayoutModel::findItemAbove ()
564
619
{
565
620
QgsLayout layout ( QgsProject::instance () );
0 commit comments