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