Implement printing with QtWebEngine
This commit is contained in:
parent
9a9fa3ab41
commit
2f4c185da0
@ -23,6 +23,7 @@ Added
|
|||||||
- Userscripts now have a new `$QUTE_COMMANDLINE_TEXT` environment variable, containing the current commandline contents.
|
- 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
|
- 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.
|
- 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
|
Changed
|
||||||
~~~~~~~
|
~~~~~~~
|
||||||
|
@ -107,10 +107,20 @@ class AbstractPrinting:
|
|||||||
def check_printer_support(self):
|
def check_printer_support(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def check_preview_support(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def to_pdf(self, filename):
|
def to_pdf(self, filename):
|
||||||
raise NotImplementedError
|
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
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
@ -370,16 +370,23 @@ class CommandDispatcher:
|
|||||||
tab.printing.check_pdf_support()
|
tab.printing.check_pdf_support()
|
||||||
else:
|
else:
|
||||||
tab.printing.check_printer_support()
|
tab.printing.check_printer_support()
|
||||||
|
if preview:
|
||||||
|
tab.printing.check_preview_support()
|
||||||
except browsertab.WebTabError as e:
|
except browsertab.WebTabError as e:
|
||||||
raise cmdexc.CommandError(e)
|
raise cmdexc.CommandError(e)
|
||||||
|
|
||||||
if preview:
|
if preview:
|
||||||
|
def print_callback(ok):
|
||||||
|
if not ok:
|
||||||
|
message.error("Printing failed!")
|
||||||
|
|
||||||
diag = QPrintPreviewDialog()
|
diag = QPrintPreviewDialog()
|
||||||
diag.setAttribute(Qt.WA_DeleteOnClose)
|
diag.setAttribute(Qt.WA_DeleteOnClose)
|
||||||
diag.setWindowFlags(diag.windowFlags() |
|
diag.setWindowFlags(diag.windowFlags() |
|
||||||
Qt.WindowMaximizeButtonHint |
|
Qt.WindowMaximizeButtonHint |
|
||||||
Qt.WindowMinimizeButtonHint)
|
Qt.WindowMinimizeButtonHint)
|
||||||
diag.paintRequested.connect(tab.printing.to_printer)
|
diag.paintRequested.connect(functools.partial(
|
||||||
|
tab.printing.to_printer, callback=print_callback))
|
||||||
diag.exec_()
|
diag.exec_()
|
||||||
elif pdf:
|
elif pdf:
|
||||||
pdf = os.path.expanduser(pdf)
|
pdf = os.path.expanduser(pdf)
|
||||||
@ -389,9 +396,14 @@ class CommandDispatcher:
|
|||||||
tab.printing.to_pdf(pdf)
|
tab.printing.to_pdf(pdf)
|
||||||
log.misc.debug("Print to file: {}".format(pdf))
|
log.misc.debug("Print to file: {}".format(pdf))
|
||||||
else:
|
else:
|
||||||
|
def print_callback(ok):
|
||||||
|
if not ok:
|
||||||
|
message.error("Printing failed!")
|
||||||
|
diag.deleteLater()
|
||||||
|
|
||||||
diag = QPrintDialog()
|
diag = QPrintDialog()
|
||||||
diag.setAttribute(Qt.WA_DeleteOnClose)
|
diag.open(lambda:
|
||||||
diag.open(lambda: tab.printing.to_printer(diag.printer()))
|
tab.printing.to_printer(diag.printer(), print_callback))
|
||||||
|
|
||||||
@cmdutils.register(instance='command-dispatcher', scope='window')
|
@cmdutils.register(instance='command-dispatcher', scope='window')
|
||||||
def tab_clone(self, bg=False, window=False):
|
def tab_clone(self, bg=False, window=False):
|
||||||
|
@ -89,15 +89,21 @@ class WebEnginePrinting(browsertab.AbstractPrinting):
|
|||||||
"Printing to PDF is unsupported with QtWebEngine on Qt < 5.7")
|
"Printing to PDF is unsupported with QtWebEngine on Qt < 5.7")
|
||||||
|
|
||||||
def check_printer_support(self):
|
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(
|
raise browsertab.WebTabError(
|
||||||
"Printing is unsupported with QtWebEngine")
|
"Print previews are unsupported with QtWebEngine")
|
||||||
|
|
||||||
def to_pdf(self, filename):
|
def to_pdf(self, filename):
|
||||||
self._widget.page().printToPdf(filename)
|
self._widget.page().printToPdf(filename)
|
||||||
|
|
||||||
def to_printer(self, printer):
|
def to_printer(self, printer, callback=None):
|
||||||
# Should never be called
|
if callback is None:
|
||||||
assert False
|
callback = lambda _ok: None
|
||||||
|
self._widget.page().print(printer, callback)
|
||||||
|
|
||||||
|
|
||||||
class WebEngineSearch(browsertab.AbstractSearch):
|
class WebEngineSearch(browsertab.AbstractSearch):
|
||||||
|
@ -68,13 +68,19 @@ class WebKitPrinting(browsertab.AbstractPrinting):
|
|||||||
def check_printer_support(self):
|
def check_printer_support(self):
|
||||||
self._do_check()
|
self._do_check()
|
||||||
|
|
||||||
|
def check_preview_support(self):
|
||||||
|
self._do_check()
|
||||||
|
|
||||||
def to_pdf(self, filename):
|
def to_pdf(self, filename):
|
||||||
printer = QPrinter()
|
printer = QPrinter()
|
||||||
printer.setOutputFileName(filename)
|
printer.setOutputFileName(filename)
|
||||||
self.to_printer(printer)
|
self.to_printer(printer)
|
||||||
|
|
||||||
def to_printer(self, printer):
|
def to_printer(self, printer, callback):
|
||||||
self._widget.print(printer)
|
self._widget.print(printer)
|
||||||
|
# Can't find out whether there was an error...
|
||||||
|
if callback is not None:
|
||||||
|
callback(True)
|
||||||
|
|
||||||
|
|
||||||
class WebKitSearch(browsertab.AbstractSearch):
|
class WebKitSearch(browsertab.AbstractSearch):
|
||||||
|
Loading…
Reference in New Issue
Block a user