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

''' 

Task Coach - Your friendly task manager 

Copyright (C) 2004-2010 Task Coach developers <developers@taskcoach.org> 

Copyright (C) 2008 Rob McMullen <rob.mcmullen@gmail.com> 

 

Task Coach 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 3 of the License, or 

(at your option) any later version. 

 

Task Coach 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, see <http://www.gnu.org/licenses/>. 

''' 

 

import wx 

from taskcoachlib import persistence, patterns 

from taskcoachlib.i18n import _ 

 

 

class PrinterSettings(object): 

    __metaclass__ = patterns.Singleton 

 

    edges = ('top', 'left', 'bottom', 'right') 

 

    def __init__(self, settings): 

        self.settings = settings 

        self.printData = wx.PrintData() 

        self.pageSetupData = wx.PageSetupDialogData(self.printData) 

        self._initializeFromSettings() 

 

    def updatePageSetupData(self, data): 

        self.pageSetupData = wx.PageSetupDialogData(data) 

        self.updatePrintData(data.GetPrintData()) 

        self._saveToSettings() 

 

    def updatePrintData(self, printData): 

        self.printData = wx.PrintData(printData) 

        self.pageSetupData.SetPrintData(self.printData) 

 

    def __getattr__(self, attr): 

        try: 

            return getattr(self.pageSetupData, attr) 

        except AttributeError: 

            return getattr(self.printData, attr) 

 

    def _initializeFromSettings(self): 

        ''' Load the printer settings from the user settings. ''' 

        margin = dict() 

        for edge in self.edges: 

            margin[edge] = self._getSetting('margin_'+edge) 

        topLeft = wx.Point(margin['top'], margin['left']) 

        bottomRight = wx.Point(margin['bottom'], margin['right']) 

        self.SetMarginTopLeft(topLeft) 

        self.SetMarginBottomRight(bottomRight) 

        self.SetPaperId(self._getSetting('paper_id')) 

        self.SetOrientation(self._getSetting('orientation')) 

 

    def _saveToSettings(self): 

        ''' Save the printer settings to the user settings. ''' 

        margin = dict() 

        margin['top'], margin['left'] = self.GetMarginTopLeft() 

        margin['bottom'], margin['right'] = self.GetMarginBottomRight() 

        for edge in self.edges: 

            self._setSetting('margin_'+edge, margin[edge]) 

        self._setSetting('paper_id', self.GetPaperId()) 

        self._setSetting('orientation', self.GetOrientation()) 

 

    def _getSetting(self, option): 

        return self.settings.getint('printer', option) 

 

    def _setSetting(self, option, value): 

        self.settings.set('printer', option, str(value)) 

 

 

class HTMLPrintout(wx.html.HtmlPrintout): 

    def __init__(self, htmlText, settings): 

        super(HTMLPrintout, self).__init__() 

        self.SetHtmlText(htmlText) 

        self.SetFooter(_('Page') + ' @PAGENUM@/@PAGESCNT@', wx.html.PAGE_ALL) 

        self.SetFonts('Arial', 'Courier') 

        printerSettings = PrinterSettings(settings) 

        top, left = printerSettings.pageSetupData.GetMarginTopLeft() 

        bottom, right = printerSettings.pageSetupData.GetMarginBottomRight() 

        self.SetMargins(top, bottom, left, right) 

 

 

class DCPrintout(wx.Printout): 

    def __init__(self, widget): 

        self.widget = widget 

        super(DCPrintout, self).__init__() 

 

    def OnPrintPage(self, page): # pylint: disable-msg=W0613 

        self.widget.Draw(self.GetDC()) 

 

    def GetPageInfo(self): # pylint: disable-msg=W0221 

        return (1, 1, 1, 1) 

 

 

def Printout(viewer, settings, printSelectionOnly=False, 

             twoPrintouts=False): 

    widget = viewer.getWidget() 

    if hasattr(widget, 'Draw'): 

        def _printout(): 

            return DCPrintout(widget) 

    else: 

        htmlText = persistence.viewer2html(viewer, settings, 

                                           selectionOnly=printSelectionOnly)[0] 

        def _printout(): 

            return HTMLPrintout(htmlText, settings) 

    result = _printout() 

    if twoPrintouts: 

        result = (result, _printout()) 

    return result