def dibujar(self):
        pen = QPen()
        brush = QBrush()
        pen.setWidth(3)

        for i in self.organizador:
            color = QColor(i.red, i.green, i.blue)
            brush.setStyle(Qt.SolidPattern)
            brush.setColor(color)
            pen.setColor(color)

            self.scene.addEllipse(i.or_x, i.or_y, 7, 7, pen, brush)
            self.scene.addEllipse(i.de_x, i.de_y, 7, 7, pen, brush)
            self.scene.addLine((i.or_x) + 3.5, (i.or_y) + 3.5, (i.de_x) + 3.5,
                               (i.de_y) + 3.5, pen)

        for keys in self.organizador.grafo_dic:
            text = QGraphicsTextItem(str(keys))
            text.setFlag(QGraphicsItem.ItemIsMovable)
            text.setFont(QFont("TimesNewRoman", 12, QFont.ExtraBold))
            self.scene.addItem(text)
            text.setPos(keys[0], keys[1])
Exemple #2
0
class GraphicsPortPainter:
    """The GraphicsPortPainter class provides a set of functions and configurations for
        painting a GraphicsPort object.
    """
    class DrawingState(Enum):
        Normal = 0
        Target = 1

    class PortNamePosition(Enum):
        Left = 0
        Right = 1

    drawing_state = DrawingState.Normal
    target_port_type = None

    def __init__(self, graphics_port: "GraphicsPort",
                 port_name_position: "PortNamePosition"):
        self.__graphics_port = graphics_port
        self.__port_name = QGraphicsTextItem(parent=graphics_port)
        self.__port_name.setPlainText(graphics_port._port.name)
        self.__port_name.setDefaultTextColor("#FFFFFF")
        self.__port_name.setFlag(QGraphicsItem.ItemStacksBehindParent)

        self.port_name_position = port_name_position

        # Colors/Pens/Brushes

        self.__color = TypeColor.get_color_for(graphics_port._port.port_type)

        self.__outline_pen = QPen(self.__color.darker())
        self.__outline_pen.setWidthF(2)
        self.__background_brush = QBrush(self.__color)

        self.__dashed_outline_pen = QPen(self.__color)
        self.__dashed_outline_pen.setStyle(Qt.DashLine)
        self.__dashed_outline_pen.setWidth(2)

    @property
    def port_name_position(self) -> "PortNamePosition":
        return self.__port_name_position

    @port_name_position.setter
    def port_name_position(self, position: "PortNamePosition"):
        if position == self.PortNamePosition.Left:
            self.__port_name.setPos(
                -self.__port_name.boundingRect().width() - 3, 1)
        elif position == self.PortNamePosition.Right:
            self.__port_name.setPos(3, 1)

        self.__port_name_position = position

    @property
    def color(self):
        return self.__color

    def paint_area(self):
        return QRectF(
            -self.__graphics_port.radius,
            -self.__graphics_port.radius,
            2 * self.__graphics_port.radius,
            2 * self.__graphics_port.radius,
        )

    def paint(
        self,
        painter: "QPainter",
        option: "QStyleOptionGraphicsItem",
        widget: "QWidget" = None,
    ):
        if (GraphicsPortPainter.drawing_state
                == GraphicsPortPainter.DrawingState.Target
                and self.__graphics_port.port_type
                == GraphicsPortPainter.target_port_type):
            painter.setPen(self.__dashed_outline_pen)
            painter.setBrush(Qt.NoBrush)
            painter.drawEllipse(self.__graphics_port.boundingRect())

        painter.setPen(self.__outline_pen)
        painter.setBrush(self.__background_brush)
        painter.drawEllipse(self.paint_area())