source: sasview/src/sas/guiframe/report_dialog.py @ 0ea31ca

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 0ea31ca was e8bb5b6, checked in by Mathieu Doucet <doucetm@…>, 10 years ago

Re #336 Move common report code to guiframe.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1"""
2    Base class for reports. Child classes will need to implement
3    the onSave() method.
4"""
5import wx
6import logging
7import sys
8import wx.html as html
9
10ISPDF = False
11if sys.platform == "win32":
12    _STATICBOX_WIDTH = 450
13    PANEL_WIDTH = 500 
14    PANEL_HEIGHT = 700
15    FONT_VARIANT = 0
16    ISPDF = True
17elif sys.platform == "darwin":
18    _STATICBOX_WIDTH = 480
19    PANEL_WIDTH = 530
20    PANEL_HEIGHT = 700
21    FONT_VARIANT = 1
22    ISPDF = True
23
24class BaseReportDialog(wx.Dialog):
25   
26    def __init__(self, report_list, *args, **kwds):
27        """
28        Initialization. The parameters added to Dialog are:
29       
30        :param report_list: list of html_str, text_str, image for report
31        """
32        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
33        super(BaseReportDialog, self).__init__(*args, **kwds)
34        kwds["image"] = 'Dynamic Image'
35
36        # title
37        self.SetTitle("Report")
38        # size
39        self.SetSize((720, 650))
40        # font size
41        self.SetWindowVariant(variant=FONT_VARIANT)
42        # check if tit is MAC
43        self.is_pdf = ISPDF
44        # report string
45        self.report_list = report_list
46        # wild card
47        # pdf supporting only on MAC
48        if self.is_pdf:
49            self.wild_card = ' PDF files (*.pdf)|*.pdf|'
50            self.index_offset = 0
51        else:
52            self.wild_card = ''
53            self.index_offset = 1
54        self.wild_card += 'HTML files (*.html)|*.html|'
55        self.wild_card += 'Text files (*.txt)|*.txt'
56
57    def _setup_layout(self):
58        """
59        Set up layout
60        """
61        hbox = wx.BoxSizer(wx.HORIZONTAL)
62       
63        # buttons
64        button_close = wx.Button(self, wx.ID_OK, "Close")
65        button_close.SetToolTipString("Close this report window.")
66        hbox.Add(button_close)
67        button_close.SetFocus()
68
69        button_preview = wx.Button(self, wx.NewId(), "Preview")
70        button_preview.SetToolTipString("Print preview this report.")
71        button_preview.Bind(wx.EVT_BUTTON, self.onPreview,
72                            id=button_preview.GetId()) 
73        hbox.Add(button_preview)
74
75        button_print = wx.Button(self, wx.NewId(), "Print")
76        button_print.SetToolTipString("Print this report.")
77        button_print.Bind(wx.EVT_BUTTON, self.onPrint,
78                          id=button_print.GetId())
79        hbox.Add(button_print)
80       
81        button_save = wx.Button(self, wx.NewId(), "Save")
82        button_save.SetToolTipString("Save this report.")
83        button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId())
84        hbox.Add(button_save)
85       
86        # panel for report page
87        vbox = wx.BoxSizer(wx.VERTICAL)
88        # html window
89        self.hwindow = html.HtmlWindow(self, style=wx.BORDER)
90        # set the html page with the report string
91        self.hwindow.SetPage(self.report_html)
92       
93        # add panels to boxsizers
94        vbox.Add(hbox)
95        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0)
96
97        self.SetSizer(vbox)
98        self.Centre()
99        self.Show(True)
100
101    def onPreview(self, event=None):
102        """
103        Preview
104        : event: Preview button event
105        """
106        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
107        previewh.PreviewText(self.report_html)
108       
109    def onPrint(self, event=None):
110        """
111        Print
112        : event: Print button event
113        """
114        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
115        printh.PrintText(self.report_html)
116
117    def OnClose(self, event=None):
118        """
119        Close the Dialog
120        : event: Close button event
121        """
122        self.Close()
123   
124    def HTML2PDF(self, data, filename):
125        """
126        Create a PDF file from html source string.
127        Returns True is the file creation was successful.
128        : data: html string
129        : filename: name of file to be saved
130        """
131        try:
132            from xhtml2pdf import pisa
133            # open output file for writing (truncated binary)
134            resultFile = open(filename, "w+b")
135            # convert HTML to PDF
136            pisaStatus = pisa.CreatePDF(data, dest=resultFile)
137            # close output file
138            resultFile.close()
139            self.Update()
140            return pisaStatus.err
141        except:
142            logging.error("Error creating pdf: %s" % sys.exc_value)
143        return False
144
Note: See TracBrowser for help on using the repository browser.