def fit_scene(self): """ Fit scene into canvas. """ margin = 50.0 rawBoundary = visible_bounding_rect(self.scene().items()) rawBoundary.adjust(-margin, -margin, margin, margin) self.fitInView(rawBoundary, Qt.KeepAspectRatio)
def printer(canvas, printer): """ The main print routine. """ theScene = visible_bounding_rect(canvas.items()) theScene.adjust(-10, -10, 10, 10) painter = QPainter(printer) painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing) canvas.render(painter, QRectF(), theScene) painter.end()
def update_dimensions(self): """ Update values with the current canvas dimensions """ size = visible_bounding_rect(self.canvas.items()) imageWidth = math.floor(size.width()) imageHeight = math.floor(size.height()) self.imageWidth = imageWidth self.imageHeight = imageHeight self._aspectRatio = imageWidth / imageHeight self.imageWidthSpinner.setValue(int(self._convert_pixels_to_length(self.imageWidth))) self.imageHeightSpinner.setValue(int(self._convert_pixels_to_length(self.imageHeight)))
def update_dimensions(self): """ Update values with the current canvas dimensions """ size = visible_bounding_rect(self.canvas.items()) imageWidth = math.floor(size.width()) imageHeight = math.floor(size.height()) self.imageWidth = imageWidth self.imageHeight = imageHeight self._aspectRatio = imageWidth / imageHeight self.imageWidthSpinner.setValue( int(self._convert_pixels_to_length(self.imageWidth))) self.imageHeightSpinner.setValue( int(self._convert_pixels_to_length(self.imageHeight)))
def export_scene(canvas, width, height, dpi, exportFileName): """ This function exports the scene to a file. """ # need this to make sure we take away focus from # any currently selected legend items canvas.clearFocus() with HiddenStitchManager(canvas): # NOTE: We seem to need the 1px buffer region to avoid # the image being cut off margin = 10 theScene = visible_bounding_rect(canvas.items()) theScene.adjust(-margin, -margin, margin, margin) # check if user requested an svg file svg = True if QFileInfo(exportFileName).completeSuffix() == "svg" \ else False if svg: generator = QSvgGenerator() generator.setFileName(exportFileName) generator.setSize(QSize(width, height)) generator.setViewBox(QRect(0, 0, width, height)) generator.setTitle("sconcho generated SVG image") generator.setDescription("this svg image was exported from " "a sconcho project") generator.setResolution(dpi) else: generator = QImage(width+2*margin, height+2*margin, QImage.Format_ARGB32_Premultiplied) generator.fill(1) inchesToMeter = 39.3700787 generator.setDotsPerMeterX(dpi*inchesToMeter) generator.setDotsPerMeterY(dpi*inchesToMeter) painter = QPainter(generator) painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing ) painter.setBackgroundMode(Qt.TransparentMode ) canvas.render(painter, QRectF(), theScene ) painter.end() if not svg: generator.save(exportFileName)
def export_scene(canvas, width, height, dpi, exportFileName): """ This function exports the scene to a file. """ # need this to make sure we take away focus from # any currently selected legend items canvas.clearFocus() with HiddenStitchManager(canvas): # NOTE: We seem to need the 1px buffer region to avoid # the image being cut off margin = 10 theScene = visible_bounding_rect(canvas.items()) theScene.adjust(-margin, -margin, margin, margin) # check if user requested an svg file svg = True if QFileInfo(exportFileName).completeSuffix() == "svg" \ else False if svg: generator = QSvgGenerator() generator.setFileName(exportFileName) generator.setSize(QSize(width, height)) generator.setViewBox(QRect(0, 0, width, height)) generator.setTitle("sconcho generated SVG image") generator.setDescription("this svg image was exported from " "a sconcho project") generator.setResolution(dpi) else: generator = QImage(width + 2 * margin, height + 2 * margin, QImage.Format_ARGB32_Premultiplied) generator.fill(1) inchesToMeter = 39.3700787 generator.setDotsPerMeterX(dpi * inchesToMeter) generator.setDotsPerMeterY(dpi * inchesToMeter) painter = QPainter(generator) painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing) painter.setBackgroundMode(Qt.TransparentMode) canvas.render(painter, QRectF(), theScene) painter.end() if not svg: generator.save(exportFileName)