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