[cb463b4] | 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 | """ |
---|
[4a2b054] | 13 | Dialog report panel to show and summarize the results of |
---|
| 14 | the invariant calculation. |
---|
[cb463b4] | 15 | """ |
---|
| 16 | import wx |
---|
[4a2b054] | 17 | import sys |
---|
| 18 | import os |
---|
[cb463b4] | 19 | import wx.html as html |
---|
[4ec242e] | 20 | import logging |
---|
[d8e3f7c] | 21 | ISPDF = False |
---|
| 22 | if sys.platform == "win32": |
---|
[cb463b4] | 23 | _STATICBOX_WIDTH = 450 |
---|
| 24 | PANEL_WIDTH = 500 |
---|
| 25 | PANEL_HEIGHT = 700 |
---|
| 26 | FONT_VARIANT = 0 |
---|
[dec793e] | 27 | ISMAC = False |
---|
[d8e3f7c] | 28 | ISPDF = True |
---|
| 29 | elif sys.platform == "darwin": |
---|
[cb463b4] | 30 | _STATICBOX_WIDTH = 480 |
---|
| 31 | PANEL_WIDTH = 530 |
---|
| 32 | PANEL_HEIGHT = 700 |
---|
| 33 | FONT_VARIANT = 1 |
---|
[dec793e] | 34 | ISMAC = True |
---|
[d8e3f7c] | 35 | ISPDF = True |
---|
[dec793e] | 36 | |
---|
[cb463b4] | 37 | class ReportDialog(wx.Dialog): |
---|
| 38 | """ |
---|
| 39 | The report dialog box. |
---|
| 40 | """ |
---|
| 41 | |
---|
[b281210] | 42 | def __init__(self, list, *args, **kwds): |
---|
[cb463b4] | 43 | """ |
---|
| 44 | Initialization. The parameters added to Dialog are: |
---|
| 45 | |
---|
[4a2b054] | 46 | :param list: report_list (list of html_str, text_str, image) |
---|
| 47 | from invariant_state |
---|
[cb463b4] | 48 | """ |
---|
| 49 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
| 50 | wx.Dialog.__init__(self, *args, **kwds) |
---|
[a94c4e1] | 51 | kwds["image"] = 'Dynamic Image' |
---|
[cb463b4] | 52 | # title |
---|
| 53 | self.SetTitle("Report: Invariant computaion") |
---|
| 54 | # size |
---|
| 55 | self.SetSize((720, 650)) |
---|
| 56 | # font size |
---|
| 57 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[dec793e] | 58 | |
---|
| 59 | # check if tit is MAC |
---|
[7954acd] | 60 | self.is_pdf = ISPDF |
---|
[dec793e] | 61 | |
---|
[cb463b4] | 62 | # report string |
---|
[4a2b054] | 63 | self.report_list = list |
---|
[b281210] | 64 | # put image path in the report string |
---|
[4a2b054] | 65 | self.report_html = self.report_list[0] % "memory:img_inv.png" |
---|
[cb463b4] | 66 | # layout |
---|
| 67 | self._setup_layout() |
---|
[dec793e] | 68 | # wild card |
---|
| 69 | # pdf supporting only on MAC |
---|
[7954acd] | 70 | if self.is_pdf: |
---|
[dec793e] | 71 | self.wild_card = ' PDF files (*.pdf)|*.pdf|' |
---|
| 72 | else: |
---|
| 73 | self.wild_card = '' |
---|
| 74 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
| 75 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
| 76 | |
---|
| 77 | |
---|
[a94c4e1] | 78 | |
---|
[cb463b4] | 79 | def _setup_layout(self): |
---|
| 80 | """ |
---|
| 81 | Set up layout |
---|
| 82 | """ |
---|
| 83 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 84 | |
---|
| 85 | # buttons |
---|
[5b03122] | 86 | id = wx.ID_OK |
---|
| 87 | button_close = wx.Button(self, id, "Close") |
---|
| 88 | button_close.SetToolTipString("Close this report window.") |
---|
| 89 | #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 90 | hbox.Add(button_close) |
---|
| 91 | button_close.SetFocus() |
---|
[cb463b4] | 92 | |
---|
| 93 | id = wx.NewId() |
---|
| 94 | button_preview = wx.Button(self, id, "Preview") |
---|
| 95 | button_preview.SetToolTipString("Print preview this report.") |
---|
[4a2b054] | 96 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, |
---|
| 97 | id=button_preview.GetId()) |
---|
[a94c4e1] | 98 | hbox.Add(button_preview) |
---|
[cb463b4] | 99 | |
---|
| 100 | id = wx.NewId() |
---|
| 101 | button_print = wx.Button(self, id, "Print") |
---|
| 102 | button_print.SetToolTipString("Print this report.") |
---|
[4a2b054] | 103 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
| 104 | id=button_print.GetId()) |
---|
[cb463b4] | 105 | hbox.Add(button_print) |
---|
| 106 | |
---|
[5b03122] | 107 | id = wx.NewId() |
---|
| 108 | button_save = wx.Button(self, id, "Save" ) |
---|
| 109 | button_save.SetToolTipString("Save this report.") |
---|
| 110 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) |
---|
| 111 | hbox.Add(button_save) |
---|
[cb463b4] | 112 | |
---|
| 113 | # panel for report page |
---|
[cf9b6950] | 114 | #panel = wx.Panel(self, -1) |
---|
[4a2b054] | 115 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[cb463b4] | 116 | # html window |
---|
[cf9b6950] | 117 | self.hwindow = html.HtmlWindow(self,style=wx.BORDER) |
---|
[cb463b4] | 118 | # set the html page with the report string |
---|
[b281210] | 119 | self.hwindow.SetPage(self.report_html) |
---|
[5b03122] | 120 | |
---|
[cb463b4] | 121 | # add panels to boxsizers |
---|
[cf9b6950] | 122 | vbox.Add(hbox) |
---|
| 123 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
[cb463b4] | 124 | |
---|
[cf9b6950] | 125 | self.SetSizer(vbox) |
---|
[cb463b4] | 126 | self.Centre() |
---|
| 127 | self.Show(True) |
---|
[dec793e] | 128 | |
---|
[cf9b6950] | 129 | |
---|
[4a2b054] | 130 | def onSave(self, event=None): |
---|
[cb463b4] | 131 | """ |
---|
[a94c4e1] | 132 | Save |
---|
[cb463b4] | 133 | """ |
---|
[a94c4e1] | 134 | #todo: complete saving fig file and as a txt file |
---|
[4a2b054] | 135 | dlg = wx.FileDialog(self, "Choose a file", |
---|
[dec793e] | 136 | wildcard=self.wild_card, |
---|
[4a2b054] | 137 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
[a94c4e1] | 138 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
| 139 | |
---|
| 140 | if dlg.ShowModal() != wx.ID_OK: |
---|
[4a2b054] | 141 | dlg.Destroy() |
---|
| 142 | return |
---|
[178bfea] | 143 | |
---|
[dec793e] | 144 | fName = dlg.GetPath() |
---|
| 145 | ext_num = dlg.GetFilterIndex() |
---|
| 146 | # index correction |
---|
[7954acd] | 147 | if not self.is_pdf: |
---|
[dec793e] | 148 | ind_cor = 1 |
---|
| 149 | else: |
---|
| 150 | ind_cor = 0 |
---|
[178bfea] | 151 | #set file extensions |
---|
[dec793e] | 152 | if ext_num == (0 + 2 * ind_cor): |
---|
| 153 | # TODO: Sort this case out |
---|
| 154 | ext = '.pdf' |
---|
| 155 | img_ext = '_img.png' |
---|
| 156 | fName = os.path.splitext(fName)[0] + ext |
---|
| 157 | dlg.Destroy() |
---|
| 158 | |
---|
| 159 | #pic (png) file path/name |
---|
| 160 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
| 161 | # save the image for use with pdf writer |
---|
| 162 | self.report_list[2].savefig(pic_fname) |
---|
| 163 | |
---|
| 164 | # put the image file path in the html data |
---|
[091c702] | 165 | html = self.report_list[0] % str(pic_fname) |
---|
[dec793e] | 166 | |
---|
| 167 | # make/open file in case of absence |
---|
| 168 | f = open(fName, 'w') |
---|
| 169 | f.close() |
---|
| 170 | # write pdf as a pdf file |
---|
| 171 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
| 172 | |
---|
| 173 | #open pdf |
---|
| 174 | if pdf: |
---|
[d8e3f7c] | 175 | try: |
---|
| 176 | #Windows |
---|
| 177 | os.startfile(str(fName)) |
---|
| 178 | except: |
---|
| 179 | try: |
---|
| 180 | #Mac |
---|
| 181 | os.system("open %s"% fName) |
---|
| 182 | except: |
---|
| 183 | #DO not open |
---|
| 184 | pass |
---|
[dec793e] | 185 | #delete image file |
---|
| 186 | os.remove(pic_fname) |
---|
| 187 | return |
---|
| 188 | elif ext_num == (1 - ind_cor): |
---|
[178bfea] | 189 | ext = '.html' |
---|
[4a2b054] | 190 | img_ext = '_img4html.png' |
---|
[178bfea] | 191 | report_frame = self.report_list[0] |
---|
[dec793e] | 192 | elif ext_num == (2 - ind_cor): |
---|
[178bfea] | 193 | ext = '.txt' |
---|
[4a2b054] | 194 | # changing the image extension actually changes the image |
---|
| 195 | # format on saving |
---|
| 196 | img_ext = '_img4txt.pdf' |
---|
[178bfea] | 197 | report = self.report_list[1] |
---|
| 198 | else: |
---|
| 199 | return |
---|
[dec793e] | 200 | |
---|
[178bfea] | 201 | #file name |
---|
| 202 | fName = os.path.splitext(fName)[0] + ext |
---|
| 203 | dlg.Destroy() |
---|
| 204 | #pic (png) file path/name |
---|
| 205 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
| 206 | #put the image path in html string |
---|
[dec793e] | 207 | if ext_num == (1 - ind_cor): |
---|
[664c5a7] | 208 | report = report_frame % os.path.basename(pic_fname) |
---|
[dec793e] | 209 | |
---|
[178bfea] | 210 | f = open(fName, 'w') |
---|
| 211 | f.write(report) |
---|
| 212 | f.close() |
---|
| 213 | #save png file using pic_fname |
---|
| 214 | self.report_list[2].savefig(pic_fname) |
---|
[b281210] | 215 | |
---|
| 216 | |
---|
[4a2b054] | 217 | def onPreview(self, event=None): |
---|
[cb463b4] | 218 | """ |
---|
[a94c4e1] | 219 | Preview |
---|
| 220 | |
---|
| 221 | : event: Preview button event |
---|
[cb463b4] | 222 | """ |
---|
[4a2b054] | 223 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 224 | previewh.PreviewText(self.report_html) |
---|
[4a2b054] | 225 | if event is not None: |
---|
| 226 | event.Skip() |
---|
[cb463b4] | 227 | |
---|
[4a2b054] | 228 | def onPrint(self, event=None): |
---|
[cb463b4] | 229 | """ |
---|
[a94c4e1] | 230 | Print |
---|
[cb463b4] | 231 | |
---|
[a94c4e1] | 232 | : event: Print button event |
---|
| 233 | """ |
---|
[4a2b054] | 234 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 235 | printh.PrintText(self.report_html) |
---|
[4a2b054] | 236 | if event is not None: |
---|
| 237 | event.Skip() |
---|
[a94c4e1] | 238 | |
---|
[b281210] | 239 | def OnClose(self,event=None): |
---|
| 240 | """ |
---|
| 241 | Close the Dialog |
---|
| 242 | |
---|
| 243 | : event: Close button event |
---|
| 244 | """ |
---|
| 245 | self.Close() |
---|
[9fb814a] | 246 | |
---|
| 247 | def HTML2PDF(self, data, filename): |
---|
| 248 | """ |
---|
[4ec242e] | 249 | Create a PDF file from html source string. |
---|
| 250 | Returns True is the file creation was successful. |
---|
[9fb814a] | 251 | |
---|
| 252 | : data: html string |
---|
| 253 | : filename: name of file to be saved |
---|
| 254 | """ |
---|
[4ec242e] | 255 | try: |
---|
| 256 | from xhtml2pdf import pisa |
---|
| 257 | # open output file for writing (truncated binary) |
---|
| 258 | resultFile = open(filename, "w+b") |
---|
| 259 | # convert HTML to PDF |
---|
| 260 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
| 261 | # close output file |
---|
| 262 | resultFile.close() |
---|
| 263 | self.Update() |
---|
| 264 | return pisaStatus.err |
---|
| 265 | except: |
---|
| 266 | logging.error("Error creating pdf: %s" % sys.exc_value) |
---|
| 267 | return False |
---|