def GetCubicPath(points: List[Tuple[int, int]]): """Returns a drawable path representing a 4 point cubic curve. Points should be a list of 4 tuples representing four (x, y) pixel values.""" path = QPainterPath() x0, y0 = points[0] x1, y1 = points[1] x2, y2 = points[2] x3, y3 = points[3] path.moveTo(x0, y0) path.cubicTo(x1, y1, x2, y2, x3, y3) return path
def __update(self) -> None: color = port_colors[self.__connection.type] if self.__highlighted: pen = QtGui.QPen() pen.setColor(color) pen.setWidth(4) self.setPen(pen) else: pen = QtGui.QPen() pen.setColor(color) pen.setWidth(2) self.setPen(pen) pos1 = self.__src_node.portHandleScenePos( self.__connection.source_port) pos2 = self.__dest_node.portHandleScenePos(self.__connection.dest_port) cpos = QtCore.QPointF(min(100, abs(pos2.x() - pos1.x()) / 2), 0) path = QtGui.QPainterPath() path.moveTo(pos1) path.cubicTo(pos1 + cpos, pos2 - cpos, pos2) self.setPath(path)
def polygonToCurvedPath(polygon, radius): path = QPainterPath() for i, pt in enumerate(polygon): #TODO: if two points are too close to draw the desired radius, either remove those points or draw at smaller radius px, py = polygon[i - 1] if i > 0 else polygon[-1] nx, ny = polygon[i + 1] if i < len(polygon) - 1 else polygon[0] x, y = pt if px == x: dy = y - py r = radius if dy < 0 else -radius p1 = QPointF(x, y + r) else: dx = x - px r = radius if dx < 0 else -radius p1 = QPointF(x + r, y) if x == nx: dy = y - ny r = radius if dy < 0 else -radius p2 = QPointF(x, y + r) else: dx = x - nx r = radius if dx < 0 else -radius p2 = QPointF(x + r, y) if i == 0: path.moveTo(p1) else: path.lineTo(p1) path.cubicTo(pt, pt, p2) path.closeSubpath() return path
def paintEvent(self, event): ## Draw Background Grid minorGridSpacing = 25 lineLength = 10000 lineCount = 100 majorGridSpacing = 8 minorGridColor = [100, 100, 100, 100] majorGridColor = [0, 0, 0, 255] ## Draw grid background painter = QPainter(self) painter.setPen( QPen( QColor(minorGridColor[0], minorGridColor[1], minorGridColor[2], minorGridColor[3]), 1, Qt.SolidLine)) ##TODO: This is hapazard with the line distances. Probably should be fixed for i in range(0, lineCount): painter.drawLine(minorGridSpacing * i, 0, minorGridSpacing * i, lineLength) painter.drawLine(0, minorGridSpacing * i, lineLength, minorGridSpacing * i) painter.setPen( QPen( QColor(majorGridColor[0], majorGridColor[1], majorGridColor[2], majorGridColor[3]), 2, Qt.SolidLine)) for i in range(0, lineCount): painter.drawLine(majorGridSpacing * minorGridSpacing * i, 0, majorGridSpacing * minorGridSpacing * i, lineLength) painter.drawLine(0, majorGridSpacing * minorGridSpacing * i, lineLength, majorGridSpacing * minorGridSpacing * i) painter.setPen(QPen(QColor(255, 255, 255), 2, Qt.SolidLine)) painter.setRenderHint(QPainter.Antialiasing) cubicCurveFactor = 0.5 if (self.mouseIsHot): mousePos = self.mapFromGlobal(QtGui.QCursor.pos()) hotPortLoc = self.hotPort.parent.pos() + self.hotPort.pos( ) + QPoint(self.hotPort.width / 2, self.hotPort.height / 2) path = QPainterPath() path.moveTo(hotPortLoc) ##TODO CUBIC PATH path.cubicTo( hotPortLoc + QPoint( (mousePos.x() - hotPortLoc.x()) * cubicCurveFactor, 0), mousePos - QPoint( (mousePos.x() - hotPortLoc.x()) * cubicCurveFactor, 0), mousePos) painter.drawPath(path) # painter.drawLine(hotPortLoc.x(), hotPortLoc.y(), mousePos.x(), mousePos.y()) for input in self.connectionManager.inputPorts: for connection in input.connections: painter.setPen(QPen(QColor(255, 255, 255), 2, Qt.SolidLine)) painter.setRenderHint(QPainter.Antialiasing) port1Pos = input.parent.mapToParent(input.pos()) + QPoint( input.size().width(), input.size().height()) / 2 - 0 * QPoint( self.size().width(), 0) port2Pos = connection.parent.mapToParent(connection.pos( )) + QPoint(connection.size().width(), connection.size().height()) / 2 - 0 * QPoint( self.size().width(), 0) path = QPainterPath() path.moveTo(port1Pos) ##TODO CUBIC PATH path.cubicTo( port1Pos - QPoint( (port1Pos.x() - port2Pos.x()) * cubicCurveFactor, 0), port2Pos + QPoint( (port1Pos.x() - port2Pos.x()) * cubicCurveFactor, 0), port2Pos) painter.drawPath(path) if self.shouldDrawSelectionBox: painter.setPen(QPen(QColor(255, 255, 255), 1, Qt.DashLine)) selectionRect = QRect( np.min( [self.mousePressLocation.x(), self.mouseDragLocation.x()]), np.min( [self.mousePressLocation.y(), self.mouseDragLocation.y()]), np.abs(self.mousePressLocation.x() - self.mouseDragLocation.x()), np.abs(self.mousePressLocation.y() - self.mouseDragLocation.y())) painter.drawRect(selectionRect) for brick in self.bricks: P1 = brick.rect().topLeft() brickRect = QRect( brick.mapTo(self, P1), QSize(brick.rect().size().width(), brick.rect().size().height())) if selectionRect.intersects(brickRect): self.addToSelected(brick) if brick not in self.boxedRects: self.boxedRects.append(brick) else: if brick in self.boxedRects: self.deselect(brick) self.boxedRects.remove(brick)