|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2019 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 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSlot, QMarginsF, QStandardPaths |
|
15 from PyQt5.QtGui import QPageLayout, QPageSize |
|
16 from PyQt5.QtPrintSupport import QPrinter, QPageSetupDialog |
|
17 from PyQt5.QtWidgets import QDialog |
|
18 |
|
19 from E5Gui.E5PathPicker import E5PathPickerModes |
|
20 |
|
21 from .Ui_PrintToPdfDialog import Ui_PrintToPdfDialog |
|
22 |
|
23 |
|
24 class PrintToPdfDialog(QDialog, Ui_PrintToPdfDialog): |
|
25 """ |
|
26 Class implementing a dialog to enter the data for printing a web page to |
|
27 PDF. |
|
28 """ |
|
29 def __init__(self, filePath, parent=None): |
|
30 """ |
|
31 Constructor |
|
32 |
|
33 @param filePath path of the file to write into |
|
34 @type str |
|
35 @param parent reference to the parent widget |
|
36 @type QWidget |
|
37 """ |
|
38 super(PrintToPdfDialog, self).__init__(parent) |
|
39 self.setupUi(self) |
|
40 |
|
41 self.pdfFilePicker.setMode(E5PathPickerModes.SaveFileOverwriteMode) |
|
42 self.pdfFilePicker.setFilters(self.tr( |
|
43 "PDF Files (*.pdf);;" |
|
44 "All Files (*)")) |
|
45 if not os.path.isabs(filePath): |
|
46 documentsPath = QStandardPaths.writableLocation( |
|
47 QStandardPaths.DocumentsLocation) |
|
48 if documentsPath: |
|
49 filePath = os.path.join(documentsPath, filePath) |
|
50 else: |
|
51 filePath = os.path.abspath(filePath) |
|
52 self.pdfFilePicker.setText(filePath, toNative=True) |
|
53 |
|
54 self.__currentPageLayout = QPageLayout( |
|
55 QPageSize(QPageSize.A4), QPageLayout.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.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 if self.__currentPageLayout.orientation() == QPageLayout.Portrait: |
|
78 orientation = self.tr("Portrait") |
|
79 else: |
|
80 orientation = self.tr("Landscape") |
|
81 self.pageLayoutLabel.setText( |
|
82 self.tr("{0}, {1}", "page size, page orientation").format( |
|
83 self.__currentPageLayout.pageSize().name(), |
|
84 orientation)) |
|
85 |
|
86 def getData(self): |
|
87 """ |
|
88 Public method to get the dialog data. |
|
89 |
|
90 @return tuple containing the file path and the page layout |
|
91 @rtype tuple of str and QPageLayout |
|
92 """ |
|
93 return ( |
|
94 self.pdfFilePicker.text(toNative=True), |
|
95 self.__currentPageLayout, |
|
96 ) |