src/eric7/WebBrowser/Tools/PrintToPdfDialog.py

branch
eric7
changeset 10240
cbd566d49a88
parent 9653
e67609152c5e
child 10439
21c28b0f9e41
equal deleted inserted replaced
10239:63fe209a1e71 10240:cbd566d49a88
5 5
6 """ 6 """
7 Module implementing a dialog to enter the data for printing a web page to PDF. 7 Module implementing a dialog to enter the data for printing a web page to PDF.
8 """ 8 """
9 9
10 import os 10 from PyQt6.QtCore import pyqtSlot
11 11 from PyQt6.QtGui import QPageLayout
12 from PyQt6.QtCore import QMarginsF, QStandardPaths, pyqtSlot 12 from PyQt6.QtPrintSupport import QPageSetupDialog
13 from PyQt6.QtGui import QPageLayout, QPageSize
14 from PyQt6.QtPrintSupport import QPageSetupDialog, QPrinter
15 from PyQt6.QtWidgets import QDialog 13 from PyQt6.QtWidgets import QDialog
16 14
17 from eric7.EricWidgets.EricPathPicker import EricPathPickerModes 15 from eric7.EricWidgets.EricPathPicker import EricPathPickerModes
18 16
19 from .Ui_PrintToPdfDialog import Ui_PrintToPdfDialog 17 from .Ui_PrintToPdfDialog import Ui_PrintToPdfDialog
23 """ 21 """
24 Class implementing a dialog to enter the data for printing a web page to 22 Class implementing a dialog to enter the data for printing a web page to
25 PDF. 23 PDF.
26 """ 24 """
27 25
28 def __init__(self, filePath, parent=None): 26 def __init__(self, printer, parent=None):
29 """ 27 """
30 Constructor 28 Constructor
31 29
32 @param filePath path of the file to write into 30 @param printer reference to an initialized QPrinter object
33 @type str 31 @type QPrinter
34 @param parent reference to the parent widget 32 @param parent reference to the parent widget
35 @type QWidget 33 @type QWidget
36 """ 34 """
37 super().__init__(parent) 35 super().__init__(parent)
38 self.setupUi(self) 36 self.setupUi(self)
39 37
38 self.__printer = printer
39
40 self.pdfFilePicker.setMode(EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE) 40 self.pdfFilePicker.setMode(EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE)
41 self.pdfFilePicker.setFilters(self.tr("PDF Files (*.pdf);;All Files (*)")) 41 self.pdfFilePicker.setFilters(self.tr("PDF Files (*.pdf);;All Files (*)"))
42 if not os.path.isabs(filePath): 42 self.pdfFilePicker.setText(self.__printer.outputFileName(), toNative=True)
43 documentsPath = QStandardPaths.writableLocation(
44 QStandardPaths.StandardLocation.DocumentsLocation
45 )
46 if documentsPath:
47 filePath = os.path.join(documentsPath, filePath)
48 else:
49 filePath = os.path.abspath(filePath)
50 self.pdfFilePicker.setText(filePath, toNative=True)
51
52 self.__currentPageLayout = QPageLayout(
53 QPageSize(QPageSize.PageSizeId.A4),
54 QPageLayout.Orientation.Portrait,
55 QMarginsF(0.0, 0.0, 0.0, 0.0),
56 )
57 43
58 self.__updatePageLayoutLabel() 44 self.__updatePageLayoutLabel()
59 45
60 @pyqtSlot() 46 @pyqtSlot()
61 def on_pageLayoutButton_clicked(self): 47 def on_pageLayoutButton_clicked(self):
62 """ 48 """
63 Private slot to define the page layout. 49 Private slot to define the page layout.
64 """ 50 """
65 printer = QPrinter() 51 dlg = QPageSetupDialog(self.__printer, self)
66 printer.setPageLayout(self.__currentPageLayout)
67
68 dlg = QPageSetupDialog(printer, self)
69 if dlg.exec() == QDialog.DialogCode.Accepted: 52 if dlg.exec() == QDialog.DialogCode.Accepted:
70 self.__currentPageLayout = printer.pageLayout()
71 self.__updatePageLayoutLabel() 53 self.__updatePageLayoutLabel()
72 54
73 def __updatePageLayoutLabel(self): 55 def __updatePageLayoutLabel(self):
74 """ 56 """
75 Private method to update the page layout label. 57 Private method to update the page layout label.
76 """ 58 """
77 orientation = ( 59 orientation = (
78 self.tr("Portrait") 60 self.tr("Portrait")
79 if ( 61 if (
80 self.__currentPageLayout.orientation() 62 self.__printer.pageLayout().orientation()
81 == QPageLayout.Orientation.Portrait 63 == QPageLayout.Orientation.Portrait
82 ) 64 )
83 else self.tr("Landscape") 65 else self.tr("Landscape")
84 ) 66 )
85 self.pageLayoutLabel.setText( 67 self.pageLayoutLabel.setText(
86 self.tr("{0}, {1}", "page size, page orientation").format( 68 self.tr("{0}, {1}", "page size, page orientation").format(
87 self.__currentPageLayout.pageSize().name(), orientation 69 self.__printer.pageLayout().pageSize().name(), orientation
88 ) 70 )
89 ) 71 )
90 72
91 def getData(self): 73 def getData(self):
92 """ 74 """
95 @return tuple containing the file path and the page layout 77 @return tuple containing the file path and the page layout
96 @rtype tuple of str and QPageLayout 78 @rtype tuple of str and QPageLayout
97 """ 79 """
98 return ( 80 return (
99 self.pdfFilePicker.text(toNative=True), 81 self.pdfFilePicker.text(toNative=True),
100 self.__currentPageLayout, 82 self.__printer.pageLayout(),
101 ) 83 )

eric ide

mercurial