Example #1
0
    def export(self):
        """ Displays a dialog for exporting HTML generated by Qt's rich text
        system.

        Returns
        -------
        The name of the file that was saved, or None if no file was saved.
        """
        parent = self.control.window()
        dialog = QtWidgets.QFileDialog(parent, 'Save as...')
        dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
        filters = [
            'HTML with PNG figures (*.html *.htm)',
            'XHTML with inline SVG figures (*.xhtml *.xml)'
        ]
        dialog.setNameFilters(filters)
        if self.filename:
            dialog.selectFile(self.filename)
            root, ext = os.path.splitext(self.filename)
            if ext.lower() in ('.xml', '.xhtml'):
                dialog.selectNameFilter(filters[-1])

        if dialog.exec_():
            self.filename = dialog.selectedFiles()[0]
            choice = dialog.selectedNameFilter()
            html = py3compat.cast_unicode(self.control.document().toHtml())

            # Configure the exporter.
            if choice.startswith('XHTML'):
                exporter = export_xhtml
            else:
                # If there are PNGs, decide how to export them.
                inline = self.inline_png
                if inline is None and IMG_RE.search(html):
                    dialog = QtGui.QDialog(parent)
                    dialog.setWindowTitle('Save as...')
                    layout = QtGui.QVBoxLayout(dialog)
                    msg = "Exporting HTML with PNGs"
                    info = "Would you like inline PNGs (single large html " \
                        "file) or external image files?"
                    checkbox = QtGui.QCheckBox("&Don't ask again")
                    checkbox.setShortcut('D')
                    ib = QtWidgets.QPushButton("&Inline")
                    ib.setShortcut('I')
                    eb = QtWidgets.QPushButton("&External")
                    eb.setShortcut('E')
                    box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question,
                                                dialog.windowTitle(), msg)
                    box.setInformativeText(info)
                    box.addButton(ib, QtWidgets.QMessageBox.NoRole)
                    box.addButton(eb, QtWidgets.QMessageBox.YesRole)
                    layout.setSpacing(0)
                    layout.addWidget(box)
                    layout.addWidget(checkbox)
                    dialog.setLayout(layout)
                    dialog.show()
                    reply = box.exec_()
                    dialog.hide()
                    inline = (reply == 0)
                    if checkbox.checkState():
                        # Don't ask anymore; always use this choice.
                        self.inline_png = inline
                exporter = lambda h, f, i: export_html(h, f, i, inline)

            # Perform the export!
            try:
                return exporter(html, self.filename, self.image_tag)
            except Exception as e:
                msg = "Error exporting HTML to %s\n" % self.filename + str(e)
                reply = QtWidgets.QMessageBox.warning(parent, 'Error', msg,
                                                      QtWidgets.QMessageBox.Ok,
                                                      QtWidgets.QMessageBox.Ok)

        return None