Skip to content
Permalink
Browse files

Fix layout order on drag drop action

 fix #39136
  • Loading branch information
SebastienPeillet committed Oct 8, 2020
1 parent 12da1a6 commit ec2cc48451bba5200933b7c0359cac06aa9d99b8
Showing with 57 additions and 3 deletions.
  1. +2 −3 src/core/layout/qgslayoutmodel.cpp
  2. +55 −0 tests/src/core/testqgslayoutmodel.cpp
@@ -308,7 +308,7 @@ bool zOrderDescending( QgsLayoutItem *item1, QgsLayoutItem *item2 )
bool QgsLayoutModel::dropMimeData( const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent )
{
if ( column != ItemId )
if ( column != ItemId && column != -1 )
{
return false;
}
@@ -362,7 +362,7 @@ bool QgsLayoutModel::dropMimeData( const QMimeData *data,
int destPos = 0;
if ( beginRow < rowCount() )
{
QgsLayoutItem *itemBefore = mItemsInScene.at( beginRow );
QgsLayoutItem *itemBefore = mItemsInScene.at( beginRow - 1 );
destPos = mItemZList.indexOf( itemBefore );
}
else
@@ -1050,4 +1050,3 @@ bool QgsLayoutProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &so

return true;
}

@@ -51,6 +51,7 @@ class TestQgsLayoutModel : public QObject
void reorderDown(); //test reordering an item down
void reorderTop(); //test reordering an item to top
void reorderBottom(); //test reordering an item to bottom
void moveItem(); //test move an item in the item tree
void findItemAbove(); //test getting composer item above
void findItemBelow(); //test getting composer item below
void setItemRemoved(); //test setting an item as removed
@@ -561,6 +562,60 @@ void TestQgsLayoutModel::reorderBottom()
delete label;
}

void TestQgsLayoutModel::moveItem()
{
QgsLayout layout( QgsProject::instance() );

//some items in layout
QgsLayoutItem *item1 = new QgsLayoutItemMap( &layout );
layout.addLayoutItem( item1 );
item1->setId( QStringLiteral( "i1" ) );
QgsLayoutItem *item2 = new QgsLayoutItemMap( &layout );
layout.addLayoutItem( item2 );
item2->setId( QStringLiteral( "i2" ) );
QgsLayoutItem *item3 = new QgsLayoutItemMap( &layout );
layout.addLayoutItem( item3 );
item3->setId( QStringLiteral( "i3" ) );

// start with an empty model
layout.itemsModel()->clear();

layout.itemsModel()->rebuildZList();

//check z list
QCOMPARE( layout.itemsModel()->zOrderListSize(), 3 );
QCOMPARE( layout.itemsModel()->zOrderList().at( 0 ), item3 );
QCOMPARE( layout.itemsModel()->zOrderList().at( 1 ), item2 );
QCOMPARE( layout.itemsModel()->zOrderList().at( 2 ), item1 );

QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 0, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QString() );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 1, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i3" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 2, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i2" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 3, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i1" ) );

QgsLayoutModel *model = layout.itemsModel();
QMimeData *mimedata = model->mimeData( QModelIndexList() << model->index( 2, 2 ) ); // get i2
model->dropMimeData( mimedata, Qt::MoveAction, 1, 2, QModelIndex() ); // move i2 at the top

QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 1, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i2" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 2, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i3" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 3, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i1" ) );

mimedata = model->mimeData( QModelIndexList() << model->index( 1, 2 ) ); // get i2
model->dropMimeData( mimedata, Qt::MoveAction, -1, -1, QModelIndex() ); // move i2 at the bottom

QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 1, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i3" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 2, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i1" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 3, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i2" ) );

mimedata = model->mimeData( QModelIndexList() << model->index( 3, 2 ) ); // get i2
model->dropMimeData( mimedata, Qt::MoveAction, 2, 2, QModelIndex() ); // move i2 between i3 and i1

QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 1, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i3" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 2, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i2" ) );
QCOMPARE( layout.itemsModel()->data( layout.itemsModel()->index( 3, 2, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "i1" ) );
}

void TestQgsLayoutModel::findItemAbove()
{
QgsLayout layout( QgsProject::instance() );

0 comments on commit ec2cc48

Please sign in to comment.
You can’t perform that action at this time.