Exemplo n.º 1
0
 def __getDiagram(self, rect, format="PNG", filename=None):
     """
     Private method to retrieve the diagram from the scene fitting it
     in the minimum rectangle.
     
     @param rect minimum rectangle fitting the diagram (QRectF)
     @param format format for the image file (string)
     @param filename name of the file for non pixmaps (string)
     @return diagram pixmap to receive the diagram (QPixmap)
     """
     selectedItems = self.scene().selectedItems()
     
     # step 1: deselect all widgets
     if selectedItems:
         for item in selectedItems:
             item.setSelected(False)
         
     # step 2: grab the diagram
     if format == "PNG":
         paintDevice = QPixmap(int(rect.width()), int(rect.height()))
         paintDevice.fill(self.backgroundBrush().color())
     else:
         from PyQt5.QtSvg import QSvgGenerator
         paintDevice = QSvgGenerator()
         paintDevice.setResolution(100)  # 100 dpi
         paintDevice.setSize(QSize(int(rect.width()), int(rect.height())))
         paintDevice.setViewBox(rect)
         paintDevice.setFileName(filename)
     painter = QPainter(paintDevice)
     painter.setRenderHint(QPainter.Antialiasing, True)
     self.scene().render(painter, QRectF(), rect)
     
     # step 3: reselect the widgets
     if selectedItems:
         for item in selectedItems:
             item.setSelected(True)
     
     return paintDevice
Exemplo n.º 2
0
    def __getDiagram(self, rect, format="PNG", filename=None):
        """
        Private method to retrieve the diagram from the scene fitting it
        in the minimum rectangle.
        
        @param rect minimum rectangle fitting the diagram (QRectF)
        @param format format for the image file (string)
        @param filename name of the file for non pixmaps (string)
        @return diagram pixmap to receive the diagram (QPixmap)
        """
        selectedItems = self.scene().selectedItems()

        # step 1: deselect all widgets
        if selectedItems:
            for item in selectedItems:
                item.setSelected(False)

        # step 2: grab the diagram
        if format == "PNG":
            paintDevice = QPixmap(int(rect.width()), int(rect.height()))
            paintDevice.fill(self.backgroundBrush().color())
        else:
            from PyQt5.QtSvg import QSvgGenerator
            paintDevice = QSvgGenerator()
            paintDevice.setResolution(100)  # 100 dpi
            paintDevice.setSize(QSize(int(rect.width()), int(rect.height())))
            paintDevice.setViewBox(rect)
            paintDevice.setFileName(filename)
        painter = QPainter(paintDevice)
        painter.setRenderHint(QPainter.Antialiasing, True)
        self.scene().render(painter, QRectF(), rect)

        # step 3: reselect the widgets
        if selectedItems:
            for item in selectedItems:
                item.setSelected(True)

        return paintDevice