9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 import os |
12 import os |
13 |
13 |
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QFile, qVersion |
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QDir, QFile, \ |
|
15 QFileDevice, QTemporaryFile, qVersion |
15 from PyQt5.QtGui import QIcon, QPixmap, QPainter |
16 from PyQt5.QtGui import QIcon, QPixmap, QPainter |
16 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QMenu, QToolButton, QDialog |
17 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QMenu, QToolButton, QDialog |
17 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog |
18 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QAbstractPrintDialog |
18 |
19 |
19 from E5Gui.E5TabWidget import E5TabWidget |
20 from E5Gui.E5TabWidget import E5TabWidget |
20 from E5Gui import E5MessageBox |
21 from E5Gui import E5MessageBox |
21 from E5Gui.E5Application import e5App |
22 from E5Gui.E5Application import e5App |
22 |
23 |
23 from .WebBrowserView import WebBrowserView |
24 from .WebBrowserView import WebBrowserView |
24 from .WebBrowserPage import WebBrowserPage |
25 from .WebBrowserPage import WebBrowserPage |
25 from .Tools import WebBrowserTools |
26 from .Tools import WebBrowserTools |
|
27 from .Tools import FilePrinter |
26 from . import WebInspector |
28 from . import WebInspector |
27 |
29 |
28 import UI.PixmapCache |
30 import UI.PixmapCache |
29 |
31 |
30 import Utilities |
32 import Utilities |
177 UI.PixmapCache.getIcon("tabCloseOther.png"), |
181 UI.PixmapCache.getIcon("tabCloseOther.png"), |
178 self.tr("Close Others"), self.__tabContextMenuCloseOthers) |
182 self.tr("Close Others"), self.__tabContextMenuCloseOthers) |
179 self.__tabContextMenu.addAction( |
183 self.__tabContextMenu.addAction( |
180 self.tr('Close All'), self.closeAllBrowsers) |
184 self.tr('Close All'), self.closeAllBrowsers) |
181 self.__tabContextMenu.addSeparator() |
185 self.__tabContextMenu.addSeparator() |
182 if not Globals.isWindowsPlatform(): |
186 if not Globals.isWindowsPlatform() and qVersion() < "5.7.0": |
183 # TODO: implement printing based on printToPdf() |
|
184 self.__tabContextMenu.addAction( |
187 self.__tabContextMenu.addAction( |
185 UI.PixmapCache.getIcon("printPreview.png"), |
188 UI.PixmapCache.getIcon("printPreview.png"), |
186 self.tr('Print Preview'), self.__tabContextMenuPrintPreview) |
189 self.tr('Print Preview'), self.__tabContextMenuPrintPreview) |
|
190 if not Globals.isWindowsPlatform() or qVersion() >= "5.7.0": |
187 self.__tabContextMenu.addAction( |
191 self.__tabContextMenu.addAction( |
188 UI.PixmapCache.getIcon("print.png"), |
192 UI.PixmapCache.getIcon("print.png"), |
189 self.tr('Print'), self.__tabContextMenuPrint) |
193 self.tr('Print'), self.__tabContextMenuPrint) |
190 if Globals.isLinuxPlatform() or qVersion() >= "5.7.0": |
194 if Globals.isLinuxPlatform() or qVersion() >= "5.7.0": |
191 self.__tabContextMenu.addAction( |
195 self.__tabContextMenu.addAction( |
622 ) |
626 ) |
623 printerName = Preferences.getPrinter("PrinterName") |
627 printerName = Preferences.getPrinter("PrinterName") |
624 if printerName: |
628 if printerName: |
625 printer.setPrinterName(printerName) |
629 printer.setPrinterName(printerName) |
626 printer.setResolution(Preferences.getPrinter("Resolution")) |
630 printer.setResolution(Preferences.getPrinter("Resolution")) |
|
631 documentName = WebBrowserTools.getFileNameFromUrl(browser.url()) |
|
632 printer.setDocName(documentName) |
627 |
633 |
628 printDialog = QPrintDialog(printer, self) |
634 printDialog = QPrintDialog(printer, self) |
|
635 printDialog.setOptions(QAbstractPrintDialog.PrintToFile | |
|
636 QAbstractPrintDialog.PrintShowPageSize) |
|
637 if not Globals.isWindowsPlatform(): |
|
638 if FilePrinter.isCupsAvailable(): |
|
639 printDialog.setOption(QAbstractPrintDialog.PrintCollateCopies) |
|
640 printDialog.setOption(QAbstractPrintDialog.PrintPageRange) |
629 if printDialog.exec_() == QDialog.Accepted: |
641 if printDialog.exec_() == QDialog.Accepted: |
630 browser.render(printer) |
642 if not hasattr(browser.page(), "printToPdf"): |
|
643 browser.render(printer) |
|
644 else: |
|
645 if printer.outputFormat() == QPrinter.PdfFormat: |
|
646 # print to PDF file selected |
|
647 browser.page().printToPdf( |
|
648 lambda pdf: self.__pdfGeneratedForSave( |
|
649 printer.outputFileName(), pdf), |
|
650 printer.pageLayout()) |
|
651 else: |
|
652 # print to printer |
|
653 self.__pdfPrinter = printer |
|
654 browser.page().printToPdf( |
|
655 self.__pdfGeneratedForPrinting, |
|
656 printer.pageLayout()) |
631 |
657 |
632 @pyqtSlot() |
658 @pyqtSlot() |
633 def printBrowserPdf(self, browser=None): |
659 def printBrowserPdf(self, browser=None): |
634 """ |
660 """ |
635 Public slot called to print the displayed page to PDF. |
661 Public slot called to print the displayed page to PDF. |
663 E5MessageBox.Yes), |
689 E5MessageBox.Yes), |
664 E5MessageBox.No) |
690 E5MessageBox.No) |
665 if res == E5MessageBox.No: |
691 if res == E5MessageBox.No: |
666 return |
692 return |
667 browser.page().printToPdf( |
693 browser.page().printToPdf( |
668 lambda p: self.__pdfGenerated(filePath, p), |
694 lambda pdf: self.__pdfGeneratedForSave(filePath, pdf), |
669 pageLayout) |
695 pageLayout) |
670 elif Globals.isLinuxPlatform(): |
696 elif Globals.isLinuxPlatform(): |
671 printer = QPrinter(mode=QPrinter.HighResolution) |
697 printer = QPrinter(mode=QPrinter.HighResolution) |
672 if Preferences.getPrinter("ColorMode"): |
698 if Preferences.getPrinter("ColorMode"): |
673 printer.setColorMode(QPrinter.Color) |
699 printer.setColorMode(QPrinter.Color) |
683 |
709 |
684 printDialog = QPrintDialog(printer, self) |
710 printDialog = QPrintDialog(printer, self) |
685 if printDialog.exec_() == QDialog.Accepted: |
711 if printDialog.exec_() == QDialog.Accepted: |
686 browser.render(printer) |
712 browser.render(printer) |
687 |
713 |
688 def __pdfGenerated(self, filePath, pdfData): |
714 def __pdfGeneratedForSave(self, filePath, pdfData): |
689 """ |
715 """ |
690 Private slot handling the generated PDF data. |
716 Private slot to save the generated PDF data to a file. |
691 |
717 |
692 @param filePath path to save the PDF to |
718 @param filePath path to save the PDF to |
693 @type str |
719 @type str |
694 @param pdfData generated PDF document |
720 @param pdfData generated PDF document |
695 @type QByteArray |
721 @type QByteArray |
696 """ |
722 """ |
697 if pdfData.size() == 0: |
723 if pdfData.size() == 0: |
698 return |
724 return |
699 |
725 |
700 pdfFile = QFile(filePath) |
726 pdfFile = QFile(filePath) |
701 if not pdfFile.open(QFile.WriteOnly): |
727 if pdfFile.open(QFile.WriteOnly): |
|
728 pdfFile.write(pdfData) |
|
729 pdfFile.close() |
|
730 if pdfFile.error() != QFileDevice.NoError: |
|
731 E5MessageBox.critical( |
|
732 self, |
|
733 self.tr("Print to PDF"), |
|
734 self.tr("""<p>The PDF could not be written to file <b>{0}""" |
|
735 """</b>.</p><p><b>Error:</b> {1}</p>""").format( |
|
736 filePath, pdfFile.errorString()), |
|
737 E5MessageBox.StandardButtons( |
|
738 E5MessageBox.Ok)) |
|
739 |
|
740 def __pdfGeneratedForPrinting(self, pdfData): |
|
741 """ |
|
742 Private slot to print the generated PDF data. |
|
743 |
|
744 @param pdfData generated PDF document |
|
745 @type QByteArray |
|
746 """ |
|
747 if self.__pdfPrinter is None or pdfData.isEmpty(): |
702 return |
748 return |
703 |
749 |
704 pdfFile.write(pdfData) |
750 tempFile = QTemporaryFile(QDir.tempPath() + "/ericBrowserXXXXXX.pdf") |
705 pdfFile.close() |
751 tempFile.setAutoRemove(False) |
|
752 if tempFile.open(): |
|
753 bytesWritten = tempFile.write(pdfData) |
|
754 tempFile.close() |
|
755 if bytesWritten == pdfData.size(): |
|
756 if Globals.isWindowsPlatform(): |
|
757 printerName = self.__pdfPrinter.printerName() |
|
758 import ctypes |
|
759 ctypes.windll.shell32.ShellExecuteW( |
|
760 self.winId(), "printto", tempFile.fileName(), |
|
761 '"{0}"'.format(printerName), None, 0) |
|
762 else: |
|
763 FilePrinter.printFile( |
|
764 self.__pdfPrinter, tempFile.fileName(), |
|
765 FilePrinter.FilePrinter.SystemDeletesFiles, |
|
766 FilePrinter.FilePrinter.SystemSelectsPages) |
|
767 else: |
|
768 tempFile.remove() |
|
769 |
|
770 self.__pdfPrinter = None |
706 |
771 |
707 @pyqtSlot() |
772 @pyqtSlot() |
708 def printPreviewBrowser(self, browser=None): |
773 def printPreviewBrowser(self, browser=None): |
709 """ |
774 """ |
710 Public slot called to show a print preview of the displayed file. |
775 Public slot called to show a print preview of the displayed file. |