def print_(self): printer = QPrinter(QPrinter.HighResolution) printer.setCreator('Bode example') printer.setOrientation(QPrinter.Landscape) printer.setColorMode(QPrinter.Color) docName = str(self.plot.title().text()) if not docName: docName.replace('\n', ' -- ') printer.setDocName(docName) dialog = QPrintDialog(printer) if dialog.exec_(): renderer = QwtPlotRenderer() if (QPrinter.GrayScale == printer.colorMode()): renderer.setDiscardFlag(QwtPlotRenderer.DiscardBackground) renderer.setDiscardFlag(QwtPlotRenderer.DiscardCanvasBackground) renderer.setDiscardFlag(QwtPlotRenderer.DiscardCanvasFrame) renderer.setLayoutFlag(QwtPlotRenderer.FrameWithScales) renderer.renderTo(self.plot, printer)
def renderDocument(self, plot, filename, sizeMM=(300, 200), resolution=85, format_=None): if isinstance(sizeMM, tuple): sizeMM = QSizeF(*sizeMM) if format_ is None: ext = osp.splitext(filename)[1] if not ext: raise TypeError("Unable to determine target format from filename") format_ = ext[1:] if plot is None or sizeMM.isEmpty() or resolution <= 0: return title = plot.title().text() if not title: title = "Plot Document" mmToInch = 1./25.4 size = sizeMM * mmToInch * resolution documentRect = QRectF(0.0, 0.0, size.width(), size.height()) fmt = format_.lower() if fmt in ("pdf", "ps"): printer = QPrinter() if fmt == "pdf": printer.setOutputFormat(QPrinter.PdfFormat) else: printer.setOutputFormat(QPrinter.PostScriptFormat) printer.setColorMode(QPrinter.Color) printer.setFullPage(True) printer.setPaperSize(sizeMM, QPrinter.Millimeter) printer.setDocName(title) printer.setOutputFileName(filename) printer.setResolution(resolution) painter = QPainter(printer) self.render(plot, painter, documentRect) painter.end() elif fmt == "svg": generator = QSvgGenerator() generator.setTitle(title) generator.setFileName(filename) generator.setResolution(resolution) generator.setViewBox(documentRect) painter = QPainter(generator) self.render(plot, painter, documentRect) painter.end() elif fmt in QImageWriter.supportedImageFormats(): imageRect = documentRect.toRect() dotsPerMeter = int(round(resolution*mmToInch*1000.)) image = QImage(imageRect.size(), QImage.Format_ARGB32) image.setDotsPerMeterX(dotsPerMeter) image.setDotsPerMeterY(dotsPerMeter) image.fill(QColor(Qt.white).rgb()) painter = QPainter(image) self.render(plot, painter, imageRect) painter.end() image.save(filename, fmt) else: raise TypeError("Unsupported file format '%s'" % fmt)
def print_(self): printer = QPrinter(QPrinter.HighResolution) printer.setCreator('Bode example') printer.setOrientation(QPrinter.Landscape) printer.setColorMode(QPrinter.Color) docName = str(self.plot.title().text()) if not docName: docName.replace('\n', ' -- ') printer.setDocName(docName) dialog = QPrintDialog(printer) if dialog.exec_(): renderer = QwtPlotRenderer() if (QPrinter.GrayScale == printer.colorMode()): renderer.setDiscardFlag(QwtPlotRenderer.DiscardBackground) renderer.setDiscardFlag( QwtPlotRenderer.DiscardCanvasBackground) renderer.setDiscardFlag(QwtPlotRenderer.DiscardCanvasFrame) renderer.setLayoutFlag(QwtPlotRenderer.FrameWithScales) renderer.renderTo(self.plot, printer)
def renderDocument(self, plot, filename, sizeMM=(300, 200), resolution=85, format_=None): """ Render a plot to a file The format of the document will be auto-detected from the suffix of the file name. :param qwt.plot.QwtPlot plot: Plot widget :param str fileName: Path of the file, where the document will be stored :param QSizeF sizeMM: Size for the document in millimeters :param int resolution: Resolution in dots per Inch (dpi) """ if isinstance(sizeMM, tuple): sizeMM = QSizeF(*sizeMM) if format_ is None: ext = osp.splitext(filename)[1] if not ext: raise TypeError( "Unable to determine target format from filename") format_ = ext[1:] if plot is None or sizeMM.isEmpty() or resolution <= 0: return title = plot.title().text() if not title: title = "Plot Document" mmToInch = 1. / 25.4 size = sizeMM * mmToInch * resolution documentRect = QRectF(0.0, 0.0, size.width(), size.height()) fmt = format_.lower() if fmt in ("pdf", "ps"): printer = QPrinter() if fmt == "pdf": printer.setOutputFormat(QPrinter.PdfFormat) else: printer.setOutputFormat(QPrinter.PostScriptFormat) printer.setColorMode(QPrinter.Color) printer.setFullPage(True) printer.setPaperSize(sizeMM, QPrinter.Millimeter) printer.setDocName(title) printer.setOutputFileName(filename) printer.setResolution(resolution) painter = QPainter(printer) self.render(plot, painter, documentRect) painter.end() elif fmt == "svg": generator = QSvgGenerator() generator.setTitle(title) generator.setFileName(filename) generator.setResolution(resolution) generator.setViewBox(documentRect) painter = QPainter(generator) self.render(plot, painter, documentRect) painter.end() elif fmt in QImageWriter.supportedImageFormats(): imageRect = documentRect.toRect() dotsPerMeter = int(round(resolution * mmToInch * 1000.)) image = QImage(imageRect.size(), QImage.Format_ARGB32) image.setDotsPerMeterX(dotsPerMeter) image.setDotsPerMeterY(dotsPerMeter) image.fill(QColor(Qt.white).rgb()) painter = QPainter(image) self.render(plot, painter, imageRect) painter.end() image.save(filename, fmt) else: raise TypeError("Unsupported file format '%s'" % fmt)