Exemplo n.º 1
0
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('%s file (*.%s)' % (
        str(fmt).upper(), str(fmt))
        for fmt in QtGui.QImageWriter.supportedImageFormats())

    formats = set(
        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 alsa 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.'))
Exemplo n.º 2
0
def coreprint(obj, printer):
    painter = QtGui.QPainter(printer)
    painter.setRenderHint(QtGui.QPainter.Antialiasing)
    obj.render(painter)
    painter.end()