def setDragPoint(self, dragPoint): topLeft = QtCore.QPointF(self.__mouseDownPos) bottomRight = QtCore.QPointF(dragPoint) if dragPoint.x() < self.__mouseDownPos.x(): topLeft.setX(dragPoint.x()) bottomRight.setX(self.__mouseDownPos.x()) if dragPoint.y() < self.__mouseDownPos.y(): topLeft.setY(dragPoint.y()) bottomRight.setY(self.__mouseDownPos.y()) self.setPos(topLeft) self.resize(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y())
def paint(self, painter, option, widget): srcPoint = self.mapFromScene( self.__srcPortCircle.centerInSceneCoords()) dstPoint = self.mapFromScene( self.__dstPortCircle.centerInSceneCoords()) dist_between = dstPoint - srcPoint self.__path = QtGui.QPainterPath() self.__path.moveTo(srcPoint) self.__path.cubicTo( srcPoint + QtCore.QPointF(dist_between.x() * 0.4, 0), dstPoint - QtCore.QPointF(dist_between.x() * 0.4, 0), dstPoint) self.setPath(self.__path) super(Connection, self).paint(painter, option, widget)
def generateNodes(count, offset, depth): for i in range(count): node1 = Node(graph, 'node' + str(depth) + str(i)) node1.addPort( InputPort(node1, graph, 'InPort', QtGui.QColor(128, 170, 170, 255), 'MyDataX')) node1.addPort( OutputPort(node1, graph, 'OutPort', QtGui.QColor(32, 255, 32, 255), 'MyDataX')) node1.setGraphPos(QtCore.QPointF(offset, i * 80)) graph.addNode(node1) global totalCount totalCount += 1 if depth < 6: generateNodes(count * 2, offset + 160, depth + 1) for i in range(count): graph.connectPorts('node' + str(depth) + str(i), 'OutPort', 'node' + str(depth + 1) + str(i * 2), 'InPort') graph.connectPorts('node' + str(depth) + str(i), 'OutPort', 'node' + str(depth + 1) + str(i * 2 + 1), 'InPort') elif depth < 12: generateNodes(int(count / 2), offset + 160, depth + 1) for i in range(count / 2): graph.connectPorts('node' + str(depth) + str(i), 'OutPort', 'node' + str(depth + 1) + str(int(i)), 'InPort')
def setData(self, data): """Sets the data on a backdrop after loading. Args: data (dict): Name, comment, graph pos, size, and color. Returns: bool: True if successful. """ self.setComment(data.get('comment', '')) size = data.get('size', (self.minimumWidth(), self.minimumHeight())) self.resize(size[0], size[1]) position = data.get('graphPos', (0, 0)) self.setGraphPos(QtCore.QPointF(position[0], position[1])) color = data.get('color', self.__defaultColor.toTuple()) self.setColor( color=QtGui.QColor(color[0], color[1], color[2], color[3])) self.setUnselectedColor(self.getColor().darker(125)) self.setSelectedColor(self.getColor().lighter(175)) self.setHoveredColor(self.getColor().lighter(110)) return True
def __init__(self, graph, component): super(KNode, self).__init__(graph, component.getDecoratedName()) self.__component = component self.__inspectorWidget = None for i in range(self.__component.getNumInputs()): componentInput = component.getInputByIndex(i) self.addPort(KNodeInputPort(self, graph, componentInput)) for i in range(self.__component.getNumOutputs()): componentOutput = component.getOutputByIndex(i) self.addPort(KNodeOutputPort(self, graph, componentOutput)) self.setGraphPos( QtCore.QPointF(self.__component.getGraphPos().x, self.__component.getGraphPos().y)) nodeColor = component.getComponentColor() self.setColor( QtGui.QColor(nodeColor[0], nodeColor[1], nodeColor[2], nodeColor[3])) self.setUnselectedColor(self.getColor().darker(125)) self.setSelectedColor(self.getColor().lighter(175))
def mouseMoveEvent(self, event): if self.__dragging: newPos = self.mapToScene(event.pos()) graph = self.getGraph() if graph.getSnapToGrid() is True: gridSize = graph.getGridSize() newNodePos = newPos - self._mouseDelta snapPosX = math.floor(newNodePos.x() / gridSize) * gridSize snapPosY = math.floor(newNodePos.y() / gridSize) * gridSize snapPos = QtCore.QPointF(snapPosX, snapPosY) newPosOffset = snapPos - newNodePos newPos = newPos + newPosOffset delta = newPos - self._lastDragPoint self.__graph.moveSelectedNodes(delta) self._lastDragPoint = newPos self._nodesMoved = True else: super(Node, self).mouseMoveEvent(event)
node1.addPort( InputPort(node1, graph, 'InPort1', QtGui.QColor(128, 170, 170, 255), 'MyDataX')) node1.addPort( InputPort(node1, graph, 'InPort2', QtGui.QColor(128, 170, 170, 255), 'MyDataX')) node1.addPort( OutputPort(node1, graph, 'OutPort', QtGui.QColor(32, 255, 32, 255), 'MyDataY')) node1.addPort( IOPort(node1, graph, 'IOPort1', QtGui.QColor(32, 255, 32, 255), 'MyDataY')) node1.addPort( IOPort(node1, graph, 'IOPort2', QtGui.QColor(32, 255, 32, 255), 'MyDataY')) node1.setGraphPos(QtCore.QPointF(-100, 0)) graph.addNode(node1) node2 = Node(graph, 'ReallyLongLabel') node2.addPort( InputPort(node2, graph, 'InPort1', QtGui.QColor(128, 170, 170, 255), 'MyDataX')) node2.addPort( InputPort(node2, graph, 'InPort2', QtGui.QColor(128, 170, 170, 255), 'MyDataX')) node2.addPort( OutputPort(node2, graph, 'OutPort', QtGui.QColor(32, 255, 32, 255), 'MyDataY')) node2.addPort( IOPort(node2, graph, 'IOPort1', QtGui.QColor(32, 255, 32, 255),
def getGraphPos(self): transform = self.transform() size = self.size() return QtCore.QPointF(transform.dx()+(size.width()*0.5), transform.dy()+(size.height()*0.5))
def mouseMoveEvent(self, event): if self.__dragging: newPos = self.mapToScene(event.pos()) graph = self.getGraph() if graph.getSnapToGrid() is True: gridSize = graph.getGridSize() newNodePos = newPos - self._mouseDelta snapPosX = math.floor(newNodePos.x() / gridSize) * gridSize snapPosY = math.floor(newNodePos.y() / gridSize) * gridSize snapPos = QtCore.QPointF(snapPosX, snapPosY) newPosOffset = snapPos - newNodePos newPos = newPos + newPosOffset delta = newPos - self._lastDragPoint self.__graph.moveSelectedNodes(delta) self._lastDragPoint = newPos self._nodesMoved = True elif self.__resizing: newPos = self.mapToScene(event.pos()) delta = newPos - self._mouseDownPoint self._resizedBackdrop = True newPosX = 0 newPosY = 0 newWidth = self._initBoundingRect.width() newHeight = self._initBoundingRect.height() if self.__resizeCorner == 0: newWidth = self._initBoundingRect.width() + (delta.x() * -1.0) newHeight = self._initBoundingRect.height() + (delta.y() * -1.0) if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() else: newPosX = self._initPos.x() + delta.x() if newHeight <= self.minimumHeight(): newHeight = self.minimumHeight() else: newPosY = self._initPos.y() + delta.y() elif self.__resizeCorner == 1: newWidth = self._initBoundingRect.width() + delta.x() newHeight = self._initBoundingRect.height() + (delta.y() * -1.0) if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() else: newPosX = self._initPos.x() if newHeight <= self.minimumHeight(): newHeight = self.minimumHeight() else: newPosY = self._initPos.y() + delta.y() elif self.__resizeCorner == 2: newWidth = self._initBoundingRect.width() + (delta.x() * -1.0) newHeight = self._initBoundingRect.height() + delta.y() if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() else: newPosX = self._initPos.x() + delta.x() if newHeight <= self.minimumHeight(): newHeight = self.minimumHeight() else: newPosY = self._initPos.y() elif self.__resizeCorner == 3: newPosX = self._initPos.x() newPosY = self._initPos.y() newWidth = self._initBoundingRect.width() + delta.x() newHeight = self._initBoundingRect.height() + delta.y() if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() if newHeight <= self.minimumHeight(): newHeight = self.minimumHeight() elif self.__resizeEdge == 0: pass elif self.__resizeEdge == 1: newWidth = self._initBoundingRect.width() + delta.x() newHeight = self._initBoundingRect.height() newPosY = self._initPos.y() if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() else: newPosX = self._initPos.x() elif self.__resizeEdge == 2: newWidth = self._initBoundingRect.width() newHeight = self._initBoundingRect.height() + delta.y() newPosX = self._initPos.x() if newHeight <= self.minimumHeight(): newHeight = self.minimumHeight() else: newPosY = self._initPos.y() elif self.__resizeEdge == 3: newWidth = self._initBoundingRect.width() + (delta.x() * -1.0) newHeight = self._initBoundingRect.height() newPosY = self._initPos.y() if newWidth <= self.minimumWidth(): newWidth = self.minimumWidth() else: newPosX = self._initPos.x() + delta.x() self.setPos(newPosX, newPosY) self.resize(newWidth, newHeight) self.sizeChanged.emit(newWidth) self.prepareGeometryChange() else: super(KBackdrop, self).mouseMoveEvent(event)