|
@@ -38,7 +38,6 @@ class Canvas(QWidget):
|
|
self.selectedShapeCopy=None
|
|
self.selectedShapeCopy=None
|
|
self.lineColor = QColor(0, 0, 255)
|
|
self.lineColor = QColor(0, 0, 255)
|
|
self.line = Shape(line_color=self.lineColor)
|
|
self.line = Shape(line_color=self.lineColor)
|
|
- self.mouseButtonIsPressed=False #when it is true and shape is selected , move the shape with the mouse move event
|
|
|
|
self.prevPoint = QPointF()
|
|
self.prevPoint = QPointF()
|
|
self.offsets = QPointF(), QPointF()
|
|
self.offsets = QPointF(), QPointF()
|
|
self.scale = 1.0
|
|
self.scale = 1.0
|
|
@@ -46,8 +45,8 @@ class Canvas(QWidget):
|
|
self.visible = {}
|
|
self.visible = {}
|
|
self._hideBackround = False
|
|
self._hideBackround = False
|
|
self.hideBackround = False
|
|
self.hideBackround = False
|
|
- self.highlightedShape = None
|
|
|
|
- self._nearest = None
|
|
|
|
|
|
+ self.hShape = None
|
|
|
|
+ self.hVertex = None
|
|
self._painter = QPainter()
|
|
self._painter = QPainter()
|
|
self._cursor = CURSOR_DEFAULT
|
|
self._cursor = CURSOR_DEFAULT
|
|
# Menus:
|
|
# Menus:
|
|
@@ -76,10 +75,15 @@ class Canvas(QWidget):
|
|
|
|
|
|
def setEditing(self, value=True):
|
|
def setEditing(self, value=True):
|
|
self.mode = self.EDIT if value else self.CREATE
|
|
self.mode = self.EDIT if value else self.CREATE
|
|
- if not value:
|
|
|
|
- self.highlightedShape = None
|
|
|
|
|
|
+ if not value: # Create
|
|
|
|
+ self.unHighlight()
|
|
self.deSelectShape()
|
|
self.deSelectShape()
|
|
|
|
|
|
|
|
+ def unHighlight(self):
|
|
|
|
+ self.hVertex = self.hShape = None
|
|
|
|
+
|
|
|
|
+ def selectedVertex(self):
|
|
|
|
+ return self.hVertex is not None
|
|
|
|
|
|
def mouseMoveEvent(self, ev):
|
|
def mouseMoveEvent(self, ev):
|
|
"""Update line with last point and current coordinates."""
|
|
"""Update line with last point and current coordinates."""
|
|
@@ -119,10 +123,11 @@ class Canvas(QWidget):
|
|
self.repaint()
|
|
self.repaint()
|
|
return
|
|
return
|
|
|
|
|
|
- # Polygon moving.
|
|
|
|
|
|
+ # Polygon/Vertex moving.
|
|
if Qt.LeftButton & ev.buttons():
|
|
if Qt.LeftButton & ev.buttons():
|
|
- if self._nearest and self.prevPoint:
|
|
|
|
|
|
+ if self.selectedVertex():
|
|
self.boundedMoveVertex(pos)
|
|
self.boundedMoveVertex(pos)
|
|
|
|
+ self.shapeMoved.emit()
|
|
self.repaint()
|
|
self.repaint()
|
|
elif self.selectedShape and self.prevPoint:
|
|
elif self.selectedShape and self.prevPoint:
|
|
self.overrideCursor(CURSOR_MOVE)
|
|
self.overrideCursor(CURSOR_MOVE)
|
|
@@ -131,34 +136,37 @@ class Canvas(QWidget):
|
|
self.repaint()
|
|
self.repaint()
|
|
return
|
|
return
|
|
|
|
|
|
- # Just hovering over the canvas:
|
|
|
|
- # Update tooltip value and fill topmost shape.
|
|
|
|
|
|
+ # Just hovering over the canvas, 2 posibilities:
|
|
|
|
+ # - Highlight shapes
|
|
|
|
+ # - Highlight vertex
|
|
|
|
+ # Update shape/vertex fill and tooltip value accordingly.
|
|
self.setToolTip("Image")
|
|
self.setToolTip("Image")
|
|
- previous = self.highlightedShape
|
|
|
|
- for shape in reversed(self.shapes):
|
|
|
|
- if self.isVisible(shape):
|
|
|
|
- v = shape.nearestVertex(pos, self.epsilon)
|
|
|
|
- if v is not None:
|
|
|
|
- self._nearest = (v, shape)
|
|
|
|
- shape.highlightVertex(v, shape.MOVE_VERTEX)
|
|
|
|
- self.highlightedShape = shape
|
|
|
|
- self.overrideCursor(CURSOR_POINT)
|
|
|
|
- self.setStatusTip("Click & drag to move point")
|
|
|
|
- break
|
|
|
|
- if shape.containsPoint(pos):
|
|
|
|
- self.setToolTip("Object '%s'" % shape.label)
|
|
|
|
- self.highlightedShape = shape
|
|
|
|
- self.overrideCursor(CURSOR_GRAB)
|
|
|
|
- break
|
|
|
|
- else:
|
|
|
|
- if self.highlightedShape:
|
|
|
|
- self.highlightedShape.highlightClear()
|
|
|
|
- self.highlightedShape = None
|
|
|
|
- self._nearest = None
|
|
|
|
-
|
|
|
|
- if previous != self.highlightedShape:
|
|
|
|
- # Try to minimise repaints.
|
|
|
|
- self.update()
|
|
|
|
|
|
+ for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
|
|
|
|
+ # Look for a nearby vertex to highlight. If that fails,
|
|
|
|
+ # check if we happen to be inside a shape.
|
|
|
|
+ index = shape.nearestVertex(pos, self.epsilon)
|
|
|
|
+ if index is not None:
|
|
|
|
+ self.hVertex, self.hShape = index, shape
|
|
|
|
+ shape.highlightVertex(index, shape.MOVE_VERTEX)
|
|
|
|
+ self.overrideCursor(CURSOR_POINT)
|
|
|
|
+ self.setToolTip("Click & drag to move point")
|
|
|
|
+ self.setStatusTip(self.toolTip())
|
|
|
|
+ self.update()
|
|
|
|
+ break
|
|
|
|
+ elif shape.containsPoint(pos):
|
|
|
|
+ if self.selectedVertex():
|
|
|
|
+ self.hShape.highlightClear()
|
|
|
|
+ self.hVertex, self.hShape = None, shape
|
|
|
|
+ self.setToolTip("Click & drag to move shape '%s'" % shape.label)
|
|
|
|
+ self.setStatusTip(self.toolTip())
|
|
|
|
+ self.overrideCursor(CURSOR_GRAB)
|
|
|
|
+ self.update()
|
|
|
|
+ break
|
|
|
|
+ else: # Nothing found, clear highlights, reset state.
|
|
|
|
+ if self.hShape:
|
|
|
|
+ self.hShape.highlightClear()
|
|
|
|
+ self.update()
|
|
|
|
+ self.hVertex, self.hShape = None, None
|
|
|
|
|
|
def mousePressEvent(self, ev):
|
|
def mousePressEvent(self, ev):
|
|
pos = self.transformPos(ev.posF())
|
|
pos = self.transformPos(ev.posF())
|
|
@@ -247,9 +255,9 @@ class Canvas(QWidget):
|
|
def selectShapePoint(self, point):
|
|
def selectShapePoint(self, point):
|
|
"""Select the first shape created which contains this point."""
|
|
"""Select the first shape created which contains this point."""
|
|
self.deSelectShape()
|
|
self.deSelectShape()
|
|
- if self._nearest:
|
|
|
|
- i, s = self._nearest
|
|
|
|
- s.highlightVertex(i, s.MOVE_VERTEX)
|
|
|
|
|
|
+ if self.selectedVertex(): # A vertex is marked for selection.
|
|
|
|
+ index, shape = self.hVertex, self.hShape
|
|
|
|
+ shape.highlightVertex(index, shape.MOVE_VERTEX)
|
|
return
|
|
return
|
|
for shape in reversed(self.shapes):
|
|
for shape in reversed(self.shapes):
|
|
if self.isVisible(shape) and shape.containsPoint(point):
|
|
if self.isVisible(shape) and shape.containsPoint(point):
|
|
@@ -269,11 +277,11 @@ class Canvas(QWidget):
|
|
self.offsets = QPointF(x1, y1), QPointF(x2, y2)
|
|
self.offsets = QPointF(x1, y1), QPointF(x2, y2)
|
|
|
|
|
|
def boundedMoveVertex(self, pos):
|
|
def boundedMoveVertex(self, pos):
|
|
- i, shape = self._nearest
|
|
|
|
- point = shape[i]
|
|
|
|
|
|
+ index, shape = self.hVertex, self.hShape
|
|
|
|
+ point = shape[index]
|
|
if self.outOfPixmap(pos):
|
|
if self.outOfPixmap(pos):
|
|
pos = self.intersectionPoint(point, pos)
|
|
pos = self.intersectionPoint(point, pos)
|
|
- shape.moveVertexBy(i, pos - point)
|
|
|
|
|
|
+ shape.moveVertexBy(index, pos - point)
|
|
|
|
|
|
def boundedMoveShape(self, shape, pos):
|
|
def boundedMoveShape(self, shape, pos):
|
|
if self.outOfPixmap(pos):
|
|
if self.outOfPixmap(pos):
|
|
@@ -350,7 +358,7 @@ class Canvas(QWidget):
|
|
Shape.scale = self.scale
|
|
Shape.scale = self.scale
|
|
for shape in self.shapes:
|
|
for shape in self.shapes:
|
|
if (shape.selected or not self._hideBackround) and self.isVisible(shape):
|
|
if (shape.selected or not self._hideBackround) and self.isVisible(shape):
|
|
- shape.fill = shape.selected or self.highlightedShape == shape
|
|
|
|
|
|
+ shape.fill = shape.selected or shape == self.hShape
|
|
shape.paint(p)
|
|
shape.paint(p)
|
|
if self.current:
|
|
if self.current:
|
|
self.current.paint(p)
|
|
self.current.paint(p)
|