def draw_triangle_port(painter, rect, info): """ Custom paint function for drawing a Triangle shaped port. Args: painter (QtGui.QPainter): painter object. rect (QtCore.QRectF): port rect used to describe parameters needed to draw. info (dict): information describing the ports current state. { 'port_type': 'in', 'color': (0, 0, 0), 'border_color': (255, 255, 255), 'multi_connection': False, 'connected': False, 'hovered': False, } """ painter.save() size = int(rect.height() / 2) triangle = QtGui.QPolygonF() triangle.append(QtCore.QPointF(-size, size)) triangle.append(QtCore.QPointF(0.0, -size)) triangle.append(QtCore.QPointF(size, size)) transform = QtGui.QTransform() transform.translate(rect.center().x(), rect.center().y()) port_poly = transform.map(triangle) # mouse over port color. if info['hovered']: color = QtGui.QColor(14, 45, 59) border_color = QtGui.QColor(136, 255, 35) # port connected color. elif info['connected']: color = QtGui.QColor(195, 60, 60) border_color = QtGui.QColor(200, 130, 70) # default port color else: color = QtGui.QColor(*info['color']) border_color = QtGui.QColor(*info['border_color']) pen = QtGui.QPen(border_color, 1.8) pen.setJoinStyle(QtCore.Qt.MiterJoin) painter.setPen(pen) painter.setBrush(color) painter.drawPolygon(port_poly) painter.restore()
def paint(self, painter, option, widget): """ Draws the connection line between nodes. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ color = QtGui.QColor(*self._color) pen_style = PIPE_STYLES.get(self.style) pen_width = PIPE_WIDTH if self._active: color = QtGui.QColor(*PIPE_ACTIVE_COLOR) if pen_style == QtCore.Qt.DashDotDotLine: pen_width += 1 else: pen_width += 0.35 elif self._highlight: color = QtGui.QColor(*PIPE_HIGHLIGHT_COLOR) pen_style = PIPE_STYLES.get(PIPE_STYLE_DEFAULT) if self.disabled(): if not self._active: color = QtGui.QColor(*PIPE_DISABLED_COLOR) pen_width += 0.2 pen_style = PIPE_STYLES.get(PIPE_STYLE_DOTTED) pen = QtGui.QPen(color, pen_width) pen.setStyle(pen_style) pen.setCapStyle(QtCore.Qt.RoundCap) painter.save() painter.setPen(pen) painter.setRenderHint(painter.Antialiasing, True) painter.drawPath(self.path()) # draw arrow if self.input_port and self.output_port: cen_x = self.path().pointAtPercent(0.5).x() cen_y = self.path().pointAtPercent(0.5).y() loc_pt = self.path().pointAtPercent(0.49) tgt_pt = self.path().pointAtPercent(0.51) dist = math.hypot(tgt_pt.x() - cen_x, tgt_pt.y() - cen_y) if dist < 0.5: painter.restore() return color.setAlpha(255) if self._highlight: painter.setBrush(QtGui.QBrush(color.lighter(150))) elif self._active or self.disabled(): painter.setBrush(QtGui.QBrush(color.darker(200))) else: painter.setBrush(QtGui.QBrush(color.darker(130))) pen_width = 0.6 if dist < 1.0: pen_width *= (1.0 + dist) painter.setPen(QtGui.QPen(color, pen_width)) transform = QtGui.QTransform() transform.translate(cen_x, cen_y) radians = math.atan2(tgt_pt.y() - loc_pt.y(), tgt_pt.x() - loc_pt.x()) degrees = math.degrees(radians) - 90 transform.rotate(degrees) if dist < 1.0: transform.scale(dist, dist) painter.drawPolygon(transform.map(self._arrow)) painter.restore() # QPaintDevice: Cannot destroy paint device that is being painted
def paint(self, painter, option, widget): """ Draws the connection line. Args: painter (QtGui.QPainter): painter used for drawing the item. option (QtGui.QStyleOptionGraphicsItem): used to describe the parameters needed to draw. widget (QtWidgets.QWidget): not used. """ color = QtGui.QColor(*PIPE_ACTIVE_COLOR) pen_style = PIPE_STYLES.get(PIPE_STYLE_DASHED) pen_width = PIPE_WIDTH + 0.35 pen = QtGui.QPen(color, pen_width) pen.setStyle(pen_style) pen.setCapStyle(QtCore.Qt.RoundCap) painter.save() painter.setPen(pen) painter.setRenderHint(painter.Antialiasing, True) painter.drawPath(self.path()) cen_x = self.path().pointAtPercent(0.5).x() cen_y = self.path().pointAtPercent(0.5).y() loc_pt = self.path().pointAtPercent(0.9) tgt_pt = self.path().pointAtPercent(1.0) dist = math.hypot(tgt_pt.x() - cen_x, tgt_pt.y() - cen_y) if dist < 0.05: painter.restore() return # draw circle size = 10.0 if dist < 50.0: size *= (dist / 50.0) rect = QtCore.QRectF(cen_x-(size/2), cen_y-(size/2), size, size) painter.setBrush(color) painter.setPen(QtGui.QPen(color.darker(130), pen_width)) painter.drawEllipse(rect) # draw arrow color.setAlpha(255) painter.setBrush(color.darker(200)) pen_width = 0.6 if dist < 1.0: pen_width *= 1.0 + dist painter.setPen(QtGui.QPen(color, pen_width)) transform = QtGui.QTransform() transform.translate(tgt_pt.x(), tgt_pt.y()) radians = math.atan2(tgt_pt.y() - loc_pt.y(), tgt_pt.x() - loc_pt.x()) degrees = math.degrees(radians) + 90 transform.rotate(degrees) scale = 1.0 if dist < 20.0: scale = dist / 20.0 transform.scale(scale, scale) painter.drawPolygon(transform.map(self._arrow)) painter.restore()