source: sasview/src/sas/sasgui/perspectives/invariant/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, 14 months 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: 4.0 KB
Line 
1
2################################################################################
3# This software was developed by the University of Tennessee as part of the
4# Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
5# project funded by the US National Science Foundation.
6#
7# See the license text in license.txt
8#
9# copyright 2009, University of Tennessee
10################################################################################
11
12"""
13Dialog report panel to show and summarize the results of
14the invariant calculation.
15"""
16import wx
17import os
18import sys
19import logging
20
21from sas.sasgui.guiframe.report_dialog import BaseReportDialog
22
23logger = logging.getLogger(__name__)
24
25class ReportDialog(BaseReportDialog):
26    """
27    The report dialog box.
28    """
29
30    def __init__(self, report_list, *args, **kwds):
31        """
32        Initialization. The parameters added to Dialog are:
33
34        :param report_list: list of html_str, text_str, image
35        from invariant_state
36        """
37        super(ReportDialog, self).__init__(report_list, *args, **kwds)
38
39        # title
40        self.SetTitle("Report: Invariant computaion")
41
42        # put image path in the report string
43        self.report_html = self.report_list[0] % self.fig_urls[0]
44        # layout
45        self._setup_layout()
46
47    def onSave(self, event=None):
48        """
49        Save
50        """
51        # todo: complete saving fig file and as a txt file
52        dlg = wx.FileDialog(self, "Choose a file",
53                            wildcard=self.wild_card,
54                            style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
55        dlg.SetFilterIndex(0)  # Set .html files to be default
56
57        if dlg.ShowModal() != wx.ID_OK:
58            dlg.Destroy()
59            return
60
61        fName = dlg.GetPath()
62        ext_num = dlg.GetFilterIndex()
63        # set file extensions
64        if ext_num == (0 + 2 * self.index_offset):
65            # TODO: Sort this case out
66            ext = '.pdf'
67            img_ext = '_img.png'
68            fName = os.path.splitext(fName)[0] + ext
69            dlg.Destroy()
70
71            # pic (png) file path/name
72            pic_fname = os.path.splitext(fName)[0] + img_ext
73            # save the image for use with pdf writer
74            self.report_list[2].savefig(pic_fname)
75
76            # put the image file path in the html data
77            html = self.report_list[0] % str(pic_fname)
78
79            # make/open file in case of absence
80            f = open(fName, 'w')
81            f.close()
82            # write pdf as a pdf file
83            pdf = self.HTML2PDF(data=html, filename=fName)
84
85            # open pdf
86            if pdf:
87                try:
88                    # Windows
89                    os.startfile(str(fName))
90                except:
91                    try:
92                        # Mac
93                        os.system("open %s" % fName)
94                    except:
95                        # DO not open
96                        logger.error("Could not open file: %s" % sys.exc_value)
97            # delete image file
98            os.remove(pic_fname)
99            return
100        elif ext_num == (1 - self.index_offset):
101            ext = '.html'
102            img_ext = '_img4html.png'
103            report_frame = self.report_list[0]
104        elif ext_num == (2 - self.index_offset):
105            ext = '.txt'
106            # changing the image extension actually changes the image
107            # format on saving
108            img_ext = '_img4txt.pdf'
109            report = self.report_list[1]
110        else:
111            return
112
113        # file name
114        fName = os.path.splitext(fName)[0] + ext
115        dlg.Destroy()
116        # pic (png) file path/name
117        pic_fname = os.path.splitext(fName)[0] + img_ext
118        # put the image path in html string
119        if ext_num == (1 - self.index_offset):
120            report = report_frame % os.path.basename(pic_fname)
121
122        f = open(fName, 'w')
123        f.write(report)
124        f.close()
125        # save png file using pic_fname
126        self.report_list[2].savefig(pic_fname)
Note: See TracBrowser for help on using the repository browser.