Exemple #1
0
    def addPixmap(self,
                  pixmap,
                  title=None,
                  comment=None,
                  commentPosition=None):
        """Add a pixmap to the print preview scene

        :param QPixmap pixmap: Pixmap to be added to the scene
        :param str title: Title shown above (centered) the pixmap
        :param str comment: Comment displayed below the pixmap
        :param commentPosition: "CENTER" or "LEFT"
        """
        if self._toBeCleared:
            self._clearAll()
        self.ensurePrinterIsSet()
        if self.printer is None:
            _logger.error("printer is not set, cannot add pixmap to page")
            return
        if title is None:
            title = ' ' * 88
        if comment is None:
            comment = ' ' * 88
        if commentPosition is None:
            commentPosition = "CENTER"
        if qt.qVersion() < "5.0":
            rectItem = qt.QGraphicsRectItem(self.page, self.scene)
        else:
            rectItem = qt.QGraphicsRectItem(self.page)

        rectItem.setRect(qt.QRectF(1, 1, pixmap.width(), pixmap.height()))

        pen = rectItem.pen()
        color = qt.QColor(qt.Qt.red)
        color.setAlpha(1)
        pen.setColor(color)
        rectItem.setPen(pen)
        rectItem.setZValue(1)
        rectItem.setFlag(qt.QGraphicsItem.ItemIsSelectable, True)
        rectItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)
        rectItem.setFlag(qt.QGraphicsItem.ItemIsFocusable, False)

        rectItemResizeRect = _GraphicsResizeRectItem(rectItem, self.scene)
        rectItemResizeRect.setZValue(2)

        if qt.qVersion() < "5.0":
            pixmapItem = qt.QGraphicsPixmapItem(rectItem, self.scene)
        else:
            pixmapItem = qt.QGraphicsPixmapItem(rectItem)
        pixmapItem.setPixmap(pixmap)
        pixmapItem.setZValue(0)

        # I add the title
        if qt.qVersion() < "5.0":
            textItem = qt.QGraphicsTextItem(title, rectItem, self.scene)
        else:
            textItem = qt.QGraphicsTextItem(title, rectItem)
        textItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        offset = 0.5 * textItem.boundingRect().width()
        textItem.moveBy(0.5 * pixmap.width() - offset, -20)
        textItem.setZValue(2)

        # I add the comment
        if qt.qVersion() < "5.0":
            commentItem = qt.QGraphicsTextItem(comment, rectItem, self.scene)
        else:
            commentItem = qt.QGraphicsTextItem(comment, rectItem)
        commentItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        offset = 0.5 * commentItem.boundingRect().width()
        if commentPosition.upper() == "LEFT":
            x = 1
        else:
            x = 0.5 * pixmap.width() - offset
        commentItem.moveBy(x, pixmap.height() + 20)
        commentItem.setZValue(2)

        rectItem.moveBy(20, 40)
