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 | self.prevXtrans = "x" |
---|
216 | self.prevYtrans = "y" |
---|
217 | self.viewModel = "--" |
---|
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 = [] |
---|
223 | for idx in range(0, len(axes)): |
---|
224 | self.axes_label.append(axes[idx]) |
---|
225 | self.xaxis_label = '' |
---|
226 | self.xaxis_unit = '' |
---|
227 | self.yaxis_label = '' |
---|
228 | self.yaxis_unit = '' |
---|
229 | self.resizing = True |
---|
230 | self.xcolor = 'black' |
---|
231 | self.ycolor = 'black' |
---|
232 | |
---|
233 | def add_image(self, plot): |
---|
234 | """ |
---|
235 | Add image(Theory1D) |
---|
236 | """ |
---|
237 | self.plots[plot.id] = plot |
---|
238 | self.plots[plot.id].is_data = True |
---|
239 | #add plot |
---|
240 | self.graph.add(plot) |
---|
241 | #add axes |
---|
242 | x1_label = self.axes_label[0] |
---|
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) |
---|
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}') |
---|
253 | #draw |
---|
254 | #self.graph.render(self) |
---|
255 | |
---|
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 | |
---|
272 | def _add_more_tool(self): |
---|
273 | """ |
---|
274 | Not implemented |
---|
275 | """ |
---|
276 | pass |
---|
277 | |
---|
278 | def onChangeCaption(self, event): |
---|
279 | """ |
---|
280 | Not implemented |
---|
281 | """ |
---|
282 | pass |
---|
283 | |
---|
284 | def _onSave(self, evt): |
---|
285 | """ |
---|
286 | Save a data set to a text file |
---|
287 | |
---|
288 | :param evt: Menu event |
---|
289 | |
---|
290 | """ |
---|
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" |
---|
299 | if self.parent != None: |
---|
300 | # What an ancestor! |
---|
301 | fit_panel = self.parent.parent.parent |
---|
302 | fit_panel.parent.save_data1d(data, default_name) |
---|
303 | |
---|
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, |
---|
315 | wx.Size(_STATICBOX_WIDTH, PANEL_SIZE)) |
---|
316 | # Panel for 1D plot |
---|
317 | self.plotpanel = SLDplotpanel(self, -1, style=wx.RAISED_BORDER) |
---|
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() |
---|