diff --git a/coretk/coretk/graph/graph.py b/coretk/coretk/graph/graph.py index baa18170..42dbc934 100644 --- a/coretk/coretk/graph/graph.py +++ b/coretk/coretk/graph/graph.py @@ -138,6 +138,11 @@ class CanvasGraph(tk.Canvas): valid_y = y1 <= y <= y2 return valid_x and valid_y + def valid_position(self, x1, y1, x2, y2): + valid_topleft = self.inside_canvas(x1, y1) + valid_bottomright = self.inside_canvas(x2, y2) + return valid_topleft and valid_bottomright + def draw_grid(self): """ Create grid. @@ -539,6 +544,13 @@ class CanvasGraph(tk.Canvas): """ x, y = self.canvas_xy(event) if not self.inside_canvas(x, y): + if self.select_box: + self.select_box.delete() + self.select_box = None + if is_draw_shape(self.annotation_type) and self.shape_drawing: + shape = self.shapes.pop(self.selected) + shape.delete() + self.shape_drawing = False return x_offset = x - self.cursor[0] diff --git a/coretk/coretk/graph/node.py b/coretk/coretk/graph/node.py index a0f406f7..11c7eaf5 100644 --- a/coretk/coretk/graph/node.py +++ b/coretk/coretk/graph/node.py @@ -103,10 +103,19 @@ class CanvasNode: self.motion(x_offset, y_offset, update=False) def motion(self, x_offset, y_offset, update=True): + original_position = self.canvas.coords(self.id) self.canvas.move(self.id, x_offset, y_offset) + x, y = self.canvas.coords(self.id) + + # check new position + bbox = self.canvas.bbox(self.id) + if not self.canvas.valid_position(*bbox): + self.canvas.coords(self.id, original_position) + return + + # move test and selection box self.canvas.move(self.text_id, x_offset, y_offset) self.canvas.move_selection(self.id, x_offset, y_offset) - x, y = self.canvas.coords(self.id) # move antennae for antenna_id in self.antennae: diff --git a/coretk/coretk/graph/shape.py b/coretk/coretk/graph/shape.py index 99a18a9f..e7277b49 100644 --- a/coretk/coretk/graph/shape.py +++ b/coretk/coretk/graph/shape.py @@ -136,7 +136,13 @@ class Shape: self.canvas.delete(self.id) def motion(self, x_offset, y_offset): + original_position = self.canvas.coords(self.id) self.canvas.move(self.id, x_offset, y_offset) + coords = self.canvas.coords(self.id) + if not self.canvas.valid_position(*coords): + self.canvas.coords(self.id, original_position) + return + self.canvas.move_selection(self.id, x_offset, y_offset) if self.text_id is not None: self.canvas.move(self.text_id, x_offset, y_offset)