[16d7079] | 1 | """ |
---|
| 2 | SLD Profile Dialog for multifunctional models |
---|
| 3 | """ |
---|
[da9ac4e6] | 4 | import wx |
---|
| 5 | import sys |
---|
| 6 | from copy import deepcopy |
---|
[79492222] | 7 | from sas.plottools.plottables import Graph |
---|
[a54e4be] | 8 | from Plotter1D import ModelPanel1D as PlotPanel |
---|
[79492222] | 9 | from sas.guiframe.dataFitting import Data1D |
---|
| 10 | from sas.guiframe.gui_style import GUIFRAME_ID |
---|
[da9ac4e6] | 11 | |
---|
[c039589] | 12 | DEFAULT_CMAP = None #pylab.cm.jet |
---|
[da9ac4e6] | 13 | _BOX_WIDTH = 76 |
---|
| 14 | _STATICBOX_WIDTH = 400 |
---|
[16d7079] | 15 | # X Y offset on plot |
---|
| 16 | _X_OFF = 15 |
---|
| 17 | _Y_OFF = 0.5 |
---|
[da9ac4e6] | 18 | |
---|
[c039589] | 19 | #SLD panel size |
---|
[519c693] | 20 | if sys.platform.count("win32") > 0: |
---|
[515bf4f] | 21 | _STATICBOX_WIDTH = 563 |
---|
| 22 | PANEL_SIZE = 425 |
---|
[da9ac4e6] | 23 | FONT_VARIANT = 0 |
---|
| 24 | else: |
---|
[515bf4f] | 25 | _STATICBOX_WIDTH = 605 |
---|
[519c693] | 26 | PANEL_SIZE = 500 |
---|
[da9ac4e6] | 27 | FONT_VARIANT = 1 |
---|
[c039589] | 28 | |
---|
| 29 | |
---|
[da9ac4e6] | 30 | class SLDPanel(wx.Dialog): |
---|
| 31 | """ |
---|
| 32 | Provides the SLD profile plot panel. |
---|
| 33 | """ |
---|
| 34 | ## Internal nickname for the window, used by the AUI manager |
---|
[2d443fd] | 35 | window_name = "Scattering Length Density Profile" |
---|
[da9ac4e6] | 36 | ## Name to appear on the window title bar |
---|
[2d443fd] | 37 | window_caption = "Scattering Length Density Profile" |
---|
[da9ac4e6] | 38 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 39 | CENTER_PANE = True |
---|
[c039589] | 40 | def __init__(self, parent=None, base=None, data=None, axes=['Radius'], |
---|
[a54e4be] | 41 | id=-1, *args, **kwds): |
---|
| 42 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
[c039589] | 43 | kwds["size"] = wx.Size(_STATICBOX_WIDTH, PANEL_SIZE) |
---|
[a54e4be] | 44 | wx.Dialog.__init__(self, parent, id=id, *args, **kwds) |
---|
[519c693] | 45 | |
---|
[da9ac4e6] | 46 | if data != None: |
---|
[c039589] | 47 | #Font size |
---|
[a54e4be] | 48 | kwds = [] |
---|
[da9ac4e6] | 49 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[2d443fd] | 50 | |
---|
[da9ac4e6] | 51 | self.SetTitle("Scattering Length Density Profile") |
---|
[2d443fd] | 52 | self.parent = parent |
---|
[940aca7] | 53 | self._mgr = None |
---|
[da9ac4e6] | 54 | self.data = data |
---|
| 55 | self.str = self.data.__str__() |
---|
| 56 | ## when 2 data have the same id override the 1 st plotted |
---|
| 57 | self.name = self.data.name |
---|
| 58 | # Panel for plot |
---|
[c039589] | 59 | self.plotpanel = SLDplotpanel(self, axes, -1, |
---|
| 60 | style=wx.TRANSPARENT_WINDOW) |
---|
[da9ac4e6] | 61 | self.cmap = DEFAULT_CMAP |
---|
| 62 | ## Create Artist and bind it |
---|
| 63 | self.subplot = self.plotpanel.subplot |
---|
| 64 | # layout |
---|
| 65 | self._setup_layout() |
---|
[c039589] | 66 | |
---|
[da9ac4e6] | 67 | # plot |
---|
| 68 | data_plot = deepcopy(self.data) |
---|
| 69 | data_plot.dy = self._set_dy_data() |
---|
[16d7079] | 70 | # unit increase to M times for users |
---|
| 71 | data_plot.y = self._set_y_data() |
---|
[c039589] | 72 | |
---|
[7625f49] | 73 | self.newplot = Data1D(data_plot.x, data_plot.y, data_plot.dy) |
---|
[9a3d433] | 74 | self.newplot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[accbb1b] | 75 | self.newplot.dy = None |
---|
[da9ac4e6] | 76 | self.newplot.name = 'SLD' |
---|
[808da5e] | 77 | self.newplot.is_data = False |
---|
[c039589] | 78 | |
---|
[940aca7] | 79 | self.newplot.id = self.newplot.name |
---|
[c039589] | 80 | self.plotpanel.add_image(self.newplot) |
---|
| 81 | |
---|
[808da5e] | 82 | self.plotpanel.resizing = False |
---|
| 83 | self.plotpanel.canvas.set_resizing(self.plotpanel.resizing) |
---|
[c039589] | 84 | |
---|
| 85 | self.plotpanel.subplot.set_ylim(min(data_plot.y) - _Y_OFF, |
---|
| 86 | max(data_plot.y) + _Y_OFF) |
---|
| 87 | self.plotpanel.subplot.set_xlim(min(data_plot.x) - _X_OFF, |
---|
| 88 | max(data_plot.x) + _X_OFF) |
---|
[d56fc67] | 89 | self.plotpanel.graph.render(self.plotpanel) |
---|
[808da5e] | 90 | self.plotpanel.canvas.draw() |
---|
[519c693] | 91 | |
---|
[c039589] | 92 | def _set_dy_data(self): |
---|
[da9ac4e6] | 93 | """ |
---|
| 94 | make fake dy data |
---|
[c039589] | 95 | |
---|
| 96 | :return dy: |
---|
[da9ac4e6] | 97 | """ |
---|
| 98 | # set dy as zero |
---|
[a54e4be] | 99 | dy = [0 for y in self.data.y] |
---|
[c039589] | 100 | return dy |
---|
[16d7079] | 101 | |
---|
[c039589] | 102 | def _set_y_data(self): |
---|
[16d7079] | 103 | """ |
---|
| 104 | make y data unit Mega times |
---|
[c039589] | 105 | |
---|
| 106 | :return y_value: |
---|
[16d7079] | 107 | """ |
---|
| 108 | # changes the unit |
---|
| 109 | y_value = [yvalue * 1e+006 for yvalue in self.data.y] |
---|
[c039589] | 110 | |
---|
| 111 | return y_value |
---|
| 112 | |
---|
[da9ac4e6] | 113 | def _setup_layout(self): |
---|
| 114 | """ |
---|
| 115 | Set up the layout |
---|
| 116 | """ |
---|
[519c693] | 117 | # panel sizer |
---|
| 118 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
[c039589] | 119 | sizer.Add(self.plotpanel, 0, wx.LEFT | wx.RIGHT, 5) |
---|
| 120 | sizer.Add(wx.StaticLine(self), 0, wx.ALL | wx.EXPAND, 5) |
---|
[accbb1b] | 121 | sizer.Add((0, 5)) |
---|
[da9ac4e6] | 122 | #-----Button------------1 |
---|
[c039589] | 123 | button_reset = wx.Button(self, wx.NewId(), "Close") |
---|
[da9ac4e6] | 124 | button_reset.SetToolTipString("Close...") |
---|
[c039589] | 125 | button_reset.Bind(wx.EVT_BUTTON, self._close, |
---|
| 126 | id=button_reset.GetId()) |
---|
| 127 | sizer.Add(button_reset, 0, wx.LEFT, _STATICBOX_WIDTH - 80) |
---|
[a54e4be] | 128 | sizer.Add((0, 10)) |
---|
[da9ac4e6] | 129 | self.SetSizerAndFit(sizer) |
---|
| 130 | self.Centre() |
---|
| 131 | self.Show(True) |
---|
[519c693] | 132 | button_reset.SetFocus() |
---|
[c039589] | 133 | |
---|
[da9ac4e6] | 134 | def _close(self, event): |
---|
| 135 | """ |
---|
| 136 | Close the dialog |
---|
| 137 | """ |
---|
| 138 | self.Close(True) |
---|
| 139 | |
---|
[59fbcff] | 140 | def _draw_model(self, event): |
---|
| 141 | """ |
---|
| 142 | on_close, update the model2d plot |
---|
| 143 | """ |
---|
| 144 | pass |
---|
[da9ac4e6] | 145 | |
---|
[567fb8b] | 146 | def get_current_context_menu(self, graph=None): |
---|
[59fbcff] | 147 | """ |
---|
[c039589] | 148 | When the context menu of a plot is rendered, the |
---|
| 149 | get_context_menu method will be called to give you a |
---|
[59fbcff] | 150 | chance to add a menu item to the context menu. |
---|
| 151 | :param graph: the Graph object to which we attach the context menu |
---|
[c039589] | 152 | |
---|
[59fbcff] | 153 | :return: a list of menu items with call-back function |
---|
| 154 | """ |
---|
| 155 | return [] |
---|
[c039589] | 156 | |
---|
[59fbcff] | 157 | def set_schedule_full_draw(self, panel=None, func=None): |
---|
| 158 | """ |
---|
| 159 | Set_schedule for full draw |
---|
| 160 | """ |
---|
| 161 | # Not implemented |
---|
| 162 | pass |
---|
[c039589] | 163 | |
---|
[59fbcff] | 164 | def set_schedule(self, schedule=False): |
---|
| 165 | """ |
---|
| 166 | Set schedule for redraw |
---|
| 167 | """ |
---|
| 168 | # Not implemented |
---|
| 169 | pass |
---|
[c039589] | 170 | |
---|
[accbb1b] | 171 | def set_plot_unfocus(self): |
---|
| 172 | """ |
---|
| 173 | Set_plot unfocus |
---|
| 174 | """ |
---|
| 175 | # NOt implemented |
---|
| 176 | pass |
---|
[c039589] | 177 | |
---|
[2d443fd] | 178 | def on_change_caption(self, name, old_caption, new_caption): |
---|
| 179 | """ |
---|
| 180 | """ |
---|
| 181 | self.parent.parent.parent.on_change_caption(name, old_caption, new_caption) |
---|
[c039589] | 182 | |
---|
[940aca7] | 183 | def disable_app_menu(self, panel): |
---|
| 184 | """ |
---|
| 185 | Disable menu bar |
---|
| 186 | """ |
---|
| 187 | # Not implemented! |
---|
| 188 | return |
---|
[c039589] | 189 | |
---|
[4467d62] | 190 | def show_data1d(self, data, name): |
---|
| 191 | """ |
---|
| 192 | Show data dialog |
---|
[c039589] | 193 | """ |
---|
[ae84427] | 194 | self.parent._manager.parent.show_data1d(data, name) |
---|
[c039589] | 195 | |
---|
[da9ac4e6] | 196 | class SLDplotpanel(PlotPanel): |
---|
| 197 | """ |
---|
| 198 | Panel |
---|
| 199 | """ |
---|
[c039589] | 200 | def __init__(self, parent, axes=[], id=-1, color=None, dpi=None, **kwargs): |
---|
[da9ac4e6] | 201 | """ |
---|
| 202 | """ |
---|
[c039589] | 203 | PlotPanel.__init__(self, parent, id=id, xtransform='x', ytransform='y', |
---|
| 204 | color=color, dpi=dpi, |
---|
| 205 | size=(_STATICBOX_WIDTH, PANEL_SIZE - 100), **kwargs) |
---|
[519c693] | 206 | |
---|
[da9ac4e6] | 207 | # Keep track of the parent Frame |
---|
| 208 | self.parent = parent |
---|
[2d443fd] | 209 | self.window_name = "Scattering Length Density Profile" |
---|
| 210 | self.window_caption = self.window_name |
---|
[657e52c] | 211 | self.prevXtrans = "x" |
---|
| 212 | self.prevYtrans = "y" |
---|
| 213 | self.viewModel = "--" |
---|
[c039589] | 214 | # Internal list of plottable names (because graph |
---|
[da9ac4e6] | 215 | # doesn't have a dictionary of handles for the plottables) |
---|
| 216 | self.plots = {} |
---|
| 217 | self.graph = Graph() |
---|
| 218 | self.axes_label = [] |
---|
[519c693] | 219 | for idx in range(0, len(axes)): |
---|
[da9ac4e6] | 220 | self.axes_label.append(axes[idx]) |
---|
[2d443fd] | 221 | self.xaxis_label = '' |
---|
| 222 | self.xaxis_unit = '' |
---|
| 223 | self.yaxis_label = '' |
---|
| 224 | self.yaxis_unit = '' |
---|
[940aca7] | 225 | self.resizing = True |
---|
[808da5e] | 226 | self.xcolor = 'black' |
---|
| 227 | self.ycolor = 'black' |
---|
[c039589] | 228 | |
---|
[da9ac4e6] | 229 | def add_image(self, plot): |
---|
| 230 | """ |
---|
[7e3d422] | 231 | Add image(Theory1D) |
---|
[da9ac4e6] | 232 | """ |
---|
[940aca7] | 233 | self.plots[plot.id] = plot |
---|
| 234 | self.plots[plot.id].is_data = True |
---|
[da9ac4e6] | 235 | #add plot |
---|
| 236 | self.graph.add(plot) |
---|
| 237 | #add axes |
---|
| 238 | x1_label = self.axes_label[0] |
---|
[c039589] | 239 | self.xaxis_label = '\\rm{%s} ' % x1_label |
---|
[2d443fd] | 240 | self.xaxis_unit = 'A' |
---|
| 241 | self.graph.xaxis(self.xaxis_label, self.xaxis_unit) |
---|
| 242 | self.yaxis_label = '\\rm{SLD} ' |
---|
| 243 | self.yaxis_unit = '10^{-6}A^{-2}' |
---|
| 244 | self.graph.yaxis(self.yaxis_label, self.yaxis_unit) |
---|
[c039589] | 245 | # For latter scale changes |
---|
| 246 | self.plots[plot.id].xaxis('\\rm{%s} ' % x1_label, 'A') |
---|
[940aca7] | 247 | self.plots[plot.id].yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
[c039589] | 248 | |
---|
[da9ac4e6] | 249 | def on_set_focus(self, event): |
---|
| 250 | """ |
---|
| 251 | send to the parenet the current panel on focus |
---|
[c039589] | 252 | |
---|
[da9ac4e6] | 253 | """ |
---|
| 254 | #Not implemented |
---|
| 255 | pass |
---|
| 256 | |
---|
| 257 | def on_kill_focus(self, event): |
---|
| 258 | """ |
---|
| 259 | reset the panel color |
---|
[c039589] | 260 | |
---|
[da9ac4e6] | 261 | """ |
---|
| 262 | #Not implemented |
---|
| 263 | pass |
---|
[c039589] | 264 | |
---|
[2d443fd] | 265 | def onChangeCaption(self, event): |
---|
| 266 | """ |
---|
| 267 | Not implemented |
---|
| 268 | """ |
---|
| 269 | pass |
---|
[c039589] | 270 | |
---|
[2d443fd] | 271 | def _onSave(self, evt): |
---|
| 272 | """ |
---|
| 273 | Save a data set to a text file |
---|
[c039589] | 274 | |
---|
[2d443fd] | 275 | :param evt: Menu event |
---|
[c039589] | 276 | |
---|
[2d443fd] | 277 | """ |
---|
[940aca7] | 278 | menu = evt.GetEventObject() |
---|
[c039589] | 279 | event_id = evt.GetId() |
---|
| 280 | self.set_selected_from_menu(menu, event_id) |
---|
[940aca7] | 281 | data = self.plots[self.graph.selected_plottable] |
---|
| 282 | default_name = data.label |
---|
| 283 | if default_name.count('.') > 0: |
---|
| 284 | default_name = default_name.split('.')[0] |
---|
| 285 | default_name += "_out" |
---|
[2d443fd] | 286 | if self.parent != None: |
---|
[940aca7] | 287 | # What an ancestor! |
---|
| 288 | fit_panel = self.parent.parent.parent |
---|
[ae84427] | 289 | fit_panel._manager.parent.save_data1d(data, default_name) |
---|
[c039589] | 290 | |
---|
[da9ac4e6] | 291 | class ViewerFrame(wx.Frame): |
---|
| 292 | """ |
---|
| 293 | Add comment |
---|
| 294 | """ |
---|
| 295 | def __init__(self, parent, id, title): |
---|
| 296 | """ |
---|
| 297 | comment |
---|
| 298 | :param parent: parent panel/container |
---|
| 299 | """ |
---|
| 300 | # Initialize the Frame object |
---|
[c039589] | 301 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
[a54e4be] | 302 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
[da9ac4e6] | 303 | # Panel for 1D plot |
---|
[c039589] | 304 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
[da9ac4e6] | 305 | |
---|
| 306 | class ViewApp(wx.App): |
---|
| 307 | def OnInit(self): |
---|
[c039589] | 308 | frame = ViewerFrame(None, -1, 'testView') |
---|
[da9ac4e6] | 309 | frame.Show(True) |
---|
| 310 | self.SetTopWindow(frame) |
---|
[c039589] | 311 | |
---|
[da9ac4e6] | 312 | return True |
---|
[c039589] | 313 | |
---|
| 314 | if __name__ == "__main__": |
---|
[da9ac4e6] | 315 | app = ViewApp(0) |
---|
[c039589] | 316 | app.MainLoop() |
---|