1 | """ |
---|
2 | Base class for reports. Child classes will need to implement |
---|
3 | the onSave() method. |
---|
4 | """ |
---|
5 | import wx |
---|
6 | import logging |
---|
7 | import sys |
---|
8 | import wx.html as html |
---|
9 | |
---|
10 | logger = logging.getLogger(__name__) |
---|
11 | |
---|
12 | ISPDF = False |
---|
13 | if sys.platform == "win32": |
---|
14 | _STATICBOX_WIDTH = 450 |
---|
15 | PANEL_WIDTH = 500 |
---|
16 | PANEL_HEIGHT = 700 |
---|
17 | FONT_VARIANT = 0 |
---|
18 | ISPDF = True |
---|
19 | # For OSX and everything else |
---|
20 | else: |
---|
21 | _STATICBOX_WIDTH = 480 |
---|
22 | PANEL_WIDTH = 530 |
---|
23 | PANEL_HEIGHT = 700 |
---|
24 | FONT_VARIANT = 1 |
---|
25 | ISPDF = True |
---|
26 | |
---|
27 | class BaseReportDialog(wx.Dialog): |
---|
28 | |
---|
29 | def __init__(self, report_list, *args, **kwds): |
---|
30 | """ |
---|
31 | Initialization. The parameters added to Dialog are: |
---|
32 | |
---|
33 | :param report_list: list of html_str, text_str, image for report |
---|
34 | """ |
---|
35 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
36 | super(BaseReportDialog, self).__init__(*args, **kwds) |
---|
37 | kwds["image"] = 'Dynamic Image' |
---|
38 | |
---|
39 | # title |
---|
40 | self.SetTitle("Report") |
---|
41 | # size |
---|
42 | self.SetSize((720, 650)) |
---|
43 | # font size |
---|
44 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
45 | # check if tit is MAC |
---|
46 | self.is_pdf = ISPDF |
---|
47 | # report string |
---|
48 | self.report_list = report_list |
---|
49 | # wild card |
---|
50 | # pdf supporting only on MAC |
---|
51 | if self.is_pdf: |
---|
52 | self.wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
53 | self.index_offset = 0 |
---|
54 | else: |
---|
55 | self.wild_card = '' |
---|
56 | self.index_offset = 1 |
---|
57 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
58 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
59 | |
---|
60 | def _setup_layout(self): |
---|
61 | """ |
---|
62 | Set up layout |
---|
63 | """ |
---|
64 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
65 | |
---|
66 | # buttons |
---|
67 | button_close = wx.Button(self, wx.ID_OK, "Close") |
---|
68 | button_close.SetToolTipString("Close this report window.") |
---|
69 | hbox.Add(button_close) |
---|
70 | button_close.SetFocus() |
---|
71 | |
---|
72 | button_print = wx.Button(self, wx.NewId(), "Print") |
---|
73 | button_print.SetToolTipString("Print this report.") |
---|
74 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
75 | id=button_print.GetId()) |
---|
76 | hbox.Add(button_print) |
---|
77 | |
---|
78 | button_save = wx.Button(self, wx.NewId(), "Save") |
---|
79 | button_save.SetToolTipString("Save this report.") |
---|
80 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
81 | hbox.Add(button_save) |
---|
82 | |
---|
83 | # panel for report page |
---|
84 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
85 | # html window |
---|
86 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
87 | # set the html page with the report string |
---|
88 | self.hwindow.SetPage(self.report_html) |
---|
89 | |
---|
90 | # add panels to boxsizers |
---|
91 | vbox.Add(hbox) |
---|
92 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
93 | |
---|
94 | self.SetSizer(vbox) |
---|
95 | self.Centre() |
---|
96 | self.Show(True) |
---|
97 | |
---|
98 | def onPreview(self, event=None): |
---|
99 | """ |
---|
100 | Preview |
---|
101 | : event: Preview button event |
---|
102 | """ |
---|
103 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
104 | previewh.PreviewText(self.report_html) |
---|
105 | |
---|
106 | def onPrint(self, event=None): |
---|
107 | """ |
---|
108 | Print |
---|
109 | : event: Print button event |
---|
110 | """ |
---|
111 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
112 | printh.PrintText(self.report_html) |
---|
113 | |
---|
114 | def OnClose(self, event=None): |
---|
115 | """ |
---|
116 | Close the Dialog |
---|
117 | : event: Close button event |
---|
118 | """ |
---|
119 | self.Close() |
---|
120 | |
---|
121 | def HTML2PDF(self, data, filename): |
---|
122 | """ |
---|
123 | Create a PDF file from html source string. |
---|
124 | Returns True is the file creation was successful. |
---|
125 | : data: html string |
---|
126 | : filename: name of file to be saved |
---|
127 | """ |
---|
128 | try: |
---|
129 | from xhtml2pdf import pisa |
---|
130 | # open output file for writing (truncated binary) |
---|
131 | resultFile = open(filename, "w+b") |
---|
132 | # convert HTML to PDF |
---|
133 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
134 | # close output file |
---|
135 | resultFile.close() |
---|
136 | self.Update() |
---|
137 | return pisaStatus.err |
---|
138 | except: |
---|
139 | logger.error("Error creating pdf: %s" % sys.exc_value) |
---|
140 | return False |
---|
141 | |
---|