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