Implement printing with QtWebEngine

This commit is contained in:
Florian Bruhin 2017-02-06 09:51:11 +01:00
parent 9a9fa3ab41
commit 2f4c185da0
5 changed files with 44 additions and 9 deletions

View File

@ -23,6 +23,7 @@ Added
- Userscripts now have a new `$QUTE_COMMANDLINE_TEXT` environment variable, containing the current commandline contents.
- New `ripbang` userscript to create a searchengine from a duckduckgo bang
- link:https://github.com/annulen/webkit/wiki[QtWebKit Reloaded] (also called QtWebKit-NG) is now supported.
- Printing support for QtWebEngine with Qt >= 5.8
Changed
~~~~~~~

View File

@ -107,10 +107,20 @@ class AbstractPrinting:
def check_printer_support(self):
raise NotImplementedError
def check_preview_support(self):
raise NotImplementedError
def to_pdf(self, filename):
raise NotImplementedError
def to_printer(self, printer):
def to_printer(self, printer, callback=None):
"""Print the tab.
Args:
printer: The QPrinter to print to.
callback: Called with a boolean
(True if printing succeeded, False otherwise)
"""
raise NotImplementedError

View File

@ -370,16 +370,23 @@ class CommandDispatcher:
tab.printing.check_pdf_support()
else:
tab.printing.check_printer_support()
if preview:
tab.printing.check_preview_support()
except browsertab.WebTabError as e:
raise cmdexc.CommandError(e)
if preview:
def print_callback(ok):
if not ok:
message.error("Printing failed!")
diag = QPrintPreviewDialog()
diag.setAttribute(Qt.WA_DeleteOnClose)
diag.setWindowFlags(diag.windowFlags() |
Qt.WindowMaximizeButtonHint |
Qt.WindowMinimizeButtonHint)
diag.paintRequested.connect(tab.printing.to_printer)
diag.paintRequested.connect(functools.partial(
tab.printing.to_printer, callback=print_callback))
diag.exec_()
elif pdf:
pdf = os.path.expanduser(pdf)
@ -389,9 +396,14 @@ class CommandDispatcher:
tab.printing.to_pdf(pdf)
log.misc.debug("Print to file: {}".format(pdf))
else:
def print_callback(ok):
if not ok:
message.error("Printing failed!")
diag.deleteLater()
diag = QPrintDialog()
diag.setAttribute(Qt.WA_DeleteOnClose)
diag.open(lambda: tab.printing.to_printer(diag.printer()))
diag.open(lambda:
tab.printing.to_printer(diag.printer(), print_callback))
@cmdutils.register(instance='command-dispatcher', scope='window')
def tab_clone(self, bg=False, window=False):

View File

@ -89,15 +89,21 @@ class WebEnginePrinting(browsertab.AbstractPrinting):
"Printing to PDF is unsupported with QtWebEngine on Qt < 5.7")
def check_printer_support(self):
if not hasattr(self._widget.page(), 'print'):
raise browsertab.WebTabError(
"Printing is unsupported with QtWebEngine on Qt < 5.8")
def check_preview_support(self):
raise browsertab.WebTabError(
"Printing is unsupported with QtWebEngine")
"Print previews are unsupported with QtWebEngine")
def to_pdf(self, filename):
self._widget.page().printToPdf(filename)
def to_printer(self, printer):
# Should never be called
assert False
def to_printer(self, printer, callback=None):
if callback is None:
callback = lambda _ok: None
self._widget.page().print(printer, callback)
class WebEngineSearch(browsertab.AbstractSearch):

View File

@ -68,13 +68,19 @@ class WebKitPrinting(browsertab.AbstractPrinting):
def check_printer_support(self):
self._do_check()
def check_preview_support(self):
self._do_check()
def to_pdf(self, filename):
printer = QPrinter()
printer.setOutputFileName(filename)
self.to_printer(printer)
def to_printer(self, printer):
def to_printer(self, printer, callback):
self._widget.print(printer)
# Can't find out whether there was an error...
if callback is not None:
callback(True)
class WebKitSearch(browsertab.AbstractSearch):