@@ -326,6 +326,20 @@ def calculateOffsets(self, shape, point):
326
326
y2 = (rect .y () + rect .height ()) - point .y ()
327
327
self .offsets = QPointF (x1 , y1 ), QPointF (x2 , y2 )
328
328
329
+ def snapPointToCanvas (self , x , y ):
330
+ """
331
+ Moves a point x,y to within the boundaries of the canvas.
332
+ :return: (x,y,snapped) where snapped is True if x or y were changed, False if not.
333
+ """
334
+ if x < 0 or x > self .pixmap .width () or y < 0 or y > self .pixmap .height ():
335
+ x = max (x , 0 )
336
+ y = max (y , 0 )
337
+ x = min (x , self .pixmap .width ())
338
+ y = min (y , self .pixmap .height ())
339
+ return x , y , True
340
+
341
+ return x , y , False
342
+
329
343
def boundedMoveVertex (self , pos ):
330
344
index , shape = self .hVertex , self .hShape
331
345
point = shape [index ]
@@ -528,6 +542,10 @@ def intersectionPoint(self, p1, p2):
528
542
return QPointF (x3 , min (max (0 , y2 ), max (y3 , y4 )))
529
543
else : # y3 == y4
530
544
return QPointF (min (max (0 , x2 ), max (x3 , x4 )), y3 )
545
+
546
+ # Ensure the labels are within the bounds of the image. If not, fix them.
547
+ x , y , _ = self .snapPointToCanvas (x , y )
548
+
531
549
return QPointF (x , y )
532
550
533
551
def intersectingEdges (self , x1y1 , x2y2 , points ):
0 commit comments