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 | self.report_html = self.report_list[0] |
---|
41 | # layout |
---|
42 | self._setup_layout() |
---|
43 | |
---|
44 | def onSave(self, event=None): |
---|
45 | """ |
---|
46 | Save |
---|
47 | """ |
---|
48 | #todo: complete saving fig file and as a txt file |
---|
49 | dlg = wx.FileDialog(self, "Choose a file", |
---|
50 | wildcard=self.wild_card, |
---|
51 | style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) |
---|
52 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
53 | |
---|
54 | if dlg.ShowModal() != wx.ID_OK: |
---|
55 | dlg.Destroy() |
---|
56 | return |
---|
57 | |
---|
58 | fName = dlg.GetPath() |
---|
59 | ext_num = dlg.GetFilterIndex() |
---|
60 | |
---|
61 | #set file extensions |
---|
62 | img_ext = [] |
---|
63 | pic_fname = [] |
---|
64 | #PDF |
---|
65 | if ext_num == (0 + 2 * self.index_offset): |
---|
66 | # TODO: Sort this case out |
---|
67 | ext = '.pdf' |
---|
68 | |
---|
69 | fName = os.path.splitext(fName)[0] + ext |
---|
70 | dlg.Destroy() |
---|
71 | #pic (png) file path/name |
---|
72 | for num in range(self.nimages): |
---|
73 | im_ext = '_img%s.png' % num |
---|
74 | #img_ext.append(im_ext) |
---|
75 | pic_name = os.path.splitext(fName)[0] + im_ext |
---|
76 | pic_fname.append(pic_name) |
---|
77 | # save the image for use with pdf writer |
---|
78 | self.report_list[2][num].savefig(pic_name) |
---|
79 | |
---|
80 | #put the image path in html string |
---|
81 | report_frame = self.report_list[0] |
---|
82 | #put image name strings into the html file |
---|
83 | #Note:The str for pic_fname shouldn't be removed. |
---|
84 | if self.nimages == 1: |
---|
85 | html = report_frame % str(pic_fname[0]) |
---|
86 | elif self.nimages == 2: |
---|
87 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) |
---|
88 | elif self.nimages == 3: |
---|
89 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), |
---|
90 | str(pic_fname[2])) |
---|
91 | |
---|
92 | # make/open file in case of absence |
---|
93 | f = open(fName, 'w') |
---|
94 | f.close() |
---|
95 | # write pdf as a pdf file |
---|
96 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
97 | |
---|
98 | #open pdf |
---|
99 | if pdf: |
---|
100 | try: |
---|
101 | #Windows |
---|
102 | os.startfile(str(fName)) |
---|
103 | except Exception: |
---|
104 | try: |
---|
105 | #Mac |
---|
106 | os.system("open %s" % fName) |
---|
107 | except Exception: |
---|
108 | #DO not open |
---|
109 | pass |
---|
110 | #delete image file |
---|
111 | for num in range(self.nimages): |
---|
112 | os.remove(pic_fname[num]) |
---|
113 | return |
---|
114 | #HTML + png(graph) |
---|
115 | elif ext_num == (1 - self.index_offset): |
---|
116 | ext = '.html' |
---|
117 | for num in range(self.nimages): |
---|
118 | img_ext.append('_img4html%s.png' % num) |
---|
119 | report_frame = self.report_list[0] |
---|
120 | #TEXT + pdf(graph) |
---|
121 | elif ext_num == (2 - self.index_offset): |
---|
122 | ext = '.txt' |
---|
123 | # changing the image extension actually changes the image |
---|
124 | # format on saving |
---|
125 | for num in range(self.nimages): |
---|
126 | img_ext.append('_img4txt%s.pdf' % num) |
---|
127 | report = self.report_list[1] |
---|
128 | else: |
---|
129 | return |
---|
130 | |
---|
131 | #file name |
---|
132 | fName = os.path.splitext(fName)[0] + ext |
---|
133 | dlg.Destroy() |
---|
134 | |
---|
135 | #pic (png) file path/name |
---|
136 | for num in range(self.nimages): |
---|
137 | pic_name = os.path.splitext(fName)[0] + img_ext[num] |
---|
138 | pic_fname.append(pic_name) |
---|
139 | #put the image path in html string |
---|
140 | if ext_num == (1 - self.index_offset): |
---|
141 | if self.nimages == 1: |
---|
142 | report = report_frame % os.path.basename(pic_fname[0]) |
---|
143 | elif self.nimages == 2: |
---|
144 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
145 | os.path.basename(pic_fname[1])) |
---|
146 | elif self.nimages == 3: |
---|
147 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
148 | os.path.basename(pic_fname[1]), |
---|
149 | os.path.basename(pic_fname[2])) |
---|
150 | f = open(fName, 'w') |
---|
151 | f.write(report) |
---|
152 | f.close() |
---|
153 | self.Update() |
---|
154 | #save png file using pic_fname |
---|
155 | for num in range(self.nimages): |
---|
156 | self.report_list[2][num].savefig(pic_fname[num]) |
---|