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