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

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249unittest-saveload
Last change on this file since d0ce666f was d0ce666f, checked in by krzywon, 6 years ago

Remove image references when report is closed and point to correct file locations when saving reports to PDF.

  • Property mode set to 100644
File size: 3.6 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        i = 0
77        for url in self.fig_urls:
78            append_me = url.split(':')[1]
79            pic_name = basename + '_{}'.format(append_me)
80            # save the image for use with pdf writer
81            self.report_list[2][i].savefig(pic_name)
82            pictures.append(pic_name)
83            i += 1
84
85        # translate png references int html from in-memory name to on-disk name
86        html = self.report_html.replace("memory:", basename+'_')
87
88        #set file extensions
89        img_ext = []
90        if ext == ".pdf":
91            # write pdf as a pdf file
92            pdf = self.HTML2PDF(data=html, filename=filename)
93
94            # delete images used to create the pdf
95            for pic_name in pictures:
96                os.remove(pic_name)
97
98            #open pdf viewer
99            if pdf:
100                try:
101                    if os.name == 'nt':  # Windows
102                        os.startfile(fName)
103                    elif sys.platform == "darwin":  # Mac
104                        os.system("open %s" % fName)
105                except Exception as exc:
106                    # cannot open pdf
107                    logging.error(str(exc))
108
109        elif ext == ".html":
110            with open(filename, 'w') as f:
111                f.write(html)
112
113        elif ext == ".txt":
114            with open(filename, 'w') as f:
115                f.write(self.report_list[1])
116
117        self.Update()
Note: See TracBrowser for help on using the repository browser.