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