source: sasview/src/sas/perspectives/fitting/report_dialog.py @ 79492222

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 79492222 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 10.4 KB
RevLine 
[f32d144]1"""
2Dialog report panel to show and summarize the results of
3the invariant calculation.
4"""
[2296316]5################################################################################
6#This software was developed by the University of Tennessee as part of the
7#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
[f32d144]8#project funded by the US National Science Foundation.
[2296316]9#
10#See the license text in license.txt
11#
12#copyright 2009, University of Tennessee
13################################################################################
14
15import wx
16import sys
17import os
18import wx.html as html
19
[c2b0079]20_STATICBOX_WIDTH = 480
21PANEL_WIDTH = 530
22PANEL_HEIGHT = 700
23FONT_VARIANT = 1
24ISMAC = False
[d8e3f7c]25ISPDF = False
[c2b0079]26if sys.platform == "win32":
[2296316]27    _STATICBOX_WIDTH = 450
[f32d144]28    PANEL_WIDTH = 500
[2296316]29    PANEL_HEIGHT = 700
30    FONT_VARIANT = 0
[dec793e]31    ISMAC = False
[d8e3f7c]32    ISPDF = True
[c2b0079]33elif sys.platform == "darwin":
[dec793e]34    ISMAC = True
[d8e3f7c]35    ISPDF = True
[2296316]36
37       
38class ReportDialog(wx.Dialog):
39    """
[f32d144]40    The report dialog box.
[2296316]41    """
42   
[f32d144]43    def __init__(self, list, *args, **kwds):
[2296316]44        """
45        Initialization. The parameters added to Dialog are:
46       
47        :param list: report_list (list of html_str, text_str, image)
48        from invariant_state
49        """
50        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
51        wx.Dialog.__init__(self, *args, **kwds)
52        kwds["image"] = 'Dynamic Image'
53        # title
54        self.SetTitle("Report: Fitting")
55        # size
56        self.SetSize((720, 650))
[f32d144]57        # font size
[2296316]58        self.SetWindowVariant(variant=FONT_VARIANT)
[dec793e]59        # check if tit is MAC
[213b445]60        self.is_pdf = ISPDF
[2296316]61        # report string
62        self.report_list = list
63        # number of images of plot
64        self.nimages = len(list[2])
65       
66        if list[2] != None:
67            # put image path in the report string
68            if len(list[2]) == 1:
69                self.report_html = self.report_list[0] % \
70                                    "memory:img_fit0.png"
71            elif len(list[2]) == 2:
72                self.report_html = self.report_list[0] % \
[f32d144]73                                    ("memory:img_fit0.png",
[2296316]74                                     "memory:img_fit1.png")
75            # allows up to three images
76            else:
77                self.report_html = self.report_list[0] % \
[f32d144]78                                    ("memory:img_fit0.png",
[2296316]79                                     "memory:img_fit1.png",
80                                     "memory:img_fit2.png")
81        else:
82            self.report_html = self.report_list[0]
83        # layout
84        self._setup_layout()
85       
86    def _setup_layout(self):
87        """
88        Set up layout
89        """
90        hbox = wx.BoxSizer(wx.HORIZONTAL)
91       
92        # buttons
93        id = wx.ID_OK
94        button_close = wx.Button(self, id, "Close")
[f32d144]95        button_close.SetToolTipString("Close this report window.")
[2296316]96        #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0)
97        hbox.Add(button_close)
98        button_close.SetFocus()
99
100        id = wx.NewId()
101        button_preview = wx.Button(self, id, "Preview")
102        button_preview.SetToolTipString("Print preview this report.")
103        button_preview.Bind(wx.EVT_BUTTON, self.onPreview,
104                            id=button_preview.GetId()) 
105        hbox.Add(button_preview)
106
107        id = wx.NewId()
108        button_print = wx.Button(self, id, "Print")
109        button_print.SetToolTipString("Print this report.")
110        button_print.Bind(wx.EVT_BUTTON, self.onPrint,
[f32d144]111                          id=button_print.GetId())
[2296316]112        hbox.Add(button_print)
113       
114        id = wx.NewId()
[f32d144]115        button_save = wx.Button(self, id, "Save")
[2296316]116        button_save.SetToolTipString("Save this report.")
[f32d144]117        button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId())
118        hbox.Add(button_save)
[2296316]119       
120        # panel for report page
121        #panel = wx.Panel(self, -1)
122        vbox = wx.BoxSizer(wx.VERTICAL)
123        # html window
[f32d144]124        self.hwindow = html.HtmlWindow(self, style=wx.BORDER)
[2296316]125        # set the html page with the report string
126        self.hwindow.SetPage(self.report_html)
127       
128        # add panels to boxsizers
129        vbox.Add(hbox)
130        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0)
131
132        self.SetSizer(vbox)
133        self.Centre()
134        self.Show(True)
135
136    def onSave(self, event=None):
137        """
138        Save
139        """
[dec793e]140        # pdf supporting only on MAC, not on exe
[213b445]141        if self.is_pdf:
[dec793e]142            wild_card = ' PDF files (*.pdf)|*.pdf|'
[f32d144]143            ind_cor = 0
[dec793e]144        else:
145            wild_card = ''
[f32d144]146            ind_cor = 1
[dec793e]147        wild_card += 'HTML files (*.html)|*.html|'
148        wild_card += 'Text files (*.txt)|*.txt'
149
[2296316]150        #todo: complete saving fig file and as a txt file
151        dlg = wx.FileDialog(self, "Choose a file",
[dec793e]152                            wildcard=wild_card,
[2296316]153                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
[f32d144]154        dlg.SetFilterIndex(0)  # Set .html files to be default
[2296316]155
156        if dlg.ShowModal() != wx.ID_OK:
157            dlg.Destroy()
158            return
159       
160        fName = dlg.GetPath()
[f32d144]161        ext_num = dlg.GetFilterIndex()
[2296316]162
[f32d144]163        #set file extensions
[2296316]164        img_ext = []
[dec793e]165        pic_fname = []
[f32d144]166        #PDF
[dec793e]167        if ext_num == (0 + 2 * ind_cor):
168            # TODO: Sort this case out
169            ext = '.pdf'
170           
171            fName = os.path.splitext(fName)[0] + ext
172            dlg.Destroy()
173            #pic (png) file path/name
174            for num in range(self.nimages):
175                im_ext = '_img%s.png' % num
176                #img_ext.append(im_ext)
177                pic_name = os.path.splitext(fName)[0] + im_ext
178                pic_fname.append(pic_name)
179                # save the image for use with pdf writer
180                self.report_list[2][num].savefig(pic_name)
181
182            #put the image path in html string
183            report_frame = self.report_list[0]
184            #put image name strings into the html file
185            #Note:The str for pic_fname shouldn't be removed.
186            if self.nimages == 1:
[091c702]187                html = report_frame % str(pic_fname[0])
[dec793e]188            elif self.nimages == 2:
[091c702]189                html = report_frame % (str(pic_fname[0]), str(pic_fname[1]))
[dec793e]190            elif self.nimages == 3:
[091c702]191                html = report_frame % (str(pic_fname[0]), str(pic_fname[1]),
192                                          str(pic_fname[2]))
[dec793e]193
194            # make/open file in case of absence
195            f = open(fName, 'w')
196            f.close()
197            # write pdf as a pdf file
198            pdf = self.HTML2PDF(data=html, filename=fName)
199           
200            #open pdf
201            if pdf:
[d8e3f7c]202                try:
203                    #Windows
204                    os.startfile(str(fName))
205                except:
206                    try:
207                        #Mac
208                        os.system("open %s"% fName)
209                    except:
210                        #DO not open
211                        pass
[dec793e]212            #delete image file
213            for num in range(self.nimages):
214                os.remove(pic_fname[num])
215            return
216        #HTML + png(graph)
217        elif ext_num == (1 - ind_cor):
[2296316]218            ext = '.html'
219            for num in range(self.nimages):
220                img_ext.append('_img4html%s.png' % num)
221            report_frame = self.report_list[0]
[dec793e]222        #TEXT + pdf(graph)
223        elif ext_num == (2 - ind_cor):
[f32d144]224            ext = '.txt'
[2296316]225            # changing the image extension actually changes the image
226            # format on saving
227            for num in range(self.nimages):
228                img_ext.append('_img4txt%s.pdf' % num)
229            report = self.report_list[1]
230        else:
231            return
232       
[f32d144]233        #file name
[2296316]234        fName = os.path.splitext(fName)[0] + ext
235        dlg.Destroy()
[dec793e]236       
[2296316]237        #pic (png) file path/name
238        for num in range(self.nimages):
239            pic_name = os.path.splitext(fName)[0] + img_ext[num]
240            pic_fname.append(pic_name)
241        #put the image path in html string
[dec793e]242        if ext_num == (1 - ind_cor):
[2296316]243            if self.nimages == 1:
[3dac5e2]244                report = report_frame % os.path.basename(pic_fname[0])
[2296316]245            elif self.nimages == 2:
[f32d144]246                report = report_frame % (os.path.basename(pic_fname[0]),
[3dac5e2]247                                         os.path.basename(pic_fname[1]))
[2296316]248            elif self.nimages == 3:
[f32d144]249                report = report_frame % (os.path.basename(pic_fname[0]),
[3dac5e2]250                                         os.path.basename(pic_fname[1]),
251                                         os.path.basename(pic_fname[2]))
[2296316]252        f = open(fName, 'w')
253        f.write(report)
254        f.close()
[f0239a3]255        self.Update()
[2296316]256        #save png file using pic_fname
257        for num in range(self.nimages):
258            self.report_list[2][num].savefig(pic_fname[num])
259       
260    def onPreview(self, event=None):
261        """
262        Preview
263       
264        : event: Preview button event
265        """
266        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
267        previewh.PreviewText(self.report_html)
268        if event is not None:
269            event.Skip()
[f0239a3]270        self.Update()
[2296316]271   
272    def onPrint(self, event=None):
273        """
274        Print
275       
276        : event: Print button event
277        """
278        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
279        printh.PrintText(self.report_html)
280        if event is not None:
281            event.Skip()
[f0239a3]282        self.Update()
283       
[f32d144]284    def OnClose(self, event=None):
[2296316]285        """
286        Close the Dialog
287       
288        : event: Close button event
289        """
290        self.Close()
291        # Reset memory
292        #wx.MemoryFSHandler()
293        if event is not None:
294            event.Skip()
295   
296    def HTML2PDF(self, data, filename):
297        """
[f32d144]298        Create a PDF file from html source string.
[2296316]299       
300        : data: html string
301        : filename: name of file to be saved
302        """
303        import ho.pisa as pisa
304        f = file(filename, "wb")
305        # pisa requires some extra packages, see their web-site
306        pdf = pisa.CreatePDF(data, f)
[dec793e]307
[2296316]308        # close the file here otherwise it will be open until quitting
309        #the application.
310        f.close()
[f0239a3]311        self.Update()
[2296316]312        return not pdf.err
Note: See TracBrowser for help on using the repository browser.