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