source: sasview/invariantview/src/sans/perspectives/invariant/report_dialog.py @ bea9277

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 bea9277 was 7f14996, checked in by Jessica Tumarkin <jtumarki@…>, 13 years ago
  • Property mode set to 100644
File size: 7.7 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 sys
18import os
[cb463b4]19import wx.html as html
[b281210]20
[4a2b054]21if sys.platform.count("win32") > 0:
[cb463b4]22    _STATICBOX_WIDTH = 450
23    PANEL_WIDTH = 500 
24    PANEL_HEIGHT = 700
25    FONT_VARIANT = 0
[dec793e]26    ISMAC = False
[cb463b4]27else:
28    _STATICBOX_WIDTH = 480
29    PANEL_WIDTH = 530
30    PANEL_HEIGHT = 700
31    FONT_VARIANT = 1
[dec793e]32    ISMAC = True
33 
[cb463b4]34class ReportDialog(wx.Dialog):
35    """
36    The report dialog box.
37    """
38   
[b281210]39    def __init__(self,  list, *args, **kwds):
[cb463b4]40        """
41        Initialization. The parameters added to Dialog are:
42       
[4a2b054]43        :param list: report_list (list of html_str, text_str, image)
44        from invariant_state
[cb463b4]45        """
46        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
47        wx.Dialog.__init__(self, *args, **kwds)
[a94c4e1]48        kwds["image"] = 'Dynamic Image'
[cb463b4]49        # title
50        self.SetTitle("Report: Invariant computaion")
51        # size
52        self.SetSize((720, 650))
53        # font size
54        self.SetWindowVariant(variant=FONT_VARIANT)
[dec793e]55
56        # check if tit is MAC
57        self.is_mac = ISMAC
58       
[cb463b4]59        # report string
[4a2b054]60        self.report_list = list
[b281210]61        # put image path in the report string
[4a2b054]62        self.report_html = self.report_list[0] % "memory:img_inv.png"
[cb463b4]63        # layout
64        self._setup_layout()
[dec793e]65        # wild card
66        # pdf supporting only on MAC
67        if self.is_mac:
68                self.wild_card = ' PDF files (*.pdf)|*.pdf|'
69        else:
70                self.wild_card = ''
71        self.wild_card += 'HTML files (*.html)|*.html|'
72        self.wild_card += 'Text files (*.txt)|*.txt'
73 
74       
[a94c4e1]75       
[cb463b4]76    def _setup_layout(self):
77        """
78        Set up layout
79        """
80        hbox = wx.BoxSizer(wx.HORIZONTAL)
81       
82        # buttons
[5b03122]83        id = wx.ID_OK
84        button_close = wx.Button(self, id, "Close")
85        button_close.SetToolTipString("Close this report window.") 
86        #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0)
87        hbox.Add(button_close)
88        button_close.SetFocus()
[cb463b4]89
90        id = wx.NewId()
91        button_preview = wx.Button(self, id, "Preview")
92        button_preview.SetToolTipString("Print preview this report.")
[4a2b054]93        button_preview.Bind(wx.EVT_BUTTON, self.onPreview,
94                            id=button_preview.GetId()) 
[a94c4e1]95        hbox.Add(button_preview)
[cb463b4]96
97        id = wx.NewId()
98        button_print = wx.Button(self, id, "Print")
99        button_print.SetToolTipString("Print this report.")
[4a2b054]100        button_print.Bind(wx.EVT_BUTTON, self.onPrint,
101                          id=button_print.GetId()) 
[cb463b4]102        hbox.Add(button_print)
103       
[5b03122]104        id = wx.NewId()
105        button_save = wx.Button(self, id, "Save" )
106        button_save.SetToolTipString("Save this report.")
107        button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) 
108        hbox.Add(button_save)     
[cb463b4]109       
110        # panel for report page
[cf9b6950]111        #panel = wx.Panel(self, -1)
[4a2b054]112        vbox = wx.BoxSizer(wx.VERTICAL)
[cb463b4]113        # html window
[cf9b6950]114        self.hwindow = html.HtmlWindow(self,style=wx.BORDER)
[cb463b4]115        # set the html page with the report string
[b281210]116        self.hwindow.SetPage(self.report_html)
[5b03122]117       
[cb463b4]118        # add panels to boxsizers
[cf9b6950]119        vbox.Add(hbox)
120        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0)
[cb463b4]121
[cf9b6950]122        self.SetSizer(vbox)
[cb463b4]123        self.Centre()
124        self.Show(True)
[dec793e]125               
[cf9b6950]126
[4a2b054]127    def onSave(self, event=None):
[cb463b4]128        """
[a94c4e1]129        Save
[cb463b4]130        """
[a94c4e1]131        #todo: complete saving fig file and as a txt file
[4a2b054]132        dlg = wx.FileDialog(self, "Choose a file",
[dec793e]133                            wildcard=self.wild_card,
[4a2b054]134                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
[a94c4e1]135        dlg.SetFilterIndex(0) #Set .html files to be default
136
137        if dlg.ShowModal() != wx.ID_OK:
[4a2b054]138            dlg.Destroy()
139            return
[178bfea]140
[dec793e]141        fName = dlg.GetPath()
142        ext_num = dlg.GetFilterIndex() 
143        # index correction
144        if not self.is_mac:
145                ind_cor = 1 
146        else:
147                ind_cor = 0 
[178bfea]148        #set file extensions 
[dec793e]149        if ext_num == (0 + 2 * ind_cor):
150                        # TODO: Sort this case out
151            ext = '.pdf'
152            img_ext = '_img.png'
153            fName = os.path.splitext(fName)[0] + ext
154            dlg.Destroy()
155
156            #pic (png) file path/name
157            pic_fname = os.path.splitext(fName)[0] + img_ext
158            # save the image for use with pdf writer
159            self.report_list[2].savefig(pic_fname)
160
161            # put the image file path in the html data
162            html = self.report_list[0] % str(pic_fname)
163           
164            # make/open file in case of absence
165            f = open(fName, 'w')
166            f.close()
167            # write pdf as a pdf file
168            pdf = self.HTML2PDF(data=html, filename=fName)
169           
170            #open pdf
171            if pdf:
172                os.startfile(str(fName))
173            #delete image file
174            os.remove(pic_fname)
175            return
176        elif ext_num == (1 - ind_cor):
[178bfea]177            ext = '.html'
[4a2b054]178            img_ext = '_img4html.png'
[178bfea]179            report_frame = self.report_list[0]
[dec793e]180        elif ext_num == (2 - ind_cor):
[178bfea]181            ext = '.txt'   
[4a2b054]182            # changing the image extension actually changes the image
183            # format on saving
184            img_ext = '_img4txt.pdf'
[178bfea]185            report = self.report_list[1]
186        else:
187            return
[dec793e]188
[178bfea]189        #file name     
190        fName = os.path.splitext(fName)[0] + ext
191        dlg.Destroy()
192        #pic (png) file path/name
193        pic_fname = os.path.splitext(fName)[0] + img_ext
194        #put the image path in html string
[dec793e]195        if ext_num == (1 - ind_cor):
[4a2b054]196            report = report_frame % pic_fname
[dec793e]197
[178bfea]198        f = open(fName, 'w')
199        f.write(report)
200        f.close()
201        #save png file using pic_fname
202        self.report_list[2].savefig(pic_fname)
[b281210]203       
204           
[4a2b054]205    def onPreview(self, event=None):
[cb463b4]206        """
[a94c4e1]207        Preview
208       
209        : event: Preview button event
[cb463b4]210        """
[4a2b054]211        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]212        previewh.PreviewText(self.report_html)
[4a2b054]213        if event is not None:
214            event.Skip()
[cb463b4]215   
[4a2b054]216    def onPrint(self, event=None):
[cb463b4]217        """
[a94c4e1]218        Print
[cb463b4]219       
[a94c4e1]220        : event: Print button event
221        """
[4a2b054]222        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]223        printh.PrintText(self.report_html)
[4a2b054]224        if event is not None:
225            event.Skip()
[a94c4e1]226
[b281210]227    def OnClose(self,event=None):
228        """
229        Close the Dialog
230       
231        : event: Close button event
232        """
233        self.Close()
[9fb814a]234   
235    def HTML2PDF(self, data, filename):
236        """
237        Create a PDF file from html source string.
238       
239        : data: html string
240        : filename: name of file to be saved
241        """
242        import ho.pisa as pisa
243        f = file(filename, "wb")
244        # pisa requires some extra packages, see their web-site
[4a2b054]245        pdf = pisa.CreatePDF(data, f)
246        # close the file here otherwise it will be open until quitting
247        #the application.
[9fb814a]248        f.close()
249
250        return not pdf.err
251
252       
[cb463b4]253       
Note: See TracBrowser for help on using the repository browser.