From e6fd02405d91863cdc05bce1a2e1be1b22d4d458 Mon Sep 17 00:00:00 2001 From: Ynjxsjmh Date: Sat, 28 Sep 2024 22:56:23 +0800 Subject: [PATCH] Make point, line and two-point linestrip shapes selectable --- labelme/widgets/canvas.py | 41 +++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/labelme/widgets/canvas.py b/labelme/widgets/canvas.py index d71a6125b..f383c3ea8 100644 --- a/labelme/widgets/canvas.py +++ b/labelme/widgets/canvas.py @@ -342,12 +342,17 @@ def mouseMoveEvent(self, ev): self.hEdge = None shape.highlightVertex(index, shape.MOVE_VERTEX) self.overrideCursor(CURSOR_POINT) - self.setToolTip( - self.tr( - "Click & Drag to move point\n" - "ALT + SHIFT + Click to delete point" + if shape.shape_type == "point": + self.setToolTip( + self.tr("Click & drag to move shape '%s'") % shape.label + ) + else: + self.setToolTip( + self.tr( + "Click & Drag to move point\n" + "ALT + SHIFT + Click to delete point" + ) ) - ) self.setStatusTip(self.toolTip()) self.update() break @@ -363,7 +368,15 @@ def mouseMoveEvent(self, ev): self.setStatusTip(self.toolTip()) self.update() break - elif shape.containsPoint(pos): + elif ( + shape.containsPoint(pos) + or (shape.shape_type == "line" and index_edge is not None) + or ( + shape.shape_type == "linestrip" + and len(shape.points) == 2 + and index_edge is not None + ) + ): if self.selectedVertex(): self.hShape.highlightClear() self.prevhVertex = self.hVertex @@ -567,12 +580,24 @@ def selectShapes(self, shapes): def selectShapePoint(self, point, multiple_selection_mode): """Select the first shape created which contains this point.""" - if self.selectedVertex(): # A vertex is marked for selection. + if self.selectedVertex() and self.hShape.shape_type != "point": + # A vertex, not point shape, is marked for selection. index, shape = self.hVertex, self.hShape shape.highlightVertex(index, shape.MOVE_VERTEX) else: for shape in reversed(self.shapes): - if self.isVisible(shape) and shape.containsPoint(point): + index_vertex = shape.nearestVertex(point, self.epsilon) + index_edge = shape.nearestEdge(point, self.epsilon) + if self.isVisible(shape) and ( + shape.containsPoint(point) + or (shape.shape_type == "point" and index_vertex is not None) + or (shape.shape_type == "line" and index_edge is not None) + or ( + shape.shape_type == "linestrip" + and len(shape.points) == 2 + and index_edge is not None + ) + ): self.setHiding() if shape not in self.selectedShapes: if multiple_selection_mode: