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 | basename = os.path.splitext(fName)[0] |
---|
60 | ext_num = dlg.GetFilterIndex() |
---|
61 | dlg.Destroy() |
---|
62 | |
---|
63 | if ext_num == 0 and self.index_offset == 0: # has pdf |
---|
64 | ext = ".pdf" |
---|
65 | elif ext_num == 1 - self.index_offset: |
---|
66 | ext = ".html" |
---|
67 | elif ext_num == 2 - self.index_offset: |
---|
68 | ext = ".txt" |
---|
69 | else: |
---|
70 | logger.warn("unknown export format in report dialog") |
---|
71 | return |
---|
72 | filename = basename + ext |
---|
73 | |
---|
74 | # save the images for use with pdf writer |
---|
75 | pictures = [ |
---|
76 | '_'.join((basename, url.split(':')[1])) for url in self.fig_urls] |
---|
77 | for i, pic in enumerate(pictures): |
---|
78 | self.report_list[2][i].savefig(pic) |
---|
79 | |
---|
80 | # translate png references int html from in-memory name to on-disk name |
---|
81 | html = self.report_html.replace("memory:", basename+'_') |
---|
82 | |
---|
83 | #set file extensions |
---|
84 | img_ext = [] |
---|
85 | if ext == ".pdf": |
---|
86 | # write pdf as a pdf file |
---|
87 | pdf = self.HTML2PDF(data=html, filename=filename) |
---|
88 | |
---|
89 | # delete images used to create the pdf |
---|
90 | for pic_name in pictures: |
---|
91 | os.remove(pic_name) |
---|
92 | |
---|
93 | #open pdf viewer |
---|
94 | if pdf: |
---|
95 | try: |
---|
96 | if os.name == 'nt': # Windows |
---|
97 | os.startfile(fName) |
---|
98 | elif sys.platform == "darwin": # Mac |
---|
99 | os.system("open %s" % fName) |
---|
100 | except Exception as exc: |
---|
101 | # cannot open pdf |
---|
102 | logging.error(str(exc)) |
---|
103 | |
---|
104 | elif ext == ".html": |
---|
105 | with open(filename, 'w') as f: |
---|
106 | f.write(html) |
---|
107 | |
---|
108 | elif ext == ".txt": |
---|
109 | with open(filename, 'w') as f: |
---|
110 | f.write(self.report_list[1]) |
---|
111 | |
---|
112 | self.Update() |
---|