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

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 38226d26 was 7954acd, checked in by Jae Cho <jhjcho@…>, 13 years ago

try to fix tutorial view problem on MAC when the default pdf viewer was set other than the Preview

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