Clean up printing code in browser.commands
This commit is contained in:
parent
c8913cbdc4
commit
89108be013
@ -349,6 +349,43 @@ class CommandDispatcher:
|
|||||||
if tab is not None:
|
if tab is not None:
|
||||||
tab.stop()
|
tab.stop()
|
||||||
|
|
||||||
|
def _print_preview(self, tab):
|
||||||
|
"""Show a print preview."""
|
||||||
|
tab.printing.check_preview_support()
|
||||||
|
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(functools.partial(
|
||||||
|
tab.printing.to_printer, callback=print_callback))
|
||||||
|
diag.exec_()
|
||||||
|
|
||||||
|
def _print_pdf(self, tab, filename):
|
||||||
|
tab.printing.check_pdf_support()
|
||||||
|
"""Print to the given PDF file."""
|
||||||
|
filename = os.path.expanduser(filename)
|
||||||
|
directory = os.path.dirname(filename)
|
||||||
|
if directory and not os.path.exists(directory):
|
||||||
|
os.mkdir(directory)
|
||||||
|
tab.printing.to_filename(filename)
|
||||||
|
log.misc.debug("Print to file: {}".format(filename))
|
||||||
|
|
||||||
|
def _print(self, tab):
|
||||||
|
"""Print with a QPrintDialog."""
|
||||||
|
def print_callback(ok):
|
||||||
|
"""Called when printing finished."""
|
||||||
|
if not ok:
|
||||||
|
message.error("Printing failed!")
|
||||||
|
diag.deleteLater()
|
||||||
|
|
||||||
|
diag = QPrintDialog()
|
||||||
|
diag.open(lambda: tab.printing.to_printer(diag.printer(),
|
||||||
|
print_callback))
|
||||||
|
|
||||||
@cmdutils.register(instance='command-dispatcher', name='print',
|
@cmdutils.register(instance='command-dispatcher', name='print',
|
||||||
scope='window')
|
scope='window')
|
||||||
@cmdutils.argument('count', count=True)
|
@cmdutils.argument('count', count=True)
|
||||||
@ -376,34 +413,11 @@ class CommandDispatcher:
|
|||||||
raise cmdexc.CommandError(e)
|
raise cmdexc.CommandError(e)
|
||||||
|
|
||||||
if preview:
|
if preview:
|
||||||
def print_callback(ok):
|
self._print_preview(tab)
|
||||||
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(functools.partial(
|
|
||||||
tab.printing.to_printer, callback=print_callback))
|
|
||||||
diag.exec_()
|
|
||||||
elif pdf:
|
elif pdf:
|
||||||
pdf = os.path.expanduser(pdf)
|
self._print_pdf(tab, pdf)
|
||||||
directory = os.path.dirname(pdf)
|
|
||||||
if directory and not os.path.exists(directory):
|
|
||||||
os.mkdir(directory)
|
|
||||||
tab.printing.to_pdf(pdf)
|
|
||||||
log.misc.debug("Print to file: {}".format(pdf))
|
|
||||||
else:
|
else:
|
||||||
def print_callback(ok):
|
self._print(tab)
|
||||||
if not ok:
|
|
||||||
message.error("Printing failed!")
|
|
||||||
diag.deleteLater()
|
|
||||||
|
|
||||||
diag = QPrintDialog()
|
|
||||||
diag.open(lambda:
|
|
||||||
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):
|
||||||
|
Loading…
Reference in New Issue
Block a user