Skip to content

Commit

Permalink
Add tests for changing item z order
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent e74a632 commit fdba8f1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 27 deletions.
31 changes: 27 additions & 4 deletions python/core/layout/qgslayout.sip
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,57 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
.. seealso:: lockSelectedItems()
%End

bool raiseItem( QgsLayoutItem *item );
bool raiseItem( QgsLayoutItem *item, bool deferUpdate = false );
%Docstring
Raises an ``item`` up the z-order.
Returns true if the item was successfully raised.

If ``deferUpdate`` is true, the scene will not be visibly updated
to reflect the new stacking order. This allows multiple
raiseItem() calls to be made in sequence without the cost of
updating the scene for each one.

.. seealso:: lowerItem()
:rtype: bool
%End

bool lowerItem( QgsLayoutItem *item );
bool lowerItem( QgsLayoutItem *item, bool deferUpdate = false );
%Docstring
Lowers an ``item`` down the z-order.
Returns true if the item was successfully lowered.

If ``deferUpdate`` is true, the scene will not be visibly updated
to reflect the new stacking order. This allows multiple
raiseItem() calls to be made in sequence without the cost of
updating the scene for each one.

.. seealso:: raiseItem()
:rtype: bool
%End

bool moveItemToTop( QgsLayoutItem *item );
bool moveItemToTop( QgsLayoutItem *item, bool deferUpdate = false );
%Docstring
Raises an ``item`` up to the top of the z-order.
Returns true if the item was successfully raised.

If ``deferUpdate`` is true, the scene will not be visibly updated
to reflect the new stacking order. This allows multiple
raiseItem() calls to be made in sequence without the cost of
updating the scene for each one.

.. seealso:: moveItemToBottom()
:rtype: bool
%End

bool moveItemToBottom( QgsLayoutItem *item );
bool moveItemToBottom( QgsLayoutItem *item, bool deferUpdate = false );
%Docstring
Lowers an ``item`` down to the bottom of the z-order.
Returns true if the item was successfully lowered.
If ``deferUpdate`` is true, the scene will not be visibly updated
to reflect the new stacking order. This allows multiple
raiseItem() calls to be made in sequence without the cost of
updating the scene for each one.

.. seealso:: moveItemToTop()
:rtype: bool
%End
Expand Down
52 changes: 40 additions & 12 deletions src/core/layout/qgslayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,56 @@ void QgsLayout::unlockAllItems()
mUndoStack->endMacro();
}

bool QgsLayout::raiseItem( QgsLayoutItem *item )
bool QgsLayout::raiseItem( QgsLayoutItem *item, bool deferUpdate )
{
//model handles reordering items
return mItemsModel->reorderItemUp( item );
bool result = mItemsModel->reorderItemUp( item );
if ( result && !deferUpdate )
{
//update all positions
updateZValues();
update();
}
return result;
}

bool QgsLayout::lowerItem( QgsLayoutItem *item )
bool QgsLayout::lowerItem( QgsLayoutItem *item, bool deferUpdate )
{
//model handles reordering items
return mItemsModel->reorderItemDown( item );
bool result = mItemsModel->reorderItemDown( item );
if ( result && !deferUpdate )
{
//update all positions
updateZValues();
update();
}
return result;
}

bool QgsLayout::moveItemToTop( QgsLayoutItem *item )
bool QgsLayout::moveItemToTop( QgsLayoutItem *item, bool deferUpdate )
{
//model handles reordering items
return mItemsModel->reorderItemToTop( item );
bool result = mItemsModel->reorderItemToTop( item );
if ( result && !deferUpdate )
{
//update all positions
updateZValues();
update();
}
return result;
}

bool QgsLayout::moveItemToBottom( QgsLayoutItem *item )
bool QgsLayout::moveItemToBottom( QgsLayoutItem *item, bool deferUpdate )
{
//model handles reordering items
return mItemsModel->reorderItemToBottom( item );
bool result = mItemsModel->reorderItemToBottom( item );
if ( result && !deferUpdate )
{
//update all positions
updateZValues();
update();
}
return result;
}

void QgsLayout::raiseSelectedItems()
Expand All @@ -184,7 +212,7 @@ void QgsLayout::raiseSelectedItems()
bool itemsRaised = false;
for ( QgsLayoutItem *item : selectedItems )
{
itemsRaised = itemsRaised | raiseItem( item );
itemsRaised = itemsRaised | raiseItem( item, true );
}

if ( !itemsRaised )
Expand All @@ -204,7 +232,7 @@ void QgsLayout::lowerSelectedItems()
bool itemsLowered = false;
for ( QgsLayoutItem *item : selectedItems )
{
itemsLowered = itemsLowered | lowerItem( item );
itemsLowered = itemsLowered | lowerItem( item, true );
}

if ( !itemsLowered )
Expand All @@ -224,7 +252,7 @@ void QgsLayout::moveSelectedItemsToTop()
bool itemsRaised = false;
for ( QgsLayoutItem *item : selectedItems )
{
itemsRaised = itemsRaised | moveItemToTop( item );
itemsRaised = itemsRaised | moveItemToTop( item, true );
}

