[e8bb5b6] | 1 | """ |
---|
| 2 | Base class for reports. Child classes will need to implement |
---|
| 3 | the onSave() method. |
---|
| 4 | """ |
---|
| 5 | import wx |
---|
| 6 | import logging |
---|
| 7 | import sys |
---|
| 8 | import wx.html as html |
---|
| 9 | |
---|
[463e7ffc] | 10 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 11 | |
---|
[e8bb5b6] | 12 | ISPDF = False |
---|
| 13 | if sys.platform == "win32": |
---|
| 14 | _STATICBOX_WIDTH = 450 |
---|
[69a6897] | 15 | PANEL_WIDTH = 500 |
---|
[e8bb5b6] | 16 | PANEL_HEIGHT = 700 |
---|
| 17 | FONT_VARIANT = 0 |
---|
| 18 | ISPDF = True |
---|
[6dd6e32] | 19 | # For OSX and everything else |
---|
| 20 | else: |
---|
[e8bb5b6] | 21 | _STATICBOX_WIDTH = 480 |
---|
| 22 | PANEL_WIDTH = 530 |
---|
| 23 | PANEL_HEIGHT = 700 |
---|
| 24 | FONT_VARIANT = 1 |
---|
| 25 | ISPDF = True |
---|
| 26 | |
---|
| 27 | class BaseReportDialog(wx.Dialog): |
---|
[69a6897] | 28 | |
---|
[5818dae] | 29 | def __init__(self, report_list, imgRAM, fig_urls, *args, **kwds): |
---|
[e8bb5b6] | 30 | """ |
---|
| 31 | Initialization. The parameters added to Dialog are: |
---|
[69a6897] | 32 | |
---|
[e8bb5b6] | 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 | |
---|
[5818dae] | 39 | #MemoryFSHandle for storing images |
---|
| 40 | self.imgRAM = imgRAM |
---|
| 41 | #Images location in urls |
---|
| 42 | self.fig_urls = fig_urls |
---|
[e8bb5b6] | 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 |
---|
[69a6897] | 54 | if self.is_pdf: # pdf writer is available |
---|
| 55 | self.wild_card = 'PDF files (*.pdf)|*.pdf|' |
---|
[e8bb5b6] | 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) |
---|
[69a6897] | 68 | |
---|
[e8bb5b6] | 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) |
---|
[a8e368b] | 80 | |
---|
[91552b5] | 81 | if sys.platform != "darwin": |
---|
[062c9ea] | 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) |
---|
[69a6897] | 86 | |
---|
[e8bb5b6] | 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) |
---|
[69a6897] | 93 | |
---|
[e8bb5b6] | 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) |
---|
[69a6897] | 109 | |
---|
[e8bb5b6] | 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 | |
---|
[5818dae] | 118 | |
---|
[e8bb5b6] | 119 | def OnClose(self, event=None): |
---|
| 120 | """ |
---|
| 121 | Close the Dialog |
---|
| 122 | : event: Close button event |
---|
| 123 | """ |
---|
[5818dae] | 124 | for fig in self.fig_urls: |
---|
| 125 | self.imgRAM.RemoveFile(fig) |
---|
| 126 | |
---|
[e8bb5b6] | 127 | self.Close() |
---|
[69a6897] | 128 | |
---|
[e8bb5b6] | 129 | def HTML2PDF(self, data, filename): |
---|
| 130 | """ |
---|
| 131 | Create a PDF file from html source string. |
---|
[69a6897] | 132 | Returns True is the file creation was successful. |
---|
[e8bb5b6] | 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 |
---|
[69a6897] | 146 | except Exception: |
---|
[c155a16] | 147 | logger.error("Error creating pdf: %s" % sys.exc_value) |
---|
[e8bb5b6] | 148 | return False |
---|