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