Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self._drag_start = None
self._drag_box = None
self._is_selecting = False
self._fixed_vertex = None
self._value = (None, None)
self._moving_value = (None, None)
if self._is_creating is True and self._mode == Mode.ADD_PATH:
vertices = self._data_view.displayed_vertices[
self._data_view.displayed_index == index
]
if len(vertices) <= 2:
self._data_view.remove(index)
else:
data_full = self.expand_shape(vertices)
self._data_view.edit(index, data_full[:-1])
if self._is_creating is True and self._mode == Mode.ADD_POLYGON:
vertices = self._data_view.displayed_vertices[
self._data_view.displayed_index == index
]
if len(vertices) <= 2:
self._data_view.remove(index)
self._is_creating = False
self._update_dims()
vertices = self._data_view.displayed_vertices[indices]
vertices[vertex] = coord
data_full = self.expand_shape(vertices)
self._data_view.edit(
index, data_full, new_type=new_type
)
shapes = self.selected_data
self._selected_box = self.interaction_box(shapes)
self.refresh()
else:
self._is_selecting = True
if self._drag_start is None:
self._drag_start = coord
self._drag_box = np.array([self._drag_start, coord])
self._set_highlight()
elif self._mode in [Mode.VERTEX_INSERT, Mode.VERTEX_REMOVE]:
if len(self.selected_data) > 0:
pass
else:
self._is_selecting = True
if self._drag_start is None:
self._drag_start = coord
self._drag_box = np.array([self._drag_start, coord])
self._set_highlight()
def _finish_drawing(self):
"""Reset properties used in shape drawing."""
index = copy(self._moving_value[0])
self._is_moving = False
self.selected_data = []
self._drag_start = None
self._drag_box = None
self._is_selecting = False
self._fixed_vertex = None
self._value = (None, None)
self._moving_value = (None, None)
if self._is_creating is True and self._mode == Mode.ADD_PATH:
vertices = self._data_view.displayed_vertices[
self._data_view.displayed_index == index
]
if len(vertices) <= 2:
self._data_view.remove(index)
else:
data_full = self.expand_shape(vertices)
self._data_view.edit(index, data_full[:-1])
if self._is_creating is True and self._mode == Mode.ADD_POLYGON:
vertices = self._data_view.displayed_vertices[
self._data_view.displayed_index == index
]
if len(vertices) <= 2:
self._data_view.remove(index)
self._is_creating = False
self._update_dims()
self._selected_data_history = []
self._selected_box = None
self._drag_start = None
self._fixed_vertex = None
self._fixed_aspect = False
self._aspect_ratio = 1
self._is_moving = False
self._fixed_index = 0
self._is_selecting = False
self._drag_box = None
self._drag_box_stored = None
self._is_creating = False
self._clipboard = {}
self._mode = Mode.PAN_ZOOM
self._mode_history = self._mode
self._status = self.mode
self._help = 'enter a selection mode to edit shape properties'
self.events.deselect.connect(lambda x: self._finish_drawing())
self.events.face_color.connect(lambda e: self._update_thumbnail())
self.events.edge_color.connect(lambda e: self._update_thumbnail())
self.add(
data,
shape_type=shape_type,
edge_width=edge_width,
edge_color=edge_color,
face_color=face_color,
opacity=opacity,
z_index=z_index,
def set_mode(self, event):
mode = event.mode
if mode == Mode.SELECT:
self.select_button.setChecked(True)
elif mode == Mode.DIRECT:
self.direct_button.setChecked(True)
elif mode == Mode.PAN_ZOOM:
self.panzoom_button.setChecked(True)
elif mode == Mode.ADD_RECTANGLE:
self.rectangle_button.setChecked(True)
elif mode == Mode.ADD_ELLIPSE:
self.ellipse_button.setChecked(True)
elif mode == Mode.ADD_LINE:
self.line_button.setChecked(True)
elif mode == Mode.ADD_PATH:
self.path_button.setChecked(True)
elif mode == Mode.ADD_POLYGON:
self.polygon_button.setChecked(True)
elif mode == Mode.VERTEX_INSERT:
self.vertex_insert_button.setChecked(True)
elif mode == Mode.VERTEX_REMOVE:
def _move(self, coord):
"""Moves object at given mouse position and set of indices.
Parameters
----------
coord : sequence of two int
Position of mouse cursor in image coordinates.
"""
vertex = self._moving_value[1]
if self._mode in (
[Mode.SELECT, Mode.ADD_RECTANGLE, Mode.ADD_ELLIPSE, Mode.ADD_LINE]
):
if len(self.selected_data) > 0:
self._is_moving = True
if vertex is None:
# Check where dragging box from to move whole object
if self._drag_start is None:
center = self._selected_box[Box.CENTER]
self._drag_start = coord - center
center = self._selected_box[Box.CENTER]
shift = coord - center - self._drag_start
for index in self.selected_data:
self._data_view.shift(index, shift)
self._selected_box = self._selected_box + shift
self.refresh()
elif vertex < Box.LEN:
# Corner / edge vertex is being dragged so resize object
def mode(self, mode):
if isinstance(mode, str):
mode = Mode(mode)
if not self.editable:
mode = Mode.PAN_ZOOM
if mode == self._mode:
return
old_mode = self._mode
if mode == Mode.PAN_ZOOM:
self.cursor = 'standard'
self.interactive = True
self.help = 'enter a selection mode to edit shape properties'
elif mode in [Mode.SELECT, Mode.DIRECT]:
self.cursor = 'pointing'
self.interactive = False
self.help = (
'hold to pan/zoom, '
def set_mode(self, event):
mode = event.mode
if mode == Mode.SELECT:
self.select_button.setChecked(True)
elif mode == Mode.DIRECT:
self.direct_button.setChecked(True)
elif mode == Mode.PAN_ZOOM:
self.panzoom_button.setChecked(True)
elif mode == Mode.ADD_RECTANGLE:
self.rectangle_button.setChecked(True)
elif mode == Mode.ADD_ELLIPSE:
self.ellipse_button.setChecked(True)
elif mode == Mode.ADD_LINE:
self.line_button.setChecked(True)
elif mode == Mode.ADD_PATH:
self.path_button.setChecked(True)
elif mode == Mode.ADD_POLYGON:
self.polygon_button.setChecked(True)
elif mode == Mode.VERTEX_INSERT:
self.vertex_insert_button.setChecked(True)
elif mode == Mode.VERTEX_REMOVE:
self.vertex_remove_button.setChecked(True)
else:
raise ValueError("Mode not recongnized")
def hold_to_pan_zoom(layer):
"""Hold to pan and zoom in the viewer."""
if layer._mode != Mode.PAN_ZOOM:
# on key press
prev_mode = layer.mode
prev_selected = layer.selected_data.copy()
layer.mode = Mode.PAN_ZOOM
yield
# on key release
layer.mode = prev_mode
layer.selected_data = prev_selected
layer._set_highlight()
def __init__(self, layer):
super().__init__(layer)
self.layer.events.mode.connect(self.set_mode)
self.select_button = QtModeButton(
layer, 'select', Mode.SELECT, 'Select mode'
)
self.direct_button = QtModeButton(
layer, 'direct', Mode.DIRECT, 'Direct select mode'
)
self.panzoom_button = QtModeButton(
layer, 'zoom', Mode.PAN_ZOOM, 'Pan/zoom mode'
)
self.rectangle_button = QtModeButton(
layer, 'rectangle', Mode.ADD_RECTANGLE, 'Add rectangles'
)
self.ellipse_button = QtModeButton(
layer, 'ellipse', Mode.ADD_ELLIPSE, 'Add ellipses'
)
self.line_button = QtModeButton(
layer, 'line', Mode.ADD_LINE, 'Add lines'
)