Browse Source

Fix copied shape offset so that it is bounded

Michael Pitidis 13 years ago
parent
commit
22e44ace07
2 changed files with 18 additions and 4 deletions
  1. 18 3
      canvas.py
  2. 0 1
      shape.py

+ 18 - 3
canvas.py

@@ -270,7 +270,7 @@ class Canvas(QWidget):
 
     def boundedMoveShape(self, shape, pos):
         if self.outOfPixmap(pos):
-            return # No need to move
+            return False # No need to move
         o1 = pos + self.offsets[0]
         if self.outOfPixmap(o1):
             pos -= QPointF(min(0, o1.x()), min(0, o1.y()))
@@ -283,8 +283,12 @@ class Canvas(QWidget):
         # a bit "shaky" when nearing the border and allows it to
         # go outside of the shape's area for some reason. XXX
         #self.calculateOffsets(self.selectedShape, pos)
-        shape.moveBy(pos - self.prevPoint)
-        self.prevPoint = pos
+        dp = pos - self.prevPoint
+        if dp:
+            shape.moveBy(dp)
+            self.prevPoint = pos
+            return True
+        return False
 
     def deSelectShape(self):
         if self.selectedShape:
@@ -309,8 +313,19 @@ class Canvas(QWidget):
             self.shapes.append(shape)
             shape.selected = True
             self.selectedShape = shape
+            self.boundedShiftShape(shape)
             return shape
 
+    def boundedShiftShape(self, shape):
+        # Try to move in one direction, and if it fails in another.
+        # Give up if both fail.
+        point = shape[0]
+        offset = QPointF(2.0, 2.0)
+        self.calculateOffsets(shape, point)
+        self.prevPoint = point
+        if not self.boundedMoveShape(shape, point - offset):
+            self.boundedMoveShape(shape, point + offset)
+
     def paintEvent(self, event):
         if not self.pixmap:
             return super(Canvas, self).paintEvent(event)

+ 0 - 1
shape.py

@@ -150,7 +150,6 @@ class Shape(object):
         shape.fill = self.fill
         shape.selected = self.selected
         shape._closed = self._closed
-        shape.moveBy(QPointF(2.0, 2.0))
         return shape
 
     def __len__(self):