source: sasview/src/sas/sasgui/guiframe/report_dialog.py @ 5818dae

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249unittest-saveload
Last change on this file since 5818dae was 5818dae, checked in by wojciech, 6 years ago

Reverted save button as potential solution to the problem has been found

  • 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
10logger = logging.getLogger(__name__)
11
12ISPDF = False
13if sys.platform == "win32":
14    _STATICBOX_WIDTH = 450
15    PANEL_WIDTH = 500
16    PANEL_HEIGHT = 700
17    FONT_VARIANT = 0
18    ISPDF = True
19# For OSX and everything else
20else:
21    _STATICBOX_WIDTH = 480
22    PANEL_WIDTH = 530
23    PANEL_HEIGHT = 700
24    FONT_VARIANT = 1
25    ISPDF = True
26
27class BaseReportDialog(wx.Dialog):
28
29    def __init__(self, report_list, imgRAM, fig_urls, *args, **kwds):
30        """
31        Initialization. The parameters added to Dialog are:
32
33        :param report_list: list of html_str, text_str, image for report
34        """
35        kwds["style"] = wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE
36        super(BaseReportDialog, self).__init__(*args, **kwds)
37        kwds["image"] = 'Dynamic Image'
38
39        #MemoryFSHandle for storing images
40        self.imgRAM = imgRAM
41        #Images location in urls
42        self.fig_urls = fig_urls
43        # title
44        self.SetTitle("Report")
45        # size
46        self.SetSize((720, 650))
47        # font size
48        self.SetWindowVariant(variant=FONT_VARIANT)
49        # check if tit is MAC
50        self.is_pdf = ISPDF
51        # report string
52        self.report_list = report_list
53        # wild card
54        if self.is_pdf:  # pdf writer is available
55            self.wild_card = 'PDF files (*.pdf)|*.pdf|'
56            self.index_offset = 0
57        else:
58            self.wild_card = ''
59            self.index_offset = 1
60        self.wild_card += 'HTML files (*.html)|*.html|'
61        self.wild_card += 'Text files (*.txt)|*.txt'
62
63    def _setup_layout(self):
64        """
65        Set up layout
66        """
67        hbox = wx.BoxSizer(wx.HORIZONTAL)
68
69        # buttons
70        button_close = wx.Button(self, wx.ID_OK, "Close")
71        button_close.SetToolTipString("Close this report window.")
72        hbox.Add(button_close)
73        button_close.SetFocus()
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
118    def OnClose(self, event=None):
119        """
120        Close the Dialog
121        : event: Close button event
122        """
123        for fig in self.fig_urls:
124            self.imgRAM.RemoveFile(fig)
125
126        self.Close()
127
128    def HTML2PDF(self, data, filename):
129        """
130        Create a PDF file from html source string.
131        Returns True is the file creation was successful.
132        : data: html string
133        : filename: name of file to be saved
134        """
135        try:
136            from xhtml2pdf import pisa
137            # open output file for writing (truncated binary)
138            resultFile = open(filename, "w+b")
139            # convert HTML to PDF
140            pisaStatus = pisa.CreatePDF(data, dest=resultFile)
141            # close output file
142            resultFile.close()
143            self.Update()
144            return pisaStatus.err
145        except Exception:
146            logger.error("Error creating pdf: %s" % sys.exc_value)
147        return False
Note: See TracBrowser for help on using the repository browser.