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