1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2009, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | """ |
---|
13 | Dialog report panel to show and summarize the results of |
---|
14 | the invariant calculation. |
---|
15 | """ |
---|
16 | import wx |
---|
17 | import sys |
---|
18 | import os |
---|
19 | import wx.html as html |
---|
20 | |
---|
21 | if sys.platform.count("win32") > 0: |
---|
22 | _STATICBOX_WIDTH = 450 |
---|
23 | PANEL_WIDTH = 500 |
---|
24 | PANEL_HEIGHT = 700 |
---|
25 | FONT_VARIANT = 0 |
---|
26 | else: |
---|
27 | _STATICBOX_WIDTH = 480 |
---|
28 | PANEL_WIDTH = 530 |
---|
29 | PANEL_HEIGHT = 700 |
---|
30 | FONT_VARIANT = 1 |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | class ReportDialog(wx.Dialog): |
---|
35 | """ |
---|
36 | The report dialog box. |
---|
37 | """ |
---|
38 | |
---|
39 | def __init__(self, list, *args, **kwds): |
---|
40 | """ |
---|
41 | Initialization. The parameters added to Dialog are: |
---|
42 | |
---|
43 | :param list: report_list (list of html_str, text_str, image) |
---|
44 | from invariant_state |
---|
45 | """ |
---|
46 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
47 | wx.Dialog.__init__(self, *args, **kwds) |
---|
48 | kwds["image"] = 'Dynamic Image' |
---|
49 | # title |
---|
50 | self.SetTitle("Report: Invariant computaion") |
---|
51 | # size |
---|
52 | self.SetSize((720, 650)) |
---|
53 | # font size |
---|
54 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
55 | # report string |
---|
56 | self.report_list = list |
---|
57 | # put image path in the report string |
---|
58 | self.report_html = self.report_list[0] % "memory:img_inv.png" |
---|
59 | # layout |
---|
60 | self._setup_layout() |
---|
61 | |
---|
62 | def _setup_layout(self): |
---|
63 | """ |
---|
64 | Set up layout |
---|
65 | """ |
---|
66 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
67 | |
---|
68 | # buttons |
---|
69 | id = wx.ID_OK |
---|
70 | button_close = wx.Button(self, id, "Close") |
---|
71 | button_close.SetToolTipString("Close this report window.") |
---|
72 | #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
73 | hbox.Add(button_close) |
---|
74 | button_close.SetFocus() |
---|
75 | |
---|
76 | id = wx.NewId() |
---|
77 | button_preview = wx.Button(self, id, "Preview") |
---|
78 | button_preview.SetToolTipString("Print preview this report.") |
---|
79 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, |
---|
80 | id=button_preview.GetId()) |
---|
81 | hbox.Add(button_preview) |
---|
82 | |
---|
83 | id = wx.NewId() |
---|
84 | button_print = wx.Button(self, id, "Print") |
---|
85 | button_print.SetToolTipString("Print this report.") |
---|
86 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
87 | id=button_print.GetId()) |
---|
88 | hbox.Add(button_print) |
---|
89 | |
---|
90 | id = wx.NewId() |
---|
91 | button_save = wx.Button(self, id, "Save" ) |
---|
92 | button_save.SetToolTipString("Save this report.") |
---|
93 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) |
---|
94 | hbox.Add(button_save) |
---|
95 | |
---|
96 | # panel for report page |
---|
97 | #panel = wx.Panel(self, -1) |
---|
98 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
99 | # html window |
---|
100 | self.hwindow = html.HtmlWindow(self,style=wx.BORDER) |
---|
101 | # set the html page with the report string |
---|
102 | self.hwindow.SetPage(self.report_html) |
---|
103 | |
---|
104 | # add panels to boxsizers |
---|
105 | vbox.Add(hbox) |
---|
106 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
107 | |
---|
108 | self.SetSizer(vbox) |
---|
109 | self.Centre() |
---|
110 | self.Show(True) |
---|
111 | |
---|
112 | |
---|
113 | def onSave(self, event=None): |
---|
114 | """ |
---|
115 | Save |
---|
116 | """ |
---|
117 | #todo: complete saving fig file and as a txt file |
---|
118 | dlg = wx.FileDialog(self, "Choose a file", |
---|
119 | wildcard='HTML files (*.html)|*.html|'+ |
---|
120 | 'Text files (*.txt)|*.txt', |
---|
121 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
122 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
123 | |
---|
124 | if dlg.ShowModal() != wx.ID_OK: |
---|
125 | dlg.Destroy() |
---|
126 | return |
---|
127 | |
---|
128 | fName = dlg.GetPath() |
---|
129 | ext_num = dlg.GetFilterIndex() |
---|
130 | |
---|
131 | #set file extensions |
---|
132 | if ext_num == 0: |
---|
133 | ext = '.html' |
---|
134 | img_ext = '_img4html.png' |
---|
135 | report_frame = self.report_list[0] |
---|
136 | elif ext_num == 1: |
---|
137 | ext = '.txt' |
---|
138 | # changing the image extension actually changes the image |
---|
139 | # format on saving |
---|
140 | img_ext = '_img4txt.pdf' |
---|
141 | report = self.report_list[1] |
---|
142 | else: |
---|
143 | return |
---|
144 | |
---|
145 | #file name |
---|
146 | fName = os.path.splitext(fName)[0] + ext |
---|
147 | dlg.Destroy() |
---|
148 | #pic (png) file path/name |
---|
149 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
150 | #put the image path in html string |
---|
151 | if ext_num == 0: |
---|
152 | report = report_frame % pic_fname |
---|
153 | f = open(fName, 'w') |
---|
154 | f.write(report) |
---|
155 | f.close() |
---|
156 | #save png file using pic_fname |
---|
157 | self.report_list[2].savefig(pic_fname) |
---|
158 | |
---|
159 | |
---|
160 | def onPreview(self, event=None): |
---|
161 | """ |
---|
162 | Preview |
---|
163 | |
---|
164 | : event: Preview button event |
---|
165 | """ |
---|
166 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
167 | previewh.PreviewText(self.report_html) |
---|
168 | if event is not None: |
---|
169 | event.Skip() |
---|
170 | |
---|
171 | def onPrint(self, event=None): |
---|
172 | """ |
---|
173 | Print |
---|
174 | |
---|
175 | : event: Print button event |
---|
176 | """ |
---|
177 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
178 | printh.PrintText(self.report_html) |
---|
179 | if event is not None: |
---|
180 | event.Skip() |
---|
181 | |
---|
182 | def OnClose(self,event=None): |
---|
183 | """ |
---|
184 | Close the Dialog |
---|
185 | |
---|
186 | : event: Close button event |
---|
187 | """ |
---|
188 | self.Close() |
---|
189 | |
---|
190 | def HTML2PDF(self, data, filename): |
---|
191 | """ |
---|
192 | Create a PDF file from html source string. |
---|
193 | |
---|
194 | : data: html string |
---|
195 | : filename: name of file to be saved |
---|
196 | """ |
---|
197 | import ho.pisa as pisa |
---|
198 | f = file(filename, "wb") |
---|
199 | # pisa requires some extra packages, see their web-site |
---|
200 | pdf = pisa.CreatePDF(data, f) |
---|
201 | # close the file here otherwise it will be open until quitting |
---|
202 | #the application. |
---|
203 | f.close() |
---|
204 | |
---|
205 | return not pdf.err |
---|
206 | |
---|
207 | |
---|
208 | |
---|