Skip to content

Commit fdba8f1

Browse files
committedOct 6, 2017
Add tests for changing item z order
1 parent e74a632 commit fdba8f1

File tree

4 files changed

+158
-27
lines changed

4 files changed

+158
-27
lines changed
 

‎python/core/layout/qgslayout.sip

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,57 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
111111
.. seealso:: lockSelectedItems()
112112
%End
113113

114-
bool raiseItem( QgsLayoutItem *item );
114+
bool raiseItem( QgsLayoutItem *item, bool deferUpdate = false );
115115
%Docstring
116116
Raises an ``item`` up the z-order.
117117
Returns true if the item was successfully raised.
118+
119+
If ``deferUpdate`` is true, the scene will not be visibly updated
120+
to reflect the new stacking order. This allows multiple
121+
raiseItem() calls to be made in sequence without the cost of
122+
updating the scene for each one.
123+
118124
.. seealso:: lowerItem()
119125
:rtype: bool
120126
%End
121127

122-
bool lowerItem( QgsLayoutItem *item );
128+
bool lowerItem( QgsLayoutItem *item, bool deferUpdate = false );
123129
%Docstring
124130
Lowers an ``item`` down the z-order.
125131
Returns true if the item was successfully lowered.
132+
133+
If ``deferUpdate`` is true, the scene will not be visibly updated
134+
to reflect the new stacking order. This allows multiple
135+
raiseItem() calls to be made in sequence without the cost of
136+
updating the scene for each one.
137+
126138
.. seealso:: raiseItem()
127139
:rtype: bool
128140
%End
129141

130-
bool moveItemToTop( QgsLayoutItem *item );
142+
bool moveItemToTop( QgsLayoutItem *item, bool deferUpdate = false );
131143
%Docstring
132144
Raises an ``item`` up to the top of the z-order.
133145
Returns true if the item was successfully raised.
146+
147+
If ``deferUpdate`` is true, the scene will not be visibly updated
148+
to reflect the new stacking order. This allows multiple
149+
raiseItem() calls to be made in sequence without the cost of
150+
updating the scene for each one.
151+
134152
.. seealso:: moveItemToBottom()
135153
:rtype: bool
136154
%End
137155

138-
bool moveItemToBottom( QgsLayoutItem *item );
156+
bool moveItemToBottom( QgsLayoutItem *item, bool deferUpdate = false );
139157
%Docstring
140158
Lowers an ``item`` down to the bottom of the z-order.
141159
Returns true if the item was successfully lowered.
160+
If ``deferUpdate`` is true, the scene will not be visibly updated
161+
to reflect the new stacking order. This allows multiple
162+
raiseItem() calls to be made in sequence without the cost of
163+
updating the scene for each one.
164+
142165
.. seealso:: moveItemToTop()
143166
:rtype: bool
144167
%End

‎src/core/layout/qgslayout.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,28 +154,56 @@ void QgsLayout::unlockAllItems()
154154
mUndoStack->endMacro();
155155
}
156156

157-
bool QgsLayout::raiseItem( QgsLayoutItem *item )
157+
bool QgsLayout::raiseItem( QgsLayoutItem *item, bool deferUpdate )
158158
{
159159
//model handles reordering items
160-
return mItemsModel->reorderItemUp( item );
160+
bool result = mItemsModel->reorderItemUp( item );
161+
if ( result && !deferUpdate )
162+
{
163+
//update all positions
164+
updateZValues();
165+
update();
166+
}
167+
return result;
161168
}
162169

163-
bool QgsLayout::lowerItem( QgsLayoutItem *item )
170+
bool QgsLayout::lowerItem( QgsLayoutItem *item, bool deferUpdate )
164171
{
165172
//model handles reordering items
166-
return mItemsModel->reorderItemDown( item );
173+
bool result = mItemsModel->reorderItemDown( item );
174+
if ( result && !deferUpdate )
175+
{
176+
//update all positions
177+
updateZValues();
178+
update();
179+
}
180+
return result;
167181
}
168182

169-
bool QgsLayout::moveItemToTop( QgsLayoutItem *item )
183+
bool QgsLayout::moveItemToTop( QgsLayoutItem *item, bool deferUpdate )
170184
{
171185
//model handles reordering items
172-
return mItemsModel->reorderItemToTop( item );
186+
bool result = mItemsModel->reorderItemToTop( item );
187+
if ( result && !deferUpdate )
188+
{
189+
//update all positions
190+
updateZValues();
191+
update();
192+
}
193+
return result;
173194
}
174195

