Navigation Menu

Skip to content

Commit

Permalink
Fix layout order on drag drop action, fix #39136
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Oct 8, 2020
1 parent f6c42a7 commit 2207342
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/core/layout/qgslayoutmodel.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1034,4 +1034,3 @@ bool QgsLayoutProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &so

return true;
}

55 changes: 55 additions & 0 deletions tests/src/core/testqgslayoutmodel.cpp
Expand Up @@ -50,6 +50,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
Expand Down Expand Up @@ -560,6 +561,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() );
Expand Down

0 comments on commit 2207342

Please sign in to comment.