1 | """ |
---|
2 | Dialog report panel to show and summarize the results of |
---|
3 | the invariant calculation. |
---|
4 | """ |
---|
5 | ################################################################################ |
---|
6 | #This software was developed by the University of Tennessee as part of the |
---|
7 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
8 | #project funded by the US National Science Foundation. |
---|
9 | # |
---|
10 | #See the license text in license.txt |
---|
11 | # |
---|
12 | #copyright 2009, University of Tennessee |
---|
13 | ################################################################################ |
---|
14 | |
---|
15 | import wx |
---|
16 | import sys |
---|
17 | import os |
---|
18 | import wx.html as html |
---|
19 | |
---|
20 | _STATICBOX_WIDTH = 480 |
---|
21 | PANEL_WIDTH = 530 |
---|
22 | PANEL_HEIGHT = 700 |
---|
23 | FONT_VARIANT = 1 |
---|
24 | ISMAC = False |
---|
25 | ISPDF = False |
---|
26 | if sys.platform == "win32": |
---|
27 | _STATICBOX_WIDTH = 450 |
---|
28 | PANEL_WIDTH = 500 |
---|
29 | PANEL_HEIGHT = 700 |
---|
30 | FONT_VARIANT = 0 |
---|
31 | ISMAC = False |
---|
32 | ISPDF = True |
---|
33 | elif sys.platform == "darwin": |
---|
34 | ISMAC = True |
---|
35 | ISPDF = True |
---|
36 | |
---|
37 | |
---|
38 | class ReportDialog(wx.Dialog): |
---|
39 | """ |
---|
40 | The report dialog box. |
---|
41 | """ |
---|
42 | |
---|
43 | def __init__(self, list, *args, **kwds): |
---|
44 | """ |
---|
45 | Initialization. The parameters added to Dialog are: |
---|
46 | |
---|
47 | :param list: report_list (list of html_str, text_str, image) |
---|
48 | from invariant_state |
---|
49 | """ |
---|
50 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
51 | wx.Dialog.__init__(self, *args, **kwds) |
---|
52 | kwds["image"] = 'Dynamic Image' |
---|
53 | # title |
---|
54 | self.SetTitle("Report: Fitting") |
---|
55 | # size |
---|
56 | self.SetSize((720, 650)) |
---|
57 | # font size |
---|
58 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
59 | # check if tit is MAC |
---|
60 | self.is_pdf = ISPDF |
---|
61 | # report string |
---|
62 | self.report_list = list |
---|
63 | # number of images of plot |
---|
64 | self.nimages = len(list[2]) |
---|
65 | |
---|
66 | if list[2] != None: |
---|
67 | # put image path in the report string |
---|
68 | if len(list[2]) == 1: |
---|
69 | self.report_html = self.report_list[0] % \ |
---|
70 | "memory:img_fit0.png" |
---|
71 | elif len(list[2]) == 2: |
---|
72 | self.report_html = self.report_list[0] % \ |
---|
73 | ("memory:img_fit0.png", |
---|
74 | "memory:img_fit1.png") |
---|
75 | # allows up to three images |
---|
76 | else: |
---|
77 | self.report_html = self.report_list[0] % \ |
---|
78 | ("memory:img_fit0.png", |
---|
79 | "memory:img_fit1.png", |
---|
80 | "memory:img_fit2.png") |
---|
81 | else: |
---|
82 | self.report_html = self.report_list[0] |
---|
83 | # layout |
---|
84 | self._setup_layout() |
---|
85 | |
---|
86 | def _setup_layout(self): |
---|
87 | """ |
---|
88 | Set up layout |
---|
89 | """ |
---|
90 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
91 | |
---|
92 | # buttons |
---|
93 | id = wx.ID_OK |
---|
94 | button_close = wx.Button(self, id, "Close") |
---|
95 | button_close.SetToolTipString("Close this report window.") |
---|
96 | #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
97 | hbox.Add(button_close) |
---|
98 | button_close.SetFocus() |
---|
99 | |
---|
100 | id = wx.NewId() |
---|
101 | button_preview = wx.Button(self, id, "Preview") |
---|
102 | button_preview.SetToolTipString("Print preview this report.") |
---|
103 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, |
---|
104 | id=button_preview.GetId()) |
---|
105 | hbox.Add(button_preview) |
---|
106 | |
---|
107 | id = wx.NewId() |
---|
108 | button_print = wx.Button(self, id, "Print") |
---|
109 | button_print.SetToolTipString("Print this report.") |
---|
110 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
111 | id=button_print.GetId()) |
---|
112 | hbox.Add(button_print) |
---|
113 | |
---|
114 | id = wx.NewId() |
---|
115 | button_save = wx.Button(self, id, "Save") |
---|
116 | button_save.SetToolTipString("Save this report.") |
---|
117 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
118 | hbox.Add(button_save) |
---|
119 | |
---|
120 | # panel for report page |
---|
121 | #panel = wx.Panel(self, -1) |
---|
122 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
123 | # html window |
---|
124 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
125 | # set the html page with the report string |
---|
126 | self.hwindow.SetPage(self.report_html) |
---|
127 | |
---|
128 | # add panels to boxsizers |
---|
129 | vbox.Add(hbox) |
---|
130 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
131 | |
---|
132 | self.SetSizer(vbox) |
---|
133 | self.Centre() |
---|
134 | self.Show(True) |
---|
135 | |
---|
136 | def onSave(self, event=None): |
---|
137 | """ |
---|
138 | Save |
---|
139 | """ |
---|
140 | # pdf supporting only on MAC, not on exe |
---|
141 | if self.is_pdf: |
---|
142 | wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
143 | ind_cor = 0 |
---|
144 | else: |
---|
145 | wild_card = '' |
---|
146 | ind_cor = 1 |
---|
147 | wild_card += 'HTML files (*.html)|*.html|' |
---|
148 | wild_card += 'Text files (*.txt)|*.txt' |
---|
149 | |
---|
150 | #todo: complete saving fig file and as a txt file |
---|
151 | dlg = wx.FileDialog(self, "Choose a file", |
---|
152 | wildcard=wild_card, |
---|
153 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
154 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
155 | |
---|
156 | if dlg.ShowModal() != wx.ID_OK: |
---|
157 | dlg.Destroy() |
---|
158 | return |
---|
159 | |
---|
160 | fName = dlg.GetPath() |
---|
161 | ext_num = dlg.GetFilterIndex() |
---|
162 | |
---|
163 | #set file extensions |
---|
164 | img_ext = [] |
---|
165 | pic_fname = [] |
---|
166 | #PDF |
---|
167 | if ext_num == (0 + 2 * ind_cor): |
---|
168 | # TODO: Sort this case out |
---|
169 | ext = '.pdf' |
---|
170 | |
---|
171 | fName = os.path.splitext(fName)[0] + ext |
---|
172 | dlg.Destroy() |
---|
173 | #pic (png) file path/name |
---|
174 | for num in range(self.nimages): |
---|
175 | im_ext = '_img%s.png' % num |
---|
176 | #img_ext.append(im_ext) |
---|
177 | pic_name = os.path.splitext(fName)[0] + im_ext |
---|
178 | pic_fname.append(pic_name) |
---|
179 | # save the image for use with pdf writer |
---|
180 | self.report_list[2][num].savefig(pic_name) |
---|
181 | |
---|
182 | #put the image path in html string |
---|
183 | report_frame = self.report_list[0] |
---|
184 | #put image name strings into the html file |
---|
185 | #Note:The str for pic_fname shouldn't be removed. |
---|
186 | if self.nimages == 1: |
---|
187 | html = report_frame % str(pic_fname[0]) |
---|
188 | elif self.nimages == 2: |
---|
189 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) |
---|
190 | elif self.nimages == 3: |
---|
191 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), |
---|
192 | str(pic_fname[2])) |
---|
193 | |
---|
194 | # make/open file in case of absence |
---|
195 | f = open(fName, 'w') |
---|
196 | f.close() |
---|
197 | # write pdf as a pdf file |
---|
198 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
199 | |
---|
200 | #open pdf |
---|
201 | if pdf: |
---|
202 | try: |
---|
203 | #Windows |
---|
204 | os.startfile(str(fName)) |
---|
205 | except: |
---|
206 | try: |
---|
207 | #Mac |
---|
208 | os.system("open %s"% fName) |
---|
209 | except: |
---|
210 | #DO not open |
---|
211 | pass |
---|
212 | #delete image file |
---|
213 | for num in range(self.nimages): |
---|
214 | os.remove(pic_fname[num]) |
---|
215 | return |
---|
216 | #HTML + png(graph) |
---|
217 | elif ext_num == (1 - ind_cor): |
---|
218 | ext = '.html' |
---|
219 | for num in range(self.nimages): |
---|
220 | img_ext.append('_img4html%s.png' % num) |
---|
221 | report_frame = self.report_list[0] |
---|
222 | #TEXT + pdf(graph) |
---|
223 | elif ext_num == (2 - ind_cor): |
---|
224 | ext = '.txt' |
---|
225 | # changing the image extension actually changes the image |
---|
226 | # format on saving |
---|
227 | for num in range(self.nimages): |
---|
228 | img_ext.append('_img4txt%s.pdf' % num) |
---|
229 | report = self.report_list[1] |
---|
230 | else: |
---|
231 | return |
---|
232 | |
---|
233 | #file name |
---|
234 | fName = os.path.splitext(fName)[0] + ext |
---|
235 | dlg.Destroy() |
---|
236 | |
---|
237 | #pic (png) file path/name |
---|
238 | for num in range(self.nimages): |
---|
239 | pic_name = os.path.splitext(fName)[0] + img_ext[num] |
---|
240 | pic_fname.append(pic_name) |
---|
241 | #put the image path in html string |
---|
242 | if ext_num == (1 - ind_cor): |
---|
243 | if self.nimages == 1: |
---|
244 | report = report_frame % os.path.basename(pic_fname[0]) |
---|
245 | elif self.nimages == 2: |
---|
246 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
247 | os.path.basename(pic_fname[1])) |
---|
248 | elif self.nimages == 3: |
---|
249 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
250 | os.path.basename(pic_fname[1]), |
---|
251 | os.path.basename(pic_fname[2])) |
---|
252 | f = open(fName, 'w') |
---|
253 | f.write(report) |
---|
254 | f.close() |
---|
255 | self.Update() |
---|
256 | #save png file using pic_fname |
---|
257 | for num in range(self.nimages): |
---|
258 | self.report_list[2][num].savefig(pic_fname[num]) |
---|
259 | |
---|
260 | def onPreview(self, event=None): |
---|
261 | """ |
---|
262 | Preview |
---|
263 | |
---|
264 | : event: Preview button event |
---|
265 | """ |
---|
266 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
267 | previewh.PreviewText(self.report_html) |
---|
268 | if event is not None: |
---|
269 | event.Skip() |
---|
270 | self.Update() |
---|
271 | |
---|
272 | def onPrint(self, event=None): |
---|
273 | """ |
---|
274 | Print |
---|
275 | |
---|
276 | : event: Print button event |
---|
277 | """ |
---|
278 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
279 | printh.PrintText(self.report_html) |
---|
280 | if event is not None: |
---|
281 | event.Skip() |
---|
282 | self.Update() |
---|
283 | |
---|
284 | def OnClose(self, event=None): |
---|
285 | """ |
---|
286 | Close the Dialog |
---|
287 | |
---|
288 | : event: Close button event |
---|
289 | """ |
---|
290 | self.Close() |
---|
291 | # Reset memory |
---|
292 | #wx.MemoryFSHandler() |
---|
293 | if event is not None: |
---|
294 | event.Skip() |
---|
295 | |
---|
296 | def HTML2PDF(self, data, filename): |
---|
297 | """ |
---|
298 | Create a PDF file from html source string. |
---|
299 | |
---|
300 | : data: html string |
---|
301 | : filename: name of file to be saved |
---|
302 | """ |
---|
303 | import ho.pisa as pisa |
---|
304 | f = file(filename, "wb") |
---|
305 | # pisa requires some extra packages, see their web-site |
---|
306 | pdf = pisa.CreatePDF(data, f) |
---|
307 | |
---|
308 | # close the file here otherwise it will be open until quitting |
---|
309 | #the application. |
---|
310 | f.close() |
---|
311 | self.Update() |
---|
312 | return not pdf.err |
---|