source: sasview/src/sas/sasgui/perspectives/fitting/report_dialog.py @ afb311e

Last change on this file since afb311e was 69a6897, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

fix image handling for saved reports

  • Property mode set to 100644
File size: 3.5 KB
Line 
1"""
2Dialog report panel to show and summarize the results of
3the 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
15import wx
16import os
17import wx.html as html
18
19from sas.sasgui.guiframe.report_dialog import BaseReportDialog
20
21class 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 figures
75        pictures = []
76        for num in range(self.nimages):
77            pic_name = basename + '_img%s.png' % num
78            # save the image for use with pdf writer
79            self.report_list[2][num].savefig(pic_name)
80            pictures.append(pic_name)
81
82        # translate png references int html from in-memory name to on-disk name
83        html = self.report_html.replace("memory:img_fit", basename+'_img')
84
85        #set file extensions
86        img_ext = []
87        if ext == ".pdf":
88            # write pdf as a pdf file
89            pdf = self.HTML2PDF(data=html, filename=filename)
90
91            # delete images used to create the pdf
92            for pic_name in pictures:
93                os.remove(pic_name)
94
95            #open pdf viewer
96            if pdf:
97                try:
98                    if os.name == 'nt':  # Windows
99                        os.startfile(fName)
100                    elif sys.platform == "darwin":  # Mac
101                        os.system("open %s" % fName)
102                except Exception as exc:
103                    # cannot open pdf
104                    logging.error(str(exc))
105
106        elif ext == ".html":
107            with open(filename, 'w') as f:
108                f.write(html)
109
110        elif ext == ".txt":
111            with open(filename, 'w') as f:
112                f.write(self.report_list[1])
113
114        self.Update()
Note: See TracBrowser for help on using the repository browser.