WebBrowser/Tools/PrintToPdfDialog.py

changeset 5036
318e879a37fa
child 5038
df7103c3f2a6
equal deleted inserted replaced
5035:cd2a891703d3 5036:318e879a37fa
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 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 from PyQt5.QtCore import pyqtSlot, QMarginsF
13 from PyQt5.QtGui import QPageLayout, QPageSize
14 from PyQt5.QtPrintSupport import QPrinter, QPageSetupDialog
15 from PyQt5.QtWidgets import QDialog
16
17 from E5Gui.E5PathPicker import E5PathPickerModes
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(PrintToPdfDialog, self).__init__(parent)
37 self.setupUi(self)
38
39 self.pdfFilePicker.setMode(E5PathPickerModes.SaveFileOverwriteMode)
40 self.pdfFilePicker.setFilters(self.tr(
41 "PDF Files (*.pdf);;"
42 "All Files (*)"))
43 self.pdfFilePicker.setText(filePath, toNative=True)
44
45 self.__currentPageLayout = QPageLayout(
46 QPageSize(QPageSize.A4), QPageLayout.Portrait,
47 QMarginsF(0.0, 0.0, 0.0, 0.0))
48
49 self.__updatePageLayoutLabel()
50
51 @pyqtSlot()
52 def on_pageLayoutButton_clicked(self):
53 """
54 Private slot to define the page layout.
55 """
56 printer = QPrinter()
57 printer.setPageLayout(self.__currentPageLayout)
58
59 dlg = QPageSetupDialog(printer, self)
60 if dlg.exec_() == QDialog.Accepted:
61 self.__currentPageLayout = printer.pageLayout()
62 self.__updatePageLayoutLabel()
63
64 def __updatePageLayoutLabel(self):
65 """
66 Private method to update the page layout label.
67 """
68 if self.__currentPageLayout.orientation() == QPageLayout.Portrait:
69 orientation = self.tr("Portrait")
70 else:
71 orientation = self.tr("Landscape")
72 self.pageLayoutLabel.setText(
73 self.tr("{0}, {1}", "page size, page orientation").format(
74 self.__currentPageLayout.pageSize().name(),
75 orientation))
76
77 def getData(self):
78 """
79 Public method to get the dialog data.
80
81 @return tuple containing the file path and the page layout
82 @rtype tuple of str and QPageLayout
83 """
84 return (
85 self.pdfFilePicker.text(toNative=True),
86 self.__currentPageLayout,
87 )

eric ide

mercurial