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 | 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 | |
---|
56 | # check if tit is MAC |
---|
57 | self.is_mac = ISMAC |
---|
58 | |
---|
59 | # report string |
---|
60 | self.report_list = list |
---|
61 | # put image path in the report string |
---|
62 | self.report_html = self.report_list[0] % "memory:img_inv.png" |
---|
63 | # layout |
---|
64 | self._setup_layout() |
---|
65 | # wild card |
---|
66 | # pdf supporting only on MAC |
---|
67 | if self.is_mac: |
---|
68 | self.wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
69 | else: |
---|
70 | self.wild_card = '' |
---|
71 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
72 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | def _setup_layout(self): |
---|
77 | """ |
---|
78 | Set up layout |
---|
79 | """ |
---|
80 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
81 | |
---|
82 | # buttons |
---|
83 | id = wx.ID_OK |
---|
84 | button_close = wx.Button(self, id, "Close") |
---|
85 | button_close.SetToolTipString("Close this report window.") |
---|
86 | #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
87 | hbox.Add(button_close) |
---|
88 | button_close.SetFocus() |
---|
89 | |
---|
90 | id = wx.NewId() |
---|
91 | button_preview = wx.Button(self, id, "Preview") |
---|
92 | button_preview.SetToolTipString("Print preview this report.") |
---|
93 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, |
---|
94 | id=button_preview.GetId()) |
---|
95 | hbox.Add(button_preview) |
---|
96 | |
---|
97 | id = wx.NewId() |
---|
98 | button_print = wx.Button(self, id, "Print") |
---|
99 | button_print.SetToolTipString("Print this report.") |
---|
100 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
101 | id=button_print.GetId()) |
---|
102 | hbox.Add(button_print) |
---|
103 | |
---|
104 | id = wx.NewId() |
---|
105 | button_save = wx.Button(self, id, "Save" ) |
---|
106 | button_save.SetToolTipString("Save this report.") |
---|
107 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) |
---|
108 | hbox.Add(button_save) |
---|
109 | |
---|
110 | # panel for report page |
---|
111 | #panel = wx.Panel(self, -1) |
---|
112 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
113 | # html window |
---|
114 | self.hwindow = html.HtmlWindow(self,style=wx.BORDER) |
---|
115 | # set the html page with the report string |
---|
116 | self.hwindow.SetPage(self.report_html) |
---|
117 | |
---|
118 | # add panels to boxsizers |
---|
119 | vbox.Add(hbox) |
---|
120 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
121 | |
---|
122 | self.SetSizer(vbox) |
---|
123 | self.Centre() |
---|
124 | self.Show(True) |
---|
125 | |
---|
126 | |
---|
127 | def onSave(self, event=None): |
---|
128 | """ |
---|
129 | Save |
---|
130 | """ |
---|
131 | #todo: complete saving fig file and as a txt file |
---|
132 | dlg = wx.FileDialog(self, "Choose a file", |
---|
133 | wildcard=self.wild_card, |
---|
134 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
135 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
136 | |
---|
137 | if dlg.ShowModal() != wx.ID_OK: |
---|
138 | dlg.Destroy() |
---|
139 | return |
---|
140 | |
---|
141 | fName = dlg.GetPath() |
---|
142 | ext_num = dlg.GetFilterIndex() |
---|
143 | # index correction |
---|
144 | if not self.is_mac: |
---|
145 | ind_cor = 1 |
---|
146 | else: |
---|
147 | ind_cor = 0 |
---|
148 | #set file extensions |
---|
149 | if ext_num == (0 + 2 * ind_cor): |
---|
150 | # TODO: Sort this case out |
---|
151 | ext = '.pdf' |
---|
152 | img_ext = '_img.png' |
---|
153 | fName = os.path.splitext(fName)[0] + ext |
---|
154 | dlg.Destroy() |
---|
155 | |
---|
156 | #pic (png) file path/name |
---|
157 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
158 | # save the image for use with pdf writer |
---|
159 | self.report_list[2].savefig(pic_fname) |
---|
160 | |
---|
161 | # put the image file path in the html data |
---|
162 | html = self.report_list[0] % str(os.path.basename(pic_fname)) |
---|
163 | |
---|
164 | # make/open file in case of absence |
---|
165 | f = open(fName, 'w') |
---|
166 | f.close() |
---|
167 | # write pdf as a pdf file |
---|
168 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
169 | |
---|
170 | #open pdf |
---|
171 | if pdf: |
---|
172 | os.startfile(str(fName)) |
---|
173 | #delete image file |
---|
174 | os.remove(pic_fname) |
---|
175 | return |
---|
176 | elif ext_num == (1 - ind_cor): |
---|
177 | ext = '.html' |
---|
178 | img_ext = '_img4html.png' |
---|
179 | report_frame = self.report_list[0] |
---|
180 | elif ext_num == (2 - ind_cor): |
---|
181 | ext = '.txt' |
---|
182 | # changing the image extension actually changes the image |
---|
183 | # format on saving |
---|
184 | img_ext = '_img4txt.pdf' |
---|
185 | report = self.report_list[1] |
---|
186 | else: |
---|
187 | return |
---|
188 | |
---|
189 | #file name |
---|
190 | fName = os.path.splitext(fName)[0] + ext |
---|
191 | dlg.Destroy() |
---|
192 | #pic (png) file path/name |
---|
193 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
194 | #put the image path in html string |
---|
195 | if ext_num == (1 - ind_cor): |
---|
196 | report = report_frame % os.path.basename(pic_fname) |
---|
197 | |
---|
198 | f = open(fName, 'w') |
---|
199 | f.write(report) |
---|
200 | f.close() |
---|
201 | #save png file using pic_fname |
---|
202 | self.report_list[2].savefig(pic_fname) |
---|
203 | |
---|
204 | |
---|
205 | def onPreview(self, event=None): |
---|
206 | """ |
---|
207 | Preview |
---|
208 | |
---|
209 | : event: Preview button event |
---|
210 | """ |
---|
211 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
212 | previewh.PreviewText(self.report_html) |
---|
213 | if event is not None: |
---|
214 | event.Skip() |
---|
215 | |
---|
216 | def onPrint(self, event=None): |
---|
217 | """ |
---|
218 | Print |
---|
219 | |
---|
220 | : event: Print button event |
---|
221 | """ |
---|
222 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
223 | printh.PrintText(self.report_html) |
---|
224 | if event is not None: |
---|
225 | event.Skip() |
---|
226 | |
---|
227 | def OnClose(self,event=None): |
---|
228 | """ |
---|
229 | Close the Dialog |
---|
230 | |
---|
231 | : event: Close button event |
---|
232 | """ |
---|
233 | self.Close() |
---|
234 | |
---|
235 | def HTML2PDF(self, data, filename): |
---|
236 | """ |
---|
237 | Create a PDF file from html source string. |
---|
238 | |
---|
239 | : data: html string |
---|
240 | : filename: name of file to be saved |
---|
241 | """ |
---|
242 | import ho.pisa as pisa |
---|
243 | f = file(filename, "wb") |
---|
244 | # pisa requires some extra packages, see their web-site |
---|
245 | pdf = pisa.CreatePDF(data, f) |
---|
246 | # close the file here otherwise it will be open until quitting |
---|
247 | #the application. |
---|
248 | f.close() |
---|
249 | |
---|
250 | return not pdf.err |
---|
251 | |
---|
252 | |
---|
253 | |
---|