Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/perspectives/fitting/report_dialog.py

    re8bb5b6 r4ec242e  
    1414 
    1515import wx 
     16import sys 
    1617import os 
    1718import wx.html as html 
    18  
    19 from sas.guiframe.report_dialog import BaseReportDialog 
    20  
    21 class ReportDialog(BaseReportDialog): 
     19import logging 
     20 
     21_STATICBOX_WIDTH = 480 
     22PANEL_WIDTH = 530 
     23PANEL_HEIGHT = 700 
     24FONT_VARIANT = 1 
     25ISMAC = False 
     26ISPDF = False 
     27if sys.platform == "win32": 
     28    _STATICBOX_WIDTH = 450 
     29    PANEL_WIDTH = 500 
     30    PANEL_HEIGHT = 700 
     31    FONT_VARIANT = 0 
     32    ISMAC = False 
     33    ISPDF = True 
     34elif sys.platform == "darwin": 
     35    ISMAC = True 
     36    ISPDF = True 
     37 
     38         
     39class ReportDialog(wx.Dialog): 
    2240    """ 
    2341    The report dialog box. 
    2442    """ 
    2543     
    26     def __init__(self, report_list, *args, **kwds): 
     44    def __init__(self, list, *args, **kwds): 
    2745        """ 
    2846        Initialization. The parameters added to Dialog are: 
    2947         
    30         :param report_list: list of html_str, text_str, image 
     48        :param list: report_list (list of html_str, text_str, image) 
    3149        from invariant_state 
    3250        """ 
    33         super(ReportDialog, self).__init__(report_list, *args, **kwds) 
    34  
     51        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE 
     52        wx.Dialog.__init__(self, *args, **kwds) 
     53        kwds["image"] = 'Dynamic Image' 
    3554        # title 
    3655        self.SetTitle("Report: Fitting") 
    37  
     56        # size 
     57        self.SetSize((720, 650)) 
     58        # font size 
     59        self.SetWindowVariant(variant=FONT_VARIANT) 
     60        # check if tit is MAC 
     61        self.is_pdf = ISPDF 
     62        # report string 
     63        self.report_list = list 
    3864        # number of images of plot 
    39         self.nimages = len(self.report_list[2]) 
    40          
    41         if self.report_list[2] != None: 
     65        self.nimages = len(list[2]) 
     66         
     67        if list[2] != None: 
    4268            # put image path in the report string 
    43             if len(self.report_list[2]) == 1: 
     69            if len(list[2]) == 1: 
    4470                self.report_html = self.report_list[0] % \ 
    4571                                    "memory:img_fit0.png" 
    46             elif len(self.report_list) == 2: 
     72            elif len(list[2]) == 2: 
    4773                self.report_html = self.report_list[0] % \ 
    4874                                    ("memory:img_fit0.png", 
     
    5985        self._setup_layout() 
    6086         
     87    def _setup_layout(self): 
     88        """ 
     89        Set up layout 
     90        """ 
     91        hbox = wx.BoxSizer(wx.HORIZONTAL) 
     92         
     93        # buttons 
     94        id = wx.ID_OK 
     95        button_close = wx.Button(self, id, "Close") 
     96        button_close.SetToolTipString("Close this report window.") 
     97        #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) 
     98        hbox.Add(button_close) 
     99        button_close.SetFocus() 
     100 
     101        id = wx.NewId() 
     102        button_preview = wx.Button(self, id, "Preview") 
     103        button_preview.SetToolTipString("Print preview this report.") 
     104        button_preview.Bind(wx.EVT_BUTTON, self.onPreview, 
     105                            id=button_preview.GetId())  
     106        hbox.Add(button_preview) 
     107 
     108        id = wx.NewId() 
     109        button_print = wx.Button(self, id, "Print") 
     110        button_print.SetToolTipString("Print this report.") 
     111        button_print.Bind(wx.EVT_BUTTON, self.onPrint, 
     112                          id=button_print.GetId()) 
     113        hbox.Add(button_print) 
     114         
     115        id = wx.NewId() 
     116        button_save = wx.Button(self, id, "Save") 
     117        button_save.SetToolTipString("Save this report.") 
     118        button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) 
     119        hbox.Add(button_save) 
     120         
     121        # panel for report page 
     122        #panel = wx.Panel(self, -1) 
     123        vbox = wx.BoxSizer(wx.VERTICAL) 
     124        # html window 
     125        self.hwindow = html.HtmlWindow(self, style=wx.BORDER) 
     126        # set the html page with the report string 
     127        self.hwindow.SetPage(self.report_html) 
     128         
     129        # add panels to boxsizers 
     130        vbox.Add(hbox) 
     131        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) 
     132 
     133        self.SetSizer(vbox) 
     134        self.Centre() 
     135        self.Show(True) 
     136 
    61137    def onSave(self, event=None): 
    62138        """ 
    63139        Save 
    64140        """ 
     141        # pdf supporting only on MAC, not on exe 
     142        if self.is_pdf: 
     143            wild_card = ' PDF files (*.pdf)|*.pdf|' 
     144            ind_cor = 0 
     145        else: 
     146            wild_card = '' 
     147            ind_cor = 1 
     148        wild_card += 'HTML files (*.html)|*.html|' 
     149        wild_card += 'Text files (*.txt)|*.txt' 
     150 
    65151        #todo: complete saving fig file and as a txt file 
    66152        dlg = wx.FileDialog(self, "Choose a file", 
    67                             wildcard=self.wild_card, 
     153                            wildcard=wild_card, 
    68154                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) 
    69155        dlg.SetFilterIndex(0)  # Set .html files to be default 
     
    80166        pic_fname = [] 
    81167        #PDF 
    82         if ext_num == (0 + 2 * self.index_offset): 
     168        if ext_num == (0 + 2 * ind_cor): 
    83169            # TODO: Sort this case out 
    84170            ext = '.pdf' 
     
    130216            return 
    131217        #HTML + png(graph) 
    132         elif ext_num == (1 - self.index_offset): 
     218        elif ext_num == (1 - ind_cor): 
    133219            ext = '.html' 
    134220            for num in range(self.nimages): 
     
    136222            report_frame = self.report_list[0] 
    137223        #TEXT + pdf(graph) 
    138         elif ext_num == (2 - self.index_offset): 
     224        elif ext_num == (2 - ind_cor): 
    139225            ext = '.txt' 
    140226            # changing the image extension actually changes the image 
     
    155241            pic_fname.append(pic_name) 
    156242        #put the image path in html string 
    157         if ext_num == (1 - self.index_offset): 
     243        if ext_num == (1 - ind_cor): 
    158244            if self.nimages == 1: 
    159245                report = report_frame % os.path.basename(pic_fname[0]) 
     
    173259            self.report_list[2][num].savefig(pic_fname[num]) 
    174260         
     261    def onPreview(self, event=None): 
     262        """ 
     263        Preview 
     264         
     265        : event: Preview button event 
     266        """ 
     267        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) 
     268        previewh.PreviewText(self.report_html) 
     269        if event is not None: 
     270            event.Skip() 
     271        self.Update() 
     272     
     273    def onPrint(self, event=None): 
     274        """ 
     275        Print 
     276         
     277        : event: Print button event 
     278        """ 
     279        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) 
     280        printh.PrintText(self.report_html) 
     281        if event is not None: 
     282            event.Skip() 
     283        self.Update() 
     284         
     285    def OnClose(self, event=None): 
     286        """ 
     287        Close the Dialog 
     288         
     289        : event: Close button event 
     290        """ 
     291        self.Close() 
     292        # Reset memory 
     293        #wx.MemoryFSHandler() 
     294        if event is not None: 
     295            event.Skip() 
     296     
     297    def HTML2PDF(self, data, filename): 
     298        """ 
     299        Create a PDF file from html source string. 
     300        Returns True is the file creation was successful.  
     301         
     302        : data: html string 
     303        : filename: name of file to be saved 
     304        """ 
     305        try: 
     306            from xhtml2pdf import pisa 
     307            # open output file for writing (truncated binary) 
     308            resultFile = open(filename, "w+b") 
     309            # convert HTML to PDF 
     310            pisaStatus = pisa.CreatePDF(data, dest=resultFile) 
     311            # close output file 
     312            resultFile.close() 
     313            self.Update() 
     314            return pisaStatus.err 
     315        except: 
     316            logging.error("Error creating pdf: %s" % sys.exc_value) 
     317        return False 
Note: See TracChangeset for help on using the changeset viewer.