source: sasview/invariantview/perspectives/invariant/report_dialog.py @ 6450d8c

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 6450d8c was 9fb814a, checked in by Jae Cho <jhjcho@…>, 14 years ago

added save report as pdf: pisa-3.0.33, html5lib-0.9.0, and reportlab-2.4 require

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