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(panel,style=wx.BORDER,size=(700,500)) |
---|
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, 30) |
---|
106 | vbox.Add(panel, 500, wx.EXPAND|wx.ALL) |
---|
107 | |
---|
108 | self.SetSizerAndFit(vbox) |
---|
109 | self.Centre() |
---|
110 | self.Show(True) |
---|
111 | |
---|
112 | def onSave(self, event=None): |
---|
113 | """ |
---|
114 | Save |
---|
115 | """ |
---|
116 | #todo: complete saving fig file and as a txt file |
---|
117 | dlg = wx.FileDialog(self, "Choose a file", |
---|
118 | wildcard='HTML files (*.html)|*.html|'+ |
---|
119 | 'Text files (*.txt)|*.txt', |
---|
120 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
121 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
122 | |
---|
123 | if dlg.ShowModal() != wx.ID_OK: |
---|
124 | dlg.Destroy() |
---|
125 | return |
---|
126 | |
---|
127 | fName = dlg.GetPath() |
---|
128 | ext_num = dlg.GetFilterIndex() |
---|
129 | |
---|
130 | #set file extensions |
---|
131 | if ext_num == 0: |
---|
132 | ext = '.html' |
---|
133 | img_ext = '_img4html.png' |
---|
134 | report_frame = self.report_list[0] |
---|
135 | elif ext_num == 1: |
---|
136 | ext = '.txt' |
---|
137 | # changing the image extension actually changes the image |
---|
138 | # format on saving |
---|
139 | img_ext = '_img4txt.pdf' |
---|
140 | report = self.report_list[1] |
---|
141 | else: |
---|
142 | return |
---|
143 | |
---|
144 | #file name |
---|
145 | fName = os.path.splitext(fName)[0] + ext |
---|
146 | dlg.Destroy() |
---|
147 | #pic (png) file path/name |
---|
148 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
149 | #put the image path in html string |
---|
150 | if ext_num == 0: |
---|
151 | report = report_frame % pic_fname |
---|
152 | f = open(fName, 'w') |
---|
153 | f.write(report) |
---|
154 | f.close() |
---|
155 | #save png file using pic_fname |
---|
156 | self.report_list[2].savefig(pic_fname) |
---|
157 | |
---|
158 | |
---|
159 | def onPreview(self, event=None): |
---|
160 | """ |
---|
161 | Preview |
---|
162 | |
---|
163 | : event: Preview button event |
---|
164 | """ |
---|
165 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
166 | previewh.PreviewText(self.report_html) |
---|
167 | if event is not None: |
---|
168 | event.Skip() |
---|
169 | |
---|
170 | def onPrint(self, event=None): |
---|
171 | """ |
---|
172 | Print |
---|
173 | |
---|
174 | : event: Print button event |
---|
175 | """ |
---|
176 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
177 | printh.PrintText(self.report_html) |
---|
178 | if event is not None: |
---|
179 | event.Skip() |
---|
180 | |
---|
181 | def OnClose(self,event=None): |
---|
182 | """ |
---|
183 | Close the Dialog |
---|
184 | |
---|
185 | : event: Close button event |
---|
186 | """ |
---|
187 | self.Close() |
---|
188 | |
---|
189 | def HTML2PDF(self, data, filename): |
---|
190 | """ |
---|
191 | Create a PDF file from html source string. |
---|
192 | |
---|
193 | : data: html string |
---|
194 | : filename: name of file to be saved |
---|
195 | """ |
---|
196 | import ho.pisa as pisa |
---|
197 | f = file(filename, "wb") |
---|
198 | # pisa requires some extra packages, see their web-site |
---|
199 | pdf = pisa.CreatePDF(data, f) |
---|
200 | # close the file here otherwise it will be open until quitting |
---|
201 | #the application. |
---|
202 | f.close() |
---|
203 | |
---|
204 | return not pdf.err |
---|
205 | |
---|
206 | |
---|
207 | |
---|