From aff745d27f6a2fc1bf77a5609ac0046df9d6a428 Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Wed, 31 Mar 2021 09:47:24 +0200 Subject: [PATCH] Bring newly inserted items to front --- beeref/commands.py | 2 +- beeref/items.py | 12 +++++++++--- tests/test_commands.py | 22 ++++++---------------- tests/test_items.py | 5 +++++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/beeref/commands.py b/beeref/commands.py index 0c27d64..0975428 100644 --- a/beeref/commands.py +++ b/beeref/commands.py @@ -26,8 +26,8 @@ class InsertItems(QtGui.QUndoCommand): def redo(self): self.scene.clearSelection() for item in self.items: - item.setSelected(True) self.scene.addItem(item) + item.setSelected(True) def undo(self): self.scene.clearSelection() diff --git a/beeref/items.py b/beeref/items.py index c98269a..3bbbd70 100644 --- a/beeref/items.py +++ b/beeref/items.py @@ -52,7 +52,7 @@ class BeePixmapItem(QtWidgets.QGraphicsPixmapItem): self.single_select_mode = False self.scale_active = False - self.viewport_scale = None + self.viewport_scale = 1 def __str__(self): return (f'Image "{self.filename}" ' @@ -115,8 +115,14 @@ class BeePixmapItem(QtWidgets.QGraphicsPixmapItem): screen so we need to adjust the values according to the scale factor sof the view and the item.""" - scale = self.scene().views()[0].get_scale() - return value / scale / self.scale() + if self.scene(): + scale = self.scene().views()[0].get_scale() + return value / scale / self.scale() + else: + # This can happen when the item is already removed from + # the scene but its boundingRect is still needed. Use the + # last known scaling factor instead + return value * self.viewport_scale @property def select_resize_size(self): diff --git a/tests/test_commands.py b/tests/test_commands.py index c07a6aa..0485829 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -11,34 +11,26 @@ from .base import BeeTestCase class InsertItemsTestCase(BeeTestCase): def test_redo_undo(self): - def get_images(): - return list(filter(lambda i: isinstance(i, BeePixmapItem), - scene.items())) - scene = BeeGraphicsScene(None) scene.update_selection = MagicMock() + scene.max_z = 5 item1 = BeePixmapItem(QtGui.QImage()) scene.addItem(item1) item2 = BeePixmapItem(QtGui.QImage()) command = commands.InsertItems(scene, [item2]) command.redo() - assert len(get_images()) == 2 - assert item1 in scene.items() + assert list(scene.items_for_save()) == [item1, item2] assert item1.isSelected() is False - assert item2 in scene.items() assert item2.isSelected() is True + item2.zValue() > 5 command.undo() - assert get_images() == [item1] + assert list(scene.items_for_save()) == [item1] assert item1.isSelected() is False class DeleteItemsTestCase(BeeTestCase): def test_redo_undo(self): - def get_images(): - return list(filter(lambda i: isinstance(i, BeePixmapItem), - scene.items())) - scene = BeeGraphicsScene(None) scene.update_selection = MagicMock() item1 = BeePixmapItem(QtGui.QImage()) @@ -48,12 +40,10 @@ class DeleteItemsTestCase(BeeTestCase): item2.setSelected(True) command = commands.DeleteItems(scene, [item2]) command.redo() - assert get_images() == [item1] + assert list(scene.items_for_save()) == [item1] command.undo() - assert len(get_images()) == 2 - assert item1 in scene.items() + assert list(scene.items_for_save()) == [item1, item2] assert item1.isSelected() is False - assert item2 in scene.items() assert item2.isSelected() is True diff --git a/tests/test_items.py b/tests/test_items.py index 57676b9..76ec216 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -102,6 +102,11 @@ class BeePixmapItemPaintstuffTestCase(BeeTestCase): self.item.setScale(5) assert self.item.fixed_length_for_viewport(100) == 20 + def test_fixed_length_for_viewport_when_no_scene(self): + item = BeePixmapItem(QtGui.QImage()) + item.viewport_scale = 0.5 + assert item.fixed_length_for_viewport(100) == 50 + def test_resize_size_when_scaled(self): self.view.get_scale = MagicMock(return_value=2) self.item.setScale(2)