Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctrl + Click and Drag to copy instance #2072

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions sleap/gui/widgets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
>>> vp.addInstance(instance=my_instance, color=(r, g, b))

"""

from collections import deque

# FORCE_REQUESTS controls whether we emit a signal to process frame requests
Expand Down Expand Up @@ -1886,7 +1887,7 @@ def __init__(
self.track_label.setHtml(instance_label_text)

# Add nodes
for (node, point) in self.instance.nodes_points:
for node, point in self.instance.nodes_points:
if point.visible or self.show_non_visible:
node_item = QtNode(
parent=self,
Expand All @@ -1901,7 +1902,7 @@ def __init__(
self.nodes[node.name] = node_item

# Add edges
for (src, dst) in self.skeleton.edge_names:
for src, dst in self.skeleton.edge_names:
# Make sure that both nodes are present in this instance before drawing edge
if src in self.nodes and dst in self.nodes:
edge_item = QtEdge(
Expand Down Expand Up @@ -2124,6 +2125,78 @@ def hoverLeaveEvent(self, event):
self.updateBox()
return super().hoverLeaveEvent(event)

def mousePressEvent(self, event):
"""Custom event handler for mouse press."""
if event.buttons() == Qt.LeftButton:
if event.modifiers() == Qt.ControlModifier:
self._duplicate_instance()

def _duplicate_instance(self):
"""Duplicate the instance and add it to the scene."""
# Add instance to the context
if self.player.context is None:
return

context = self.player.context
context.newInstance(copy_instance=self.instance)

# Find the new instance and its last label
lf = context.labels.find(
context.state["video"], context.state["frame_idx"], return_new=True
)[0]
new_instance = lf.instances[-1]

# Select the duplicated QtInstance object
self.player.state["instance"] = new_instance

# Refresh the plot
self.player.plot()

# Track if the new instance is connected for cleanu
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
callback_connected = False

def on_selection_update():
"""Callback to set the new QtInstance to be movable."""
# Find the QtInstance corresponding to the newly created instance
for qt_inst in self.player.view.all_instances:
if qt_inst.instance == new_instance:
self.player.view.updatedSelection.disconnect(on_selection_update)
nonlocal callback_connected
callback_connected = True

# Set this QtInstance to be movable
qt_inst.setFlag(QGraphicsItem.ItemIsMovable)

# Optionally grab the mouse and change cursor, so user can immediately drag
qt_inst.setCursor(Qt.ClosedHandCursor)
qt_inst.grabMouse()
break

self.player.view.updatedSelection.connect(on_selection_update)
self.player.view.updatedSelection.emit()

# Clean up callback if QtInstance was not found
if not callback_connected:
self.player.view.updatedSelection.disconnect(on_selection_update)

def mouseMoveEvent(self, event):
"""Custom event handler to emit signal on event."""
is_move = self.flags() & QGraphicsItem.ItemIsMovable
is_ctrl_pressed = (event.modifiers() & Qt.ControlModifier) == Qt.ControlModifier
is_alt_pressed = (event.modifiers() & Qt.AltModifier) == Qt.AltModifier

if is_move and (is_ctrl_pressed or is_alt_pressed):
super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
"""Custom event handler to emit signal on event."""
if self.flags() & QGraphicsItem.ItemIsMovable:
self.setFlag(QGraphicsItem.ItemIsMovable, False)
self.updatePoints(user_change=True)
self.updateBox()
self.ungrabMouse()
super().mouseReleaseEvent(event)


class VisibleBoundingBox(QtWidgets.QGraphicsRectItem):
"""QGraphicsRectItem for user instance bounding boxes.
Expand Down
Loading