def printSource(self): cursor = self.currentView().textCursor() printer = QPrinter() dlg = QPrintDialog(printer, self) dlg.setWindowTitle(app.caption(_("dialog title", "Print Source"))) options = QAbstractPrintDialog.PrintToFile | QAbstractPrintDialog.PrintShowPageSize if cursor.hasSelection(): options |= QAbstractPrintDialog.PrintSelection dlg.setOptions(options) if dlg.exec_(): doc = highlighter.htmlCopy(self.currentDocument(), "printer") doc.setMetaInformation(QTextDocument.DocumentTitle, self.currentDocument().url().toString()) font = doc.defaultFont() font.setPointSizeF(font.pointSizeF() * 0.8) doc.setDefaultFont(font) if dlg.testOption(QAbstractPrintDialog.PrintSelection): # cut out not selected text start, end = cursor.selectionStart(), cursor.selectionEnd() cur1 = QTextCursor(doc) cur1.setPosition(start, QTextCursor.KeepAnchor) cur2 = QTextCursor(doc) cur2.setPosition(end) cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cur2.removeSelectedText() cur1.removeSelectedText() doc.print_(printer)
def copyColoredHtml(self): cursor = self.currentView().textCursor() if not cursor.hasSelection(): return doc = highlighter.htmlCopy(self.currentDocument()) cur1 = QTextCursor(doc) cur1.setPosition(cursor.anchor()) cur1.setPosition(cursor.position(), QTextCursor.KeepAnchor) data = QMimeData() html = cur1.selection().toHtml() data.setHtml(html) data.setText(html) QApplication.clipboard().setMimeData(data)
def exportColoredHtml(self): doc = self.currentDocument() name, ext = os.path.splitext(os.path.basename(doc.url().path())) if name: if ext.lower() == ".html": name += "_html" name += ".html" dir = os.path.dirname(doc.url().toLocalFile()) if dir: name = os.path.join(dir, name) filename = QFileDialog.getSaveFileName(self, app.caption(_("Export as HTML")), name, "{0} (*.html)".format("HTML Files")) if not filename: return #cancelled html = highlighter.htmlCopy(doc).toHtml('utf-8').encode('utf-8') try: with open(filename, "w") as f: f.write(str(html)) except (IOError, OSError) as err: QMessageBox.warning(self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}\n\n{error}").format(url=filename, error=err))