1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter the data for printing a web page to PDF.
8"""
9
10import os
11
12from PyQt5.QtCore import pyqtSlot, QMarginsF, QStandardPaths
13from PyQt5.QtGui import QPageLayout, QPageSize
14from PyQt5.QtPrintSupport import QPrinter, QPageSetupDialog
15from PyQt5.QtWidgets import QDialog
16
17from E5Gui.E5PathPicker import E5PathPickerModes
18
19from .Ui_PrintToPdfDialog import Ui_PrintToPdfDialog
20
21
22class 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(E5PathPickerModes.SaveFileOverwriteMode)
40        self.pdfFilePicker.setFilters(self.tr(
41            "PDF Files (*.pdf);;"
42            "All Files (*)"))
43        if not os.path.isabs(filePath):
44            documentsPath = QStandardPaths.writableLocation(
45                QStandardPaths.StandardLocation.DocumentsLocation)
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        self.__updatePageLayoutLabel()
58
59    @pyqtSlot()
60    def on_pageLayoutButton_clicked(self):
61        """
62        Private slot to define the page layout.
63        """
64        printer = QPrinter()
65        printer.setPageLayout(self.__currentPageLayout)
66
67        dlg = QPageSetupDialog(printer, self)
68        if dlg.exec() == QDialog.DialogCode.Accepted:
69            self.__currentPageLayout = printer.pageLayout()
70            self.__updatePageLayoutLabel()
71
72    def __updatePageLayoutLabel(self):
73        """
74        Private method to update the page layout label.
75        """
76        orientation = (
77            self.tr("Portrait")
78            if (self.__currentPageLayout.orientation() ==
79                QPageLayout.Orientation.Portrait) else
80            self.tr("Landscape")
81        )
82        self.pageLayoutLabel.setText(
83            self.tr("{0}, {1}", "page size, page orientation").format(
84                self.__currentPageLayout.pageSize().name(),
85                orientation))
86
87    def getData(self):
88        """
89        Public method to get the dialog data.
90
91        @return tuple containing the file path and the page layout
92        @rtype tuple of str and QPageLayout
93        """
94        return (
95            self.pdfFilePicker.text(toNative=True),
96            self.__currentPageLayout,
97        )
98