예제 #1
0
    def midLineDraw(self, event):

        newLine = QtCore.QLineF(self.line.line().p1(), event.scenePos())
        self.line.setLine(newLine)

        # attach source or sink connection
        #if QtCore.Qt.AltModifier == getKeyboardModifiers():
        #    self.line.setPen(QtGui.QPen(QtCore.Qt.blue, 2))
        #    self.dumpCursorStack()
        #    QtGui.QApplication.setOverrideCursor(
        #        QtGui.QCursor(QtCore.Qt.CrossCursor))
        #    return
        #else:
        #    self.line.setPen(QtGui.QPen(QtCore.Qt.red, 2))

        startItems = self.items(event.scenePos())
        if len(startItems) and startItems[0] == self.line:
            startItems.pop(0)

        self.dumpCursorStack()
        QtGui.QApplication.setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.ForbiddenCursor))

        if len(startItems):
            if isinstance(startItems[0], Port):
                self.dumpCursorStack()
                QtGui.QApplication.setOverrideCursor(
                    QtGui.QCursor(QtCore.Qt.CrossCursor))
예제 #2
0
    def endLineDraw(self, event):
        self.dumpCursorStack()
        QtGui.QApplication.setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.OpenHandCursor))
        QtGui.QApplication.restoreOverrideCursor()

        startItems = self.items(self.line.line().p1())

        # if there are no valid start items then mark for removal
        if len(startItems) and startItems[0] == self.line:
            startItems.pop(0)

        # reset any hovering events
        if len(startItems):
            if isinstance(startItems[0], Port):
                startItems[0].hoverLeaveEvent(event)

        # if there are no valid end items then mark for removal
        endItems = self.items(self.line.line().p2())
        if len(endItems) and endItems[0] == self.line:
            endItems.pop(0)

        # remove temporary drag line
        self.removeItem(self.line)
        self.line = None

        # if there are valid start and end items then continue
        if len(startItems) and len(endItems):

            # connect ports
            if isinstance(startItems[0], Port) and isinstance(endItems[0], Port) \
                    and startItems[0] != endItems[0] and type(startItems[0]) != type(endItems[0]):

                # find which is which
                if isinstance(startItems[0], InPort):
                    inport = startItems[0]
                    outport = endItems[0]
                else:
                    inport = endItems[0]
                    outport = startItems[0]

                # check if the ports belong to the same node
                if inport.node == outport.node:
                    log.warn(
                        "CanvasScene: inport and outport belong to the same node."
                    )
                    super(CanvasScene, self).mouseReleaseEvent(event)
                    return

                # check the number of connections on the inport
                # if one exists then skip
                if len(inport.edges()) > 0:
                    log.warn(
                        "CanvasScene: inport occupied, connection dropped")
                    super(CanvasScene, self).mouseReleaseEvent(event)
                    return

                # This, currently, is the only way edges know
                # which port is actually a source or dest.
                # NOTE: Network description files will have to enforce this.
                newEdge = Edge(outport, inport)
                self.addItem(newEdge)

                # if its cyclic then don't allow the connection
                # -at the same time, calculate the hierarchy
                nodeHierarchy = inport.getNode().graph.calcNodeHierarchy()
                if nodeHierarchy is None:
                    self.removeItem(newEdge)
                    newEdge.detachSelf()
                    # del newEdge
                    log.warn("CanvasScene: cyclic, connection dropped")
                else:
                    # CONNECTION ADDED
                    # Since node hierarchy is recalculated, also
                    # take the time to flag nodes for processing
                    # 1) check for matching spec type
                    if not (inport.checkUpstreamPortType()):
                        self.removeItem(newEdge)
                        newEdge.detachSelf(update=False)
                        # del newEdge
                        log.warn(
                            "CanvasScene: data type mismatch, connection dropped"
                        )
                    else:
                        # 2) set the downstream node's pending_event
                        inport.getNode().setEventStatus(
                            {GPI_PORT_EVENT: inport.portTitle})

                        # trigger a force recalculation
                        inport.getNode().graph.itemMoved()

                        # trigger name update
                        inport.getNode().refreshName()
                        outport.getNode().refreshName()

                        # trigger event queue, if its idle
                        inport.getNode().graph._switchSig.emit('check')

                        if len(self.portMatches):
                            for port in self.portMatches:
                                port.resetScale()
                            self.portMatches = []

                        inport.edges()[0].adjust()
                        for edge in outport.edges():
                            edge.adjust()

            # remove edge by drawing a line across it
            elif isinstance(startItems[0], Edge) and isinstance(
                    startItems[0], Edge) and startItems[0] == endItems[0]:
                # remove from scene
                self.removeItem(startItems[0])
                # remove from ports
                startItems[0].detachSelf()
                # remove from memory
                # del startItems[0]
        # reset the port size changes during port connect.
        if len(self.portMatches):
            for port in self.portMatches:
                port.resetScale()
            self.portMatches = []
        self.line = None