def mouseMoveEvent(self, ev):
        """Update line with last point and current coordinates."""
        pos = self.transformPos(ev.pos())

        # Update coordinates in status bar if image is opened
        window = self.parent().window()
        if window.filePath is not None:
            self.parent().window().labelCoordinates.setText(
                'X: %d; Y: %d' % (pos.x(), pos.y()))

        # Polygon drawing.
        if self.drawing():
            self.overrideCursor(CURSOR_DRAW)
            if self.current:
                color = self.drawingLineColor
                if self.outOfPixmap(pos):
                    # Don't allow the user to draw outside the pixmap.
                    # Project the point to the pixmap's edges.
                    pos = self.intersectionPoint(self.current[-1], pos)
                elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
                    # Attract line to starting point and colorise to alert the
                    # user:
                    pos = self.current[0]
                    color = self.current.line_color
                    self.overrideCursor(CURSOR_POINT)
                    self.current.highlightVertex(0, Shape.NEAR_VERTEX)

                if self.drawSquare:
                    initPos = self.current[0]
                    minX = initPos.x()
                    minY = initPos.y()
                    min_size = min(abs(pos.x() - minX), abs(pos.y() - minY))
                    directionX = -1 if pos.x() - minX < 0 else 1
                    directionY = -1 if pos.y() - minY < 0 else 1
                    self.line[1] = QPointF(minX + directionX * min_size, minY + directionY * min_size)
                else:
                    self.line[1] = pos

                self.line.line_color = color
                self.prevPoint = QPointF()
                self.current.highlightClear()
            else:
                self.prevPoint = pos
            self.repaint()
            return

        # Polygon copy moving.
        if Qt.RightButton & ev.buttons():
            if self.selectedShapeCopy and self.prevPoint:
                self.overrideCursor(CURSOR_MOVE)
                self.boundedMoveShape(self.selectedShapeCopy, pos)
                self.repaint()
            elif self.selectedShape:
                self.selectedShapeCopy = self.selectedShape.copy()
                self.repaint()
            return

        # Polygon/Vertex moving.
        if Qt.LeftButton & ev.buttons():
            if self.selectedVertex():
                self.boundedMoveVertex(pos)
                self.shapeMoved.emit()
                self.repaint()
            elif self.selectedShape and self.prevPoint:
                self.overrideCursor(CURSOR_MOVE)
                self.boundedMoveShape(self.selectedShape, pos)
                self.shapeMoved.emit()
                self.repaint()
            return

        # Just hovering over the canvas, 2 posibilities:
        # - Highlight shapes
        # - Highlight vertex
        # Update shape/vertex fill and tooltip value accordingly.
        self.setToolTip("Image")
        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:
                if self.selectedVertex():
                    self.hShape.highlightClear()
                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
            self.overrideCursor(CURSOR_DEFAULT)
def mouseMoveEvent ( self , ev ) : pos = self . transformPos ( ev . pos ( ) ) window = self . parent ( ) . window ( ) if window . filePath is not None : self . parent ( ) . window ( ) . labelCoordinates . setText ( 'X: %d; Y: %d' % ( pos . x ( ) , pos . y ( ) ) ) if self . drawing ( ) : self . overrideCursor ( CURSOR_DRAW ) if self . current : color = self . drawingLineColor if self . outOfPixmap ( pos ) : pos = self . intersectionPoint ( self . current [ - 1 ] , pos ) elif len ( self . current ) > 1 and self . closeEnough ( pos , self . current [ 0 ] ) : pos = self . current [ 0 ] color = self . current . line_color self . overrideCursor ( CURSOR_POINT ) self . current . highlightVertex ( 0 , Shape . NEAR_VERTEX ) if self . drawSquare : initPos = self . current [ 0 ] minX = initPos . x ( ) minY = initPos . y ( ) min_size = min ( abs ( pos . x ( ) - minX ) , abs ( pos . y ( ) - minY ) ) directionX = - 1 if pos . x ( ) - minX < 0 else 1 directionY = - 1 if pos . y ( ) - minY < 0 else 1 self . line [ 1 ] = QPointF ( minX + directionX * min_size , minY + directionY * min_size ) else : self . line [ 1 ] = pos self . line . line_color = color self . prevPoint = QPointF ( ) self . current . highlightClear ( ) else : self . prevPoint = pos self . repaint ( ) return if Qt . RightButton & ev . buttons ( ) : if self . selectedShapeCopy and self . prevPoint : self . overrideCursor ( CURSOR_MOVE ) self . boundedMoveShape ( self . selectedShapeCopy , pos ) self . repaint ( ) elif self . selectedShape : self . selectedShapeCopy = self . selectedShape . copy ( ) self . repaint ( ) return if Qt . LeftButton & ev . buttons ( ) : if self . selectedVertex ( ) : self . boundedMoveVertex ( pos ) self . shapeMoved . emit ( ) self . repaint ( ) elif self . selectedShape and self . prevPoint : self . overrideCursor ( CURSOR_MOVE ) self . boundedMoveShape ( self . selectedShape , pos ) self . shapeMoved . emit ( ) self . repaint ( ) return self . setToolTip ( "Image" ) for shape in reversed ( [ s for s in self . shapes if self . isVisible ( s ) ] ) : index = shape . nearestVertex ( pos , self . epsilon ) if index is not None : if self . selectedVertex ( ) : self . hShape . highlightClear ( ) 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 : if self . hShape : self . hShape . highlightClear ( ) self . update ( ) self . hVertex , self . hShape = None , None self . overrideCursor ( CURSOR_DEFAULT )
Update line with last point and current coordinates.