[cb463b4] | 1 | |
---|
| 2 | ################################################################################ |
---|
[78a205a] | 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. |
---|
[cb463b4] | 6 | # |
---|
[78a205a] | 7 | # See the license text in license.txt |
---|
[cb463b4] | 8 | # |
---|
[78a205a] | 9 | # copyright 2009, University of Tennessee |
---|
[cb463b4] | 10 | ################################################################################ |
---|
| 11 | |
---|
| 12 | """ |
---|
[78a205a] | 13 | Dialog report panel to show and summarize the results of |
---|
[4a2b054] | 14 | the invariant calculation. |
---|
[cb463b4] | 15 | """ |
---|
| 16 | import wx |
---|
[4a2b054] | 17 | import os |
---|
[78a205a] | 18 | import sys |
---|
| 19 | import logging |
---|
| 20 | |
---|
[e8bb5b6] | 21 | from sas.guiframe.report_dialog import BaseReportDialog |
---|
| 22 | |
---|
| 23 | class ReportDialog(BaseReportDialog): |
---|
[cb463b4] | 24 | """ |
---|
[78a205a] | 25 | The report dialog box. |
---|
[cb463b4] | 26 | """ |
---|
[78a205a] | 27 | |
---|
| 28 | def __init__(self, report_list, *args, **kwds): |
---|
[cb463b4] | 29 | """ |
---|
| 30 | Initialization. The parameters added to Dialog are: |
---|
[78a205a] | 31 | |
---|
[e8bb5b6] | 32 | :param report_list: list of html_str, text_str, image |
---|
[4a2b054] | 33 | from invariant_state |
---|
[cb463b4] | 34 | """ |
---|
[e8bb5b6] | 35 | super(ReportDialog, self).__init__(report_list, *args, **kwds) |
---|
| 36 | |
---|
[cb463b4] | 37 | # title |
---|
| 38 | self.SetTitle("Report: Invariant computaion") |
---|
[dec793e] | 39 | |
---|
[b281210] | 40 | # put image path in the report string |
---|
[4a2b054] | 41 | self.report_html = self.report_list[0] % "memory:img_inv.png" |
---|
[cb463b4] | 42 | # layout |
---|
| 43 | self._setup_layout() |
---|
[cf9b6950] | 44 | |
---|
[4a2b054] | 45 | def onSave(self, event=None): |
---|
[cb463b4] | 46 | """ |
---|
[a94c4e1] | 47 | Save |
---|
[cb463b4] | 48 | """ |
---|
[78a205a] | 49 | # todo: complete saving fig file and as a txt file |
---|
[4a2b054] | 50 | dlg = wx.FileDialog(self, "Choose a file", |
---|
[dec793e] | 51 | wildcard=self.wild_card, |
---|
[78a205a] | 52 | style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) |
---|
| 53 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
[a94c4e1] | 54 | |
---|
| 55 | if dlg.ShowModal() != wx.ID_OK: |
---|
[4a2b054] | 56 | dlg.Destroy() |
---|
| 57 | return |
---|
[178bfea] | 58 | |
---|
[dec793e] | 59 | fName = dlg.GetPath() |
---|
[78a205a] | 60 | ext_num = dlg.GetFilterIndex() |
---|
| 61 | # set file extensions |
---|
[e8bb5b6] | 62 | if ext_num == (0 + 2 * self.index_offset): |
---|
| 63 | # TODO: Sort this case out |
---|
[dec793e] | 64 | ext = '.pdf' |
---|
| 65 | img_ext = '_img.png' |
---|
| 66 | fName = os.path.splitext(fName)[0] + ext |
---|
| 67 | dlg.Destroy() |
---|
| 68 | |
---|
[78a205a] | 69 | # pic (png) file path/name |
---|
[dec793e] | 70 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
| 71 | # save the image for use with pdf writer |
---|
| 72 | self.report_list[2].savefig(pic_fname) |
---|
| 73 | |
---|
| 74 | # put the image file path in the html data |
---|
[091c702] | 75 | html = self.report_list[0] % str(pic_fname) |
---|
[78a205a] | 76 | |
---|
[dec793e] | 77 | # make/open file in case of absence |
---|
| 78 | f = open(fName, 'w') |
---|
| 79 | f.close() |
---|
| 80 | # write pdf as a pdf file |
---|
| 81 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
[78a205a] | 82 | |
---|
| 83 | # open pdf |
---|
[dec793e] | 84 | if pdf: |
---|
[d8e3f7c] | 85 | try: |
---|
[78a205a] | 86 | # Windows |
---|
[d8e3f7c] | 87 | os.startfile(str(fName)) |
---|
| 88 | except: |
---|
| 89 | try: |
---|
[78a205a] | 90 | # Mac |
---|
| 91 | os.system("open %s" % fName) |
---|
[d8e3f7c] | 92 | except: |
---|
[78a205a] | 93 | # DO not open |
---|
| 94 | logging.error("Could not open file: %s" % sys.exc_value) |
---|
| 95 | # delete image file |
---|
[dec793e] | 96 | os.remove(pic_fname) |
---|
| 97 | return |
---|
[e8bb5b6] | 98 | elif ext_num == (1 - self.index_offset): |
---|
[178bfea] | 99 | ext = '.html' |
---|
[4a2b054] | 100 | img_ext = '_img4html.png' |
---|
[178bfea] | 101 | report_frame = self.report_list[0] |
---|
[e8bb5b6] | 102 | elif ext_num == (2 - self.index_offset): |
---|
[78a205a] | 103 | ext = '.txt' |
---|
[4a2b054] | 104 | # changing the image extension actually changes the image |
---|
| 105 | # format on saving |
---|
| 106 | img_ext = '_img4txt.pdf' |
---|
[178bfea] | 107 | report = self.report_list[1] |
---|
| 108 | else: |
---|
| 109 | return |
---|
[dec793e] | 110 | |
---|
[78a205a] | 111 | # file name |
---|
[178bfea] | 112 | fName = os.path.splitext(fName)[0] + ext |
---|
| 113 | dlg.Destroy() |
---|
[78a205a] | 114 | # pic (png) file path/name |
---|
[178bfea] | 115 | pic_fname = os.path.splitext(fName)[0] + img_ext |
---|
[78a205a] | 116 | # put the image path in html string |
---|
[e8bb5b6] | 117 | if ext_num == (1 - self.index_offset): |
---|
[664c5a7] | 118 | report = report_frame % os.path.basename(pic_fname) |
---|
[dec793e] | 119 | |
---|
[178bfea] | 120 | f = open(fName, 'w') |
---|
| 121 | f.write(report) |
---|
| 122 | f.close() |
---|
[78a205a] | 123 | # save png file using pic_fname |
---|
[178bfea] | 124 | self.report_list[2].savefig(pic_fname) |
---|