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