Exemple #2
0
    def addSvgItem(self,
                   item,
                   title=None,
                   comment=None,
                   commentPosition=None,
                   viewBox=None):
        """Add a SVG item to the scene.

        :param QSvgRenderer item: SVG item to be added to the scene.
        :param str title: Title shown above (centered) the SVG item.
        :param str comment: Comment displayed below the SVG item.
        :param str commentPosition: "CENTER" or "LEFT"
        :param QRectF viewBox: Bounding box for the item on the print page
            (xOffset, yOffset, width, height). If None, use original
            item size.
        """
        if not qt.HAS_SVG:
            raise RuntimeError("Missing QtSvg library.")
        if not isinstance(item, qt.QSvgRenderer):
            raise TypeError("addSvgItem: QSvgRenderer expected")
        if self._toBeCleared:
            self._clearAll()
        self.ensurePrinterIsSet()
        if self.printer is None:
            _logger.error("printer is not set, cannot add SvgItem to page")
            return

        if title is None:
            title = 50 * ' '
        if comment is None:
            comment = 80 * ' '
        if commentPosition is None:
            commentPosition = "CENTER"

        if viewBox is None:
            if hasattr(item, "_viewBox"):
                # PyMca compatibility: viewbox attached to item
                viewBox = item._viewBox
            else:
                # try the original item viewbox
                viewBox = item.viewBoxF()

        svgItem = _GraphicsSvgRectItem(viewBox, self.page)
        svgItem.setSvgRenderer(item)

        svgItem.setCacheMode(qt.QGraphicsItem.NoCache)
        svgItem.setZValue(0)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsSelectable, True)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsFocusable, False)

        rectItemResizeRect = _GraphicsResizeRectItem(svgItem, self.scene)
        rectItemResizeRect.setZValue(2)

        self._svgItems.append(item)

        if qt.qVersion() < '5.0':
            textItem = qt.QGraphicsTextItem(title, svgItem, self.scene)
        else:
            textItem = qt.QGraphicsTextItem(title, svgItem)
        textItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        title_offset = 0.5 * textItem.boundingRect().width()
        textItem.setZValue(1)
        textItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)

        dummyComment = 80 * "1"
        if qt.qVersion() < '5.0':
            commentItem = qt.QGraphicsTextItem(dummyComment, svgItem,
                                               self.scene)
        else:
            commentItem = qt.QGraphicsTextItem(dummyComment, svgItem)
        commentItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        scaleCalculationRect = qt.QRectF(commentItem.boundingRect())
        scale = svgItem.boundingRect().width() / scaleCalculationRect.width()
        comment_offset = 0.5 * commentItem.boundingRect().width()
        if commentPosition.upper() == "LEFT":
            x = 1
        else:
            x = 0.5 * svgItem.boundingRect().width(
            ) - comment_offset * scale  # fixme: centering
        commentItem.moveBy(
            svgItem.boundingRect().x() + x,
            svgItem.boundingRect().y() + svgItem.boundingRect().height())
        commentItem.setPlainText(comment)
        commentItem.setZValue(1)

        commentItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)
        if qt.qVersion() < "5.0":
            commentItem.scale(scale, scale)
        else:
            # the correct equivalent would be:
            # rectItem.setTransform(qt.QTransform.fromScale(scalex, scaley))
            commentItem.setScale(scale)
        textItem.moveBy(
            svgItem.boundingRect().x() + 0.5 * svgItem.boundingRect().width() -
            title_offset * scale,
            svgItem.boundingRect().y())
        if qt.qVersion() < "5.0":
            textItem.scale(scale, scale)
        else:
            # the correct equivalent would be:
            # rectItem.setTransform(qt.QTransform.fromScale(scalex, scaley))
            textItem.setScale(scale)
    def addSvgItem(self, item, title=None,
                   comment=None, commentPosition=None,
                   viewBox=None, keepRatio=True):
        """Add a SVG item to the scene.

        :param QSvgRenderer item: SVG item to be added to the scene.
        :param str title: Title shown above (centered) the SVG item.
        :param str comment: Comment displayed below the SVG item.
        :param str commentPosition: "CENTER" or "LEFT"
        :param QRectF viewBox: Bounding box for the item on the print page
            (xOffset, yOffset, width, height). If None, use original
            item size.
        :param bool keepRatio: If True, resizing the item will preserve its
            original aspect ratio.
        """
        if not qt.HAS_SVG:
            raise RuntimeError("Missing QtSvg library.")
        if not isinstance(item, qt.QSvgRenderer):
            raise TypeError("addSvgItem: QSvgRenderer expected")
        if self._toBeCleared:
            self._clearAll()
        self.ensurePrinterIsSet()
        if self.printer is None:
            _logger.error("printer is not set, cannot add SvgItem to page")
            return

        if title is None:
            title = 50 * ' '
        if comment is None:
            comment = 80 * ' '
        if commentPosition is None:
            commentPosition = "CENTER"

        if viewBox is None:
            if hasattr(item, "_viewBox"):
                # PyMca compatibility: viewbox attached to item
                viewBox = item._viewBox
            else:
                # try the original item viewbox
                viewBox = item.viewBoxF()

        svgItem = _GraphicsSvgRectItem(viewBox, self.page)
        svgItem.setSvgRenderer(item)

        svgItem.setCacheMode(qt.QGraphicsItem.NoCache)
        svgItem.setZValue(0)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsSelectable, True)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)
        svgItem.setFlag(qt.QGraphicsItem.ItemIsFocusable, False)

        rectItemResizeRect = _GraphicsResizeRectItem(svgItem, self.scene,
                                                     keepratio=keepRatio)
        rectItemResizeRect.setZValue(2)

        self._svgItems.append(item)

        # Comment / legend
        dummyComment = 80 * "1"
        if qt.qVersion() < '5.0':
            commentItem = qt.QGraphicsTextItem(dummyComment, svgItem, self.scene)
        else:
            commentItem = qt.QGraphicsTextItem(dummyComment, svgItem)
        commentItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        # we scale the text to have the legend  box have the same width as the graph
        scaleCalculationRect = qt.QRectF(commentItem.boundingRect())
        scale = svgItem.boundingRect().width() / scaleCalculationRect.width()

        commentItem.setPlainText(comment)
        commentItem.setZValue(1)

        commentItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)
        if qt.qVersion() < "5.0":
            commentItem.scale(scale, scale)
        else:
            commentItem.setScale(scale)

        # align
        if commentPosition.upper() == "CENTER":
            alignment = qt.Qt.AlignCenter
        elif commentPosition.upper() == "RIGHT":
            alignment = qt.Qt.AlignRight
        else:
            alignment = qt.Qt.AlignLeft
        commentItem.setTextWidth(commentItem.boundingRect().width())
        center_format = qt.QTextBlockFormat()
        center_format.setAlignment(alignment)
        cursor = commentItem.textCursor()
        cursor.select(qt.QTextCursor.Document)
        cursor.mergeBlockFormat(center_format)
        cursor.clearSelection()
        commentItem.setTextCursor(cursor)
        if alignment == qt.Qt.AlignLeft:
            deltax = 0
        else:
            deltax = (svgItem.boundingRect().width() - commentItem.boundingRect().width()) / 2.
        commentItem.moveBy(svgItem.boundingRect().x() + deltax,
                           svgItem.boundingRect().y() + svgItem.boundingRect().height())

        # Title
        if qt.qVersion() < '5.0':
            textItem = qt.QGraphicsTextItem(title, svgItem, self.scene)
        else:
            textItem = qt.QGraphicsTextItem(title, svgItem)
        textItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction)
        textItem.setZValue(1)
        textItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True)

        title_offset = 0.5 * textItem.boundingRect().width()
        textItem.moveBy(svgItem.boundingRect().x() +
                        0.5 * svgItem.boundingRect().width() - title_offset * scale,
                        svgItem.boundingRect().y())
        if qt.qVersion() < "5.0":
            textItem.scale(scale, scale)
        else:
            textItem.setScale(scale)