Skip to content

Commit

Permalink
Fix #1059. Ensure display item not changed by selecting graphics. Test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed May 28, 2024
1 parent fd07bd9 commit 7acebcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
16 changes: 11 additions & 5 deletions nion/swift/DisplayPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,11 +2651,17 @@ def nudge_selected_graphics(self, mapping: Graphics.CoordinateMappingLike, delta

def adjust_graphics(self, widget_mapping: Graphics.CoordinateMappingLike, graphic_drag_items: typing.Sequence[Graphics.Graphic], graphic_drag_part: str, graphic_part_data: typing.Dict[int, Graphics.DragPartData], graphic_drag_start_pos: Geometry.FloatPoint, pos: Geometry.FloatPoint, modifiers: UserInterface.KeyboardModifiers) -> None:
if self.__display_item:
with self.__display_item.display_item_changes():
for graphic in graphic_drag_items:
index = self.__display_item.graphics.index(graphic)
part_data = (graphic_drag_part, ) + graphic_part_data[index]
graphic.adjust_part(widget_mapping, graphic_drag_start_pos, pos, part_data, modifiers)
graphic_changed = False
for graphic in graphic_drag_items:
index = self.__display_item.graphics.index(graphic)
part_data = (graphic_drag_part, ) + graphic_part_data[index]
graphic_modified = graphic.modified_count
graphic.adjust_part(widget_mapping, graphic_drag_start_pos, pos, part_data, modifiers)
if graphic.modified_count != graphic_modified:
graphic_changed = True
if graphic_changed:
self.__display_item._begin_display_item_changes()
self.__display_item._end_display_item_changes()

def nudge_slice(self, delta: int) -> None:
display_data_channel = self.__display_item.display_data_channel if self.__display_item else None
Expand Down
37 changes: 37 additions & 0 deletions nion/swift/test/DisplayPanel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,43 @@ def test_image_display_canvas_item_only_updates_once_if_graphic_changes(self):
graphic.bounds = Geometry.FloatRect.from_tlbr(0.1, 0.1, 0.2, 0.2)
self.assertEqual(update_count + 1, display_panel.display_canvas_item._update_count)

def test_image_display_canvas_item_does_not_update_if_graphic_does_not_change(self):
# confirm that clicking on a graphic does not change the display item (causing it or its thumbnail to redraw).
with TestContext.create_memory_context() as test_context:
document_controller = test_context.create_document_controller()
document_model = document_controller.document_model
display_panel = document_controller.selected_display_panel
data_item = DataItem.DataItem(numpy.random.randn(8, 8))
document_model.append_data_item(data_item)
display_item = document_model.get_display_item_for_data_item(data_item)
graphic = Graphics.RectangleGraphic()
display_item.add_graphic(graphic)
display_panel.set_display_panel_display_item(display_item)
display_panel.root_container.layout_immediate(Geometry.IntSize(240, 240))
document_controller.periodic()
self.assertIsInstance(display_panel.display_canvas_item, ImageCanvasItem.ImageCanvasItem)
document_controller.periodic()
# click once
display_panel.display_canvas_item.simulate_click(Geometry.IntPoint(120, 120))
document_controller.periodic()
update_count = display_panel.display_canvas_item._update_count

display_item_did_change = False

def item_changed() -> None:
nonlocal display_item_did_change
display_item_did_change = True

with display_item.item_changed_event.listen(item_changed):
# click again, nothing should change. use explicit press/release to perform periodic.
display_panel.display_canvas_item.simulate_press(Geometry.IntPoint(120, 120))
document_controller.periodic()
display_panel.display_canvas_item.simulate_release(Geometry.IntPoint(120, 120))
document_controller.periodic()
self.assertEqual(update_count, display_panel.display_canvas_item._update_count)

self.assertFalse(display_item_did_change)

def test_image_display_canvas_item_only_updates_once_if_color_map_changes(self):
with TestContext.create_memory_context() as test_context:
document_controller = test_context.create_document_controller()
Expand Down

0 comments on commit 7acebcb

Please sign in to comment.