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

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

Restoring save button for linux as it seems to be working

  • 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        if sys.platform != "darwin":
82            button_save = wx.Button(self, wx.NewId(), "Save")
83            button_save.SetToolTipString("Save this report.")
84            button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId())
85            hbox.Add(button_save)
86
87        # panel for report page
88        vbox = wx.BoxSizer(wx.VERTICAL)
89        # html window
90        self.hwindow = html.HtmlWindow(self, style=wx.BORDER)
91        # set the html page with the report string
92        self.hwindow.SetPage(self.report_html)
93
94        # add panels to boxsizers
95        vbox.Add(hbox)
96        vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0)
97
98        self.SetSizer(vbox)
99        self.Centre()
100        self.Show(True)
101
102    def onPreview(self, event=None):
103        """
104        Preview
105        : event: Preview button event
106        """
107        previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
108        previewh.PreviewText(self.report_html)
109
110    def onPrint(self, event=None):
111        """
112        Print
113        : event: Print button event
114        """
115        printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self)
116        printh.PrintText(self.report_html)
117
118
119    def OnClose(self, event=None):
120        """
121        Close the Dialog
122        : event: Close button event
123        """
124        for fig in self.fig_urls:
125            self.imgRAM.RemoveFile(fig)
126
127        self.Close()
128
129    def HTML2PDF(self, data, filename):
130        """
131        Create a PDF file from html source string.
132        Returns True is the file creation was successful.
133        : data: html string
134        : filename: name of file to be saved
135        """
136        try:
137            from xhtml2pdf import pisa
138            # open output file for writing (truncated binary)
139            resultFile = open(filename, "w+b")
140            # convert HTML to PDF
141            pisaStatus = pisa.CreatePDF(data, dest=resultFile)
142            # close output file
143            resultFile.close()
144            self.Update()
145            return pisaStatus.err
146        except Exception:
147            logger.error("Error creating pdf: %s" % sys.exc_value)
148        return False
Note: See TracBrowser for help on using the repository browser.