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

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 79492222 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

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