175-
bool QgsLayout::moveItemToBottom( QgsLayoutItem *item )
196+
bool QgsLayout::moveItemToBottom( QgsLayoutItem *item, bool deferUpdate )
176197
{
177198
//model handles reordering items
178-
return mItemsModel->reorderItemToBottom( item );
199+
bool result = mItemsModel->reorderItemToBottom( item );
200+
if ( result && !deferUpdate )
201+
{
202+
//update all positions
203+
updateZValues();
204+
update();
205+
}
206+
return result;
179207
}
180208

181209
void QgsLayout::raiseSelectedItems()
@@ -184,7 +212,7 @@ void QgsLayout::raiseSelectedItems()
184212
bool itemsRaised = false;
185213
for ( QgsLayoutItem *item : selectedItems )
186214
{
187-
itemsRaised = itemsRaised | raiseItem( item );
215+
itemsRaised = itemsRaised | raiseItem( item, true );
188216
}
189217

190218
if ( !itemsRaised )
@@ -204,7 +232,7 @@ void QgsLayout::lowerSelectedItems()
204232
bool itemsLowered = false;
205233
for ( QgsLayoutItem *item : selectedItems )
206234
{
207-
itemsLowered = itemsLowered | lowerItem( item );
235+
itemsLowered = itemsLowered | lowerItem( item, true );
208236
}
209237

210238
if ( !itemsLowered )
@@ -224,7 +252,7 @@ void QgsLayout::moveSelectedItemsToTop()
224252
bool itemsRaised = false;
225253
for ( QgsLayoutItem *item : selectedItems )
226254
{
227-
itemsRaised = itemsRaised | moveItemToTop( item );
255+
itemsRaised = itemsRaised | moveItemToTop( item, true );
228256
}
229257

230258
if ( !itemsRaised )
@@ -244,7 +272,7 @@ void QgsLayout::moveSelectedItemsToBottom()
244272
bool itemsLowered = false;
245273
for ( QgsLayoutItem *item : selectedItems )
246274
{
247-
itemsLowered = itemsLowered | moveItemToBottom( item );
275+
itemsLowered = itemsLowered | moveItemToBottom( item, true );
248276
}
249277

250278
if ( !itemsLowered )

‎src/core/layout/qgslayout.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,53 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
151151
/**
152152
* Raises an \a item up the z-order.
153153
* Returns true if the item was successfully raised.
154+
*
155+
* If \a deferUpdate is true, the scene will not be visibly updated
156+
* to reflect the new stacking order. This allows multiple
157+
* raiseItem() calls to be made in sequence without the cost of
158+
* updating the scene for each one.
159+
*
154160
* \see lowerItem()
155161
*/
156-
bool raiseItem( QgsLayoutItem *item );
162+
bool raiseItem( QgsLayoutItem *item, bool deferUpdate = false );
157163

158164
/**
159165
* Lowers an \a item down the z-order.
160166
* Returns true if the item was successfully lowered.
167+
*
168+
* If \a deferUpdate is true, the scene will not be visibly updated
169+
* to reflect the new stacking order. This allows multiple
170+
* raiseItem() calls to be made in sequence without the cost of
171+
* updating the scene for each one.
172+
*
161173
* \see raiseItem()
162174
*/
163-
bool lowerItem( QgsLayoutItem *item );
175+
bool lowerItem( QgsLayoutItem *item, bool deferUpdate = false );
164176

165177
/**
166178
* Raises an \a item up to the top of the z-order.
167179
* Returns true if the item was successfully raised.
180+
*
181+
* If \a deferUpdate is true, the scene will not be visibly updated
182+
* to reflect the new stacking order. This allows multiple
183+
* raiseItem() calls to be made in sequence without the cost of
184+
* updating the scene for each one.
185+
*
168186
* \see moveItemToBottom()
169187
*/
170-
bool moveItemToTop( QgsLayoutItem *item );
188+
bool moveItemToTop( QgsLayoutItem *item, bool deferUpdate = false );
171189

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

