source: sasview/src/sas/perspectives/invariant/report_dialog.py @ 59b1b92

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 59b1b92 was e8bb5b6, checked in by Mathieu Doucet <doucetm@…>, 10 years ago

Re #336 Move common report code to guiframe.

  • 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 wx.html as html
19 
20from sas.guiframe.report_dialog import BaseReportDialog
21
22class ReportDialog(BaseReportDialog):
23    """
24    The report dialog box.
25    """
26   
27    def __init__(self,  report_list, *args, **kwds):
28        """
29        Initialization. The parameters added to Dialog are:
30       
31        :param report_list: list of html_str, text_str, image
32        from invariant_state
33        """
34        super(ReportDialog, self).__init__(report_list, *args, **kwds)
35
36        # title
37        self.SetTitle("Report: Invariant computaion")
38
39        # put image path in the report string
40        self.report_html = self.report_list[0] % "memory:img_inv.png"
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        ext_num = dlg.GetFilterIndex() 
60        #set file extensions 
61        if ext_num == (0 + 2 * self.index_offset):
62            # TODO: Sort this case out
63            ext = '.pdf'
64            img_ext = '_img.png'
65            fName = os.path.splitext(fName)[0] + ext
66            dlg.Destroy()
67
68            #pic (png) file path/name
69            pic_fname = os.path.splitext(fName)[0] + img_ext
70            # save the image for use with pdf writer
71            self.report_list[2].savefig(pic_fname)
72
73            # put the image file path in the html data
74            html = self.report_list[0] % str(pic_fname)
75           
76            # make/open file in case of absence
77            f = open(fName, 'w')
78            f.close()
79            # write pdf as a pdf file
80            pdf = self.HTML2PDF(data=html, filename=fName)
81           
82            #open pdf
83            if pdf:
84                try:
85                    #Windows
86                    os.startfile(str(fName))
87                except:
88                    try:
89                        #Mac
90                        os.system("open %s"% fName)
91                    except:
92                        #DO not open
93                        pass
94            #delete image file
95            os.remove(pic_fname)
96            return
97        elif ext_num == (1 - self.index_offset):
98            ext = '.html'
99            img_ext = '_img4html.png'
100            report_frame = self.report_list[0]
101        elif ext_num == (2 - self.index_offset):
102            ext = '.txt'   
103            # changing the image extension actually changes the image
104            # format on saving
105            img_ext = '_img4txt.pdf'
106            report = self.report_list[1]
107        else:
108            return
109
110        #file name     
111        fName = os.path.splitext(fName)[0] + ext
112        dlg.Destroy()
113        #pic (png) file path/name
114        pic_fname = os.path.splitext(fName)[0] + img_ext
115        #put the image path in html string
116        if ext_num == (1 - self.index_offset):
117            report = report_frame % os.path.basename(pic_fname)
118
119        f = open(fName, 'w')
120        f.write(report)
121        f.close()
122        #save png file using pic_fname
123        self.report_list[2].savefig(pic_fname)
124       
Note: See TracBrowser for help on using the repository browser.