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