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