source: sasview/src/sas/sasgui/perspectives/invariant/report_dialog.py @ 2abe6bf

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 2abe6bf was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 4.1 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
23class ReportDialog(BaseReportDialog):
24    """
25    The report dialog box.
26    """
27
28    def __init__(self, report_list, *args, **kwds):
29        """
30        Initialization. The parameters added to Dialog are:
31
32        :param report_list: list of html_str, text_str, image
33        from invariant_state
34        """
35        super(ReportDialog, self).__init__(report_list, *args, **kwds)
36
37        # title
38        self.SetTitle("Report: Invariant computaion")
39
40        # put image path in the report string
41        self.report_html = self.report_list[0] % "memory:img_inv.png"
42        # layout
43        self._setup_layout()
44
45    def onSave(self, event=None):
46        """
47        Save
48        """
49        # todo: complete saving fig file and as a txt file
50        dlg = wx.FileDialog(self, "Choose a file",
51                            wildcard=self.wild_card,
52                            style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
53        dlg.SetFilterIndex(0)  # Set .html files to be default
54
55        if dlg.ShowModal() != wx.ID_OK:
56            dlg.Destroy()
57            return
58
59        fName = dlg.GetPath()
60        ext_num = dlg.GetFilterIndex()
61        # set file extensions
62        if ext_num == (0 + 2 * self.index_offset):
63            # TODO: Sort this case out
64            ext = '.pdf'
65            img_ext = '_img.png'
66            fName = os.path.splitext(fName)[0] + ext
67            dlg.Destroy()
68
69            # pic (png) file path/name
70            pic_fname = os.path.splitext(fName)[0] + img_ext
71            # save the image for use with pdf writer
72            self.report_list[2].savefig(pic_fname)
73
74            # put the image file path in the html data
75            html = self.report_list[0] % str(pic_fname)
76
77            # make/open file in case of absence
78            f = open(fName, 'w')
79            f.close()
80            # write pdf as a pdf file
81            pdf = self.HTML2PDF(data=html, filename=fName)
82
83            # open pdf
84            if pdf:
85                try:
86                    # Windows
87                    os.startfile(str(fName))
88                except:
89                    try:
90                        # Mac
91                        os.system("open %s" % fName)
92                    except:
93                        # DO not open
94                        logging.error("Could not open file: %s" % sys.exc_value)
95            # delete image file
96            os.remove(pic_fname)
97            return
98        elif ext_num == (1 - self.index_offset):
99            ext = '.html'
100            img_ext = '_img4html.png'
101            report_frame = self.report_list[0]
102        elif ext_num == (2 - self.index_offset):
103            ext = '.txt'
104            # changing the image extension actually changes the image
105            # format on saving
106            img_ext = '_img4txt.pdf'
107            report = self.report_list[1]
108        else:
109            return
110
111        # file name
112        fName = os.path.splitext(fName)[0] + ext
113        dlg.Destroy()
114        # pic (png) file path/name
115        pic_fname = os.path.splitext(fName)[0] + img_ext
116        # put the image path in html string
117        if ext_num == (1 - self.index_offset):
118            report = report_frame % os.path.basename(pic_fname)
119
120        f = open(fName, 'w')
121        f.write(report)
122        f.close()
123        # save png file using pic_fname
124        self.report_list[2].savefig(pic_fname)
Note: See TracBrowser for help on using the repository browser.