Skip to content

Commit

Permalink
Remove index from ChangeDisplayLayerDisplayDataChannelCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
KRLango authored and cmeyer committed Nov 7, 2024
1 parent a64adc8 commit ec249cc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
14 changes: 8 additions & 6 deletions nion/swift/Inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,11 @@ def can_merge(self, command: Undo.UndoableCommand) -> bool:


class ChangeDisplayLayerDisplayDataChannelCommand(Undo.UndoableCommand):
def __init__(self, document_model: DocumentModel.DocumentModel, display_item: DisplayItem.DisplayItem, display_layer_index: int, display_data_channel: DisplayItem.DisplayDataChannel) -> None:
def __init__(self, document_model: DocumentModel.DocumentModel, display_item: DisplayItem.DisplayItem, display_layer: DisplayItem.DisplayLayer, display_data_channel: DisplayItem.DisplayDataChannel) -> None:
super().__init__(_("Change Display Layer Data"), command_id="change_display_layer_data", is_mergeable=True)
self.__document_model = document_model
self.__display_item_proxy = display_item.create_proxy()
self.__display_layer_index = display_layer_index
self.__display_layer_uuid = display_layer.uuid
self.__display_data_channel_proxy = display_data_channel.create_proxy() if display_data_channel else None
self.initialize()

Expand All @@ -775,8 +775,11 @@ def perform(self) -> None:
display_item = self.__display_item_proxy.item
display_data_channel = self.__display_data_channel_proxy.item if self.__display_data_channel_proxy else None
if display_item:
old_display_data_channel = display_item.get_display_layer_display_data_channel(self.__display_layer_index)
display_item.set_display_layer_display_data_channel(self.__display_layer_index, display_data_channel)
display_layer = display_item.get_display_layer_by_uuid(self.__display_layer_uuid)
assert display_layer
old_display_data_channel = display_layer.display_data_channel
assert display_data_channel is None or display_data_channel in display_item.display_data_channels
display_layer.display_data_channel = display_data_channel
if old_display_data_channel:
if not self.__display_data_channel_proxy:
self.__display_data_channel_proxy = old_display_data_channel.create_proxy() if old_display_data_channel else None
Expand Down Expand Up @@ -986,10 +989,9 @@ def __on_data_index_changed(self, property: str) -> None:
if property == "value":
display_data_channel = self.display_item.display_data_channels[self.data_index_model.value] if self.data_index_model.value is not None else None
if display_data_channel:
index = self.display_item.display_layers.index(self.display_layer)
command = ChangeDisplayLayerDisplayDataChannelCommand(self.document_controller.document_model,
self.display_item,
index,
self.display_layer,
display_data_channel)
command.perform()
self.document_controller.push_undo_command(command)
Expand Down
6 changes: 6 additions & 0 deletions nion/swift/model/DisplayItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,12 @@ def _add_display_layer_for_data_item(self, data_item: DataItem.DataItem, **kwarg
display_data_channel = self.display_data_channels[self.data_items.index(data_item)]
self.add_display_layer_for_display_data_channel(display_data_channel, **kwargs)

def get_display_layer_by_uuid(self, display_layer_uuid: uuid.UUID) -> typing.Optional[DisplayLayer]:
for display_layer in self.display_layers:
if display_layer.uuid == display_layer_uuid:
return display_layer
return None

def get_display_layer_display_data_channel(self, index: int) -> typing.Optional[DisplayDataChannel]:
assert 0 <= index < len(self.display_layers)
return self.display_layers[index].display_data_channel
Expand Down
2 changes: 1 addition & 1 deletion nion/swift/test/Inspector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ def test_change_display_layer_data_command(self):
self.assertEqual(3, len(display_item.display_data_channels))
self.assertEqual(2, len(display_item.display_layers))
self.assertEqual(display_item.display_data_channels[1], display_item.get_display_layer_display_data_channel(1))
command = Inspector.ChangeDisplayLayerDisplayDataChannelCommand(document_model, display_item, 1, display_item.display_data_channels[2])
command = Inspector.ChangeDisplayLayerDisplayDataChannelCommand(document_model, display_item, display_item.display_layers[1], display_item.display_data_channels[2])
command.perform()
document_controller.push_undo_command(command)
self.assertEqual(display_item.display_data_channels[2], display_item.get_display_layer_display_data_channel(1))
Expand Down

0 comments on commit ec249cc

Please sign in to comment.