def createImage(self, matrix):
        if self.type == DemoTextItem.DYNAMIC_TEXT:
            return None

        sx = min(matrix.m11(), matrix.m22())
        sy = max(matrix.m22(), sx)

        textItem = QtGui.QGraphicsTextItem()
        textItem.setHtml(self.text)
        textItem.setTextWidth(self.textWidth)
        textItem.setFont(self.font)
        textItem.setDefaultTextColor(self.textColor)
        textItem.document().setDocumentMargin(2)

        w = textItem.boundingRect().width()
        h = textItem.boundingRect().height()
        image = QtGui.QImage(int(w * sx), int(h * sy),
                QtGui.QImage.Format_ARGB32_Premultiplied)
        image.fill(QtGui.QColor(0, 0, 0, 0).rgba())
        painter = QtGui.QPainter(image)
        painter.scale(sx, sy)
        style = QtGui.QStyleOptionGraphicsItem()
        textItem.paint(painter, style, None)

        return image
示例#2
0
文件: network.py 项目: soo1234/pynet
    def _create_control(self, control_name, control_position, is_output=False,
                        control_width=12, margin=5):
        """ Create a control representation: small glyph and control name.

        Parameters
        ----------
        control_name: str (mandatory)
            the name of the control to render.
        control_position: int (mandatory)
            the position (height) of the control to render.
        control_name: bool (optional, default False)
            an input control glyph is diplayed on the left while an output
            control glyph is displayed on the right.
        control_width: int (optional, default 12)
            the default size of the control glyph.
        margin: int (optional, default 5)
            the default margin.

        Returns
        -------
        control_text: QGraphicsTextItem
            the control text item.
        control_glyph: Control
            the associated control glyph item.
        """
        # Detect if the control is optional
        is_optional = False

        # Create the control representation
        control_text = QtGui.QGraphicsTextItem(self)
        control_text.setHtml(control_name)
        control_name = "{0}:{1}".format(self.name, control_name)
        control_glyph = Control(
            control_name, control_text.boundingRect().size().height(),
            control_width, optional=is_optional, parent=self)
        control_text.setZValue(2)
        control_glyph_width = control_glyph.boundingRect().size().width()
        control_title_width = self.title.boundingRect().size().width()
        control_text.setPos(control_glyph_width + margin, control_position)
        if is_output:
            control_glyph.setPos(
                control_title_width - control_glyph_width,
                control_position)
        else:
            control_glyph.setPos(margin, control_position)
        control_text.setParentItem(self)
        control_glyph.setParentItem(self)

        return control_glyph, control_text
示例#3
0
文件: network.py 项目: soo1234/pynet
    def _build(self, margin=5):
        """ Create a node reprensenting a box.

        Parameters
        ----------
        margin: int (optional, default 5)
            the default margin.
        """
        # Create a title for the node
        self.title = QtGui.QGraphicsTextItem(self.get_title(), self)
        font = self.title.font()
        font.setWeight(QtGui.QFont.Bold)
        self.title.setFont(font)
        self.title.setPos(margin, margin)
        self.title.setZValue(2)
        self.title.setParentItem(self)

        # Define the default control position
        control_position = (
            margin + margin + self.title.boundingRect().size().height())

        # Create the input controls
        for input_name in self.inputs:

            # Create the control representation
            control_glyph, control_text = self._create_control(
                input_name, control_position, is_output=False, margin=margin)

            # Update the class parameters
            self.input_controls[input_name] = (control_glyph, control_text)

            # Update the next control position
            control_position += control_text.boundingRect().size().height()

        # Create the output controls
        for output_name in self.outputs:

            # Create the control representation
            control_glyph, control_text = self._create_control(
                output_name, control_position, is_output=True, margin=margin)

            # Update the class parameters
            self.output_controls[output_name] = (control_glyph, control_text)

            # Update the next control position
            control_position += control_text.boundingRect().size().height()

        # Define the box node
        self.box = QtGui.QGraphicsRectItem(self)
        self.box.setBrush(self.background_brush)
        self.box.setPen(QtGui.QPen(QtCore.Qt.NoPen))
        self.box.setZValue(-1)
        self.box.setParentItem(self)
        self.box.setRect(self.contentsRect())
        self.box_title = QtGui.QGraphicsRectItem(self)
        rect = self.title.mapRectToParent(self.title.boundingRect())
        brect = self.contentsRect()
        brect.setWidth(brect.right() - margin)
        rect.setWidth(brect.width())
        self.box_title.setRect(rect)
        self.box_title.setBrush(self.title_brush)
        self.box_title.setPen(QtGui.QPen(QtCore.Qt.NoPen))
        self.box_title.setParentItem(self)