[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 | """ |
---|
| 13 | Dialog report panel to show and summarize the results of the invariant calculation. |
---|
| 14 | """ |
---|
| 15 | import wx |
---|
| 16 | import sys,os |
---|
| 17 | import wx.html as html |
---|
[b281210] | 18 | |
---|
[cb463b4] | 19 | if sys.platform.count("win32")>0: |
---|
| 20 | _STATICBOX_WIDTH = 450 |
---|
| 21 | PANEL_WIDTH = 500 |
---|
| 22 | PANEL_HEIGHT = 700 |
---|
| 23 | FONT_VARIANT = 0 |
---|
| 24 | else: |
---|
| 25 | _STATICBOX_WIDTH = 480 |
---|
| 26 | PANEL_WIDTH = 530 |
---|
| 27 | PANEL_HEIGHT = 700 |
---|
| 28 | FONT_VARIANT = 1 |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | class ReportDialog(wx.Dialog): |
---|
| 33 | """ |
---|
| 34 | The report dialog box. |
---|
| 35 | """ |
---|
| 36 | |
---|
[b281210] | 37 | def __init__(self, list, *args, **kwds): |
---|
[cb463b4] | 38 | """ |
---|
| 39 | Initialization. The parameters added to Dialog are: |
---|
| 40 | |
---|
[b281210] | 41 | :param list: report_list (list of html_str, text_str, image) from invariant_state |
---|
[cb463b4] | 42 | """ |
---|
| 43 | kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE |
---|
| 44 | wx.Dialog.__init__(self, *args, **kwds) |
---|
[a94c4e1] | 45 | kwds["image"] = 'Dynamic Image' |
---|
[cb463b4] | 46 | # title |
---|
| 47 | self.SetTitle("Report: Invariant computaion") |
---|
| 48 | # size |
---|
| 49 | self.SetSize((720, 650)) |
---|
| 50 | # font size |
---|
| 51 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 52 | # report string |
---|
[b281210] | 53 | self.report_list =list |
---|
| 54 | # put image path in the report string |
---|
| 55 | self.report_html = self.report_list[0]% "memory:img_inv.png" |
---|
[cb463b4] | 56 | # layout |
---|
| 57 | self._setup_layout() |
---|
[a94c4e1] | 58 | |
---|
[cb463b4] | 59 | def _setup_layout(self): |
---|
| 60 | """ |
---|
| 61 | Set up layout |
---|
| 62 | """ |
---|
| 63 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 64 | |
---|
| 65 | # buttons |
---|
| 66 | id = wx.NewId() |
---|
[b281210] | 67 | button_save = wx.Button(self, id, "Save" ) |
---|
[cb463b4] | 68 | button_save.SetToolTipString("Save this report.") |
---|
| 69 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) |
---|
| 70 | hbox.Add(button_save) |
---|
| 71 | |
---|
| 72 | id = wx.NewId() |
---|
| 73 | button_preview = wx.Button(self, id, "Preview") |
---|
| 74 | button_preview.SetToolTipString("Print preview this report.") |
---|
| 75 | button_preview.Bind(wx.EVT_BUTTON, self.onPreview, id = button_preview.GetId()) |
---|
[a94c4e1] | 76 | hbox.Add(button_preview) |
---|
[cb463b4] | 77 | |
---|
| 78 | id = wx.NewId() |
---|
| 79 | button_print = wx.Button(self, id, "Print") |
---|
| 80 | button_print.SetToolTipString("Print this report.") |
---|
| 81 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, id = button_print.GetId()) |
---|
| 82 | hbox.Add(button_print) |
---|
| 83 | |
---|
[b281210] | 84 | id = wx.ID_OK |
---|
| 85 | button_close = wx.Button(self, id, "Close") |
---|
| 86 | button_close.SetToolTipString("Close this report window.") |
---|
| 87 | hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 88 | hbox.Add(button_close) |
---|
| 89 | button_close.SetFocus() |
---|
| 90 | |
---|
[cb463b4] | 91 | |
---|
| 92 | # panel for report page |
---|
| 93 | panel = wx.Panel(self, -1) |
---|
| 94 | vbox= wx.BoxSizer(wx.VERTICAL) |
---|
| 95 | # html window |
---|
| 96 | self.hwindow = html.HtmlWindow(panel, -1,style=wx.BORDER,size=(700,500)) |
---|
| 97 | # set the html page with the report string |
---|
[b281210] | 98 | self.hwindow.SetPage(self.report_html) |
---|
[cb463b4] | 99 | # add panels to boxsizers |
---|
[b281210] | 100 | #hbox.Add(bpanel) |
---|
| 101 | vbox.Add(hbox,30, wx.EXPAND) |
---|
[cb463b4] | 102 | vbox.Add(panel,500, wx.EXPAND) |
---|
| 103 | |
---|
| 104 | self.SetSizerAndFit(vbox) |
---|
| 105 | self.Centre() |
---|
| 106 | self.Show(True) |
---|
| 107 | |
---|
[a94c4e1] | 108 | def onSave(self,event=None): |
---|
[cb463b4] | 109 | """ |
---|
[a94c4e1] | 110 | Save |
---|
[cb463b4] | 111 | """ |
---|
[a94c4e1] | 112 | #todo: complete saving fig file and as a txt file |
---|
| 113 | dlg = wx.FileDialog(self, "Choose a file",\ |
---|
| 114 | wildcard ='HTML files (*.html)|*.html|'+ |
---|
[f310316] | 115 | 'Text files (*.txt)|*.txt', |
---|
[a94c4e1] | 116 | style = wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
| 117 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
| 118 | |
---|
| 119 | if dlg.ShowModal() != wx.ID_OK: |
---|
| 120 | dlg.Destroy() |
---|
| 121 | return |
---|
[b281210] | 122 | |
---|
[a94c4e1] | 123 | fName = dlg.GetPath() |
---|
[178bfea] | 124 | ext_num = dlg.GetFilterIndex() |
---|
| 125 | |
---|
| 126 | #set file extensions |
---|
| 127 | if ext_num == 0: |
---|
| 128 | ext = '.html' |
---|
| 129 | img_ext= '_img4html.png' |
---|
| 130 | report_frame = self.report_list[0] |
---|
| 131 | elif ext_num == 1: |
---|
| 132 | ext = '.txt' |
---|
| 133 | # changing the image extension actually changes the image format on saving |
---|
| 134 | img_ext= '_img4txt.pdf' |
---|
| 135 | report = self.report_list[1] |
---|
| 136 | else: |
---|
| 137 | return |
---|
| 138 | |
---|
| 139 | #file name |
---|
| 140 | fName = os.path.splitext(fName)[0] + ext |
---|
| 141 | dlg.Destroy() |
---|
| 142 | #pic (png) file path/name |
---|
| 143 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
| 144 | #put the image path in html string |
---|
| 145 | if ext_num == 0: report = report_frame % pic_fname |
---|
| 146 | f = open(fName, 'w') |
---|
| 147 | f.write(report) |
---|
| 148 | f.close() |
---|
| 149 | #save png file using pic_fname |
---|
| 150 | self.report_list[2].savefig(pic_fname) |
---|
[b281210] | 151 | |
---|
| 152 | |
---|
[a94c4e1] | 153 | def onPreview(self,event=None): |
---|
[cb463b4] | 154 | """ |
---|
[a94c4e1] | 155 | Preview |
---|
| 156 | |
---|
| 157 | : event: Preview button event |
---|
[cb463b4] | 158 | """ |
---|
[a94c4e1] | 159 | previewh=html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 160 | previewh.PreviewText(self.report_html) |
---|
[cb463b4] | 161 | |
---|
[a94c4e1] | 162 | def onPrint(self,event=None): |
---|
[cb463b4] | 163 | """ |
---|
[a94c4e1] | 164 | Print |
---|
[cb463b4] | 165 | |
---|
[a94c4e1] | 166 | : event: Print button event |
---|
| 167 | """ |
---|
| 168 | printh=html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 169 | printh.PrintText(self.report_html) |
---|
| 170 | |
---|
[a94c4e1] | 171 | |
---|
[b281210] | 172 | def OnClose(self,event=None): |
---|
| 173 | """ |
---|
| 174 | Close the Dialog |
---|
| 175 | |
---|
| 176 | : event: Close button event |
---|
| 177 | """ |
---|
[cb463b4] | 178 | |
---|
[b281210] | 179 | self.Close() |
---|
[9fb814a] | 180 | |
---|
| 181 | def HTML2PDF(self, data, filename): |
---|
| 182 | """ |
---|
| 183 | Create a PDF file from html source string. |
---|
| 184 | |
---|
| 185 | : data: html string |
---|
| 186 | : filename: name of file to be saved |
---|
| 187 | """ |
---|
| 188 | import ho.pisa as pisa |
---|
| 189 | f = file(filename, "wb") |
---|
| 190 | # pisa requires some extra packages, see their web-site |
---|
| 191 | pdf = pisa.CreatePDF(data,f) |
---|
| 192 | # close the file here otherwise it will be open until quitting the application. |
---|
| 193 | f.close() |
---|
| 194 | |
---|
| 195 | return not pdf.err |
---|
| 196 | |
---|
| 197 | |
---|
[cb463b4] | 198 | |
---|