source: sasview/invariantview/src/sans/perspectives/invariant/report_dialog.py @ 075d073

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 075d073 was 7954acd, checked in by Jae Cho <jhjcho@…>, 12 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
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"""
[4a2b054]13Dialog report panel to show and summarize the results of
14the invariant calculation.
[cb463b4]15"""
16import wx
[4a2b054]17import sys
18import os
[cb463b4]19import wx.html as html
[b281210]20
[4a2b054]21if sys.platform.count("win32") > 0:
[cb463b4]22    _STATICBOX_WIDTH = 450
23    PANEL_WIDTH = 500 
24    PANEL_HEIGHT = 700
25    FONT_VARIANT = 0
[dec793e]26    ISMAC = False
[cb463b4]27else:
28    _STATICBOX_WIDTH = 480
29    PANEL_WIDTH = 530
30    PANEL_HEIGHT = 700
31    FONT_VARIANT = 1
[dec793e]32    ISMAC = True
[7954acd]33ISPDF = True
[dec793e]34 
[cb463b4]35class ReportDialog(wx.Dialog):
36    """
37    The report dialog box.
38    """
39   
[b281210]40    def __init__(self,  list, *args, **kwds):
[cb463b4]41        """
42        Initialization. The parameters added to Dialog are:
43       
[4a2b054]44        :param list: report_list (list of html_str, text_str, image)
45        from invariant_state
[cb463b4]46        """
47        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
48        wx.Dialog.__init__(self, *args, **kwds)
[a94c4e1]49        kwds["image"] = 'Dynamic Image'
[cb463b4]50        # title
51        self.SetTitle("Report: Invariant computaion")
52        # size
53        self.SetSize((720, 650))
54        # font size
55        self.SetWindowVariant(variant=FONT_VARIANT)
[dec793e]56
57        # check if tit is MAC
[7954acd]58        self.is_pdf = ISPDF
[dec793e]59       
[cb463b4]60        # report string
[4a2b054]61        self.report_list = list
[b281210]62        # put image path in the report string
[4a2b054]63        self.report_html = self.report_list[0] % "memory:img_inv.png"
[cb463b4]64        # layout
65        self._setup_layout()
[dec793e]66        # wild card
67        # pdf supporting only on MAC
[7954acd]68        if self.is_pdf:
[dec793e]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       
[a94c4e1]76       
[cb463b4]77    def _setup_layout(self):
78        """
79        Set up layout
80        """
81        hbox = wx.BoxSizer(wx.HORIZONTAL)
82       
83        # buttons
[5b03122]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()
[cb463b4]90
91        id = wx.NewId()
92        button_preview = wx.Button(self, id, "Preview")
93        button_preview.SetToolTipString("Print preview this report.")
[4a2b054]94        button_preview.Bind(wx.EVT_BUTTON, self.onPreview,
95                            id=button_preview.GetId()) 
[a94c4e1]96        hbox.Add(button_preview)
[cb463b4]97
98        id = wx.NewId()
99        button_print = wx.Button(self, id, "Print")
100        button_print.SetToolTipString("Print this report.")
[4a2b054]101        button_print.Bind(wx.EVT_BUTTON, self.onPrint,
102                          id=button_print.GetId()) 
[cb463b4]103        hbox.Add(button_print)
104       
[5b03122]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)     
[cb463b4]110       
111        # panel for report page
[cf9b6950]112        #panel = wx.Panel(self, -1)
[4a2b054]113        vbox = wx.BoxSizer(wx.VERTICAL)
[cb463b4]114        # html window
[cf9b6950]115        self.hwindow = html.HtmlWindow(self,style=wx.BORDER)
[cb463b4]116        # set the html page with the report string
[b281210]117        self.hwindow.SetPage(self.report_html)
[5b03122]118       
[cb463b4]119        # add panels to boxsizers
[cf9b6950]120        vbox.Add(hbox)
121        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0)
[cb463b4]122
[cf9b6950]123        self.SetSizer(vbox)
[cb463b4]124        self.Centre()
125        self.Show(True)
[dec793e]126               
[cf9b6950]127
[4a2b054]128    def onSave(self, event=None):
[cb463b4]129        """
[a94c4e1]130        Save
[cb463b4]131        """
[a94c4e1]132        #todo: complete saving fig file and as a txt file
[4a2b054]133        dlg = wx.FileDialog(self, "Choose a file",
[dec793e]134                            wildcard=self.wild_card,
[4a2b054]135                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
[a94c4e1]136        dlg.SetFilterIndex(0) #Set .html files to be default
137
138        if dlg.ShowModal() != wx.ID_OK:
[4a2b054]139            dlg.Destroy()
140            return
[178bfea]141
[dec793e]142        fName = dlg.GetPath()
143        ext_num = dlg.GetFilterIndex() 
144        # index correction
[7954acd]145        if not self.is_pdf:
[dec793e]146                ind_cor = 1 
147        else:
148                ind_cor = 0 
[178bfea]149        #set file extensions 
[dec793e]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
[091c702]163            html = self.report_list[0] % str(pic_fname)
[dec793e]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):
[178bfea]178            ext = '.html'
[4a2b054]179            img_ext = '_img4html.png'
[178bfea]180            report_frame = self.report_list[0]
[dec793e]181        elif ext_num == (2 - ind_cor):
[178bfea]182            ext = '.txt'   
[4a2b054]183            # changing the image extension actually changes the image
184            # format on saving
185            img_ext = '_img4txt.pdf'
[178bfea]186            report = self.report_list[1]
187        else:
188            return
[dec793e]189
[178bfea]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
[dec793e]196        if ext_num == (1 - ind_cor):
[664c5a7]197            report = report_frame % os.path.basename(pic_fname)
[dec793e]198
[178bfea]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)
[b281210]204       
205           
[4a2b054]206    def onPreview(self, event=None):
[cb463b4]207        """
[a94c4e1]208        Preview
209       
210        : event: Preview button event
[cb463b4]211        """
[4a2b054]212        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]213        previewh.PreviewText(self.report_html)
[4a2b054]214        if event is not None:
215            event.Skip()
[cb463b4]216   
[4a2b054]217    def onPrint(self, event=None):
[cb463b4]218        """
[a94c4e1]219        Print
[cb463b4]220       
[a94c4e1]221        : event: Print button event
222        """
[4a2b054]223        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
[b281210]224        printh.PrintText(self.report_html)
[4a2b054]225        if event is not None:
226            event.Skip()
[a94c4e1]227
[b281210]228    def OnClose(self,event=None):
229        """
230        Close the Dialog
231       
232        : event: Close button event
233        """
234        self.Close()
[9fb814a]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
[4a2b054]246        pdf = pisa.CreatePDF(data, f)
247        # close the file here otherwise it will be open until quitting
248        #the application.
[9fb814a]249        f.close()
250
251        return not pdf.err
252
253       
[cb463b4]254       
Note: See TracBrowser for help on using the repository browser.