if ( !itemsRaised )
Expand All @@ -244,7 +272,7 @@ void QgsLayout::moveSelectedItemsToBottom()
bool itemsLowered = false;
for ( QgsLayoutItem *item : selectedItems )
{
itemsLowered = itemsLowered | moveItemToBottom( item );
itemsLowered = itemsLowered | moveItemToBottom( item, true );
}

if ( !itemsLowered )
Expand Down
31 changes: 27 additions & 4 deletions src/core/layout/qgslayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,53 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
/**
* Raises an \a item up the z-order.
* Returns true if the item was successfully raised.
*
* If \a deferUpdate is true, the scene will not be visibly updated
* to reflect the new stacking order. This allows multiple
* raiseItem() calls to be made in sequence without the cost of
* updating the scene for each one.
*
* \see lowerItem()
*/
bool raiseItem( QgsLayoutItem *item );
bool raiseItem( QgsLayoutItem *item, bool deferUpdate = false );

/**
* Lowers an \a item down the z-order.
* Returns true if the item was successfully lowered.
*
* If \a deferUpdate is true, the scene will not be visibly updated
* to reflect the new stacking order. This allows multiple
* raiseItem() calls to be made in sequence without the cost of
* updating the scene for each one.
*
* \see raiseItem()
*/
bool lowerItem( QgsLayoutItem *item );
bool lowerItem( QgsLayoutItem *item, bool deferUpdate = false );

/**
* Raises an \a item up to the top of the z-order.
* Returns true if the item was successfully raised.
*
* If \a deferUpdate is true, the scene will not be visibly updated
* to reflect the new stacking order. This allows multiple
* raiseItem() calls to be made in sequence without the cost of
* updating the scene for each one.
*
* \see moveItemToBottom()
*/
bool moveItemToTop( QgsLayoutItem *item );
bool moveItemToTop( QgsLayoutItem *item, bool deferUpdate = false );

/**
* Lowers an \a item down to the bottom of the z-order.
* Returns true if the item was successfully lowered.
* If \a deferUpdate is true, the scene will not be visibly updated
* to reflect the new stacking order. This allows multiple
* raiseItem() calls to be made in sequence without the cost of
* updating the scene for each one.
*
* \see moveItemToTop()
*/
bool moveItemToBottom( QgsLayoutItem *item );
bool moveItemToBottom( QgsLayoutItem *item, bool deferUpdate = false );

/**
* Raises the selected items up the z-order.
Expand Down
71 changes: 64 additions & 7 deletions tests/src/python/test_qgslayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ def testStacking(self):

# add some items
item1 = QgsLayoutItemMap(l)
l.addItem(item1)
l.addLayoutItem(item1)
item2 = QgsLayoutItemMap(l)
l.addItem(item2)
l.addLayoutItem(item2)
item3 = QgsLayoutItemMap(l)
l.addItem(item3)
l.addLayoutItem(item3)

self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 2)
Expand All @@ -240,17 +240,74 @@ def testStacking(self):
self.assertEqual(item3.zValue(), 3)

# raising
l.setSelectedItem(item3)
l.raiseSelectedItems()
self.assertFalse(l.raiseItem(item3))
self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 2)
self.assertEqual(item3.zValue(), 3)
l.setSelectedItem(item2)
l.raiseSelectedItems()

self.assertTrue(l.raiseItem(item2))
self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 2)

self.assertFalse(l.raiseItem(item2))
self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 2)

self.assertTrue(l.raiseItem(item1))
self.assertEqual(item1.zValue(), 2)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 1)

# lower
self.assertFalse(l.lowerItem(item3))
self.assertEqual(item1.zValue(), 2)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 1)

self.assertTrue(l.lowerItem(item2))
self.assertEqual(item1.zValue(), 3)
self.assertEqual(item2.zValue(), 2)
self.assertEqual(item3.zValue(), 1)

self.assertTrue(l.lowerItem(item2))
self.assertEqual(item1.zValue(), 3)
self.assertEqual(item2.zValue(), 1)
self.assertEqual(item3.zValue(), 2)

# raise to top
self.assertFalse(l.moveItemToTop(item1))
self.assertEqual(item1.zValue(), 3)
self.assertEqual(item2.zValue(), 1)
self.assertEqual(item3.zValue(), 2)

self.assertTrue(l.moveItemToTop(item3))
self.assertEqual(item1.zValue(), 2)
self.assertEqual(item2.zValue(), 1)
self.assertEqual(item3.zValue(), 3)

self.assertTrue(l.moveItemToTop(item2))
self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 2)

# move to bottom
self.assertFalse(l.moveItemToBottom(item1))
self.assertEqual(item1.zValue(), 1)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 2)

self.assertTrue(l.moveItemToBottom(item3))
self.assertEqual(item1.zValue(), 2)
self.assertEqual(item2.zValue(), 3)
self.assertEqual(item3.zValue(), 1)

self.assertTrue(l.moveItemToBottom(item2))
self.assertEqual(item1.zValue(), 3)
self.assertEqual(item2.zValue(), 1)
self.assertEqual(item3.zValue(), 2)


if __name__ == '__main__':
unittest.main()

0 comments on commit fdba8f1

Please sign in to comment.