Skip to content

Commit

Permalink
Update ImageCanvasItem.py
Browse files Browse the repository at this point in the history
Re-attempt at fixing some of the type errors.
  • Loading branch information
Tiomat85 committed Jun 19, 2024
1 parent 94b264e commit 06da54c
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions nion/swift/ImageCanvasItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,13 @@ async def _reactor_loop(self, r: Stream.ValueChangeStreamReactorInterface[MouseP
mouse_pos, modifiers = value_change_value
start_drag_pos = mouse_pos

start_drag_pos_norm = image_canvas_item.convert_pixel_to_normalised(start_drag_pos)

#document_controller = image_canvas_item.__document_controller
#document_model = document_controller.document_model
#display_item = document_model.get_display_item_for_data_item(image_canvas_item.data_item)

# Commented code block for retrieving display_item for use on displaying rectangle for drag-zoom
# start_drag_pos_norm = image_canvas_item.convert_pixel_to_normalised(start_drag_pos)
# document_controller = image_canvas_item.__document_controller
# document_model = document_controller.document_model
# display_item = document_model.get_display_item_for_data_item(image_canvas_item.data_item)

with (delegate.create_change_display_properties_task() as change_display_properties_task):
# mouse tracking loop. wait for values and update the image position.
Expand All @@ -677,23 +679,25 @@ async def _reactor_loop(self, r: Stream.ValueChangeStreamReactorInterface[MouseP
mouse_pos, modifiers = value_change.value
end_drag_pos = mouse_pos
if (self._is_zooming_in and
((abs(start_drag_pos[0] - end_drag_pos[0]) > 3) or (abs(start_drag_pos[1] - end_drag_pos[1]) > 3))):
((abs(start_drag_pos[0] - end_drag_pos[0]) > 3)
or (abs(start_drag_pos[1] - end_drag_pos[1]) > 3))):
image_canvas_item._apply_selection_zoom(start_drag_pos, end_drag_pos)
else:
image_canvas_item._apply_fixed_zoom(self._is_zooming_in, start_drag_pos)
break
if value_change.value is not None:
pass
# Not released for the zoom target, we could do with drawing a rectangle
mouse_pos, modifiers = value_change.value
assert start_drag_pos
#if crop_region:
#display_item.remove_graphic(crop_region)
#else:
#crop_region = Graphics.RectangleGraphic()
# mouse_pos, modifiers = value_change.value
# assert start_drag_pos
# if crop_region:
# display_item.remove_graphic(crop_region)
# else:
# crop_region = Graphics.RectangleGraphic()

#end_drag_pos_norm = image_canvas_item.convert_pixel_to_normalised(mouse_pos)
#crop_region.bounds = (start_drag_pos_norm, end_drag_pos_norm)
#display_item.add_graphic(crop_region)
# end_drag_pos_norm = image_canvas_item.convert_pixel_to_normalised(mouse_pos)
# crop_region.bounds = (start_drag_pos_norm, end_drag_pos_norm)
# display_item.add_graphic(crop_region)

# if the image position was set, it means the user moved the image. perform the task.
if image_position:
Expand Down Expand Up @@ -1133,7 +1137,7 @@ def _update_image_canvas_position(self, widget_delta: Geometry.FloatSize) -> Geo
self._set_image_canvas_position(new_image_canvas_position)
return new_image_canvas_position

def convert_pixel_to_normalised(self, coord: tuple[int, int]) -> Geometry.FloatPoint:
def convert_pixel_to_normalised(self, coord: Geometry.IntPoint) -> Geometry.FloatPoint:
if coord:
widget_mapping = ImageCanvasItemMapping.make(self.__data_shape, self.__composite_canvas_item.canvas_bounds,
list())
Expand All @@ -1143,10 +1147,9 @@ def convert_pixel_to_normalised(self, coord: tuple[int, int]) -> Geometry.FloatP
return Geometry.FloatPoint(norm_coord[0], norm_coord[1]) # y,x

#Apply a zoom factor to the widget, optionally focussed on a specific point
def _apply_fixed_zoom(self, zoom_in: bool, coord: tuple[int, int] = None):
# print('Applying zoom factor {0}, at coordinate {1},{2}'.format(zoom_in, coord[0], coord[1]))
def _apply_fixed_zoom(self, zoom_in: bool, coord: Geometry.IntPoint = None) -> None:
if coord:
#Coordinate specified, so needing to recenter to that point before we adjust zoom levels
# Coordinate specified, so needing to recenter to that point before we adjust zoom levels
widget_mapping = ImageCanvasItemMapping.make(self.__data_shape, self.__composite_canvas_item.canvas_bounds, list())
if widget_mapping:
mapped = self.map_widget_to_image(coord)
Expand All @@ -1165,39 +1168,28 @@ def _apply_fixed_zoom(self, zoom_in: bool, coord: tuple[int, int] = None):
else:
self.zoom_out()

def _apply_selection_zoom(self, coord1: tuple[int, int], coord2: tuple[int, int]):
# print('Applying zoom factor {0}, at coordinate {1},{2}'.format(zoom_in, coord[0], coord[1]))
def _apply_selection_zoom(self, coord1: Geometry.IntPoint, coord2: Geometry.IntPoint) -> None:
assert coord1
assert coord2
# print('from {0} to {1}'.format(coord1, coord2))
widget_mapping = ImageCanvasItemMapping.make(self.__data_shape, self.__composite_canvas_item.canvas_bounds, list())
if widget_mapping:
coord1_mapped = self.map_widget_to_image(coord1)
coord2_mapped = self.map_widget_to_image(coord2)
norm_coord1 = tuple(ele1 / ele2 for ele1, ele2 in zip(coord1_mapped, self.__data_shape))
norm_coord2 = tuple(ele1 / ele2 for ele1, ele2 in zip(coord2_mapped, self.__data_shape))
# print('norm from {0} to {1}'.format(norm_coord1, norm_coord2))

norm_coord = tuple((ele1 + ele2)/2 for ele1, ele2 in zip(norm_coord1, norm_coord2))
self._set_image_canvas_position(norm_coord)
# image now centered on middle of selection, need to calculate new zoom level required
# selection size in widget pixels
selection_size_screen_space = tuple(
abs(ele1 - ele2) for ele1, ele2 in zip(coord1, coord2)) # y,x
# print(selection_size_screen_space)
widget_width = self.__composite_canvas_item.canvas_bounds.width / self.__image_zoom
widget_height = self.__composite_canvas_item.canvas_bounds.height / self.__image_zoom
# print(widget_width)
# print(widget_height)
widget_width_factor = widget_width / selection_size_screen_space[1]
widget_height_factor = widget_height / selection_size_screen_space[0]
widget_overall_factor = max(widget_height_factor, widget_width_factor)
# print('factor {0}'.format(widget_overall_factor))
# print('old zoom {0}'.format(self.__image_zoom))
self.__apply_display_properties_command({"image_zoom": widget_overall_factor * self.__image_zoom, "image_canvas_mode": "custom"})
# print('new zoom {0}'.format(self.__image_zoom))
# print(self.__composite_canvas_item.canvas_bounds)


def mouse_clicked(self, x: int, y: int, modifiers: UserInterface.KeyboardModifiers) -> bool:
if super().mouse_clicked(x, y, modifiers):
Expand Down

0 comments on commit 06da54c

Please sign in to comment.