179202
/**
180203
* Raises the selected items up the z-order.

‎tests/src/python/test_qgslayout.py

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ def testStacking(self):
219219

220220
# add some items
221221
item1 = QgsLayoutItemMap(l)
222-
l.addItem(item1)
222+
l.addLayoutItem(item1)
223223
item2 = QgsLayoutItemMap(l)
224-
l.addItem(item2)
224+
l.addLayoutItem(item2)
225225
item3 = QgsLayoutItemMap(l)
226-
l.addItem(item3)
226+
l.addLayoutItem(item3)
227227

228228
self.assertEqual(item1.zValue(), 1)
229229
self.assertEqual(item2.zValue(), 2)
@@ -240,17 +240,74 @@ def testStacking(self):
240240
self.assertEqual(item3.zValue(), 3)
241241

242242
# raising
243-
l.setSelectedItem(item3)
244-
l.raiseSelectedItems()
243+
self.assertFalse(l.raiseItem(item3))
245244
self.assertEqual(item1.zValue(), 1)
246245
self.assertEqual(item2.zValue(), 2)
247246
self.assertEqual(item3.zValue(), 3)
248-
l.setSelectedItem(item2)
249-
l.raiseSelectedItems()
247+
248+
self.assertTrue(l.raiseItem(item2))
250249
self.assertEqual(item1.zValue(), 1)
250+
self.assertEqual(item2.zValue(), 3)
251+
self.assertEqual(item3.zValue(), 2)
252+
253+
self.assertFalse(l.raiseItem(item2))
254+
self.assertEqual(item1.zValue(), 1)
255+
self.assertEqual(item2.zValue(), 3)
256+
self.assertEqual(item3.zValue(), 2)
257+
258+
self.assertTrue(l.raiseItem(item1))
259+
self.assertEqual(item1.zValue(), 2)
260+
self.assertEqual(item2.zValue(), 3)
261+
self.assertEqual(item3.zValue(), 1)
262+
263+
# lower
264+
self.assertFalse(l.lowerItem(item3))
265+
self.assertEqual(item1.zValue(), 2)
266+
self.assertEqual(item2.zValue(), 3)
267+
self.assertEqual(item3.zValue(), 1)
268+
269+
self.assertTrue(l.lowerItem(item2))
270+
self.assertEqual(item1.zValue(), 3)
251271
self.assertEqual(item2.zValue(), 2)
272+
self.assertEqual(item3.zValue(), 1)
273+
274+
self.assertTrue(l.lowerItem(item2))
275+
self.assertEqual(item1.zValue(), 3)
276+
self.assertEqual(item2.zValue(), 1)
277+
self.assertEqual(item3.zValue(), 2)
278+
279+
# raise to top
280+
self.assertFalse(l.moveItemToTop(item1))
281+
self.assertEqual(item1.zValue(), 3)
282+
self.assertEqual(item2.zValue(), 1)
283+
self.assertEqual(item3.zValue(), 2)
284+
285+
self.assertTrue(l.moveItemToTop(item3))
286+
self.assertEqual(item1.zValue(), 2)
287+
self.assertEqual(item2.zValue(), 1)
252288
self.assertEqual(item3.zValue(), 3)
253289

290+
self.assertTrue(l.moveItemToTop(item2))
291+
self.assertEqual(item1.zValue(), 1)
292+
self.assertEqual(item2.zValue(), 3)
293+
self.assertEqual(item3.zValue(), 2)
294+
295+
# move to bottom
296+
self.assertFalse(l.moveItemToBottom(item1))
297+
self.assertEqual(item1.zValue(), 1)
298+
self.assertEqual(item2.zValue(), 3)
299+
self.assertEqual(item3.zValue(), 2)
300+
301+
self.assertTrue(l.moveItemToBottom(item3))
302+
self.assertEqual(item1.zValue(), 2)
303+
self.assertEqual(item2.zValue(), 3)
304+
self.assertEqual(item3.zValue(), 1)
305+
306+
self.assertTrue(l.moveItemToBottom(item2))
307+
self.assertEqual(item1.zValue(), 3)
308+
self.assertEqual(item2.zValue(), 1)
309+
self.assertEqual(item3.zValue(), 2)
310+
254311

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

0 commit comments

Comments
 (0)
Please sign in to comment.