src/eric7/WebBrowser/Tools/PrintToPdfDialog.py

Fri, 04 Nov 2022 13:52:26 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 04 Nov 2022 13:52:26 +0100
branch
eric7
changeset 9473
3f23dbf37dbe
parent 9413
80c06d472826
child 9576
be9f8e7e42e0
permissions
-rw-r--r--

Resorted the import statements using isort.

# -*- coding: utf-8 -*-

# Copyright (c) 2016 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to enter the data for printing a web page to PDF.
"""

import os

from PyQt6.QtCore import QMarginsF, QStandardPaths, pyqtSlot
from PyQt6.QtGui import QPageLayout, QPageSize
from PyQt6.QtPrintSupport import QPageSetupDialog, QPrinter
from PyQt6.QtWidgets import QDialog

from eric7.EricWidgets.EricPathPicker import EricPathPickerModes

from .Ui_PrintToPdfDialog import Ui_PrintToPdfDialog


class PrintToPdfDialog(QDialog, Ui_PrintToPdfDialog):
    """
    Class implementing a dialog to enter the data for printing a web page to
    PDF.
    """

    def __init__(self, filePath, parent=None):
        """
        Constructor

        @param filePath path of the file to write into
        @type str
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.pdfFilePicker.setMode(EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE)
        self.pdfFilePicker.setFilters(self.tr("PDF Files (*.pdf);;" "All Files (*)"))
        if not os.path.isabs(filePath):
            documentsPath = QStandardPaths.writableLocation(
                QStandardPaths.StandardLocation.DocumentsLocation
            )
            if documentsPath:
                filePath = os.path.join(documentsPath, filePath)
            else:
                filePath = os.path.abspath(filePath)
        self.pdfFilePicker.setText(filePath, toNative=True)

        self.__currentPageLayout = QPageLayout(
            QPageSize(QPageSize.PageSizeId.A4),
            QPageLayout.Orientation.Portrait,
            QMarginsF(0.0, 0.0, 0.0, 0.0),
        )

        self.__updatePageLayoutLabel()

    @pyqtSlot()
    def on_pageLayoutButton_clicked(self):
        """
        Private slot to define the page layout.
        """
        printer = QPrinter()
        printer.setPageLayout(self.__currentPageLayout)

        dlg = QPageSetupDialog(printer, self)
        if dlg.exec() == QDialog.DialogCode.Accepted:
            self.__currentPageLayout = printer.pageLayout()
            self.__updatePageLayoutLabel()

    def __updatePageLayoutLabel(self):
        """
        Private method to update the page layout label.
        """
        orientation = (
            self.tr("Portrait")
            if (
                self.__currentPageLayout.orientation()
                == QPageLayout.Orientation.Portrait
            )
            else self.tr("Landscape")
        )
        self.pageLayoutLabel.setText(
            self.tr("{0}, {1}", "page size, page orientation").format(
                self.__currentPageLayout.pageSize().name(), orientation
            )
        )

    def getData(self):
        """
        Public method to get the dialog data.

        @return tuple containing the file path and the page layout
        @rtype tuple of str and QPageLayout
        """
        return (
            self.pdfFilePicker.text(toNative=True),
            self.__currentPageLayout,
        )

eric ide

mercurial