[f32d144] | 1 | """ |
---|
[2f4b430] | 2 | Dialog report panel to show and summarize the results of |
---|
[d06ae30] | 3 | the fitting calculation. |
---|
[f32d144] | 4 | """ |
---|
[2296316] | 5 | ################################################################################ |
---|
| 6 | #This software was developed by the University of Tennessee as part of the |
---|
| 7 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[f32d144] | 8 | #project funded by the US National Science Foundation. |
---|
[2296316] | 9 | # |
---|
| 10 | #See the license text in license.txt |
---|
| 11 | # |
---|
| 12 | #copyright 2009, University of Tennessee |
---|
| 13 | ################################################################################ |
---|
| 14 | |
---|
| 15 | import wx |
---|
| 16 | import os |
---|
| 17 | import wx.html as html |
---|
| 18 | |
---|
[e8bb5b6] | 19 | from sas.guiframe.report_dialog import BaseReportDialog |
---|
[2296316] | 20 | |
---|
[e8bb5b6] | 21 | class ReportDialog(BaseReportDialog): |
---|
[2296316] | 22 | """ |
---|
[f32d144] | 23 | The report dialog box. |
---|
[2296316] | 24 | """ |
---|
[2f4b430] | 25 | |
---|
[e8bb5b6] | 26 | def __init__(self, report_list, *args, **kwds): |
---|
[2296316] | 27 | """ |
---|
| 28 | Initialization. The parameters added to Dialog are: |
---|
[2f4b430] | 29 | |
---|
[e8bb5b6] | 30 | :param report_list: list of html_str, text_str, image |
---|
[2296316] | 31 | from invariant_state |
---|
| 32 | """ |
---|
[e8bb5b6] | 33 | super(ReportDialog, self).__init__(report_list, *args, **kwds) |
---|
| 34 | |
---|
[2296316] | 35 | # title |
---|
| 36 | self.SetTitle("Report: Fitting") |
---|
[e8bb5b6] | 37 | |
---|
[2296316] | 38 | # number of images of plot |
---|
[e8bb5b6] | 39 | self.nimages = len(self.report_list[2]) |
---|
[2f4b430] | 40 | |
---|
[e8bb5b6] | 41 | if self.report_list[2] != None: |
---|
[2296316] | 42 | # put image path in the report string |
---|
[e8bb5b6] | 43 | if len(self.report_list[2]) == 1: |
---|
[2296316] | 44 | self.report_html = self.report_list[0] % \ |
---|
| 45 | "memory:img_fit0.png" |
---|
[a15e754] | 46 | elif len(self.report_list[2]) == 2: |
---|
[2296316] | 47 | self.report_html = self.report_list[0] % \ |
---|
[f32d144] | 48 | ("memory:img_fit0.png", |
---|
[2296316] | 49 | "memory:img_fit1.png") |
---|
| 50 | # allows up to three images |
---|
| 51 | else: |
---|
| 52 | self.report_html = self.report_list[0] % \ |
---|
[f32d144] | 53 | ("memory:img_fit0.png", |
---|
[2296316] | 54 | "memory:img_fit1.png", |
---|
| 55 | "memory:img_fit2.png") |
---|
| 56 | else: |
---|
| 57 | self.report_html = self.report_list[0] |
---|
| 58 | # layout |
---|
| 59 | self._setup_layout() |
---|
[2f4b430] | 60 | |
---|
[2296316] | 61 | def onSave(self, event=None): |
---|
| 62 | """ |
---|
| 63 | Save |
---|
| 64 | """ |
---|
| 65 | #todo: complete saving fig file and as a txt file |
---|
| 66 | dlg = wx.FileDialog(self, "Choose a file", |
---|
[e8bb5b6] | 67 | wildcard=self.wild_card, |
---|
[2f4b430] | 68 | style=wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) |
---|
[f32d144] | 69 | dlg.SetFilterIndex(0) # Set .html files to be default |
---|
[2296316] | 70 | |
---|
| 71 | if dlg.ShowModal() != wx.ID_OK: |
---|
| 72 | dlg.Destroy() |
---|
| 73 | return |
---|
[2f4b430] | 74 | |
---|
[2296316] | 75 | fName = dlg.GetPath() |
---|
[f32d144] | 76 | ext_num = dlg.GetFilterIndex() |
---|
[2296316] | 77 | |
---|
[f32d144] | 78 | #set file extensions |
---|
[2296316] | 79 | img_ext = [] |
---|
[dec793e] | 80 | pic_fname = [] |
---|
[f32d144] | 81 | #PDF |
---|
[e8bb5b6] | 82 | if ext_num == (0 + 2 * self.index_offset): |
---|
[dec793e] | 83 | # TODO: Sort this case out |
---|
| 84 | ext = '.pdf' |
---|
[2f4b430] | 85 | |
---|
[dec793e] | 86 | fName = os.path.splitext(fName)[0] + ext |
---|
| 87 | dlg.Destroy() |
---|
| 88 | #pic (png) file path/name |
---|
| 89 | for num in range(self.nimages): |
---|
| 90 | im_ext = '_img%s.png' % num |
---|
| 91 | #img_ext.append(im_ext) |
---|
| 92 | pic_name = os.path.splitext(fName)[0] + im_ext |
---|
| 93 | pic_fname.append(pic_name) |
---|
| 94 | # save the image for use with pdf writer |
---|
| 95 | self.report_list[2][num].savefig(pic_name) |
---|
| 96 | |
---|
| 97 | #put the image path in html string |
---|
| 98 | report_frame = self.report_list[0] |
---|
| 99 | #put image name strings into the html file |
---|
| 100 | #Note:The str for pic_fname shouldn't be removed. |
---|
| 101 | if self.nimages == 1: |
---|
[091c702] | 102 | html = report_frame % str(pic_fname[0]) |
---|
[dec793e] | 103 | elif self.nimages == 2: |
---|
[091c702] | 104 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) |
---|
[dec793e] | 105 | elif self.nimages == 3: |
---|
[091c702] | 106 | html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), |
---|
| 107 | str(pic_fname[2])) |
---|
[dec793e] | 108 | |
---|
| 109 | # make/open file in case of absence |
---|
| 110 | f = open(fName, 'w') |
---|
| 111 | f.close() |
---|
| 112 | # write pdf as a pdf file |
---|
| 113 | pdf = self.HTML2PDF(data=html, filename=fName) |
---|
[2f4b430] | 114 | |
---|
[dec793e] | 115 | #open pdf |
---|
| 116 | if pdf: |
---|
[d8e3f7c] | 117 | try: |
---|
| 118 | #Windows |
---|
| 119 | os.startfile(str(fName)) |
---|
| 120 | except: |
---|
| 121 | try: |
---|
| 122 | #Mac |
---|
[2f4b430] | 123 | os.system("open %s" % fName) |
---|
[d8e3f7c] | 124 | except: |
---|
| 125 | #DO not open |
---|
| 126 | pass |
---|
[dec793e] | 127 | #delete image file |
---|
| 128 | for num in range(self.nimages): |
---|
| 129 | os.remove(pic_fname[num]) |
---|
| 130 | return |
---|
| 131 | #HTML + png(graph) |
---|
[e8bb5b6] | 132 | elif ext_num == (1 - self.index_offset): |
---|
[2296316] | 133 | ext = '.html' |
---|
| 134 | for num in range(self.nimages): |
---|
| 135 | img_ext.append('_img4html%s.png' % num) |
---|
| 136 | report_frame = self.report_list[0] |
---|
[dec793e] | 137 | #TEXT + pdf(graph) |
---|
[e8bb5b6] | 138 | elif ext_num == (2 - self.index_offset): |
---|
[f32d144] | 139 | ext = '.txt' |
---|
[2296316] | 140 | # changing the image extension actually changes the image |
---|
| 141 | # format on saving |
---|
| 142 | for num in range(self.nimages): |
---|
| 143 | img_ext.append('_img4txt%s.pdf' % num) |
---|
| 144 | report = self.report_list[1] |
---|
| 145 | else: |
---|
| 146 | return |
---|
[2f4b430] | 147 | |
---|
[f32d144] | 148 | #file name |
---|
[2296316] | 149 | fName = os.path.splitext(fName)[0] + ext |
---|
| 150 | dlg.Destroy() |
---|
[2f4b430] | 151 | |
---|
[2296316] | 152 | #pic (png) file path/name |
---|
| 153 | for num in range(self.nimages): |
---|
| 154 | pic_name = os.path.splitext(fName)[0] + img_ext[num] |
---|
| 155 | pic_fname.append(pic_name) |
---|
| 156 | #put the image path in html string |
---|
[e8bb5b6] | 157 | if ext_num == (1 - self.index_offset): |
---|
[2296316] | 158 | if self.nimages == 1: |
---|
[3dac5e2] | 159 | report = report_frame % os.path.basename(pic_fname[0]) |
---|
[2296316] | 160 | elif self.nimages == 2: |
---|
[f32d144] | 161 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
[3dac5e2] | 162 | os.path.basename(pic_fname[1])) |
---|
[2296316] | 163 | elif self.nimages == 3: |
---|
[f32d144] | 164 | report = report_frame % (os.path.basename(pic_fname[0]), |
---|
[3dac5e2] | 165 | os.path.basename(pic_fname[1]), |
---|
| 166 | os.path.basename(pic_fname[2])) |
---|
[2296316] | 167 | f = open(fName, 'w') |
---|
| 168 | f.write(report) |
---|
| 169 | f.close() |
---|
[f0239a3] | 170 | self.Update() |
---|
[2296316] | 171 | #save png file using pic_fname |
---|
| 172 | for num in range(self.nimages): |
---|
| 173 | self.report_list[2][num].savefig(pic_fname[num]) |
---|