source: sasview/src/sas/perspectives/invariant/report_dialog.py @ 8a22b5b

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 8a22b5b 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
RevLine 
[cb463b4]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"""
[4a2b054]13Dialog report panel to show and summarize the results of
14the invariant calculation.
[cb463b4]15"""
16import wx
[4a2b054]17import os
[cb463b4]18import wx.html as html
[e8bb5b6]19 
20from sas.guiframe.report_dialog import BaseReportDialog
21
22class ReportDialog(BaseReportDialog):
[cb463b4]23    """
24    The report dialog box.
25    """
26   
[e8bb5b6]27    def __init__(self,  report_list, *args, **kwds):
[cb463b4]28        """
29        Initialization. The parameters added to Dialog are:
30       
[e8bb5b6]31        :param report_list: list of html_str, text_str, image
[4a2b054]32        from invariant_state
[cb463b4]33        """
[e8bb5b6]34        super(ReportDialog, self).__init__(report_list, *args, **kwds)
35
[cb463b4]36        # title
37        self.SetTitle("Report: Invariant computaion")
[dec793e]38
[b281210]39        # put image path in the report string
[4a2b054]40        self.report_html = self.report_list[0] % "memory:img_inv.png"
[cb463b4]41        # layout
42        self._setup_layout()
[cf9b6950]43
[4a2b054]44    def onSave(self, event=None):
[cb463b4]45        """
[a94c4e1]46        Save
[cb463b4]47        """
[a94c4e1]48        #todo: complete saving fig file and as a txt file
[4a2b054]49        dlg = wx.FileDialog(self, "Choose a file",
[dec793e]50                            wildcard=self.wild_card,
[4a2b054]51                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
[a94c4e1]52        dlg.SetFilterIndex(0) #Set .html files to be default
53
54        if dlg.ShowModal() != wx.ID_OK:
[4a2b054]55            dlg.Destroy()
56            return
[178bfea]57
[dec793e]58        fName = dlg.GetPath()
59        ext_num = dlg.GetFilterIndex() 
[178bfea]60        #set file extensions 
[e8bb5b6]61        if ext_num == (0 + 2 * self.index_offset):
62            # TODO: Sort this case out
[dec793e]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
[091c702]74            html = self.report_list[0] % str(pic_fname)
[dec793e]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:
[d8e3f7c]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
[dec793e]94            #delete image file
95            os.remove(pic_fname)
96            return
[e8bb5b6]97        elif ext_num == (1 - self.index_offset):
[178bfea]98            ext = '.html'
[4a2b054]99            img_ext = '_img4html.png'
[178bfea]100            report_frame = self.report_list[0]
[e8bb5b6]101        elif ext_num == (2 - self.index_offset):
[178bfea]102            ext = '.txt'   
[4a2b054]103            # changing the image extension actually changes the image
104            # format on saving
105            img_ext = '_img4txt.pdf'
[178bfea]106            report = self.report_list[1]
107        else:
108            return
[dec793e]109
[178bfea]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
[e8bb5b6]116        if ext_num == (1 - self.index_offset):
[664c5a7]117            report = report_frame % os.path.basename(pic_fname)
[dec793e]118
[178bfea]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)
[e8bb5b6]124       
Note: See TracBrowser for help on using the repository browser.