Skip to content

Commit 2207342

Browse files
github-actions[bot]nyalldawson
authored andcommittedOct 8, 2020
Fix layout order on drag drop action, fix #39136
1 parent f6c42a7 commit 2207342

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed
 

‎src/core/layout/qgslayoutmodel.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ bool zOrderDescending( QgsLayoutItem *item1, QgsLayoutItem *item2 )
308308
bool QgsLayoutModel::dropMimeData( const QMimeData *data,
309309
Qt::DropAction action, int row, int column, const QModelIndex &parent )
310310
{
311-
if ( column != ItemId )
311+
if ( column != ItemId && column != -1 )
312312
{
313313
return false;
314314
}
@@ -362,7 +362,7 @@ bool QgsLayoutModel::dropMimeData( const QMimeData *data,
362362
int destPos = 0;
363363
if ( beginRow < rowCount() )
364364
{
365-
QgsLayoutItem *itemBefore = mItemsInScene.at( beginRow );
365+
QgsLayoutItem *itemBefore = mItemsInScene.at( beginRow - 1 );
366366
destPos = mItemZList.indexOf( itemBefore );
367367
}
368368
else
@@ -1034,4 +1034,3 @@ bool QgsLayoutProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &so
10341034

10351035
return true;
10361036
}
1037-

‎tests/src/core/testqgslayoutmodel.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class TestQgsLayoutModel : public QObject
5050
void reorderDown(); //test reordering an item down
5151
void reorderTop(); //test reordering an item to top
5252
void reorderBottom(); //test reordering an item to bottom
53+
void moveItem(); //test move an item in the item tree
5354
void findItemAbove(); //test getting composer item above
5455
void findItemBelow(); //test getting composer item below
5556
void setItemRemoved(); //test setting an item as removed
@@ -560,6 +561,60 @@ void TestQgsLayoutModel::reorderBottom()
560561
delete label;
561562
}
562563

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+
563618
void TestQgsLayoutModel::findItemAbove()
564619
{
565620
QgsLayout layout( QgsProject::instance() );

0 commit comments

Comments
 (0)
Please sign in to comment.