Changeset 69a6897 in sasview for src/sas/sasgui
- Timestamp:
- Sep 26, 2017 5:18:01 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 9be22cd, fca1f50
- Parents:
- 24d9e84
- Location:
- src/sas/sasgui
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/guiframe/report_dialog.py
r463e7ffc r69a6897 13 13 if sys.platform == "win32": 14 14 _STATICBOX_WIDTH = 450 15 PANEL_WIDTH = 500 15 PANEL_WIDTH = 500 16 16 PANEL_HEIGHT = 700 17 17 FONT_VARIANT = 0 … … 26 26 27 27 class BaseReportDialog(wx.Dialog): 28 28 29 29 def __init__(self, report_list, *args, **kwds): 30 30 """ 31 31 Initialization. The parameters added to Dialog are: 32 32 33 33 :param report_list: list of html_str, text_str, image for report 34 34 """ … … 48 48 self.report_list = report_list 49 49 # wild card 50 # pdf supporting only on MAC 51 if self.is_pdf: 52 self.wild_card = ' PDF files (*.pdf)|*.pdf|' 50 if self.is_pdf: # pdf writer is available 51 self.wild_card = 'PDF files (*.pdf)|*.pdf|' 53 52 self.index_offset = 0 54 53 else: … … 63 62 """ 64 63 hbox = wx.BoxSizer(wx.HORIZONTAL) 65 64 66 65 # buttons 67 66 button_close = wx.Button(self, wx.ID_OK, "Close") … … 75 74 id=button_print.GetId()) 76 75 hbox.Add(button_print) 77 76 78 77 button_save = wx.Button(self, wx.NewId(), "Save") 79 78 button_save.SetToolTipString("Save this report.") 80 79 button_save.Bind(wx.EVT_BUTTON, self.onSave, id=button_save.GetId()) 81 80 hbox.Add(button_save) 82 81 83 82 # panel for report page 84 83 vbox = wx.BoxSizer(wx.VERTICAL) … … 87 86 # set the html page with the report string 88 87 self.hwindow.SetPage(self.report_html) 89 88 90 89 # add panels to boxsizers 91 90 vbox.Add(hbox) … … 103 102 previewh = html.HtmlEasyPrinting(name="Printing", parentWindow=self) 104 103 previewh.PreviewText(self.report_html) 105 104 106 105 def onPrint(self, event=None): 107 106 """ … … 118 117 """ 119 118 self.Close() 120 119 121 120 def HTML2PDF(self, data, filename): 122 121 """ 123 122 Create a PDF file from html source string. 124 Returns True is the file creation was successful. 123 Returns True is the file creation was successful. 125 124 : data: html string 126 125 : filename: name of file to be saved … … 136 135 self.Update() 137 136 return pisaStatus.err 138 except :137 except Exception: 139 138 logger.error("Error creating pdf: %s" % sys.exc_value) 140 139 return False 141 -
src/sas/sasgui/perspectives/fitting/report_dialog.py
r78312f7 r69a6897 57 57 58 58 fName = dlg.GetPath() 59 basename = os.path.splitext(fName)[0] 59 60 ext_num = dlg.GetFilterIndex() 61 dlg.Destroy() 62 63 if ext_num == 0 and self.index_offset == 0: # has pdf 64 ext = ".pdf" 65 elif ext_num == 1 - self.index_offset: 66 ext = ".html" 67 elif ext_num == 2 - self.index_offset: 68 ext = ".txt" 69 else: 70 logger.warn("unknown export format in report dialog") 71 return 72 filename = basename + ext 73 74 # save figures 75 pictures = [] 76 for num in range(self.nimages): 77 pic_name = basename + '_img%s.png' % num 78 # save the image for use with pdf writer 79 self.report_list[2][num].savefig(pic_name) 80 pictures.append(pic_name) 81 82 # translate png references int html from in-memory name to on-disk name 83 html = self.report_html.replace("memory:img_fit", basename+'_img') 60 84 61 85 #set file extensions 62 86 img_ext = [] 63 pic_fname = [] 64 #PDF 65 if ext_num == (0 + 2 * self.index_offset): 66 # TODO: Sort this case out 67 ext = '.pdf' 87 if ext == ".pdf": 88 # write pdf as a pdf file 89 pdf = self.HTML2PDF(data=html, filename=filename) 68 90 69 fName = os.path.splitext(fName)[0] + ext 70 dlg.Destroy() 71 #pic (png) file path/name 72 for num in range(self.nimages): 73 im_ext = '_img%s.png' % num 74 #img_ext.append(im_ext) 75 pic_name = os.path.splitext(fName)[0] + im_ext 76 pic_fname.append(pic_name) 77 # save the image for use with pdf writer 78 self.report_list[2][num].savefig(pic_name) 91 # delete images used to create the pdf 92 for pic_name in pictures: 93 os.remove(pic_name) 79 94 80 #put the image path in html string 81 report_frame = self.report_list[0] 82 #put image name strings into the html file 83 #Note:The str for pic_fname shouldn't be removed. 84 if self.nimages == 1: 85 html = report_frame % str(pic_fname[0]) 86 elif self.nimages == 2: 87 html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) 88 elif self.nimages == 3: 89 html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), 90 str(pic_fname[2])) 91 92 # make/open file in case of absence 93 f = open(fName, 'w') 94 f.close() 95 # write pdf as a pdf file 96 pdf = self.HTML2PDF(data=html, filename=fName) 97 98 #open pdf 95 #open pdf viewer 99 96 if pdf: 100 97 try: 101 #Windows 102 os.startfile(str(fName)) 103 except Exception: 104 try: 105 #Mac 98 if os.name == 'nt': # Windows 99 os.startfile(fName) 100 elif sys.platform == "darwin": # Mac 106 101 os.system("open %s" % fName) 107 except Exception: 108 #DO not open 109 pass 110 #delete image file 111 for num in range(self.nimages): 112 os.remove(pic_fname[num]) 113 return 114 #HTML + png(graph) 115 elif ext_num == (1 - self.index_offset): 116 ext = '.html' 117 for num in range(self.nimages): 118 img_ext.append('_img4html%s.png' % num) 119 report_frame = self.report_list[0] 120 #TEXT + pdf(graph) 121 elif ext_num == (2 - self.index_offset): 122 ext = '.txt' 123 # changing the image extension actually changes the image 124 # format on saving 125 for num in range(self.nimages): 126 img_ext.append('_img4txt%s.pdf' % num) 127 report = self.report_list[1] 128 else: 129 return 102 except Exception as exc: 103 # cannot open pdf 104 logging.error(str(exc)) 130 105 131 #file name132 fName = os.path.splitext(fName)[0] + ext133 dlg.Destroy()106 elif ext == ".html": 107 with open(filename, 'w') as f: 108 f.write(html) 134 109 135 #pic (png) file path/name 136 for num in range(self.nimages): 137 pic_name = os.path.splitext(fName)[0] + img_ext[num] 138 pic_fname.append(pic_name) 139 #put the image path in html string 140 if ext_num == (1 - self.index_offset): 141 if self.nimages == 1: 142 report = report_frame % os.path.basename(pic_fname[0]) 143 elif self.nimages == 2: 144 report = report_frame % (os.path.basename(pic_fname[0]), 145 os.path.basename(pic_fname[1])) 146 elif self.nimages == 3: 147 report = report_frame % (os.path.basename(pic_fname[0]), 148 os.path.basename(pic_fname[1]), 149 os.path.basename(pic_fname[2])) 150 f = open(fName, 'w') 151 f.write(report) 152 f.close() 110 elif ext == ".txt": 111 with open(filename, 'w') as f: 112 f.write(self.report_list[1]) 113 153 114 self.Update() 154 #save png file using pic_fname155 for num in range(self.nimages):156 self.report_list[2][num].savefig(pic_fname[num])
Note: See TracChangeset
for help on using the changeset viewer.