def printPreview(obj, printer=None, parent=None): if printer is None: printer = QtPrintSupport.QPrinter( QtPrintSupport.QPrinter.PrinterResolution) # @TODO: check if parent is None: try: parent = obj.window() except AttributeError: parent = None dialog = QtWidgets.QPrintPreviewDialog(printer, parent) dialog.paintRequested.connect(coreprint) ret = dialog.exec_() # @WARNING: duplicate code ret = QtPrintSupport.QPrintDialog(printer, parent).exec_() if ret == QtWidgets.QDialog.Accepted: if isinstance(obj, (QtGui.QTextDocument, QtWidgets.QTextEdit)): obj.print_(printer) elif hasattr(object, 'model'): model = obj.model() doc = modelToTextDocument(model) obj.print_(printer) elif isinstance(obj, QtCore.QAbstractItemModel): doc = modelToTextDocument(obj) doc.print_(printer) else: coreprint(obj, printer)
def print_figure(self): printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution) printer.setOrientation(QtPrintSupport.QPrinter.Landscape) print_dlg = QtPrintSupport.QPrintDialog(printer) if print_dlg.exec_() == QtWidgets.QDialog.Accepted: painter = QtGui.QPainter(printer) page_size = printer.pageRect() pixmap = self.canvas.grab().scaled(page_size.width(), page_size.height(), QtCore.Qt.KeepAspectRatio) painter.drawPixmap(0, 0, pixmap) painter.end()
def PDF(self, name=None): printer = QtPrintSupport.QPrinter() printer.setOutputFormat(printer.NativeFormat) if name: printer.setOutputFileName(name) self.text.document().print_(printer) else: dialog = QtPrintSupport.QPrintDialog(printer) dialog.setOption(dialog.PrintToFile) if dialog.exec_() == QtWidgets.QDialog.Accepted: self.text.document().print_(dialog.printer())
def printObject(obj, printer=None, parent=None): if printer is None: printer = QtPrintSupport.QPrinter( QtPrintSupport.QPrinter.PrinterResolution) # printer.setOutputFile(os.path.join(utils.default_workdir(). # 'filename.pdf')) # @TODO: check if parent is None: try: parent = obj.window() except AttributeError: parent = None # dialog = QtPrintSupport.QPrintDialog(printer) # try: # window = obj.window() # except AttributeError: # window = = None # preview = QtWidgets.QPrintPreviewWidget(printer, window) # preview.paintRequested.connect(coreprint) # dialog.setOptionTabs([preview]) # ret = d.exec_() ret = QtPrintSupport.QPrintDialog(printer, parent).exec_() if ret == QtWidgets.QDialog.Accepted: if isinstance(obj, (QtGui.QTextDocument, QtWidgets.QTextEdit)): obj.print_(printer) elif hasattr(obj, 'model'): model = obj.model() doc = modelToTextDocument(model) doc.print_(printer) elif isinstance(obj, QtCore.QAbstractItemModel): doc = modelToTextDocument(obj) doc.print_(printer) else: coreprint(obj, printer)
def imgexport(obj, parent=None): filters = [ obj.tr('All files (*)'), obj.tr('Simple Vector Graphics file (*.svg)'), obj.tr('PDF file (*.pdf)'), obj.tr('PostScript file (*.ps)'), ] filters.extend('{} file (*.{})'.format(str(fmt).upper(), str(fmt)) for fmt in QtGui.QImageWriter.supportedImageFormats()) formats = { str(fmt).lower() for fmt in QtGui.QImageWriter.supportedImageFormats() } formats.update(('svg', 'pdf', 'ps')) # @TODO: check if parent is None: try: parent = obj.window() except AttributeError: parent = None target = os.path.join(utils.default_workdir(), 'image.jpeg') filename, filter_ = QtWidgets.QFileDialog.getSaveFileName( parent, obj.tr('Save picture'), target, ';;'.join(filters)) ext = 'unknown' while filename and (ext not in formats): ext = os.path.splitext(filename)[1] if ext: ext = ext[1:].lower() if ext in formats: break else: QtWidgets.QMessageBox.information( parent, obj.tr('Unknown file format'), obj.tr('Unknown file format "%s".\nPlease retry.') % ext) filename, filter_ = QtWidgets.QFileDialog.getSaveFileName( parent, obj.tr('Save draw'), filename, ';;'.join(filters), filter_) else: ext = 'unknown' if filename: if hasattr(obj, 'viewport'): srcsize = obj.viewport().rect().size() elif hasattr(obj, 'sceneRect'): # QGraphicsViews also has a viewport method so they should be # trapped by the previous check srcsize = obj.sceneRect().toRect().size() else: srcsize = QtWidgets.QSize(800, 600) if ext in ('pdf', 'ps'): device = QtPrintSupport.QPrinter( QtPrintSupport.QPrinter.HighResolution) device.setOutputFileName(filename) if ext == 'pdf': device.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat) else: # ext == 'ps' device.setOutputFormat( QtPrintSupport.QPrinter.PostScriptFormat) elif ext == 'svg': device = QtSvg.QSvgGenerator() device.setFileName(filename) device.setSize(srcsize) # device.setViewBox(obj.sceneRect().toRect()) # device.setTitle(obj.tr('Graphics Draw')) # device.setDescription(obj.tr('Qt SVG drawing.')) else: device = QtGui.QPixmap(srcsize) # @TODO: check device.fill(QtCore.Qt.white) painter = QtGui.QPainter() if painter.begin(device): # painter.setRenderHint(QtGui.QPainter.Antialiasing) obj.render(painter) painter.end() if hasattr(device, 'save'): device.save(filename) else: QtWidgets.QMessageBox.warning( parent, obj.tr('Warning'), obj.tr('Unable initialize painting device.'))