[f32d144] | 1 | """ |
---|
| 2 | Dialog report panel to show and summarize the results of |
---|
[d06ae30] | 3 | the fitting calculation. |
---|
[f32d144] | 4 | """ |
---|
[2296316] | 5 | ################################################################################ |
---|
| 6 | #This software was developed by the University of Tennessee as part of the |
---|
| 7 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[f32d144] | 8 | #project funded by the US National Science Foundation. |
---|
[2296316] | 9 | # |
---|
| 10 | #See the license text in license.txt |
---|
| 11 | # |
---|
| 12 | #copyright 2009, University of Tennessee |
---|
| 13 | ################################################################################ |
---|
| 14 | |
---|
| 15 | import wx |
---|
| 16 | import sys |
---|
| 17 | import os |
---|
| 18 | import wx.html as html |
---|
[4ec242e] | 19 | import logging |
---|
[2296316] | 20 | |
---|
[c2b0079] | 21 | _STATICBOX_WIDTH = 480 |
---|
| 22 | PANEL_WIDTH = 530 |
---|
| 23 | PANEL_HEIGHT = 700 |
---|
| 24 | FONT_VARIANT = 1 |
---|
| 25 | ISMAC = False |
---|
[d8e3f7c] | 26 | ISPDF = False |
---|
[c2b0079] | 27 | if sys.platform == "win32": |
---|
[2296316] | 28 | _STATICBOX_WIDTH = 450 |
---|
[f32d144] | 29 | PANEL_WIDTH = 500 |
---|
[2296316] | 30 | PANEL_HEIGHT = 700 |
---|
| 31 | FONT_VARIANT = 0 |
---|
[dec793e] | 32 | ISMAC = False |
---|
[d8e3f7c] | 33 | ISPDF = True |
---|
[c2b0079] | 34 | elif sys.platform == "darwin": |
---|
[dec793e] | 35 | ISMAC = True |
---|
[d8e3f7c] | 36 | ISPDF = True |
---|
[2296316] | 37 | |
---|
| 38 | |
---|
| 39 | class ReportDialog(wx.Dialog): |
---|
| 40 | """ |
---|
[f32d144] | 41 | The report dialog box. |
---|
[2296316] | 42 | """ |
---|
| 43 | |
---|
[f32d144] | 44 | def __init__(self, list, *args, **kwds): |
---|
[2296316] | 45 | """ |
---|
| 46 | Initialization. The parameters added to Dialog are: |
---|
| 47 | |
---|
| 48 | :param list: report_list (list of html_str, text_str, image) |
---|
| 49 | from invariant_state |
---|
| 50 | """ |
---|
| 51 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
| 52 | wx.Dialog.__init__(self, *args, **kwds) |
---|
| 53 | kwds["image"] = 'Dynamic Image' |
---|
| 54 | # title |
---|
| 55 | self.SetTitle("Report: Fitting") |
---|
| 56 | # size |
---|
| 57 | self.SetSize((720, 650)) |
---|
[f32d144] | 58 | # font size |
---|
[2296316] | 59 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[dec793e] | 60 | # check if tit is MAC |
---|
[213b445] | 61 | self.is_pdf = ISPDF |
---|
[2296316] | 62 | # report string |
---|
| 63 | self.report_list = list |
---|
| 64 | # number of images of plot |
---|
| 65 | self.nimages = len(list[2]) |
---|
| 66 | |
---|
| 67 | if list[2] != None: |
---|
| 68 | # put image path in the report string |
---|
| 69 | if len(list[2]) == 1: |
---|
| 70 | self.report_html = self.report_list[0] % \ |
---|
| 71 | "memory:img_fit0.png" |
---|
| 72 | elif len(list[2]) == 2: |
---|
| 73 | self.report_html = self.report_list[0] % \ |
---|
[f32d144] | 74 | ("memory:img_fit0.png", |
---|
[2296316] | 75 | "memory:img_fit1.png") |
---|
| 76 | # allows up to three images |
---|
| 77 | else: |
---|
| 78 | self.report_html = self.report_list[0] % \ |
---|
[f32d144] | 79 | ("memory:img_fit0.png", |
---|
[2296316] | 80 | "memory:img_fit1.png", |
---|
| 81 | "memory:img_fit2.png") |
---|
| 82 | else: |
---|
| 83 | self.report_html = self.report_list[0] |
---|
| 84 | # layout |
---|
| 85 | self._setup_layout() |
---|
| 86 | |
---|
| 87 | def _setup_layout(self): |
---|
| 88 | """ |
---|
| 89 | Set up layout |
---|
| 90 | """ |
---|
| 91 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 92 | |
---|
| 93 | # buttons |
---|
| 94 | id = wx.ID_OK |
---|
| 95 | button_close = wx.Button(self, id, "Close") |
---|
[f32d144] | 96 | button_close.SetToolTipString("Close this report window.") |
---|
[2296316] | 97 | #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 98 | hbox.Add(button_close) |
---|
| 99 | button_close.SetFocus() |
---|
| 100 | |
---|
| 101 | id = wx.NewId() |
---|
| 102 | button_preview = wx.Button(self, id, "Preview") |
---|
| 103 | button_preview.SetToolTipString("Print preview this report.") |
---|
| 104 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, |
---|
| 105 | id=button_preview.GetId()) |
---|
| 106 | hbox.Add(button_preview) |
---|
| 107 | |
---|
| 108 | id = wx.NewId() |
---|
| 109 | button_print = wx.Button(self, id, "Print") |
---|
| 110 | button_print.SetToolTipString("Print this report.") |
---|
| 111 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
[f32d144] | 112 | id=button_print.GetId()) |
---|
[2296316] | 113 | hbox.Add(button_print) |
---|
| 114 | |
---|
| 115 | id = wx.NewId() |
---|
[f32d144] | 116 | button_save = wx.Button(self, id, "Save") |
---|
[2296316] | 117 | button_save.SetToolTipString("Save this report.") |
---|
[f32d144] | 118 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
| 119 | hbox.Add(button_save) |
---|
[2296316] | 120 | |
---|
| 121 | # panel for report page |
---|
| 122 | #panel = wx.Panel(self, -1) |
---|
| 123 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 124 | # html window |
---|
[f32d144] | 125 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
[2296316] | 126 | # set the html page with the report string |
---|
| 127 | self.hwindow.SetPage(self.report_html) |
---|
| 128 | |
---|
| 129 | # add panels to boxsizers |
---|
| 130 | vbox.Add(hbox) |
---|
| 131 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
| 132 | |
---|
| 133 | self.SetSizer(vbox) |
---|
| 134 | self.Centre() |
---|
| 135 | self.Show(True) |
---|
| 136 | |
---|
| 137 | def onSave(self, event=None): |
---|
| 138 | """ |
---|
| 139 | Save |
---|
| 140 | """ |
---|
[dec793e] | 141 | # pdf supporting only on MAC, not on exe |
---|
[213b445] | 142 | if self.is_pdf: |
---|
[dec793e] | 143 | wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
[f32d144] | 144 | ind_cor = 0 |
---|
[dec793e] | 145 | else: |
---|
| 146 | wild_card = '' |
---|
[f32d144] | 147 | ind_cor = 1 |
---|
[dec793e] | 148 | wild_card += 'HTML files (*.html)|*.html|' |
---|
| 149 | wild_card += 'Text files (*.txt)|*.txt' |
---|
| 150 | |
---|
[2296316] | 151 | #todo: complete saving fig file and as a txt file |
---|
| 152 | dlg = wx.FileDialog(self, "Choose a file", |
---|
[dec793e] | 153 | wildcard=wild_card, |
---|
[2296316] | 154 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
[f32d144] | 155 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
[2296316] | 156 | |
---|
| 157 | if dlg.ShowModal() != wx.ID_OK: |
---|
| 158 | dlg.Destroy() |
---|
| 159 | return |
---|
| 160 | |
---|
| 161 | fName = dlg.GetPath() |
---|
[f32d144] | 162 | ext_num = dlg.GetFilterIndex() |
---|
[2296316] | 163 | |
---|
[f32d144] | 164 | #set file extensions |
---|
[2296316] | 165 | img_ext = [] |
---|
[dec793e] | 166 | pic_fname = [] |
---|
[f32d144] | 167 | #PDF |
---|
[dec793e] | 168 | if ext_num == (0 + 2 * ind_cor): |
---|
| 169 | # TODO: Sort this case out |
---|
| 170 | ext = '.pdf' |
---|
| 171 | |
---|
| 172 | fName = os.path.splitext(fName)[0] + ext |
---|
| 173 | dlg.Destroy() |
---|
| 174 | #pic (png) file path/name |
---|
| 175 | for num in range(self.nimages): |
---|
| 176 | im_ext = '_img%s.png' % num |
---|
| 177 | #img_ext.append(im_ext) |
---|
| 178 | pic_name = os.path.splitext(fName)[0] + im_ext |
---|
| 179 | pic_fname.append(pic_name) |
---|
| 180 | # save the image for use with pdf writer |
---|
| 181 | self.report_list[2][num].savefig(pic_name) |
---|
| 182 | |
---|
| 183 | #put the image path in html string |
---|
| 184 | report_frame = self.report_list[0] |
---|
| 185 | #put image name strings into the html file |
---|
| 186 | #Note:The str for pic_fname shouldn't be removed. |
---|
| 187 | if self.nimages == 1: |
---|
[091c702] | 188 | html = report_frame % str(pic_fname[0]) |
---|
[dec793e] | 189 | elif self.nimages == 2: |
---|
[091c702] | 190 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) |
---|
[dec793e] | 191 | elif self.nimages == 3: |
---|
[091c702] | 192 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), |
---|
| 193 | str(pic_fname[2])) |
---|
[dec793e] | 194 | |
---|
| 195 | # make/open file in case of absence |
---|
| 196 | f = open(fName, 'w') |
---|
| 197 | f.close() |
---|
| 198 | # write pdf as a pdf file |
---|
| 199 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
| 200 | |
---|
| 201 | #open pdf |
---|
| 202 | if pdf: |
---|
[d8e3f7c] | 203 | try: |
---|
| 204 | #Windows |
---|
| 205 | os.startfile(str(fName)) |
---|
| 206 | except: |
---|
| 207 | try: |
---|
| 208 | #Mac |
---|
| 209 | os.system("open %s"% fName) |
---|
| 210 | except: |
---|
| 211 | #DO not open |
---|
| 212 | pass |
---|
[dec793e] | 213 | #delete image file |
---|
| 214 | for num in range(self.nimages): |
---|
| 215 | os.remove(pic_fname[num]) |
---|
| 216 | return |
---|
| 217 | #HTML + png(graph) |
---|
| 218 | elif ext_num == (1 - ind_cor): |
---|
[2296316] | 219 | ext = '.html' |
---|
| 220 | for num in range(self.nimages): |
---|
| 221 | img_ext.append('_img4html%s.png' % num) |
---|
| 222 | report_frame = self.report_list[0] |
---|
[dec793e] | 223 | #TEXT + pdf(graph) |
---|
| 224 | elif ext_num == (2 - ind_cor): |
---|
[f32d144] | 225 | ext = '.txt' |
---|
[2296316] | 226 | # changing the image extension actually changes the image |
---|
| 227 | # format on saving |
---|
| 228 | for num in range(self.nimages): |
---|
| 229 | img_ext.append('_img4txt%s.pdf' % num) |
---|
| 230 | report = self.report_list[1] |
---|
| 231 | else: |
---|
| 232 | return |
---|
| 233 | |
---|
[f32d144] | 234 | #file name |
---|
[2296316] | 235 | fName = os.path.splitext(fName)[0] + ext |
---|
| 236 | dlg.Destroy() |
---|
[dec793e] | 237 | |
---|
[2296316] | 238 | #pic (png) file path/name |
---|
| 239 | for num in range(self.nimages): |
---|
| 240 | pic_name = os.path.splitext(fName)[0] + img_ext[num] |
---|
| 241 | pic_fname.append(pic_name) |
---|
| 242 | #put the image path in html string |
---|
[dec793e] | 243 | if ext_num == (1 - ind_cor): |
---|
[2296316] | 244 | if self.nimages == 1: |
---|
[3dac5e2] | 245 | report = report_frame % os.path.basename(pic_fname[0]) |
---|
[2296316] | 246 | elif self.nimages == 2: |
---|
[f32d144] | 247 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
[3dac5e2] | 248 | os.path.basename(pic_fname[1])) |
---|
[2296316] | 249 | elif self.nimages == 3: |
---|
[f32d144] | 250 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
[3dac5e2] | 251 | os.path.basename(pic_fname[1]), |
---|
| 252 | os.path.basename(pic_fname[2])) |
---|
[2296316] | 253 | f = open(fName, 'w') |
---|
| 254 | f.write(report) |
---|
| 255 | f.close() |
---|
[f0239a3] | 256 | self.Update() |
---|
[2296316] | 257 | #save png file using pic_fname |
---|
| 258 | for num in range(self.nimages): |
---|
| 259 | self.report_list[2][num].savefig(pic_fname[num]) |
---|
| 260 | |
---|
| 261 | def onPreview(self, event=None): |
---|
| 262 | """ |
---|
| 263 | Preview |
---|
| 264 | |
---|
| 265 | : event: Preview button event |
---|
| 266 | """ |
---|
| 267 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 268 | previewh.PreviewText(self.report_html) |
---|
| 269 | if event is not None: |
---|
| 270 | event.Skip() |
---|
[f0239a3] | 271 | self.Update() |
---|
[2296316] | 272 | |
---|
| 273 | def onPrint(self, event=None): |
---|
| 274 | """ |
---|
| 275 | Print |
---|
| 276 | |
---|
| 277 | : event: Print button event |
---|
| 278 | """ |
---|
| 279 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 280 | printh.PrintText(self.report_html) |
---|
| 281 | if event is not None: |
---|
| 282 | event.Skip() |
---|
[f0239a3] | 283 | self.Update() |
---|
| 284 | |
---|
[f32d144] | 285 | def OnClose(self, event=None): |
---|
[2296316] | 286 | """ |
---|
| 287 | Close the Dialog |
---|
| 288 | |
---|
| 289 | : event: Close button event |
---|
| 290 | """ |
---|
| 291 | self.Close() |
---|
| 292 | # Reset memory |
---|
| 293 | #wx.MemoryFSHandler() |
---|
| 294 | if event is not None: |
---|
| 295 | event.Skip() |
---|
| 296 | |
---|
| 297 | def HTML2PDF(self, data, filename): |
---|
| 298 | """ |
---|
[f32d144] | 299 | Create a PDF file from html source string. |
---|
[4ec242e] | 300 | Returns True is the file creation was successful. |
---|
[2296316] | 301 | |
---|
| 302 | : data: html string |
---|
| 303 | : filename: name of file to be saved |
---|
| 304 | """ |
---|
[4ec242e] | 305 | try: |
---|
| 306 | from xhtml2pdf import pisa |
---|
| 307 | # open output file for writing (truncated binary) |
---|
| 308 | resultFile = open(filename, "w+b") |
---|
| 309 | # convert HTML to PDF |
---|
| 310 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
| 311 | # close output file |
---|
| 312 | resultFile.close() |
---|
| 313 | self.Update() |
---|
| 314 | return pisaStatus.err |
---|
| 315 | except: |
---|
| 316 | logging.error("Error creating pdf: %s" % sys.exc_value) |
---|
| 317 | return False |
---|