Ensure a vertex always stays within the boundaries of the canvas.

This commit is contained in:
Lieven Govaerts 2018-12-29 13:57:13 +01:00 committed by darrenl
parent a0fa187eff
commit 4bb6be6986
2 changed files with 20 additions and 5 deletions

View File

@ -760,11 +760,8 @@ class MainWindow(QMainWindow, WindowMixin):
for x, y in points: for x, y in points:
# Ensure the labels are within the bounds of the image. If not, fix them. # Ensure the labels are within the bounds of the image. If not, fix them.
if x < 0 or x > self.canvas.pixmap.width() or y < 0 or y > self.canvas.pixmap.height(): x, y, snapped = self.canvas.snapPointToCanvas(x, y)
x = max(x, 0) if snapped:
y = max(y, 0)
x = min(x, self.canvas.pixmap.width())
y = min(y, self.canvas.pixmap.height())
self.setDirty() self.setDirty()
shape.addPoint(QPointF(x, y)) shape.addPoint(QPointF(x, y))

View File

@ -326,6 +326,20 @@ class Canvas(QWidget):
y2 = (rect.y() + rect.height()) - point.y() y2 = (rect.y() + rect.height()) - point.y()
self.offsets = QPointF(x1, y1), QPointF(x2, y2) self.offsets = QPointF(x1, y1), QPointF(x2, y2)
def snapPointToCanvas(self, x, y):
"""
Moves a point x,y to within the boundaries of the canvas.
:return: (x,y,snapped) where snapped is True if x or y were changed, False if not.
"""
if x < 0 or x > self.pixmap.width() or y < 0 or y > self.pixmap.height():
x = max(x, 0)
y = max(y, 0)
x = min(x, self.pixmap.width())
y = min(y, self.pixmap.height())
return x, y, True
return x, y, False
def boundedMoveVertex(self, pos): def boundedMoveVertex(self, pos):
index, shape = self.hVertex, self.hShape index, shape = self.hVertex, self.hShape
point = shape[index] point = shape[index]
@ -528,6 +542,10 @@ class Canvas(QWidget):
return QPointF(x3, min(max(0, y2), max(y3, y4))) return QPointF(x3, min(max(0, y2), max(y3, y4)))
else: # y3 == y4 else: # y3 == y4
return QPointF(min(max(0, x2), max(x3, x4)), y3) return QPointF(min(max(0, x2), max(x3, x4)), y3)
# Ensure the labels are within the bounds of the image. If not, fix them.
x, y, _ = self.snapPointToCanvas(x, y)
return QPointF(x, y) return QPointF(x, y)
def intersectingEdges(self, x1y1, x2y2, points): def intersectingEdges(self, x1y1, x2y2, points):