File: printing.py

package info (click to toggle)
python-qpageview 0.6.2-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 780 kB
  • sloc: python: 5,215; makefile: 22
file content (137 lines) | stat: -rw-r--r-- 4,778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
#
# This file is part of the qpageview package.
#
# Copyright (c) 2019 - 2019 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Printing facilities for qpageview.
"""


from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QPainter, QTransform
from PyQt5.QtWidgets import QMessageBox, QProgressDialog

from . import backgroundjob


class PrintJob(backgroundjob.Job):
    """Performs a print job in the background.

    Emits the following signals:

        ``progress(pageNumber, num, total)``
            before each Page
        ``finished()``
            when done

    """
    progress = pyqtSignal(int, int, int)

    aborted = False

    def __init__(self, printer, pageList, parent=None):
        """Initialize with a QPrinter object and a list of pages.

        pageList may be a list of two-tuples (num, page). Otherwise, the pages
        are numbered from 1 in the progress message. The pages are copied.

        """
        super().__init__(parent)
        self.printer = printer
        self.setPageList(pageList)

    def setPageList(self, pageList):
        """Set the pagelist to print.

        pageList may be a list of two-tuples (num, page). Otherwise, the pages
        are numbered from 1 in the progress message. The pages are copied.

        """
        self.pageList = []
        for n, page in enumerate(pageList, 1):
            if isinstance(page, tuple):
                pageNum, page = page
            else:
                pageNum = n
            page = page.copy()
            # set zoom to 1.0 so computations based on geometry() are
            # accurate enough
            page.updateSize(page.dpi, page.dpi, 1.0)
            self.pageList.append((pageNum, page))

    def work(self):
        """Paint the pages to the printer in the background."""
        p = self.printer
        p.setFullPage(True)
        painter = QPainter(p)
        for n, (num, page) in enumerate(self.pageList):
            if self.isInterruptionRequested():
                self.aborted = True
                return p.abort()
            self.progress.emit(num, n+1, len(self.pageList))
            if n:
                p.newPage()
            painter.save()
            # center on the page and use scale 100% (TEMP)
            r = p.pageRect()
            m = QTransform()
            m.translate(r.center().x(), r.center().y())
            m.scale(p.logicalDpiX() / page.dpi, p.logicalDpiY() / page.dpi)
            m.rotate(page.rotation * 90)
            m.scale(page.scaleX, page.scaleY)
            m.translate(page.pageWidth / -2, page.pageHeight / -2)
            painter.setTransform(m, True)
            page.print(painter)
            painter.restore()
        return painter.end()


class PrintProgressDialog(QProgressDialog):
    """A simple progress dialog displaying the printing progress."""
    def __init__(self, job, parent=None):
        """Initializes ourselves with the print job and optional parent widget."""
        super().__init__(parent)
        self._job = job
        job.progress.connect(self.showProgress)
        job.finished.connect(self.jobFinished)
        self.canceled.connect(job.requestInterruption)
        self.setMinimumDuration(0)
        self.setRange(0, len(job.pageList))
        self.setLabelText("Preparing to print...")

    def showProgress(self, page, num, total):
        """Called by the job when printing a page."""
        self.setValue(num)
        self.setLabelText("Printing page {page} ({num} of {total})...".format(
                page=page, num=num, total=total))

    def jobFinished(self):
        """Called when the print job has finished."""
        if not self._job.result and not self._job.aborted:
            self.showErrorMessage()
        del self._job
        self.deleteLater()

    def showErrorMessage(self):
        """Reimplement to show a different or translated error message."""
        QMessageBox.warning(self.parent(), "Printing Error",
                    "Could not send the document to the printer.")