1 | """ |
---|
2 | SLD Profile Dialog for multifunctional models |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import os |
---|
6 | import sys |
---|
7 | from copy import deepcopy |
---|
8 | from danse.common.plottools.plottables import Graph |
---|
9 | from Plotter1D import ModelPanel1D as PlotPanel |
---|
10 | from sans.guiframe.dataFitting import Data1D |
---|
11 | from sans.guiframe.gui_style import GUIFRAME_ID |
---|
12 | import pylab |
---|
13 | |
---|
14 | DEFAULT_CMAP = None#pylab.cm.jet |
---|
15 | _BOX_WIDTH = 76 |
---|
16 | _STATICBOX_WIDTH = 400 |
---|
17 | # X Y offset on plot |
---|
18 | _X_OFF = 15 |
---|
19 | _Y_OFF = 0.5 |
---|
20 | |
---|
21 | #SLD panel size |
---|
22 | if sys.platform.count("win32") > 0: |
---|
23 | _STATICBOX_WIDTH = 563 |
---|
24 | PANEL_SIZE = 425 |
---|
25 | FONT_VARIANT = 0 |
---|
26 | else: |
---|
27 | _STATICBOX_WIDTH = 605 |
---|
28 | PANEL_SIZE = 500 |
---|
29 | FONT_VARIANT = 1 |
---|
30 | |
---|
31 | |
---|
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 |
---|
37 | window_name = "Scattering Length Density Profile" |
---|
38 | ## Name to appear on the window title bar |
---|
39 | window_caption = "Scattering Length Density Profile" |
---|
40 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
41 | CENTER_PANE = True |
---|
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) |
---|
47 | |
---|
48 | if data != None: |
---|
49 | #Font size |
---|
50 | kwds = [] |
---|
51 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
52 | |
---|
53 | self.SetTitle("Scattering Length Density Profile") |
---|
54 | self.parent = parent |
---|
55 | self._mgr = None |
---|
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 |
---|
61 | self.plotpanel = SLDplotpanel(self, axes, -1, |
---|
62 | style=wx.TRANSPARENT_WINDOW) |
---|
63 | self.cmap = DEFAULT_CMAP |
---|
64 | ## Create Artist and bind it |
---|
65 | self.subplot = self.plotpanel.subplot |
---|
66 | # layout |
---|
67 | self._setup_layout() |
---|
68 | |
---|
69 | # plot |
---|
70 | data_plot = deepcopy(self.data) |
---|
71 | data_plot.dy = self._set_dy_data() |
---|
72 | # unit increase to M times for users |
---|
73 | data_plot.y = self._set_y_data() |
---|
74 | |
---|
75 | self.newplot = Data1D(data_plot.x, data_plot.y, data_plot.dy) |
---|
76 | self.newplot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
77 | self.newplot.dy = None |
---|
78 | self.newplot.name = 'SLD' |
---|
79 | self.newplot.is_data = False |
---|
80 | |
---|
81 | self.newplot.id = self.newplot.name |
---|
82 | self.plotpanel.add_image(self.newplot) |
---|
83 | |
---|
84 | self.plotpanel.resizing = False |
---|
85 | self.plotpanel.canvas.set_resizing(self.plotpanel.resizing) |
---|
86 | |
---|
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) |
---|
91 | self.plotpanel.graph.render(self.plotpanel) |
---|
92 | self.plotpanel.canvas.draw() |
---|
93 | |
---|
94 | def _set_dy_data(self): |
---|
95 | """ |
---|
96 | make fake dy data |
---|
97 | |
---|
98 | :return dy: |
---|
99 | """ |
---|
100 | # set dy as zero |
---|
101 | dy = [0 for y in self.data.y] |
---|
102 | return dy |
---|
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 |
---|
114 | |
---|
115 | def _setup_layout(self): |
---|
116 | """ |
---|
117 | Set up the layout |
---|
118 | """ |
---|
119 | # panel sizer |
---|
120 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
121 | sizer.Add(self.plotpanel, 0, wx.LEFT|wx.RIGHT, 5) |
---|
122 | sizer.Add(wx.StaticLine(self), 0, wx.ALL|wx.EXPAND, 5) |
---|
123 | sizer.Add((0, 5)) |
---|
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, |
---|
129 | id=button_reset.GetId()) |
---|
130 | sizer.Add(button_reset, 0, wx.LEFT, _STATICBOX_WIDTH - 80) |
---|
131 | sizer.Add((0, 10)) |
---|
132 | self.SetSizerAndFit(sizer) |
---|
133 | self.Centre() |
---|
134 | self.Show(True) |
---|
135 | button_reset.SetFocus() |
---|
136 | |
---|
137 | def _close(self, event): |
---|
138 | """ |
---|
139 | Close the dialog |
---|
140 | """ |
---|
141 | self.Close(True) |
---|
142 | |
---|
143 | def _draw_model(self, event): |
---|
144 | """ |
---|
145 | on_close, update the model2d plot |
---|
146 | """ |
---|
147 | pass |
---|
148 | |
---|
149 | def get_current_context_menu(self, graph=None): |
---|
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 |
---|
173 | |
---|
174 | def set_plot_unfocus(self): |
---|
175 | """ |
---|
176 | Set_plot unfocus |
---|
177 | """ |
---|
178 | # NOt implemented |
---|
179 | pass |
---|
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) |
---|
185 | |
---|
186 | def disable_app_menu(self, panel): |
---|
187 | """ |
---|
188 | Disable menu bar |
---|
189 | """ |
---|
190 | # Not implemented! |
---|
191 | return |
---|
192 | |
---|
193 | def show_data1d(self, data, name): |
---|
194 | """ |
---|
195 | Show data dialog |
---|
196 | """ |
---|
197 | self.parent.parent.parent.show_data1d(data, name) |
---|
198 | |
---|
199 | class SLDplotpanel(PlotPanel): |
---|
200 | """ |
---|
201 | Panel |
---|
202 | """ |
---|
203 | def __init__(self, parent, axes=[], id=-1, color=None, dpi=None, |
---|
204 | **kwargs): |
---|
205 | """ |
---|
206 | """ |
---|
207 | PlotPanel.__init__(self, parent, id=id, xtransform='x', ytransform='y', |
---|
208 | color=color, dpi=dpi, |
---|
209 | size=(_STATICBOX_WIDTH, PANEL_SIZE-100), **kwargs) |
---|
210 | |
---|
211 | # Keep track of the parent Frame |
---|
212 | self.parent = parent |
---|
213 | self.window_name = "Scattering Length Density Profile" |
---|
214 | self.window_caption = self.window_name |
---|
215 | # Internal list of plottable names (because graph |
---|
216 | # doesn't have a dictionary of handles for the plottables) |
---|
217 | self.plots = {} |
---|
218 | self.graph = Graph() |
---|
219 | self.axes_label = [] |
---|
220 | for idx in range(0, len(axes)): |
---|
221 | self.axes_label.append(axes[idx]) |
---|
222 | self.xaxis_label = '' |
---|
223 | self.xaxis_unit = '' |
---|
224 | self.yaxis_label = '' |
---|
225 | self.yaxis_unit = '' |
---|
226 | self.resizing = True |
---|
227 | self.xcolor = 'black' |
---|
228 | self.ycolor = 'black' |
---|
229 | |
---|
230 | def add_image(self, plot): |
---|
231 | """ |
---|
232 | Add image(Theory1D) |
---|
233 | """ |
---|
234 | self.plots[plot.id] = plot |
---|
235 | self.plots[plot.id].is_data = True |
---|
236 | #add plot |
---|
237 | self.graph.add(plot) |
---|
238 | #add axes |
---|
239 | x1_label = self.axes_label[0] |
---|
240 | self.xaxis_label = '\\rm{%s} '% x1_label |
---|
241 | self.xaxis_unit = 'A' |
---|
242 | self.graph.xaxis(self.xaxis_label, self.xaxis_unit) |
---|
243 | self.yaxis_label = '\\rm{SLD} ' |
---|
244 | self.yaxis_unit = '10^{-6}A^{-2}' |
---|
245 | self.graph.yaxis(self.yaxis_label, self.yaxis_unit) |
---|
246 | #self.subplot.figure.canvas.draw_idle() |
---|
247 | # For latter scale changes |
---|
248 | self.plots[plot.id].xaxis('\\rm{%s} '% x1_label, 'A') |
---|
249 | self.plots[plot.id].yaxis('\\rm{SLD} ', '10^{-6}A^{-2}') |
---|
250 | #draw |
---|
251 | #self.graph.render(self) |
---|
252 | |
---|
253 | def on_set_focus(self, event): |
---|
254 | """ |
---|
255 | send to the parenet the current panel on focus |
---|
256 | |
---|
257 | """ |
---|
258 | #Not implemented |
---|
259 | pass |
---|
260 | |
---|
261 | def on_kill_focus(self, event): |
---|
262 | """ |
---|
263 | reset the panel color |
---|
264 | |
---|
265 | """ |
---|
266 | #Not implemented |
---|
267 | pass |
---|
268 | |
---|
269 | def _add_more_tool(self): |
---|
270 | """ |
---|
271 | Not implemented |
---|
272 | """ |
---|
273 | pass |
---|
274 | |
---|
275 | def onChangeCaption(self, event): |
---|
276 | """ |
---|
277 | Not implemented |
---|
278 | """ |
---|
279 | pass |
---|
280 | |
---|
281 | def _onSave(self, evt): |
---|
282 | """ |
---|
283 | Save a data set to a text file |
---|
284 | |
---|
285 | :param evt: Menu event |
---|
286 | |
---|
287 | """ |
---|
288 | menu = evt.GetEventObject() |
---|
289 | id = evt.GetId() |
---|
290 | self.set_selected_from_menu(menu, id) |
---|
291 | data = self.plots[self.graph.selected_plottable] |
---|
292 | default_name = data.label |
---|
293 | if default_name.count('.') > 0: |
---|
294 | default_name = default_name.split('.')[0] |
---|
295 | default_name += "_out" |
---|
296 | if self.parent != None: |
---|
297 | # What an ancestor! |
---|
298 | fit_panel = self.parent.parent.parent |
---|
299 | fit_panel.parent.save_data1d(data, default_name) |
---|
300 | |
---|
301 | class ViewerFrame(wx.Frame): |
---|
302 | """ |
---|
303 | Add comment |
---|
304 | """ |
---|
305 | def __init__(self, parent, id, title): |
---|
306 | """ |
---|
307 | comment |
---|
308 | :param parent: parent panel/container |
---|
309 | """ |
---|
310 | # Initialize the Frame object |
---|
311 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
312 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
313 | # Panel for 1D plot |
---|
314 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
315 | |
---|
316 | class ViewApp(wx.App): |
---|
317 | def OnInit(self): |
---|
318 | frame = ViewerFrame(None, -1, 'testView') |
---|
319 | frame.Show(True) |
---|
320 | self.SetTopWindow(frame) |
---|
321 | |
---|
322 | return True |
---|
323 | |
---|
324 | if __name__ == "__main__": |
---|
325 | app = ViewApp(0) |
---|
326 | app.MainLoop() |
---|