1 | """ |
---|
2 | Dialog report panel to show and summarize the results of |
---|
3 | the fitting calculation. |
---|
4 | """ |
---|
5 | ################################################################################ |
---|
6 | #This software was developed by the University of Tennessee as part of the |
---|
7 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
8 | #project funded by the US National Science Foundation. |
---|
9 | # |
---|
10 | #See the license text in license.txt |
---|
11 | # |
---|
12 | #copyright 2009, University of Tennessee |
---|
13 | ################################################################################ |
---|
14 | |
---|
15 | import wx |
---|
16 | import os |
---|
17 | import wx.html as html |
---|
18 | |
---|
19 | from sas.sasgui.guiframe.report_dialog import BaseReportDialog |
---|
20 | |
---|
21 | class ReportDialog(BaseReportDialog): |
---|
22 | """ |
---|
23 | The report dialog box. |
---|
24 | """ |
---|
25 | |
---|
26 | def __init__(self, report_list, *args, **kwds): |
---|
27 | """ |
---|
28 | Initialization. The parameters added to Dialog are: |
---|
29 | |
---|
30 | :param report_list: list of html_str, text_str, image |
---|
31 | from invariant_state |
---|
32 | """ |
---|
33 | super(ReportDialog, self).__init__(report_list, *args, **kwds) |
---|
34 | |
---|
35 | # title |
---|
36 | self.SetTitle("Report: Fitting") |
---|
37 | |
---|
38 | # number of images of plot |
---|
39 | self.nimages = len(self.report_list[2]) |
---|
40 | |
---|
41 | if self.report_list[2] != None: |
---|
42 | # put image path in the report string |
---|
43 | if len(self.report_list[2]) == 1: |
---|
44 | self.report_html = self.report_list[0] % \ |
---|
45 | "memory:img_fit0.png" |
---|
46 | elif len(self.report_list[2]) == 2: |
---|
47 | self.report_html = self.report_list[0] % \ |
---|
48 | ("memory:img_fit0.png", |
---|
49 | "memory:img_fit1.png") |
---|
50 | # allows up to three images |
---|
51 | else: |
---|
52 | self.report_html = self.report_list[0] % \ |
---|
53 | ("memory:img_fit0.png", |
---|
54 | "memory:img_fit1.png", |
---|
55 | "memory:img_fit2.png") |
---|
56 | else: |
---|
57 | self.report_html = self.report_list[0] |
---|
58 | # layout |
---|
59 | self._setup_layout() |
---|
60 | |
---|
61 | def onSave(self, event=None): |
---|
62 | """ |
---|
63 | Save |
---|
64 | """ |
---|
65 | #todo: complete saving fig file and as a txt file |
---|
66 | dlg = wx.FileDialog(self, "Choose a file", |
---|
67 | wildcard=self.wild_card, |
---|
68 | style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) |
---|
69 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
70 | |
---|
71 | if dlg.ShowModal() != wx.ID_OK: |
---|
72 | dlg.Destroy() |
---|
73 | return |
---|
74 | |
---|
75 | fName = dlg.GetPath() |
---|
76 | ext_num = dlg.GetFilterIndex() |
---|
77 | |
---|
78 | #set file extensions |
---|
79 | img_ext = [] |
---|
80 | pic_fname = [] |
---|
81 | #PDF |
---|
82 | if ext_num == (0 + 2 * self.index_offset): |
---|
83 | # TODO: Sort this case out |
---|
84 | ext = '.pdf' |
---|
85 | |
---|
86 | fName = os.path.splitext(fName)[0] + ext |
---|
87 | dlg.Destroy() |
---|
88 | #pic (png) file path/name |
---|
89 | for num in range(self.nimages): |
---|
90 | im_ext = '_img%s.png' % num |
---|
91 | #img_ext.append(im_ext) |
---|
92 | pic_name = os.path.splitext(fName)[0] + im_ext |
---|
93 | pic_fname.append(pic_name) |
---|
94 | # save the image for use with pdf writer |
---|
95 | self.report_list[2][num].savefig(pic_name) |
---|
96 | |
---|
97 | #put the image path in html string |
---|
98 | report_frame = self.report_list[0] |
---|
99 | #put image name strings into the html file |
---|
100 | #Note:The str for pic_fname shouldn't be removed. |
---|
101 | if self.nimages == 1: |
---|
102 | html = report_frame % str(pic_fname[0]) |
---|
103 | elif self.nimages == 2: |
---|
104 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) |
---|
105 | elif self.nimages == 3: |
---|
106 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), |
---|
107 | str(pic_fname[2])) |
---|
108 | |
---|
109 | # make/open file in case of absence |
---|
110 | f = open(fName, 'w') |
---|
111 | f.close() |
---|
112 | # write pdf as a pdf file |
---|
113 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
114 | |
---|
115 | #open pdf |
---|
116 | if pdf: |
---|
117 | try: |
---|
118 | #Windows |
---|
119 | os.startfile(str(fName)) |
---|
120 | except: |
---|
121 | try: |
---|
122 | #Mac |
---|
123 | os.system("open %s" % fName) |
---|
124 | except: |
---|
125 | #DO not open |
---|
126 | pass |
---|
127 | #delete image file |
---|
128 | for num in range(self.nimages): |
---|
129 | os.remove(pic_fname[num]) |
---|
130 | return |
---|
131 | #HTML + png(graph) |
---|
132 | elif ext_num == (1 - self.index_offset): |
---|
133 | ext = '.html' |
---|
134 | for num in range(self.nimages): |
---|
135 | img_ext.append('_img4html%s.png' % num) |
---|
136 | report_frame = self.report_list[0] |
---|
137 | #TEXT + pdf(graph) |
---|
138 | elif ext_num == (2 - self.index_offset): |
---|
139 | ext = '.txt' |
---|
140 | # changing the image extension actually changes the image |
---|
141 | # format on saving |
---|
142 | for num in range(self.nimages): |
---|
143 | img_ext.append('_img4txt%s.pdf' % num) |
---|
144 | report = self.report_list[1] |
---|
145 | else: |
---|
146 | return |
---|
147 | |
---|
148 | #file name |
---|
149 | fName = os.path.splitext(fName)[0] + ext |
---|
150 | dlg.Destroy() |
---|
151 | |
---|
152 | #pic (png) file path/name |
---|
153 | for num in range(self.nimages): |
---|
154 | pic_name = os.path.splitext(fName)[0] + img_ext[num] |
---|
155 | pic_fname.append(pic_name) |
---|
156 | #put the image path in html string |
---|
157 | if ext_num == (1 - self.index_offset): |
---|
158 | if self.nimages == 1: |
---|
159 | report = report_frame % os.path.basename(pic_fname[0]) |
---|
160 | elif self.nimages == 2: |
---|
161 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
162 | os.path.basename(pic_fname[1])) |
---|
163 | elif self.nimages == 3: |
---|
164 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
165 | os.path.basename(pic_fname[1]), |
---|
166 | os.path.basename(pic_fname[2])) |
---|
167 | f = open(fName, 'w') |
---|
168 | f.write(report) |
---|
169 | f.close() |
---|
170 | self.Update() |
---|
171 | #save png file using pic_fname |
---|
172 | for num in range(self.nimages): |
---|
173 | self.report_list[2][num].savefig(pic_fname[num]) |
---|