[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 |
---|
| 97 | panel = wx.Panel(self, -1) |
---|
[4a2b054] | 98 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[cb463b4] | 99 | # html window |
---|
[5b03122] | 100 | self.hwindow = html.HtmlWindow(panel,style=wx.BORDER,size=(700,500)) |
---|
[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 |
---|
[4a2b054] | 105 | vbox.Add(hbox, 30) |
---|
| 106 | vbox.Add(panel, 500, wx.EXPAND|wx.ALL) |
---|
[cb463b4] | 107 | |
---|
| 108 | self.SetSizerAndFit(vbox) |
---|
| 109 | self.Centre() |
---|
| 110 | self.Show(True) |
---|
| 111 | |
---|
[4a2b054] | 112 | def onSave(self, event=None): |
---|
[cb463b4] | 113 | """ |
---|
[a94c4e1] | 114 | Save |
---|
[cb463b4] | 115 | """ |
---|
[a94c4e1] | 116 | #todo: complete saving fig file and as a txt file |
---|
[4a2b054] | 117 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 118 | wildcard='HTML files (*.html)|*.html|'+ |
---|
[f310316] | 119 | 'Text files (*.txt)|*.txt', |
---|
[4a2b054] | 120 | style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) |
---|
[a94c4e1] | 121 | dlg.SetFilterIndex(0) #Set .html files to be default |
---|
| 122 | |
---|
| 123 | if dlg.ShowModal() != wx.ID_OK: |
---|
[4a2b054] | 124 | dlg.Destroy() |
---|
| 125 | return |
---|
[b281210] | 126 | |
---|
[a94c4e1] | 127 | fName = dlg.GetPath() |
---|
[178bfea] | 128 | ext_num = dlg.GetFilterIndex() |
---|
| 129 | |
---|
| 130 | #set file extensions |
---|
| 131 | if ext_num == 0: |
---|
| 132 | ext = '.html' |
---|
[4a2b054] | 133 | img_ext = '_img4html.png' |
---|
[178bfea] | 134 | report_frame = self.report_list[0] |
---|
| 135 | elif ext_num == 1: |
---|
| 136 | ext = '.txt' |
---|
[4a2b054] | 137 | # changing the image extension actually changes the image |
---|
| 138 | # format on saving |
---|
| 139 | img_ext = '_img4txt.pdf' |
---|
[178bfea] | 140 | report = self.report_list[1] |
---|
| 141 | else: |
---|
| 142 | return |
---|
| 143 | |
---|
| 144 | #file name |
---|
| 145 | fName = os.path.splitext(fName)[0] + ext |
---|
| 146 | dlg.Destroy() |
---|
| 147 | #pic (png) file path/name |
---|
| 148 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
| 149 | #put the image path in html string |
---|
[4a2b054] | 150 | if ext_num == 0: |
---|
| 151 | report = report_frame % pic_fname |
---|
[178bfea] | 152 | f = open(fName, 'w') |
---|
| 153 | f.write(report) |
---|
| 154 | f.close() |
---|
| 155 | #save png file using pic_fname |
---|
| 156 | self.report_list[2].savefig(pic_fname) |
---|
[b281210] | 157 | |
---|
| 158 | |
---|
[4a2b054] | 159 | def onPreview(self, event=None): |
---|
[cb463b4] | 160 | """ |
---|
[a94c4e1] | 161 | Preview |
---|
| 162 | |
---|
| 163 | : event: Preview button event |
---|
[cb463b4] | 164 | """ |
---|
[4a2b054] | 165 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 166 | previewh.PreviewText(self.report_html) |
---|
[4a2b054] | 167 | if event is not None: |
---|
| 168 | event.Skip() |
---|
[cb463b4] | 169 | |
---|
[4a2b054] | 170 | def onPrint(self, event=None): |
---|
[cb463b4] | 171 | """ |
---|
[a94c4e1] | 172 | Print |
---|
[cb463b4] | 173 | |
---|
[a94c4e1] | 174 | : event: Print button event |
---|
| 175 | """ |
---|
[4a2b054] | 176 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
[b281210] | 177 | printh.PrintText(self.report_html) |
---|
[4a2b054] | 178 | if event is not None: |
---|
| 179 | event.Skip() |
---|
[a94c4e1] | 180 | |
---|
[b281210] | 181 | def OnClose(self,event=None): |
---|
| 182 | """ |
---|
| 183 | Close the Dialog |
---|
| 184 | |
---|
| 185 | : event: Close button event |
---|
| 186 | """ |
---|
| 187 | self.Close() |
---|
[9fb814a] | 188 | |
---|
| 189 | def HTML2PDF(self, data, filename): |
---|
| 190 | """ |
---|
| 191 | Create a PDF file from html source string. |
---|
| 192 | |
---|
| 193 | : data: html string |
---|
| 194 | : filename: name of file to be saved |
---|
| 195 | """ |
---|
| 196 | import ho.pisa as pisa |
---|
| 197 | f = file(filename, "wb") |
---|
| 198 | # pisa requires some extra packages, see their web-site |
---|
[4a2b054] | 199 | pdf = pisa.CreatePDF(data, f) |
---|
| 200 | # close the file here otherwise it will be open until quitting |
---|
| 201 | #the application. |
---|
[9fb814a] | 202 | f.close() |
---|
| 203 | |
---|
| 204 | return not pdf.err |
---|
| 205 | |
---|
| 206 | |
---|
[cb463b4] | 207 | |
---|