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 7 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
63 changes: 61 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,64 @@ def hoverLeaveEvent(self, event):
self.updateBox()
return super().hoverLeaveEvent(event)

def mousePressEvent(self, event):
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
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()

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)

# 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()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for context operations

The instance duplication logic should handle potential failures in context operations and instance creation. Also, ensure callback cleanup if the new instance is not found.

     def _duplicate_instance(self):
         """Duplicate the instance and add it to the scene."""
+        if not self.player.context:
+            return
+
         # Add instance to the context
         context = self.player.context
-        context.newInstance(copy_instance=self.instance)
+        try:
+            context.newInstance(copy_instance=self.instance)
+        except Exception as e:
+            print(f"Failed to duplicate instance: {e}")
+            return

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

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

         # Refresh the plot
         self.player.plot()

+        # Track if callback was connected for cleanup
+        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 instance was not found
+        if not callback_connected:
+            self.player.view.updatedSelection.disconnect(on_selection_update)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def _duplicate_instance(self):
"""Duplicate the instance and add it to the scene."""
# Add instance to the context
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()
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)
# 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()
def _duplicate_instance(self):
"""Duplicate the instance and add it to the scene."""
if not self.player.context:
return
# Add instance to the context
context = self.player.context
try:
context.newInstance(copy_instance=self.instance)
except Exception as e:
print(f"Failed to duplicate instance: {e}")
return
# Find the new instance and its last label
lf = context.labels.find(
context.state["video"], context.state["frame_idx"], return_new=True
)[0]
if not lf or not lf.instances:
return
new_instance = lf.instances[-1]
# Select the duplicated QtInstance object
self.player.state["instance"] = new_instance
# Refresh the plot
self.player.plot()
# Track if callback was connected for cleanup
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 instance 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

if is_move and is_ctrl_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