source: sasview/invariantview/perspectives/invariant/report_dialog.py @ 5b03122

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

removed commented (unused) imports

  • Property mode set to 100644
File size: 6.1 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"""
13Dialog report panel to show and summarize the results of the invariant calculation.
14"""
15import wx
16import sys,os
17import wx.html as html
[b281210]18
[cb463b4]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   
[b281210]37    def __init__(self,  list, *args, **kwds):
[cb463b4]38        """
39        Initialization. The parameters added to Dialog are:
40       
[b281210]41        :param list: report_list (list of html_str, text_str, image) from invariant_state
[cb463b4]42        """
43        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
44        wx.Dialog.__init__(self, *args, **kwds)
[a94c4e1]45        kwds["image"] = 'Dynamic Image'
[cb463b4]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
[b281210]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"
[cb463b4]56        # layout
57        self._setup_layout()
[a94c4e1]58       
[cb463b4]59    def _setup_layout(self):
60        """
61        Set up layout
62        """
63        hbox = wx.BoxSizer(wx.HORIZONTAL)
64       
65        # buttons
[5b03122]66        id = wx.ID_OK
67        button_close = wx.Button(self, id, "Close")
68        button_close.SetToolTipString("Close this report window.") 
69        #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0)
70        hbox.Add(button_close)
71        button_close.SetFocus()
[cb463b4]72
73        id = wx.NewId()
74        button_preview = wx.Button(self, id, "Preview")
75        button_preview.SetToolTipString("Print preview this report.")
76        button_preview.Bind(wx.EVT_BUTTON, self.onPreview, id = button_preview.GetId()) 
[a94c4e1]77        hbox.Add(button_preview)
[cb463b4]78
79        id = wx.NewId()
80        button_print = wx.Button(self, id, "Print")
81        button_print.SetToolTipString("Print this report.")
82        button_print.Bind(wx.EVT_BUTTON, self.onPrint, id = button_print.GetId()) 
83        hbox.Add(button_print)
84       
[5b03122]85        id = wx.NewId()
86        button_save = wx.Button(self, id, "Save" )
87        button_save.SetToolTipString("Save this report.")
88        button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId()) 
89        hbox.Add(button_save)     
[cb463b4]90       
91        # panel for report page
92        panel = wx.Panel(self, -1)
93        vbox= wx.BoxSizer(wx.VERTICAL)
94        # html window
[5b03122]95        self.hwindow = html.HtmlWindow(panel,style=wx.BORDER,size=(700,500))
[cb463b4]96        # set the html page with the report string
[b281210]97        self.hwindow.SetPage(self.report_html)
[5b03122]98       
[cb463b4]99        # add panels to boxsizers
[5b03122]100        vbox.Add(hbox,30)
101        vbox.Add(panel,500, wx.EXPAND|wx.ALL)
[cb463b4]102
103        self.SetSizerAndFit(vbox)
104        self.Centre()
105        self.Show(True)
106
[a94c4e1]107    def onSave(self,event=None):
[cb463b4]108        """
[a94c4e1]109        Save
[cb463b4]110        """
[a94c4e1]111        #todo: complete saving fig file and as a txt file
112        dlg = wx.FileDialog(self, "Choose a file",\
113                            wildcard ='HTML files (*.html)|*.html|'+
[f310316]114                            'Text files (*.txt)|*.txt',
[a94c4e1]115                            style = wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
116        dlg.SetFilterIndex(0) #Set .html files to be default
117
118        if dlg.ShowModal() != wx.ID_OK:
119          dlg.Destroy()
120          return
[b281210]121       
[a94c4e1]122        fName = dlg.GetPath()
[178bfea]123        ext_num = dlg.GetFilterIndex()     
124
125        #set file extensions 
126        if ext_num == 0:
127            ext = '.html'
128            img_ext= '_img4html.png'
129            report_frame = self.report_list[0]
130        elif ext_num == 1:
131            ext = '.txt'   
132            # changing the image extension actually changes the image format on saving
133            img_ext= '_img4txt.pdf'
134            report = self.report_list[1]
135        else:
136            return
137       
138        #file name     
139        fName = os.path.splitext(fName)[0] + ext
140        dlg.Destroy()
141        #pic (png) file path/name
142        pic_fname = os.path.splitext(fName)[0] + img_ext
143        #put the image path in html string
144        if ext_num == 0: report = report_frame % pic_fname
145        f = open(fName, 'w')
146        f.write(report)
147        f.close()
148        #save png file using pic_fname
149        self.report_list[2].savefig(pic_fname)
[b281210]150       
151           
[a94c4e1]152    def onPreview(self,event=None):
[cb463b4]153        """
[a94c4e1]154        Preview
155       
156        : event: Preview button event
[cb463b4]157        """
[a94c4e1]158        previewh=html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]159        previewh.PreviewText(self.report_html)
[cb463b4]160   
[a94c4e1]161    def onPrint(self,event=None):
[cb463b4]162        """
[a94c4e1]163        Print
[cb463b4]164       
[a94c4e1]165        : event: Print button event
166        """
167        printh=html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]168        printh.PrintText(self.report_html)
169
[a94c4e1]170
[b281210]171    def OnClose(self,event=None):
172        """
173        Close the Dialog
174       
175        : event: Close button event
176        """
[cb463b4]177       
[b281210]178        self.Close()
[9fb814a]179   
180    def HTML2PDF(self, data, filename):
181        """
182        Create a PDF file from html source string.
183       
184        : data: html string
185        : filename: name of file to be saved
186        """
187        import ho.pisa as pisa
188        f = file(filename, "wb")
189        # pisa requires some extra packages, see their web-site
190        pdf = pisa.CreatePDF(data,f)
191        # close the file here otherwise it will be open until quitting the application.
192        f.close()
193
194        return not pdf.err
195
196       
[cb463b4]197       
Note: See TracBrowser for help on using the repository browser.