Changeset e8bb5b6 in sasview


Ignore:
Timestamp:
Feb 18, 2015 1:20:54 PM (9 years ago)
Author:
Mathieu Doucet <doucetm@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
66f21cd
Parents:
054a3ad
Message:

Re #336 Move common report code to guiframe.

Location:
src/sas
Files:
1 added
2 edited

Legend:

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

    r4ec242e re8bb5b6  
    1414 
    1515import wx 
    16 import sys 
    1716import os 
    1817import wx.html as html 
    19 import logging 
    2018 
    21 _STATICBOX_WIDTH = 480 
    22 PANEL_WIDTH = 530 
    23 PANEL_HEIGHT = 700 
    24 FONT_VARIANT = 1 
    25 ISMAC = False 
    26 ISPDF = False 
    27 if 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 
    34 elif sys.platform == "darwin": 
    35     ISMAC = True 
    36     ISPDF = True 
     19from sas.guiframe.report_dialog import BaseReportDialog 
    3720 
    38          
    39 class ReportDialog(wx.Dialog): 
     21class ReportDialog(BaseReportDialog): 
    4022    """ 
    4123    The report dialog box. 
    4224    """ 
    4325     
    44     def __init__(self, list, *args, **kwds): 
     26    def __init__(self, report_list, *args, **kwds): 
    4527        """ 
    4628        Initialization. The parameters added to Dialog are: 
    4729         
    48         :param list: report_list (list of html_str, text_str, image) 
     30        :param report_list: list of html_str, text_str, image 
    4931        from invariant_state 
    5032        """ 
    51         kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE 
    52         wx.Dialog.__init__(self, *args, **kwds) 
    53         kwds["image"] = 'Dynamic Image' 
     33        super(ReportDialog, self).__init__(report_list, *args, **kwds) 
     34 
    5435        # title 
    5536        self.SetTitle("Report: Fitting") 
    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 
     37 
    6438        # number of images of plot 
    65         self.nimages = len(list[2]) 
     39        self.nimages = len(self.report_list[2]) 
    6640         
    67         if list[2] != None: 
     41        if self.report_list[2] != None: 
    6842            # put image path in the report string 
    69             if len(list[2]) == 1: 
     43            if len(self.report_list[2]) == 1: 
    7044                self.report_html = self.report_list[0] % \ 
    7145                                    "memory:img_fit0.png" 
    72             elif len(list[2]) == 2: 
     46            elif len(self.report_list) == 2: 
    7347                self.report_html = self.report_list[0] % \ 
    7448                                    ("memory:img_fit0.png", 
     
    8559        self._setup_layout() 
    8660         
    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  
    13761    def onSave(self, event=None): 
    13862        """ 
    13963        Save 
    14064        """ 
    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  
    15165        #todo: complete saving fig file and as a txt file 
    15266        dlg = wx.FileDialog(self, "Choose a file", 
    153                             wildcard=wild_card, 
     67                            wildcard=self.wild_card, 
    15468                            style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR) 
    15569        dlg.SetFilterIndex(0)  # Set .html files to be default 
     
    16680        pic_fname = [] 
    16781        #PDF 
    168         if ext_num == (0 + 2 * ind_cor): 
     82        if ext_num == (0 + 2 * self.index_offset): 
    16983            # TODO: Sort this case out 
    17084            ext = '.pdf' 
     
    216130            return 
    217131        #HTML + png(graph) 
    218         elif ext_num == (1 - ind_cor): 
     132        elif ext_num == (1 - self.index_offset): 
    219133            ext = '.html' 
    220134            for num in range(self.nimages): 
     
    222136            report_frame = self.report_list[0] 
    223137        #TEXT + pdf(graph) 
    224         elif ext_num == (2 - ind_cor): 
     138        elif ext_num == (2 - self.index_offset): 
    225139            ext = '.txt' 
    226140            # changing the image extension actually changes the image 
     
    241155            pic_fname.append(pic_name) 
    242156        #put the image path in html string 
    243         if ext_num == (1 - ind_cor): 
     157        if ext_num == (1 - self.index_offset): 
    244158            if self.nimages == 1: 
    245159                report = report_frame % os.path.basename(pic_fname[0]) 
     
    259173            self.report_list[2][num].savefig(pic_fname[num]) 
    260174         
    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 
  • src/sas/perspectives/invariant/report_dialog.py

    r4ec242e re8bb5b6  
    1515""" 
    1616import wx 
    17 import sys 
    1817import os 
    1918import wx.html as html 
    20 import logging 
    21 ISPDF = False 
    22 if sys.platform == "win32": 
    23     _STATICBOX_WIDTH = 450 
    24     PANEL_WIDTH = 500  
    25     PANEL_HEIGHT = 700 
    26     FONT_VARIANT = 0 
    27     ISMAC = False 
    28     ISPDF = True 
    29 elif sys.platform == "darwin": 
    30     _STATICBOX_WIDTH = 480 
    31     PANEL_WIDTH = 530 
    32     PANEL_HEIGHT = 700 
    33     FONT_VARIANT = 1 
    34     ISMAC = True 
    35     ISPDF = True 
    36    
    37 class ReportDialog(wx.Dialog): 
     19  
     20from sas.guiframe.report_dialog import BaseReportDialog 
     21 
     22class ReportDialog(BaseReportDialog): 
    3823    """ 
    3924    The report dialog box.  
    4025    """ 
    4126     
    42     def __init__(self,  list, *args, **kwds): 
     27    def __init__(self,  report_list, *args, **kwds): 
    4328        """ 
    4429        Initialization. The parameters added to Dialog are: 
    4530         
    46         :param list: report_list (list of html_str, text_str, image) 
     31        :param report_list: list of html_str, text_str, image 
    4732        from invariant_state 
    4833        """ 
    49         kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE 
    50         wx.Dialog.__init__(self, *args, **kwds) 
    51         kwds["image"] = 'Dynamic Image' 
     34        super(ReportDialog, self).__init__(report_list, *args, **kwds) 
     35 
    5236        # title 
    5337        self.SetTitle("Report: Invariant computaion") 
    54         # size 
    55         self.SetSize((720, 650)) 
    56         # font size  
    57         self.SetWindowVariant(variant=FONT_VARIANT) 
    5838 
    59         # check if tit is MAC 
    60         self.is_pdf = ISPDF 
    61          
    62         # report string 
    63         self.report_list = list 
    6439        # put image path in the report string 
    6540        self.report_html = self.report_list[0] % "memory:img_inv.png" 
    6641        # layout 
    6742        self._setup_layout() 
    68         # wild card 
    69         # pdf supporting only on MAC 
    70         if self.is_pdf: 
    71                 self.wild_card = ' PDF files (*.pdf)|*.pdf|' 
    72         else: 
    73                 self.wild_card = '' 
    74         self.wild_card += 'HTML files (*.html)|*.html|' 
    75         self.wild_card += 'Text files (*.txt)|*.txt' 
    76    
    77          
    78          
    79     def _setup_layout(self): 
    80         """ 
    81         Set up layout 
    82         """ 
    83         hbox = wx.BoxSizer(wx.HORIZONTAL) 
    84          
    85         # buttons 
    86         id = wx.ID_OK 
    87         button_close = wx.Button(self, id, "Close") 
    88         button_close.SetToolTipString("Close this report window.")  
    89         #hbox.Add((5,10), 1 , wx.EXPAND|wx.ADJUST_MINSIZE,0) 
    90         hbox.Add(button_close) 
    91         button_close.SetFocus() 
    92  
    93         id = wx.NewId() 
    94         button_preview = wx.Button(self, id, "Preview") 
    95         button_preview.SetToolTipString("Print preview this report.") 
    96         button_preview.Bind(wx.EVT_BUTTON, self.onPreview, 
    97                             id=button_preview.GetId())  
    98         hbox.Add(button_preview) 
    99  
    100         id = wx.NewId() 
    101         button_print = wx.Button(self, id, "Print") 
    102         button_print.SetToolTipString("Print this report.") 
    103         button_print.Bind(wx.EVT_BUTTON, self.onPrint, 
    104                           id=button_print.GetId())  
    105         hbox.Add(button_print) 
    106          
    107         id = wx.NewId() 
    108         button_save = wx.Button(self, id, "Save" ) 
    109         button_save.SetToolTipString("Save this report.") 
    110         button_save.Bind(wx.EVT_BUTTON, self.onSave, id = button_save.GetId())  
    111         hbox.Add(button_save)      
    112          
    113         # panel for report page 
    114         #panel = wx.Panel(self, -1) 
    115         vbox = wx.BoxSizer(wx.VERTICAL) 
    116         # html window 
    117         self.hwindow = html.HtmlWindow(self,style=wx.BORDER) 
    118         # set the html page with the report string 
    119         self.hwindow.SetPage(self.report_html) 
    120          
    121         # add panels to boxsizers 
    122         vbox.Add(hbox) 
    123         vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) 
    124  
    125         self.SetSizer(vbox) 
    126         self.Centre() 
    127         self.Show(True) 
    128                  
    12943 
    13044    def onSave(self, event=None): 
     
    14458        fName = dlg.GetPath() 
    14559        ext_num = dlg.GetFilterIndex()   
    146         # index correction  
    147         if not self.is_pdf: 
    148                 ind_cor = 1  
    149         else: 
    150                 ind_cor = 0  
    15160        #set file extensions   
    152         if ext_num == (0 + 2 * ind_cor): 
    153                         # TODO: Sort this case out 
     61        if ext_num == (0 + 2 * self.index_offset): 
     62            # TODO: Sort this case out 
    15463            ext = '.pdf' 
    15564            img_ext = '_img.png' 
     
    18695            os.remove(pic_fname) 
    18796            return 
    188         elif ext_num == (1 - ind_cor): 
     97        elif ext_num == (1 - self.index_offset): 
    18998            ext = '.html' 
    19099            img_ext = '_img4html.png' 
    191100            report_frame = self.report_list[0] 
    192         elif ext_num == (2 - ind_cor): 
     101        elif ext_num == (2 - self.index_offset): 
    193102            ext = '.txt'    
    194103            # changing the image extension actually changes the image 
     
    205114        pic_fname = os.path.splitext(fName)[0] + img_ext 
    206115        #put the image path in html string 
    207         if ext_num == (1 - ind_cor): 
     116        if ext_num == (1 - self.index_offset): 
    208117            report = report_frame % os.path.basename(pic_fname) 
    209118 
     
    214123        self.report_list[2].savefig(pic_fname) 
    215124         
    216              
    217     def onPreview(self, event=None): 
    218         """ 
    219         Preview 
    220          
    221         : event: Preview button event 
    222         """ 
    223         previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) 
    224         previewh.PreviewText(self.report_html) 
    225         if event is not None: 
    226             event.Skip() 
    227      
    228     def onPrint(self, event=None): 
    229         """ 
    230         Print 
    231          
    232         : event: Print button event 
    233         """ 
    234         printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) 
    235         printh.PrintText(self.report_html) 
    236         if event is not None: 
    237             event.Skip() 
    238  
    239     def OnClose(self,event=None): 
    240         """ 
    241         Close the Dialog 
    242          
    243         : event: Close button event 
    244         """ 
    245         self.Close() 
    246      
    247     def HTML2PDF(self, data, filename): 
    248         """ 
    249         Create a PDF file from html source string. 
    250         Returns True is the file creation was successful.  
    251          
    252         : data: html string 
    253         : filename: name of file to be saved 
    254         """ 
    255         try: 
    256             from xhtml2pdf import pisa 
    257             # open output file for writing (truncated binary) 
    258             resultFile = open(filename, "w+b") 
    259             # convert HTML to PDF 
    260             pisaStatus = pisa.CreatePDF(data, dest=resultFile) 
    261             # close output file 
    262             resultFile.close() 
    263             self.Update() 
    264             return pisaStatus.err 
    265         except: 
    266             logging.error("Error creating pdf: %s" % sys.exc_value) 
    267         return False 
Note: See TracChangeset for help on using the changeset viewer.