source: sasview/invariantview/perspectives/invariant/report_dialog.py @ 178bfea

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 178bfea was 178bfea, checked in by Jae Cho <jhjcho@…>, 14 years ago

cleaned up report codes

  • Property mode set to 100644
File size: 6.2 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                            style = wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
117        dlg.SetFilterIndex(0) #Set .html files to be default
118
119        if dlg.ShowModal() != wx.ID_OK:
120          dlg.Destroy()
121          return
122       
123        fName = dlg.GetPath()
124        ext_num = dlg.GetFilterIndex()     
125
126        #set file extensions 
127        if ext_num == 0:
128            ext = '.html'
129            img_ext= '_img4html.png'
130            report_frame = self.report_list[0]
131        elif ext_num == 1:
132            ext = '.txt'   
133            # changing the image extension actually changes the image format on saving
134            img_ext= '_img4txt.pdf'
135            report = self.report_list[1]
136        else:
137            return
138       
139        #file name     
140        fName = os.path.splitext(fName)[0] + ext
141        dlg.Destroy()
142        #pic (png) file path/name
143        pic_fname = os.path.splitext(fName)[0] + img_ext
144        #put the image path in html string
145        if ext_num == 0: report = report_frame % pic_fname
146        f = open(fName, 'w')
147        f.write(report)
148        f.close()
149        #save png file using pic_fname
150        self.report_list[2].savefig(pic_fname)
151       
152           
153    def onPreview(self,event=None):
154        """
155        Preview
156       
157        : event: Preview button event
158        """
159        previewh=html.HtmlEasyPrinting(name="Printing", parentWindow=self)
160        previewh.PreviewText(self.report_html)
161   
162    def onPrint(self,event=None):
163        """
164        Print
165       
166        : event: Print button event
167        """
168        printh=html.HtmlEasyPrinting(name="Printing", parentWindow=self)
169        printh.PrintText(self.report_html)
170
171
172    def OnClose(self,event=None):
173        """
174        Close the Dialog
175       
176        : event: Close button event
177        """
178       
179        self.Close()
180   
181    def HTML2PDF(self, data, filename):
182        """
183        Create a PDF file from html source string.
184       
185        : data: html string
186        : filename: name of file to be saved
187        """
188        import ho.pisa as pisa
189        f = file(filename, "wb")
190        # pisa requires some extra packages, see their web-site
191        pdf = pisa.CreatePDF(data,f)
192        # close the file here otherwise it will be open until quitting the application.
193        f.close()
194
195        return not pdf.err
196
197       
198       
Note: See TracBrowser for help on using the repository browser.