|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the printer functionality. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 from PyQt4.Qsci import QsciScintilla, QsciPrinter |
|
13 |
|
14 import Preferences |
|
15 |
|
16 class Printer(QsciPrinter): |
|
17 """ |
|
18 Class implementing the QextScintillaPrinter with a header. |
|
19 """ |
|
20 def __init__(self, mode = QPrinter.ScreenResolution): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param mode mode of the printer (QPrinter.PrinterMode) |
|
25 """ |
|
26 QsciPrinter.__init__(self, mode) |
|
27 |
|
28 self.setMagnification(Preferences.getPrinter("Magnification")) |
|
29 if Preferences.getPrinter("ColorMode"): |
|
30 self.setColorMode(QPrinter.Color) |
|
31 else: |
|
32 self.setColorMode(QPrinter.GrayScale) |
|
33 if Preferences.getPrinter("FirstPageFirst"): |
|
34 self.setPageOrder(QPrinter.FirstPageFirst) |
|
35 else: |
|
36 self.setPageOrder(QPrinter.LastPageFirst) |
|
37 self.setPrinterName(Preferences.getPrinter("PrinterName")) |
|
38 self.time = QTime.currentTime().toString(Qt.LocalDate) |
|
39 self.date = QDate.currentDate().toString(Qt.LocalDate) |
|
40 self.headerFont = Preferences.getPrinter("HeaderFont") |
|
41 |
|
42 def formatPage(self, painter, drawing, area, pagenr): |
|
43 """ |
|
44 Private method to generate a header line. |
|
45 |
|
46 @param painter the paint canvas (QPainter) |
|
47 @param drawing flag indicating that something should be drawn |
|
48 @param area the drawing area (QRect) |
|
49 @param pagenr the page number (int) |
|
50 """ |
|
51 fn = self.docName() |
|
52 |
|
53 header = QApplication.translate('Printer', |
|
54 '{0} - Printed on {1}, {2} - Page {3}')\ |
|
55 .format(fn, self.date, self.time, pagenr) |
|
56 |
|
57 painter.save() |
|
58 painter.setFont(self.headerFont) # set our header font |
|
59 painter.setPen(QColor(Qt.black)) # set color |
|
60 if drawing: |
|
61 painter.drawText(area.right() - painter.fontMetrics().width(header), |
|
62 area.top() + painter.fontMetrics().ascent(), header) |
|
63 area.setTop(area.top() + painter.fontMetrics().height() + 5) |
|
64 painter.restore() |