5 |
5 |
6 """ |
6 """ |
7 Module implementing the central widget showing the web pages. |
7 Module implementing the central widget showing the web pages. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 from PyQt6.QtCore import Qt, QUrl, pyqtSignal, pyqtSlot |
11 |
11 from PyQt6.QtGui import QIcon, QPainter, QPixmap |
12 from PyQt6.QtCore import QMarginsF, Qt, QUrl, pyqtSignal, pyqtSlot |
12 from PyQt6.QtWidgets import QHBoxLayout, QMenu, QToolButton, QWidget |
13 from PyQt6.QtGui import QIcon, QPageLayout, QPainter, QPixmap |
|
14 from PyQt6.QtPrintSupport import ( |
|
15 QAbstractPrintDialog, |
|
16 QPrintDialog, |
|
17 QPrinter, |
|
18 QPrintPreviewDialog, |
|
19 ) |
|
20 from PyQt6.QtWidgets import QDialog, QHBoxLayout, QMenu, QToolButton, QWidget |
|
21 |
13 |
22 from eric7 import Preferences |
14 from eric7 import Preferences |
23 from eric7.EricGui import EricPixmapCache |
15 from eric7.EricGui import EricPixmapCache |
24 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
|
25 from eric7.EricWidgets import EricMessageBox |
16 from eric7.EricWidgets import EricMessageBox |
26 from eric7.EricWidgets.EricApplication import ericApp |
17 from eric7.EricWidgets.EricApplication import ericApp |
27 from eric7.EricWidgets.EricTabWidget import EricTabWidget |
18 from eric7.EricWidgets.EricTabWidget import EricTabWidget |
28 from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
19 from eric7.SystemUtilities import FileSystemUtilities |
29 from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow |
20 from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow |
30 |
21 |
31 from . import WebInspector |
22 from . import WebInspector |
32 from .ClosedTabsManager import ClosedTabsManager |
23 from .ClosedTabsManager import ClosedTabsManager |
33 from .Tools import WebBrowserTools |
|
34 from .UrlBar.StackedUrlBar import StackedUrlBar |
24 from .UrlBar.StackedUrlBar import StackedUrlBar |
35 from .WebBrowserPage import WebBrowserPage |
25 from .WebBrowserPage import WebBrowserPage |
36 from .WebBrowserTabBar import WebBrowserTabBar |
26 from .WebBrowserTabBar import WebBrowserTabBar |
37 from .WebBrowserView import WebBrowserView |
27 from .WebBrowserView import WebBrowserView |
38 |
|
39 |
|
40 def isCupsAvailable(): |
|
41 """ |
|
42 Static method to test the availability of CUPS. |
|
43 |
|
44 @return flag indicating the availability of CUPS |
|
45 @rtype bool |
|
46 """ |
|
47 if OSUtilities.isMacPlatform(): |
|
48 # OS X/MacOS always have CUPS |
|
49 return True |
|
50 elif OSUtilities.isLinuxPlatform(): |
|
51 testPrinter = QPrinter() |
|
52 return testPrinter.supportsMultipleCopies() |
|
53 else: |
|
54 return False |
|
55 |
28 |
56 |
29 |
57 class WebBrowserTabWidget(EricTabWidget): |
30 class WebBrowserTabWidget(EricTabWidget): |
58 """ |
31 """ |
59 Class implementing the central widget showing the web pages. |
32 Class implementing the central widget showing the web pages. |
648 @param browser reference to the browser to be printed (WebBrowserView) |
621 @param browser reference to the browser to be printed (WebBrowserView) |
649 """ |
622 """ |
650 if browser is None: |
623 if browser is None: |
651 browser = self.currentBrowser() |
624 browser = self.currentBrowser() |
652 |
625 |
653 printer = QPrinter(mode=QPrinter.PrinterMode.HighResolution) |
626 if browser is not None: |
654 if Preferences.getPrinter("ColorMode"): |
627 browser.printPage() |
655 printer.setColorMode(QPrinter.ColorMode.Color) |
|
656 else: |
|
657 printer.setColorMode(QPrinter.ColorMode.GrayScale) |
|
658 if Preferences.getPrinter("FirstPageFirst"): |
|
659 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst) |
|
660 else: |
|
661 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst) |
|
662 printer.setPageMargins( |
|
663 QMarginsF( |
|
664 Preferences.getPrinter("LeftMargin") * 10, |
|
665 Preferences.getPrinter("TopMargin") * 10, |
|
666 Preferences.getPrinter("RightMargin") * 10, |
|
667 Preferences.getPrinter("BottomMargin") * 10, |
|
668 ), |
|
669 QPageLayout.Unit.Millimeter, |
|
670 ) |
|
671 printerName = Preferences.getPrinter("PrinterName") |
|
672 if printerName: |
|
673 printer.setPrinterName(printerName) |
|
674 printer.setResolution(Preferences.getPrinter("Resolution")) |
|
675 documentName = WebBrowserTools.getFileNameFromUrl(browser.url()) |
|
676 printer.setDocName(documentName) |
|
677 |
|
678 printDialog = QPrintDialog(printer, self) |
|
679 printDialog.setOptions( |
|
680 QAbstractPrintDialog.PrintDialogOption.PrintToFile |
|
681 | QAbstractPrintDialog.PrintDialogOption.PrintShowPageSize |
|
682 ) |
|
683 if not OSUtilities.isWindowsPlatform(): |
|
684 if isCupsAvailable(): |
|
685 printDialog.setOption( |
|
686 QAbstractPrintDialog.PrintDialogOption.PrintCollateCopies |
|
687 ) |
|
688 printDialog.setOption(QAbstractPrintDialog.PrintDialogOption.PrintPageRange) |
|
689 if printDialog.exec() == QDialog.DialogCode.Accepted: |
|
690 browser.page().execPrintPage(printer, 10 * 1000) |
|
691 |
628 |
692 @pyqtSlot() |
629 @pyqtSlot() |
693 def printBrowserPdf(self, browser=None): |
630 def printBrowserPdf(self, browser=None): |
694 """ |
631 """ |
695 Public slot called to print the displayed page to PDF. |
632 Public slot called to print the displayed page to PDF. |
696 |
633 |
697 @param browser reference to the browser to be printed (HelpBrowser) |
634 @param browser reference to the browser to be printed (HelpBrowser) |
698 """ |
635 """ |
699 from .Tools.PrintToPdfDialog import PrintToPdfDialog |
|
700 |
|
701 if browser is None: |
636 if browser is None: |
702 browser = self.currentBrowser() |
637 browser = self.currentBrowser() |
703 |
638 |
704 name = WebBrowserTools.getFileNameFromUrl(browser.url()) |
639 if browser is not None: |
705 if name: |
640 browser.printPageToPdf() |
706 name = name.rsplit(".", 1)[0] |
|
707 name += ".pdf" |
|
708 if hasattr(browser.page(), "printToPdf"): |
|
709 if not name: |
|
710 name = "printout.pdf" |
|
711 dlg = PrintToPdfDialog(name, self) |
|
712 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
713 filePath, pageLayout = dlg.getData() |
|
714 if filePath: |
|
715 if os.path.exists(filePath): |
|
716 res = EricMessageBox.warning( |
|
717 self, |
|
718 self.tr("Print to PDF"), |
|
719 self.tr( |
|
720 """<p>The file <b>{0}</b> exists""" |
|
721 """ already. Shall it be""" |
|
722 """ overwritten?</p>""" |
|
723 ).format(filePath), |
|
724 EricMessageBox.No | EricMessageBox.Yes, |
|
725 EricMessageBox.No, |
|
726 ) |
|
727 if res == EricMessageBox.No: |
|
728 return |
|
729 browser.page().printToPdf( |
|
730 lambda pdf: self.__pdfGeneratedForSave(filePath, pdf), |
|
731 pageLayout, |
|
732 ) |
|
733 elif OSUtilities.isLinuxPlatform(): |
|
734 printer = QPrinter(mode=QPrinter.PrinterMode.HighResolution) |
|
735 if Preferences.getPrinter("ColorMode"): |
|
736 printer.setColorMode(QPrinter.ColorMode.Color) |
|
737 else: |
|
738 printer.setColorMode(QPrinter.ColorMode.GrayScale) |
|
739 printerName = Preferences.getPrinter("PrinterName") |
|
740 if printerName: |
|
741 printer.setPrinterName(printerName) |
|
742 printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat) |
|
743 if name: |
|
744 printer.setOutputFileName(name) |
|
745 printer.setResolution(Preferences.getPrinter("Resolution")) |
|
746 |
|
747 printDialog = QPrintDialog(printer, self) |
|
748 if printDialog.exec() == QDialog.DialogCode.Accepted: |
|
749 browser.render(printer) |
|
750 |
|
751 def __pdfGeneratedForSave(self, filePath, pdfData): |
|
752 """ |
|
753 Private slot to save the generated PDF data to a file. |
|
754 |
|
755 @param filePath path to save the PDF to |
|
756 @type str |
|
757 @param pdfData generated PDF document |
|
758 @type QByteArray |
|
759 """ |
|
760 if pdfData.size() == 0: |
|
761 return |
|
762 |
|
763 try: |
|
764 with open(filePath, "wb") as f: |
|
765 f.write(pdfData) |
|
766 except OSError as err: |
|
767 EricMessageBox.critical( |
|
768 self, |
|
769 self.tr("Print to PDF"), |
|
770 self.tr( |
|
771 """<p>The PDF could not be written to file <b>{0}""" |
|
772 """</b>.</p><p><b>Error:</b> {1}</p>""" |
|
773 ).format(filePath, str(err)), |
|
774 EricMessageBox.Ok, |
|
775 ) |
|
776 |
641 |
777 @pyqtSlot() |
642 @pyqtSlot() |
778 def printPreviewBrowser(self, browser=None): |
643 def printPreviewBrowser(self, browser=None): |
779 """ |
644 """ |
780 Public slot called to show a print preview of the displayed file. |
645 Public slot called to show a print preview of the displayed file. |
782 @param browser reference to the browser to be printed (WebBrowserView) |
647 @param browser reference to the browser to be printed (WebBrowserView) |
783 """ |
648 """ |
784 if browser is None: |
649 if browser is None: |
785 browser = self.currentBrowser() |
650 browser = self.currentBrowser() |
786 |
651 |
787 printer = QPrinter(mode=QPrinter.PrinterMode.HighResolution) |
652 if browser is not None: |
788 if Preferences.getPrinter("ColorMode"): |
653 browser.printPreviewPage() |
789 printer.setColorMode(QPrinter.ColorMode.Color) |
|
790 else: |
|
791 printer.setColorMode(QPrinter.ColorMode.GrayScale) |
|
792 if Preferences.getPrinter("FirstPageFirst"): |
|
793 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst) |
|
794 else: |
|
795 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst) |
|
796 printer.setPageMargins( |
|
797 QMarginsF( |
|
798 Preferences.getPrinter("LeftMargin") * 10, |
|
799 Preferences.getPrinter("TopMargin") * 10, |
|
800 Preferences.getPrinter("RightMargin") * 10, |
|
801 Preferences.getPrinter("BottomMargin") * 10, |
|
802 ), |
|
803 QPageLayout.Unit.Millimeter, |
|
804 ) |
|
805 printerName = Preferences.getPrinter("PrinterName") |
|
806 if printerName: |
|
807 printer.setPrinterName(printerName) |
|
808 printer.setResolution(Preferences.getPrinter("Resolution")) |
|
809 |
|
810 preview = QPrintPreviewDialog(printer, self) |
|
811 preview.resize(800, 750) |
|
812 preview.paintRequested.connect( |
|
813 lambda p: self.__printPreviewRequested(p, browser) |
|
814 ) |
|
815 preview.exec() |
|
816 |
|
817 def __printPreviewRequested(self, printer, browser): |
|
818 """ |
|
819 Private slot to generate the print preview. |
|
820 |
|
821 @param printer reference to the printer object |
|
822 @type QPrinter |
|
823 @param browser reference to the browser to be printed |
|
824 @type WebBrowserView |
|
825 """ |
|
826 with EricOverrideCursor(): |
|
827 browser.page().execPrintPage(printer, 10 * 1000) |
|
828 |
654 |
829 def __sourceChanged(self, url, browser): |
655 def __sourceChanged(self, url, browser): |
830 """ |
656 """ |
831 Private slot to handle a change of a browsers source. |
657 Private slot to handle a change of a browsers source. |
832 |
658 |