[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 |
---|
[ee4a2bb] | 24 | FONT_VARIANT = 1 |
---|
[e8bb5b6] | 25 | ISPDF = True |
---|
| 26 | |
---|
| 27 | class BaseReportDialog(wx.Dialog): |
---|
[69a6897] | 28 | |
---|
[e8bb5b6] | 29 | def __init__(self, report_list, *args, **kwds): |
---|
| 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 | |
---|
| 39 | # title |
---|
| 40 | self.SetTitle("Report") |
---|
| 41 | # size |
---|
| 42 | self.SetSize((720, 650)) |
---|
| 43 | # font size |
---|
| 44 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 45 | # check if tit is MAC |
---|
| 46 | self.is_pdf = ISPDF |
---|
| 47 | # report string |
---|
| 48 | self.report_list = report_list |
---|
| 49 | # wild card |
---|
[69a6897] | 50 | if self.is_pdf: # pdf writer is available |
---|
| 51 | self.wild_card = 'PDF files (*.pdf)|*.pdf|' |
---|
[e8bb5b6] | 52 | self.index_offset = 0 |
---|
| 53 | else: |
---|
| 54 | self.wild_card = '' |
---|
| 55 | self.index_offset = 1 |
---|
| 56 | self.wild_card += 'HTML files (*.html)|*.html|' |
---|
| 57 | self.wild_card += 'Text files (*.txt)|*.txt' |
---|
| 58 | |
---|
| 59 | def _setup_layout(self): |
---|
| 60 | """ |
---|
| 61 | Set up layout |
---|
| 62 | """ |
---|
| 63 | hbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
[69a6897] | 64 | |
---|
[e8bb5b6] | 65 | # buttons |
---|
| 66 | button_close = wx.Button(self, wx.ID_OK, "Close") |
---|
| 67 | button_close.SetToolTipString("Close this report window.") |
---|
| 68 | hbox.Add(button_close) |
---|
| 69 | button_close.SetFocus() |
---|
| 70 | |
---|
| 71 | button_print = wx.Button(self, wx.NewId(), "Print") |
---|
| 72 | button_print.SetToolTipString("Print this report.") |
---|
| 73 | button_print.Bind(wx.EVT_BUTTON, self.onPrint, |
---|
| 74 | id=button_print.GetId()) |
---|
| 75 | hbox.Add(button_print) |
---|
[69a6897] | 76 | |
---|
[e8bb5b6] | 77 | button_save = wx.Button(self, wx.NewId(), "Save") |
---|
| 78 | button_save.SetToolTipString("Save this report.") |
---|
| 79 | button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) |
---|
| 80 | hbox.Add(button_save) |
---|
[69a6897] | 81 | |
---|
[e8bb5b6] | 82 | # panel for report page |
---|
| 83 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 84 | # html window |
---|
| 85 | self.hwindow = html.HtmlWindow(self, style=wx.BORDER) |
---|
| 86 | # set the html page with the report string |
---|
| 87 | self.hwindow.SetPage(self.report_html) |
---|
[69a6897] | 88 | |
---|
[e8bb5b6] | 89 | # add panels to boxsizers |
---|
| 90 | vbox.Add(hbox) |
---|
| 91 | vbox.Add(self.hwindow, 1, wx.EXPAND|wx.ALL,0) |
---|
| 92 | |
---|
| 93 | self.SetSizer(vbox) |
---|
| 94 | self.Centre() |
---|
| 95 | self.Show(True) |
---|
| 96 | |
---|
| 97 | def onPreview(self, event=None): |
---|
| 98 | """ |
---|
| 99 | Preview |
---|
| 100 | : event: Preview button event |
---|
| 101 | """ |
---|
| 102 | previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 103 | previewh.PreviewText(self.report_html) |
---|
[69a6897] | 104 | |
---|
[e8bb5b6] | 105 | def onPrint(self, event=None): |
---|
| 106 | """ |
---|
| 107 | Print |
---|
| 108 | : event: Print button event |
---|
| 109 | """ |
---|
| 110 | printh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) |
---|
| 111 | printh.PrintText(self.report_html) |
---|
| 112 | |
---|
| 113 | def OnClose(self, event=None): |
---|
| 114 | """ |
---|
| 115 | Close the Dialog |
---|
| 116 | : event: Close button event |
---|
| 117 | """ |
---|
| 118 | self.Close() |
---|
[69a6897] | 119 | |
---|
[e8bb5b6] | 120 | def HTML2PDF(self, data, filename): |
---|
| 121 | """ |
---|
| 122 | Create a PDF file from html source string. |
---|
[69a6897] | 123 | Returns True is the file creation was successful. |
---|
[e8bb5b6] | 124 | : data: html string |
---|
| 125 | : filename: name of file to be saved |
---|
| 126 | """ |
---|
[ee4a2bb] | 127 | #try: |
---|
| 128 | from xhtml2pdf import pisa |
---|
| 129 | # open output file for writing (truncated binary) |
---|
| 130 | resultFile = open(filename, "w+b") |
---|
| 131 | # convert HTML to PDF |
---|
[a16cbb0] | 132 | data = '<html><head><meta http-equiv=Content-Type content=\'text/html; charset=windows-1252\'><meta name=Generator ></head><body lang=EN-US>' \ |
---|
| 133 | '<div class=WordSection1><p class=MsoNormal><b><span ><center><font size=\'4\' >cyl_400_40.txt [Feb 26 2018 12:04:42]</font></center></span></center></b></p><p class=MsoNormal> </p>' \ |
---|
| 134 | '<p class=MsoNormal><center><font size=\'4\' > File name:/Volumes/SasView-4.2.0-MacOSX/SasView 4.2.0.app/Contents/Resources/test/1d_data/cyl_400_40.txt</font></center></p>' \ |
---|
| 135 | '<p class=MsoNormal><center><font size=\'4\' > Model name:cylinder </font></center></p>' \ |
---|
| 136 | '<p class=MsoNormal><center><font size=\'4\' > Q Range: min = 0.00925926, max = 0.5 </font></center></p>' \ |
---|
| 137 | '<p class=MsoNormal><center><font size=\'4\' > Chi2/Npts = 21178 </font></center></p><p class=MsoNormal> </p>' \ |
---|
| 138 | '<p class=MsoNormal><center><font size=\'4\' > scale = 1 +- (fixed)</font></center></p><p class=MsoNormal><center><font size=\'4\' > background = 0.001 +- (fixed) 1/cm </font>' \ |
---|
| 139 | '</center></p><p class=MsoNormal><center><font size=\'4\' > sld = 4 +- (fixed) 1e-6/Ang^2</font></center></p><p class=MsoNormal><center><font size=\'4\' > sld_solvent = 1 +- (fixed) 1e-6/Ang^2' \ |
---|
| 140 | '</font></center></p><p class=MsoNormal><center><font size=\'4\' > radius = 20 +- (fixed) Ang </font></center></p><p class=MsoNormal><center><font size=\'4\' > length = 400 +- (fixed) Ang</font></center></p>' \ |
---|
| 141 | '<p class=MsoNormal> </p><p class=MsoNormal> </p><br><p class=MsoNormal><b><span ><center> <font size=\'4\' > Graph</font></span></center></b></p>' \ |
---|
| 142 | '<p class=MsoNormal> </p><center><br><font size=\'4\' >Model Computation</font><br><font size=\'4\' >Data: "cyl_400_40.txt [Feb 26 2018 12:04:42]"</font><br>' \ |
---|
| 143 | '<img src="/Users/wojciechpotrzebowski/Desktop/sasview_reporting2_img0.png" ></img><p class=MsoNormal> </p><img src="/Users/wojciechpotrzebowski/Desktop/sasview_reporting2_img1.png" ></img>' |
---|
[ee4a2bb] | 144 | pisaStatus = pisa.CreatePDF(data, dest=resultFile) |
---|
| 145 | # close output file |
---|
| 146 | resultFile.close() |
---|
| 147 | self.Update() |
---|
| 148 | # return pisaStatus.err |
---|
| 149 | #except Exception: |
---|
| 150 | # logger.error("Error creating pdf: %s" % sys.exc_value) |
---|
| 151 | logger.error("Error creating pdf: %s" % pisaStatus.err) |
---|
[92f586e9] | 152 | return False |
---|