1 | |
---|
2 | import sys, os |
---|
3 | import wx |
---|
4 | import numpy |
---|
5 | import time |
---|
6 | import copy |
---|
7 | from sans.guiframe.utils import format_number,check_float |
---|
8 | from sans.guicomm.events import StatusEvent |
---|
9 | import pagestate |
---|
10 | from pagestate import PageState |
---|
11 | (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() |
---|
12 | (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() |
---|
13 | (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() |
---|
14 | _BOX_WIDTH = 76 |
---|
15 | |
---|
16 | class BasicPage(wx.ScrolledWindow): |
---|
17 | """ |
---|
18 | This class provide general structure of fitpanel page |
---|
19 | """ |
---|
20 | ## Internal name for the AUI manager |
---|
21 | window_name = "Basic Page" |
---|
22 | ## Title to appear on top of the window |
---|
23 | window_caption = "Basic page " |
---|
24 | |
---|
25 | def __init__(self,parent, page_info): |
---|
26 | wx.ScrolledWindow.__init__(self, parent, |
---|
27 | style= wx.FULL_REPAINT_ON_RESIZE ) |
---|
28 | ##window_name |
---|
29 | self.window_name = page_info.window_name |
---|
30 | ##window_caption |
---|
31 | self.window_caption = page_info.window_caption |
---|
32 | ## parent of the page |
---|
33 | self.parent = parent |
---|
34 | ## manager is the fitting plugin |
---|
35 | self.manager= page_info.manager |
---|
36 | ## owner of the page (fitting plugin) |
---|
37 | self.event_owner= page_info.event_owner |
---|
38 | ## current model |
---|
39 | self.model = page_info.model |
---|
40 | ## data |
---|
41 | self.data = page_info.data |
---|
42 | ## dictionary containing list of models |
---|
43 | self.model_list_box = page_info.model_list_box |
---|
44 | ## Data member to store the dispersion object created |
---|
45 | self._disp_obj_dict = {} |
---|
46 | ## selected parameters to apply dispersion |
---|
47 | self.disp_cb_dict ={} |
---|
48 | ## smearer object |
---|
49 | self.smearer = None |
---|
50 | |
---|
51 | ##list of model parameters. each item must have same length |
---|
52 | ## each item related to a given parameters |
---|
53 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
54 | self.parameters=[] |
---|
55 | ## list of parameters to fit , must be like self.parameters |
---|
56 | self.param_toFit=[] |
---|
57 | ## list of looking like parameters but with non fittable parameters info |
---|
58 | self.fixed_param=[] |
---|
59 | ## list of looking like parameters but with fittable parameters info |
---|
60 | self.fittable_param=[] |
---|
61 | ##list of dispersion parameters |
---|
62 | self.disp_list=[] |
---|
63 | self.disp_name="" |
---|
64 | ## list of orientation parameters |
---|
65 | self.orientation_params=[] |
---|
66 | self.orientation_params_disp=[] |
---|
67 | if self.model !=None: |
---|
68 | self.disp_list= self.model.getDispParamList() |
---|
69 | |
---|
70 | ##enable model 2D draw |
---|
71 | self.enable2D= False |
---|
72 | ## check that the fit range is correct to plot the model again |
---|
73 | self.fitrange= True |
---|
74 | ## Q range |
---|
75 | self.qmin_x= 0.0001 |
---|
76 | self.qmax_x= 0.13 |
---|
77 | self.num_points= 50 |
---|
78 | |
---|
79 | ## Create memento to save the current state |
---|
80 | self.state= PageState(parent= self.parent,model=self.model, data=self.data) |
---|
81 | ## flag to determine if state has change |
---|
82 | self.state_change= False |
---|
83 | ## save customized array |
---|
84 | self.values=[] |
---|
85 | self.weights=[] |
---|
86 | ## retrieve saved state |
---|
87 | self.number_saved_state= 0 |
---|
88 | ## dictionary of saved state |
---|
89 | self.saved_states={} |
---|
90 | |
---|
91 | ## Create context menu for page |
---|
92 | self.popUpMenu = wx.Menu() |
---|
93 | id = wx.NewId() |
---|
94 | self._undo = wx.MenuItem(self.popUpMenu,id, "Undo","cancel the previous action") |
---|
95 | self.popUpMenu.AppendItem(self._undo) |
---|
96 | self._undo.Enable(False) |
---|
97 | wx.EVT_MENU(self, id, self.onUndo) |
---|
98 | |
---|
99 | id = wx.NewId() |
---|
100 | self._redo = wx.MenuItem(self.popUpMenu,id,"Redo"," Restore the previous action") |
---|
101 | self.popUpMenu.AppendItem(self._redo) |
---|
102 | self._redo.Enable(False) |
---|
103 | wx.EVT_MENU(self, id, self.onRedo) |
---|
104 | self.popUpMenu.AppendSeparator() |
---|
105 | |
---|
106 | id = wx.NewId() |
---|
107 | self._keep = wx.MenuItem(self.popUpMenu,id,"Keep"," Keep the panel status to recall it later") |
---|
108 | self.popUpMenu.AppendItem(self._keep) |
---|
109 | self._keep.Enable(True) |
---|
110 | wx.EVT_MENU(self, id, self.onSave) |
---|
111 | self.popUpMenu.AppendSeparator() |
---|
112 | |
---|
113 | ## Default locations |
---|
114 | self._default_save_location = os.getcwd() |
---|
115 | ## save initial state on context menu |
---|
116 | #self.onSave(event=None) |
---|
117 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
118 | |
---|
119 | ## create the basic structure of the panel with empty sizer |
---|
120 | self.define_page_structure() |
---|
121 | ## drawing Initial dispersion parameters sizer |
---|
122 | self.set_dispers_sizer() |
---|
123 | self._fill_save_sizer() |
---|
124 | ## layout |
---|
125 | self.set_layout() |
---|
126 | |
---|
127 | class ModelTextCtrl(wx.TextCtrl): |
---|
128 | """ |
---|
129 | Text control for model and fit parameters. |
---|
130 | Binds the appropriate events for user interactions. |
---|
131 | Default callback methods can be overwritten on initialization |
---|
132 | |
---|
133 | @param kill_focus_callback: callback method for EVT_KILL_FOCUS event |
---|
134 | @param set_focus_callback: callback method for EVT_SET_FOCUS event |
---|
135 | @param mouse_up_callback: callback method for EVT_LEFT_UP event |
---|
136 | @param text_enter_callback: callback method for EVT_TEXT_ENTER event |
---|
137 | """ |
---|
138 | def __init__(self, parent, id=-1, value=wx.EmptyString, pos=wx.DefaultPosition, |
---|
139 | size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.TextCtrlNameStr, |
---|
140 | kill_focus_callback = None, set_focus_callback = None, |
---|
141 | mouse_up_callback = None, text_enter_callback = None): |
---|
142 | |
---|
143 | wx.TextCtrl.__init__(self, parent, id, value, pos, size, style, validator, name) |
---|
144 | |
---|
145 | # Bind appropriate events |
---|
146 | self.Bind(wx.EVT_SET_FOCUS, parent.onSetFocus \ |
---|
147 | if set_focus_callback is None else set_focus_callback) |
---|
148 | self.Bind(wx.EVT_KILL_FOCUS, parent._onparamEnter \ |
---|
149 | if kill_focus_callback is None else kill_focus_callback) |
---|
150 | self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \ |
---|
151 | if text_enter_callback is None else text_enter_callback) |
---|
152 | self.Bind(wx.EVT_LEFT_UP, parent._highlight_text \ |
---|
153 | if mouse_up_callback is None else mouse_up_callback) |
---|
154 | |
---|
155 | def onContextMenu(self, event): |
---|
156 | """ |
---|
157 | Retrieve the state selected state |
---|
158 | """ |
---|
159 | # Skipping the save state functionality for release 0.9.0 |
---|
160 | #return |
---|
161 | |
---|
162 | pos = event.GetPosition() |
---|
163 | pos = self.ScreenToClient(pos) |
---|
164 | |
---|
165 | self.PopupMenu(self.popUpMenu, pos) |
---|
166 | |
---|
167 | |
---|
168 | def onUndo(self, event): |
---|
169 | """ |
---|
170 | Cancel the previous action |
---|
171 | """ |
---|
172 | #print "enable undo" |
---|
173 | event = PreviousStateEvent(page = self) |
---|
174 | wx.PostEvent(self.parent, event) |
---|
175 | |
---|
176 | |
---|
177 | def onRedo(self, event): |
---|
178 | """ |
---|
179 | Restore the previous action cancelled |
---|
180 | """ |
---|
181 | print "enable redo" |
---|
182 | event = NextStateEvent(page= self) |
---|
183 | wx.PostEvent(self.parent, event) |
---|
184 | |
---|
185 | |
---|
186 | def define_page_structure(self): |
---|
187 | """ |
---|
188 | Create empty sizer for a panel |
---|
189 | """ |
---|
190 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
191 | self.sizer0 = wx.BoxSizer(wx.VERTICAL) |
---|
192 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
193 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
194 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
195 | self.sizer4 = wx.BoxSizer(wx.VERTICAL) |
---|
196 | self.sizer5 = wx.BoxSizer(wx.VERTICAL) |
---|
197 | self.sizer6 = wx.BoxSizer(wx.VERTICAL) |
---|
198 | |
---|
199 | self.sizer0.SetMinSize((375,-1)) |
---|
200 | self.sizer1.SetMinSize((375,-1)) |
---|
201 | self.sizer2.SetMinSize((375,-1)) |
---|
202 | self.sizer3.SetMinSize((375,-1)) |
---|
203 | self.sizer4.SetMinSize((375,-1)) |
---|
204 | self.sizer5.SetMinSize((375,-1)) |
---|
205 | self.sizer6.SetMinSize((375,-1)) |
---|
206 | |
---|
207 | self.vbox.Add(self.sizer0) |
---|
208 | self.vbox.Add(self.sizer1) |
---|
209 | self.vbox.Add(self.sizer2) |
---|
210 | self.vbox.Add(self.sizer3) |
---|
211 | self.vbox.Add(self.sizer4) |
---|
212 | self.vbox.Add(self.sizer5) |
---|
213 | self.vbox.Add(self.sizer6) |
---|
214 | |
---|
215 | |
---|
216 | def set_layout(self): |
---|
217 | """ |
---|
218 | layout |
---|
219 | """ |
---|
220 | self.vbox.Layout() |
---|
221 | self.vbox.Fit(self) |
---|
222 | self.SetSizer(self.vbox) |
---|
223 | |
---|
224 | self.set_scroll() |
---|
225 | self.Centre() |
---|
226 | |
---|
227 | |
---|
228 | def set_scroll(self): |
---|
229 | self.SetScrollbars(20,20,200,100) |
---|
230 | self.Layout() |
---|
231 | self.SetAutoLayout(True) |
---|
232 | |
---|
233 | |
---|
234 | def set_owner(self,owner): |
---|
235 | """ |
---|
236 | set owner of fitpage |
---|
237 | @param owner: the class responsible of plotting |
---|
238 | """ |
---|
239 | self.event_owner = owner |
---|
240 | self.state.event_owner = owner |
---|
241 | |
---|
242 | def set_manager(self, manager): |
---|
243 | """ |
---|
244 | set panel manager |
---|
245 | @param manager: instance of plugin fitting |
---|
246 | """ |
---|
247 | self.manager = manager |
---|
248 | self.state.manager = manager |
---|
249 | |
---|
250 | def populate_box(self, dict): |
---|
251 | """ |
---|
252 | Store list of model |
---|
253 | @param dict: dictionary containing list of models |
---|
254 | """ |
---|
255 | self.model_list_box = dict |
---|
256 | self.state.model_list_box = self.model_list_box |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | def set_dispers_sizer(self): |
---|
261 | """ |
---|
262 | fill sizer containing dispersity info |
---|
263 | """ |
---|
264 | self.sizer4.Clear(True) |
---|
265 | name="Polydispersity and Orientational Distribution" |
---|
266 | box_description= wx.StaticBox(self, -1,name) |
---|
267 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
268 | #---------------------------------------------------- |
---|
269 | self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10), style=wx.RB_GROUP) |
---|
270 | self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30)) |
---|
271 | |
---|
272 | |
---|
273 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.disable_disp.GetId()) |
---|
274 | self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param, id=self.enable_disp.GetId()) |
---|
275 | |
---|
276 | sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL) |
---|
277 | sizer_dispersion.Add((20,20)) |
---|
278 | name=""#Polydispersity and \nOrientational Distribution " |
---|
279 | sizer_dispersion.Add(wx.StaticText(self,-1,name)) |
---|
280 | sizer_dispersion.Add(self.enable_disp ) |
---|
281 | sizer_dispersion.Add((20,20)) |
---|
282 | sizer_dispersion.Add(self.disable_disp ) |
---|
283 | sizer_dispersion.Add((10,10)) |
---|
284 | |
---|
285 | ## fill a sizer with the combobox to select dispersion type |
---|
286 | sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
287 | self.model_disp = wx.StaticText(self, -1, 'Distribution Function ') |
---|
288 | |
---|
289 | import sans.models.dispersion_models |
---|
290 | self.polydisp= sans.models.dispersion_models.models |
---|
291 | self.disp_box = wx.ComboBox(self, -1) |
---|
292 | |
---|
293 | for key, value in self.polydisp.iteritems(): |
---|
294 | name = str(key) |
---|
295 | self.disp_box.Append(name,value) |
---|
296 | |
---|
297 | self.disp_box.SetStringSelection("gaussian") |
---|
298 | wx.EVT_COMBOBOX(self.disp_box,-1, self._on_select_Disp) |
---|
299 | |
---|
300 | sizer_select_dispers.Add((10,10)) |
---|
301 | sizer_select_dispers.Add(self.model_disp) |
---|
302 | sizer_select_dispers.Add(self.disp_box,0, |
---|
303 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
304 | |
---|
305 | self.model_disp.Hide() |
---|
306 | self.disp_box.Hide() |
---|
307 | |
---|
308 | boxsizer1.Add( sizer_dispersion,0, |
---|
309 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
310 | #boxsizer1.Add( (10,10) ) |
---|
311 | boxsizer1.Add( sizer_select_dispers ) |
---|
312 | self.sizer4_4 = wx.GridBagSizer(5,5) |
---|
313 | boxsizer1.Add( self.sizer4_4 ) |
---|
314 | #----------------------------------------------------- |
---|
315 | self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
316 | self.sizer4_4.Layout() |
---|
317 | self.sizer4.Layout() |
---|
318 | self.Layout() |
---|
319 | self.SetScrollbars(20,20,200,100) |
---|
320 | self.Refresh() |
---|
321 | ## saving the state of enable dispersity button |
---|
322 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
323 | self.state.disable_disp= self.disable_disp.GetValue() |
---|
324 | |
---|
325 | |
---|
326 | def select_disp_angle(self, event): |
---|
327 | """ |
---|
328 | Event for when a user select a parameter to average over. |
---|
329 | @param event: check box event |
---|
330 | """ |
---|
331 | self.values=[] |
---|
332 | self.weights=[] |
---|
333 | # Go through the list of dispersion check boxes to identify which one has changed |
---|
334 | |
---|
335 | for p in self.disp_cb_dict: |
---|
336 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
337 | # Catch which one of the box was just checked or unchecked. |
---|
338 | if event.GetEventObject() == self.disp_cb_dict[p]: |
---|
339 | |
---|
340 | if self.disp_cb_dict[p].GetValue() == True: |
---|
341 | # The user wants this parameter to be averaged. |
---|
342 | # Pop up the file selection dialog. |
---|
343 | path = self._selectDlg() |
---|
344 | |
---|
345 | # If nothing was selected, just return |
---|
346 | if path is None: |
---|
347 | self.disp_cb_dict[p].SetValue(False) |
---|
348 | return |
---|
349 | try: |
---|
350 | self._default_save_location = os.path.dirname(path) |
---|
351 | except: |
---|
352 | pass |
---|
353 | try: |
---|
354 | self.values,self.weights = self.read_file(path) |
---|
355 | except: |
---|
356 | msg="Could not read input file" |
---|
357 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
358 | return |
---|
359 | |
---|
360 | # If any of the two arrays is empty, notify the user that we won't |
---|
361 | # proceed |
---|
362 | if self.values is None or self.weights is None or \ |
---|
363 | self.values ==[] or self.weights ==[]: |
---|
364 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
365 | "The loaded %s distrubtion is corrupted or empty" % p)) |
---|
366 | return |
---|
367 | |
---|
368 | # Tell the user that we are about to apply the distribution |
---|
369 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
370 | "Applying loaded %s distribution: %s" % (p, path))) |
---|
371 | |
---|
372 | # Create the dispersion objects |
---|
373 | from sans.models.dispersion_models import ArrayDispersion |
---|
374 | disp_model = ArrayDispersion() |
---|
375 | disp_model.set_weights(self.values, self.weights) |
---|
376 | |
---|
377 | # Store the object to make it persist outside the scope of this method |
---|
378 | #TODO: refactor model to clean this up? |
---|
379 | self._disp_obj_dict[p] = disp_model |
---|
380 | self.state._disp_obj_dict [p]= disp_model |
---|
381 | self.state.values=[] |
---|
382 | self.state.weights=[] |
---|
383 | self.state.values = copy.deepcopy(self.values) |
---|
384 | self.state.weights = copy.deepcopy(self.weights) |
---|
385 | # Set the new model as the dispersion object for the selected parameter |
---|
386 | self.model.set_dispersion(p, disp_model) |
---|
387 | # Store a reference to the weights in the model object so that |
---|
388 | # it's not lost when we use the model within another thread. |
---|
389 | #TODO: total hack - fix this |
---|
390 | self.state.model= self.model.clone() |
---|
391 | #if not hasattr(self.model, "_persistency_dict"): |
---|
392 | self.model._persistency_dict = {} |
---|
393 | self.model._persistency_dict[p] = [self.values, self.weights] |
---|
394 | self.state.model._persistency_dict[p] = [self.values, self.weights] |
---|
395 | else: |
---|
396 | self._reset_dispersity() |
---|
397 | |
---|
398 | ## Redraw the model ??? |
---|
399 | self._draw_model() |
---|
400 | |
---|
401 | ## post state to fit panel |
---|
402 | event = PageInfoEvent(page = self) |
---|
403 | wx.PostEvent(self.parent, event) |
---|
404 | |
---|
405 | |
---|
406 | def onResetModel(self, event): |
---|
407 | """ |
---|
408 | Reset model state |
---|
409 | """ |
---|
410 | ## post help message for the selected model |
---|
411 | msg = self.popUpMenu.GetHelpString(event.GetId()) |
---|
412 | msg +=" reloaded" |
---|
413 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
414 | |
---|
415 | name= self.popUpMenu.GetLabel(event.GetId()) |
---|
416 | self._on_select_model_helper() |
---|
417 | |
---|
418 | if name in self.saved_states.keys(): |
---|
419 | previous_state = self.saved_states[name] |
---|
420 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
421 | self.reset_page(previous_state) |
---|
422 | |
---|
423 | def onSave(self, event): |
---|
424 | """ |
---|
425 | save history of the data and model |
---|
426 | """ |
---|
427 | if self.model==None: |
---|
428 | return |
---|
429 | if hasattr(self,"enable_disp"): |
---|
430 | self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue()) |
---|
431 | if hasattr(self, "disp_box"): |
---|
432 | self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection()) |
---|
433 | |
---|
434 | self.state.model = self.model.clone() |
---|
435 | self.state.model.name= self.model.name |
---|
436 | #if not hasattr(self.model, "_persistency_dict"): |
---|
437 | #self.model._persistency_dict = {} |
---|
438 | #self.state.model._persistency_dict= copy.deepcopy(self.model._persistency_dict) |
---|
439 | |
---|
440 | new_state = self.state.clone() |
---|
441 | new_state.model.name = self.state.model.name |
---|
442 | |
---|
443 | new_state.enable2D = copy.deepcopy(self.enable2D) |
---|
444 | ##Add model state on context menu |
---|
445 | self.number_saved_state += 1 |
---|
446 | name= self.model.name+"[%g]"%self.number_saved_state |
---|
447 | self.saved_states[name]= new_state |
---|
448 | |
---|
449 | ## Add item in the context menu |
---|
450 | |
---|
451 | year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime() |
---|
452 | my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" " |
---|
453 | date= str( month)+"|"+str(day)+"|"+str(year) |
---|
454 | msg= "Model saved at %s on %s"%(my_time, date) |
---|
455 | ## post help message for the selected model |
---|
456 | msg +=" Saved! right click on this page to retrieve this model" |
---|
457 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
458 | |
---|
459 | id = wx.NewId() |
---|
460 | self.popUpMenu.Append(id,name,str(msg)) |
---|
461 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
462 | |
---|
463 | |
---|
464 | def onSetFocus(self, evt): |
---|
465 | """ |
---|
466 | highlight the current textcrtl and hide the error text control shown |
---|
467 | after fitting |
---|
468 | """ |
---|
469 | |
---|
470 | if hasattr(self,"text2_3"): |
---|
471 | self.text2_3.Hide() |
---|
472 | if len(self.parameters)>0: |
---|
473 | for item in self.parameters: |
---|
474 | ## hide statictext +/- |
---|
475 | if item[3]!=None: |
---|
476 | item[3].Hide() |
---|
477 | ## hide textcrtl for error after fit |
---|
478 | if item[4]!=None: |
---|
479 | item[4].Clear() |
---|
480 | item[4].Hide() |
---|
481 | if len(self.fittable_param)>0: |
---|
482 | for item in self.fittable_param: |
---|
483 | ## hide statictext +/- |
---|
484 | if item[3]!=None: |
---|
485 | item[3].Hide() |
---|
486 | ## hide textcrtl for error after fit |
---|
487 | if item[4]!=None: |
---|
488 | item[4].Clear() |
---|
489 | item[4].Hide() |
---|
490 | self.Layout() |
---|
491 | return |
---|
492 | |
---|
493 | def _highlight_text(self, event): |
---|
494 | """ |
---|
495 | Highlight text of a TextCtrl only of no text has be selected |
---|
496 | @param event: mouse event |
---|
497 | """ |
---|
498 | control = event.GetEventObject() |
---|
499 | # Check that we have a TextCtrl |
---|
500 | if issubclass(control.__class__, wx.TextCtrl): |
---|
501 | # Check whether text has been selected, |
---|
502 | # if not, select the whole string |
---|
503 | |
---|
504 | (start, end) = control.GetSelection() |
---|
505 | if start==end: |
---|
506 | control.SetSelection(-1,-1) |
---|
507 | |
---|
508 | # Make sure the mouse event is available to other listeners |
---|
509 | event.Skip() |
---|
510 | |
---|
511 | |
---|
512 | def read_file(self, path): |
---|
513 | """ |
---|
514 | Read two columns file |
---|
515 | @param path: the path to the file to read |
---|
516 | """ |
---|
517 | try: |
---|
518 | if path==None: |
---|
519 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
520 | " Selected Distribution was not loaded: %s"%path)) |
---|
521 | return None, None |
---|
522 | input_f = open(path, 'r') |
---|
523 | buff = input_f.read() |
---|
524 | lines = buff.split('\n') |
---|
525 | |
---|
526 | angles = [] |
---|
527 | weights=[] |
---|
528 | for line in lines: |
---|
529 | toks = line.split() |
---|
530 | try: |
---|
531 | angle = float(toks[0]) |
---|
532 | weight = float(toks[1]) |
---|
533 | except: |
---|
534 | # Skip non-data lines |
---|
535 | pass |
---|
536 | angles.append(angle) |
---|
537 | weights.append(weight) |
---|
538 | return numpy.array(angles), numpy.array(weights) |
---|
539 | except: |
---|
540 | raise |
---|
541 | |
---|
542 | |
---|
543 | def createMemento(self): |
---|
544 | """ |
---|
545 | return the current state of the page |
---|
546 | """ |
---|
547 | return self.state.clone() |
---|
548 | |
---|
549 | |
---|
550 | def save_current_state(self): |
---|
551 | """ |
---|
552 | Store current state |
---|
553 | """ |
---|
554 | ## save model option |
---|
555 | if self.model!= None: |
---|
556 | self.disp_list= self.model.getDispParamList() |
---|
557 | self.state.disp_list= copy.deepcopy(self.disp_list) |
---|
558 | self.state.model = self.model.clone() |
---|
559 | |
---|
560 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
561 | self.state.values= copy.deepcopy(self.values) |
---|
562 | self.state.weights = copy.deepcopy( self.weights) |
---|
563 | ## save data |
---|
564 | self.state.data= copy.deepcopy(self.data) |
---|
565 | try: |
---|
566 | n = self.disp_box.GetCurrentSelection() |
---|
567 | dispersity= self.disp_box.GetClientData(n) |
---|
568 | name= dispersity.__name__ |
---|
569 | self.disp_name = name |
---|
570 | if name == "GaussianDispersion" : |
---|
571 | if hasattr(self,"cb1"): |
---|
572 | self.state.cb1= self.cb1.GetValue() |
---|
573 | except: |
---|
574 | pass |
---|
575 | |
---|
576 | if hasattr(self,"enable_disp"): |
---|
577 | self.state.enable_disp= self.enable_disp.GetValue() |
---|
578 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
579 | |
---|
580 | self.state.smearer = copy.deepcopy(self.smearer) |
---|
581 | if hasattr(self,"enable_smearer"): |
---|
582 | self.state.enable_smearer = copy.deepcopy(self.enable_smearer.GetValue()) |
---|
583 | self.state.disable_smearer = copy.deepcopy(self.disable_smearer.GetValue()) |
---|
584 | |
---|
585 | if hasattr(self,"disp_box"): |
---|
586 | self.state.disp_box = self.disp_box.GetCurrentSelection() |
---|
587 | |
---|
588 | if len(self.disp_cb_dict)>0: |
---|
589 | for k , v in self.disp_cb_dict.iteritems(): |
---|
590 | |
---|
591 | if v ==None : |
---|
592 | self.state.disp_cb_dict[k]= v |
---|
593 | else: |
---|
594 | try: |
---|
595 | self.state.disp_cb_dict[k]=v.GetValue() |
---|
596 | except: |
---|
597 | self.state.disp_cb_dict[k]= None |
---|
598 | |
---|
599 | if len(self._disp_obj_dict)>0: |
---|
600 | for k , v in self._disp_obj_dict.iteritems(): |
---|
601 | |
---|
602 | self.state._disp_obj_dict[k]= v |
---|
603 | |
---|
604 | |
---|
605 | self.state.values = copy.deepcopy(self.values) |
---|
606 | self.state.weights = copy.deepcopy(self.weights) |
---|
607 | ## save plotting range |
---|
608 | self._save_plotting_range() |
---|
609 | |
---|
610 | self.state.orientation_params =[] |
---|
611 | self.state.orientation_params_disp =[] |
---|
612 | self.state.parameters =[] |
---|
613 | self.state.fittable_param =[] |
---|
614 | self.state.fixed_param =[] |
---|
615 | |
---|
616 | |
---|
617 | ## save checkbutton state and txtcrtl values |
---|
618 | self._copy_parameters_state(self.orientation_params, |
---|
619 | self.state.orientation_params) |
---|
620 | self._copy_parameters_state(self.orientation_params_disp, |
---|
621 | self.state.orientation_params_disp) |
---|
622 | |
---|
623 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
624 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
625 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
626 | |
---|
627 | |
---|
628 | def reset_page_helper(self, state): |
---|
629 | """ |
---|
630 | Use page_state and change the state of existing page |
---|
631 | @precondition: the page is already drawn or created |
---|
632 | @postcondition: the state of the underlying data change as well as the |
---|
633 | state of the graphic interface |
---|
634 | """ |
---|
635 | if state ==None: |
---|
636 | self._undo.Enable(False) |
---|
637 | return |
---|
638 | |
---|
639 | self.model= state.model |
---|
640 | self.data = state.data |
---|
641 | self.smearer= state.smearer |
---|
642 | self.enable2D= state.enable2D |
---|
643 | |
---|
644 | #??? |
---|
645 | self.disp_cb_dict = state.disp_cb_dict |
---|
646 | self.disp_list =state.disp_list |
---|
647 | |
---|
648 | |
---|
649 | ## set the state of the radio box |
---|
650 | self.shape_rbutton.SetValue(state.shape_rbutton ) |
---|
651 | self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton) |
---|
652 | self.struct_rbutton.SetValue(state.struct_rbutton ) |
---|
653 | self.plugin_rbutton.SetValue(state.plugin_rbutton) |
---|
654 | ##draw sizer containing model parameters value for the current model |
---|
655 | self._set_model_sizer_selection( self.model ) |
---|
656 | self.set_model_param_sizer(self.model) |
---|
657 | |
---|
658 | ## reset value of combox box |
---|
659 | self.structurebox.SetSelection(state.structurecombobox ) |
---|
660 | self.formfactorbox.SetSelection(state.formfactorcombobox) |
---|
661 | |
---|
662 | |
---|
663 | ## enable the view 2d button if this is a modelpage type |
---|
664 | if hasattr(self,"model_view"): |
---|
665 | if self.enable2D: |
---|
666 | self.model_view.Disable() |
---|
667 | else: |
---|
668 | self.model_view.Enable() |
---|
669 | ## set the select all check box to the a given state |
---|
670 | if hasattr(self, "cb1"): |
---|
671 | self.cb1.SetValue(state.cb1) |
---|
672 | |
---|
673 | ## reset state of checkbox,textcrtl and regular parameters value |
---|
674 | |
---|
675 | self._reset_parameters_state(self.orientation_params_disp, |
---|
676 | state.orientation_params_disp) |
---|
677 | self._reset_parameters_state(self.orientation_params, |
---|
678 | state.orientation_params) |
---|
679 | self._reset_parameters_state(self.parameters,state.parameters) |
---|
680 | ## display dispersion info layer |
---|
681 | self.enable_disp.SetValue(state.enable_disp) |
---|
682 | self.disable_disp.SetValue(state.disable_disp) |
---|
683 | |
---|
684 | if hasattr(self, "disp_box"): |
---|
685 | |
---|
686 | self.disp_box.SetSelection(state.disp_box) |
---|
687 | n= self.disp_box.GetCurrentSelection() |
---|
688 | dispersity= self.disp_box.GetClientData(n) |
---|
689 | name= dispersity.__name__ |
---|
690 | |
---|
691 | self._set_dipers_Param(event=None) |
---|
692 | |
---|
693 | #self.disp_cb_dict = {} |
---|
694 | #for k,v in self.state.disp_cb_dict.iteritems(): |
---|
695 | # self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
696 | # self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
697 | |
---|
698 | if name=="ArrayDispersion": |
---|
699 | |
---|
700 | for item in self.disp_cb_dict.keys(): |
---|
701 | |
---|
702 | if hasattr(self.disp_cb_dict[item],"SetValue") : |
---|
703 | self.disp_cb_dict[item].SetValue(state.disp_cb_dict[item]) |
---|
704 | # Create the dispersion objects |
---|
705 | from sans.models.dispersion_models import ArrayDispersion |
---|
706 | disp_model = ArrayDispersion() |
---|
707 | if hasattr(state,"values")and self.disp_cb_dict[item].GetValue()==True: |
---|
708 | if len(state.values)>0: |
---|
709 | self.values=state.values |
---|
710 | self.weights=state.weights |
---|
711 | disp_model.set_weights(self.values, state.weights) |
---|
712 | else: |
---|
713 | self._reset_dispersity() |
---|
714 | |
---|
715 | self._disp_obj_dict[item] = disp_model |
---|
716 | # Set the new model as the dispersion object for the selected parameter |
---|
717 | self.model.set_dispersion(item, disp_model) |
---|
718 | |
---|
719 | #self.model._persistency_dict = {} |
---|
720 | self.model._persistency_dict[item] = [state.values, state.weights] |
---|
721 | |
---|
722 | else: |
---|
723 | |
---|
724 | for k,v in self.state.disp_cb_dict.iteritems(): |
---|
725 | self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
726 | self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
727 | |
---|
728 | #self._disp_obj_dict={} |
---|
729 | #for k , v in self.state._disp_obj_dict.iteritems(): |
---|
730 | # self._disp_obj_dict[k]=v |
---|
731 | |
---|
732 | ##plotting range restore |
---|
733 | self._reset_plotting_range(state) |
---|
734 | ## smearing info restore |
---|
735 | if hasattr(self,"enable_smearer"): |
---|
736 | ## set smearing value whether or not the data contain the smearing info |
---|
737 | self.enable_smearer.SetValue(state.enable_smearer) |
---|
738 | self.disable_smearer.SetValue(state.disable_smearer) |
---|
739 | self.tcChi.SetLabel(str( state.tcChi)) |
---|
740 | self.sizer5.Layout() |
---|
741 | self.Layout() |
---|
742 | self.Refresh() |
---|
743 | #self.compute_chisqr(smearer= self.smearer) |
---|
744 | #self._draw_model() |
---|
745 | ## reset state of checkbox,textcrtl and dispersity parameters value |
---|
746 | self._reset_parameters_state(self.fittable_param,state.fittable_param) |
---|
747 | self._reset_parameters_state(self.fixed_param,state.fixed_param) |
---|
748 | |
---|
749 | ## draw the model with previous parameters value |
---|
750 | self._onparamEnter_helper() |
---|
751 | |
---|
752 | ## reset context menu items |
---|
753 | self._reset_context_menu() |
---|
754 | |
---|
755 | #self._reset_dispersity() |
---|
756 | #self._set_dipers_Param(event=None) #to bo removed //resets disper para value |
---|
757 | |
---|
758 | #self.disp_cb_dict = {} |
---|
759 | #for k,v in self.state.disp_cb_dict.iteritems(): |
---|
760 | #self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
761 | #self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) |
---|
762 | |
---|
763 | |
---|
764 | ## set the value of the current state to the state given as parameter |
---|
765 | self.state = state.clone() |
---|
766 | self._draw_model() |
---|
767 | |
---|
768 | |
---|
769 | def _selectDlg(self): |
---|
770 | """ |
---|
771 | open a dialog file to selected the customized dispersity |
---|
772 | """ |
---|
773 | import os |
---|
774 | dlg = wx.FileDialog(self, "Choose a weight file", |
---|
775 | self._default_save_location , "", "*.*", wx.OPEN) |
---|
776 | path = None |
---|
777 | if dlg.ShowModal() == wx.ID_OK: |
---|
778 | path = dlg.GetPath() |
---|
779 | dlg.Destroy() |
---|
780 | return path |
---|
781 | |
---|
782 | |
---|
783 | def _reset_context_menu(self): |
---|
784 | """ |
---|
785 | reset the context menu |
---|
786 | """ |
---|
787 | for name, state in self.state.saved_states.iteritems(): |
---|
788 | self.number_saved_state += 1 |
---|
789 | ## Add item in the context menu |
---|
790 | id = wx.NewId() |
---|
791 | self.popUpMenu.Append(id,name, 'Save model and state %g'%self.number_saved_state) |
---|
792 | wx.EVT_MENU(self, id, self.onResetModel) |
---|
793 | |
---|
794 | |
---|
795 | def _reset_plotting_range(self, state): |
---|
796 | """ |
---|
797 | Reset the plotting range to a given state |
---|
798 | """ |
---|
799 | self.qmin.SetValue(str(state.qmin)) |
---|
800 | self.qmax.SetValue(str(state.qmax)) |
---|
801 | if self.state.npts!=None: |
---|
802 | self.npts.SetValue(str(state.npts)) |
---|
803 | |
---|
804 | |
---|
805 | def _save_typeOfmodel(self): |
---|
806 | """ |
---|
807 | save radiobutton containing the type model that can be selected |
---|
808 | """ |
---|
809 | self.state.shape_rbutton = self.shape_rbutton.GetValue() |
---|
810 | self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue() |
---|
811 | self.state.struct_rbutton = self.struct_rbutton.GetValue() |
---|
812 | self.state.plugin_rbutton = self.plugin_rbutton.GetValue() |
---|
813 | self.state.structurebox= self.structurebox.GetCurrentSelection() |
---|
814 | self.state.formfactorbox = self.formfactorbox.GetCurrentSelection() |
---|
815 | |
---|
816 | self._undo.Enable(True) |
---|
817 | ## post state to fit panel |
---|
818 | event = PageInfoEvent(page = self) |
---|
819 | wx.PostEvent(self.parent, event) |
---|
820 | |
---|
821 | |
---|
822 | def _save_plotting_range(self ): |
---|
823 | """ |
---|
824 | save the state of plotting range |
---|
825 | """ |
---|
826 | self.state.qmin = self.qmin_x |
---|
827 | self.state.qmax = self.qmax_x |
---|
828 | if self.npts!=None: |
---|
829 | self.state.npts= self.num_points |
---|
830 | |
---|
831 | |
---|
832 | def _onparamEnter_helper(self): |
---|
833 | """ |
---|
834 | check if values entered by the user are changed and valid to replot |
---|
835 | model |
---|
836 | use : _check_value_enter |
---|
837 | """ |
---|
838 | self._undo.Enable(True) |
---|
839 | if self.model !=None: |
---|
840 | |
---|
841 | |
---|
842 | # Flag to register when a parameter has changed. |
---|
843 | is_modified = False |
---|
844 | is_modified =self._check_value_enter( self.fittable_param ,is_modified) |
---|
845 | is_modified =self._check_value_enter( self.fixed_param ,is_modified) |
---|
846 | is_modified =self._check_value_enter( self.parameters ,is_modified) |
---|
847 | |
---|
848 | self.sizer3.Layout() |
---|
849 | self.Layout() |
---|
850 | self.Refresh() |
---|
851 | # Here we should check whether the boundaries have been modified. |
---|
852 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
853 | # set the is_modified flag to True |
---|
854 | from sans.guiframe.utils import check_value |
---|
855 | if check_value( self.qmin, self.qmax): |
---|
856 | if float(self.qmin.GetValue()) != self.qmin_x: |
---|
857 | self.qmin_x = float(self.qmin.GetValue()) |
---|
858 | is_modified = True |
---|
859 | if float(self.qmax.GetValue()) != self.qmax_x: |
---|
860 | self.qmax_x = float(self.qmax.GetValue()) |
---|
861 | is_modified = True |
---|
862 | self.fitrange = True |
---|
863 | else: |
---|
864 | self.fitrange = False |
---|
865 | if self.npts != None: |
---|
866 | if check_float(self.npts): |
---|
867 | if float(self.npts.GetValue()) != self.num_points: |
---|
868 | self.num_points = float(self.npts.GetValue()) |
---|
869 | is_modified = True |
---|
870 | else: |
---|
871 | msg= "Cannot Plot :Must enter a number!!! " |
---|
872 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
873 | |
---|
874 | |
---|
875 | ## if any value is modify draw model with new value |
---|
876 | if is_modified: |
---|
877 | self.state_change= True |
---|
878 | self._draw_model() |
---|
879 | self.save_current_state() |
---|
880 | |
---|
881 | |
---|
882 | def _reset_parameters_state(self, listtorestore,statelist): |
---|
883 | """ |
---|
884 | Reset the parameters at the given state |
---|
885 | """ |
---|
886 | if len(statelist)==0 or len(listtorestore)==0 : |
---|
887 | return |
---|
888 | if len(statelist)!= len(listtorestore) : |
---|
889 | return |
---|
890 | for j in range(len(listtorestore)): |
---|
891 | item_page = listtorestore[j] |
---|
892 | item_page_info = statelist[j] |
---|
893 | ##change the state of the check box for simple parameters |
---|
894 | if item_page[0]!=None: |
---|
895 | item_page[0].SetValue(item_page_info[0]) |
---|
896 | |
---|
897 | if item_page[2]!=None: |
---|
898 | item_page[2].SetValue(item_page_info[2]) |
---|
899 | |
---|
900 | if item_page[3]!=None: |
---|
901 | ## show or hide text +/- |
---|
902 | if item_page_info[2]: |
---|
903 | item_page[3].Show(True) |
---|
904 | else: |
---|
905 | item_page[3].Hide() |
---|
906 | if item_page[4]!=None: |
---|
907 | ## show of hide the text crtl for fitting error |
---|
908 | if item_page_info[4][0]: |
---|
909 | item_page[4].Show(True) |
---|
910 | item_page[4].SetValue(item_page_info[4][1]) |
---|
911 | else: |
---|
912 | item_page[3].Hide() |
---|
913 | if item_page[5]!=None: |
---|
914 | ## show of hide the text crtl for fitting error |
---|
915 | if item_page_info[5][0]: |
---|
916 | item_page[5].Show(True) |
---|
917 | item_page[5].SetValue(item_page_info[4][1]) |
---|
918 | else: |
---|
919 | item_page[5].Hide() |
---|
920 | |
---|
921 | if item_page[6]!=None: |
---|
922 | ## show of hide the text crtl for fitting error |
---|
923 | if item_page_info[6][0]: |
---|
924 | item_page[6].Show(True) |
---|
925 | item_page[6].SetValue(item_page_info[6][1]) |
---|
926 | else: |
---|
927 | item_page[6].Hide() |
---|
928 | |
---|
929 | |
---|
930 | def _copy_parameters_state(self, listtocopy, statelist): |
---|
931 | """ |
---|
932 | copy the state of button |
---|
933 | @param listtocopy: the list of check button to copy |
---|
934 | @param statelist: list of state object to store the current state |
---|
935 | """ |
---|
936 | if len(listtocopy)==0: |
---|
937 | return |
---|
938 | |
---|
939 | for item in listtocopy: |
---|
940 | |
---|
941 | checkbox_state = None |
---|
942 | if item[0]!= None: |
---|
943 | checkbox_state= item[0].GetValue() |
---|
944 | parameter_name = item[1] |
---|
945 | parameter_value = None |
---|
946 | if item[2]!=None: |
---|
947 | parameter_value = item[2].GetValue() |
---|
948 | static_text = None |
---|
949 | if item[3]!=None: |
---|
950 | static_text = item[3].IsShown() |
---|
951 | error_value = None |
---|
952 | error_state = None |
---|
953 | if item[4]!= None: |
---|
954 | error_value = item[4].GetValue() |
---|
955 | error_state = item[4].IsShown() |
---|
956 | |
---|
957 | min_value = None |
---|
958 | min_state = None |
---|
959 | if item[5]!= None: |
---|
960 | min_value = item[5].GetValue() |
---|
961 | min_state = item[5].IsShown() |
---|
962 | |
---|
963 | max_value = None |
---|
964 | max_state = None |
---|
965 | if item[6]!= None: |
---|
966 | max_value = item[6].GetValue() |
---|
967 | max_state = item[6].IsShown() |
---|
968 | unit=None |
---|
969 | if item[7]!=None: |
---|
970 | unit = item[7].GetLabel() |
---|
971 | |
---|
972 | statelist.append([checkbox_state, parameter_name, parameter_value, |
---|
973 | static_text ,[error_state,error_value], |
---|
974 | [min_state,min_value],[max_state , max_value],unit]) |
---|
975 | |
---|
976 | |
---|
977 | |
---|
978 | def _set_model_sizer_selection(self, model): |
---|
979 | """ |
---|
980 | Display the sizer according to the type of the current model |
---|
981 | """ |
---|
982 | if model ==None: |
---|
983 | return |
---|
984 | if hasattr(model ,"s_model"): |
---|
985 | |
---|
986 | class_name= model.s_model.__class__ |
---|
987 | name= model.s_model.name |
---|
988 | flag= name != "NoStructure" |
---|
989 | if flag and (class_name in self.model_list_box["Structure Factors"]): |
---|
990 | self.structurebox.Show() |
---|
991 | self.text2.Show() |
---|
992 | self.structurebox.Enable() |
---|
993 | self.text2.Enable() |
---|
994 | items = self.structurebox.GetItems() |
---|
995 | self.sizer1.Layout() |
---|
996 | self.SetScrollbars(20,20,200,100) |
---|
997 | for i in range(len(items)): |
---|
998 | if items[i]== str(name): |
---|
999 | self.structurebox.SetSelection(i) |
---|
1000 | break |
---|
1001 | |
---|
1002 | if hasattr(model ,"p_model"): |
---|
1003 | class_name = model.p_model.__class__ |
---|
1004 | name = model.p_model.name |
---|
1005 | self.formfactorbox.Clear() |
---|
1006 | |
---|
1007 | for k, list in self.model_list_box.iteritems(): |
---|
1008 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1009 | self.shape_rbutton.SetValue(True) |
---|
1010 | ## fill the form factor list with new model |
---|
1011 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1012 | items = self.formfactorbox.GetItems() |
---|
1013 | ## set comboxbox to the selected item |
---|
1014 | for i in range(len(items)): |
---|
1015 | if items[i]== str(name): |
---|
1016 | self.formfactorbox.SetSelection(i) |
---|
1017 | break |
---|
1018 | return |
---|
1019 | elif k == "Shape-Independent": |
---|
1020 | self.shape_indep_rbutton.SetValue(True) |
---|
1021 | elif k == "Structure Factors": |
---|
1022 | self.struct_rbutton.SetValue(True) |
---|
1023 | else: |
---|
1024 | self.plugin_rbutton.SetValue(True) |
---|
1025 | |
---|
1026 | if class_name in list: |
---|
1027 | ## fill the form factor list with new model |
---|
1028 | self._populate_box(self.formfactorbox, list) |
---|
1029 | items = self.formfactorbox.GetItems() |
---|
1030 | ## set comboxbox to the selected item |
---|
1031 | for i in range(len(items)): |
---|
1032 | if items[i]== str(name): |
---|
1033 | self.formfactorbox.SetSelection(i) |
---|
1034 | break |
---|
1035 | break |
---|
1036 | else: |
---|
1037 | |
---|
1038 | ## Select the model from the menu |
---|
1039 | class_name = model.__class__ |
---|
1040 | name = model.name |
---|
1041 | self.formfactorbox.Clear() |
---|
1042 | items = self.formfactorbox.GetItems() |
---|
1043 | |
---|
1044 | for k, list in self.model_list_box.iteritems(): |
---|
1045 | if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]: |
---|
1046 | if class_name in self.model_list_box["P(Q)*S(Q)"]: |
---|
1047 | self.structurebox.Show() |
---|
1048 | self.text2.Show() |
---|
1049 | self.structurebox.Enable() |
---|
1050 | self.structurebox.SetSelection(0) |
---|
1051 | self.text2.Enable() |
---|
1052 | else: |
---|
1053 | self.structurebox.Hide() |
---|
1054 | self.text2.Hide() |
---|
1055 | self.structurebox.Disable() |
---|
1056 | self.structurebox.SetSelection(0) |
---|
1057 | self.text2.Disable() |
---|
1058 | |
---|
1059 | self.shape_rbutton.SetValue(True) |
---|
1060 | ## fill the form factor list with new model |
---|
1061 | self._populate_box(self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1062 | items = self.formfactorbox.GetItems() |
---|
1063 | ## set comboxbox to the selected item |
---|
1064 | for i in range(len(items)): |
---|
1065 | if items[i]== str(name): |
---|
1066 | self.formfactorbox.SetSelection(i) |
---|
1067 | break |
---|
1068 | return |
---|
1069 | elif k == "Shape-Independent": |
---|
1070 | self.shape_indep_rbutton.SetValue(True) |
---|
1071 | elif k == "Structure Factors": |
---|
1072 | self.struct_rbutton.SetValue(True) |
---|
1073 | else: |
---|
1074 | self.plugin_rbutton.SetValue(True) |
---|
1075 | if class_name in list: |
---|
1076 | self.structurebox.SetSelection(0) |
---|
1077 | self.structurebox.Disable() |
---|
1078 | self.text2.Disable() |
---|
1079 | ## fill the form factor list with new model |
---|
1080 | self._populate_box(self.formfactorbox, list) |
---|
1081 | items = self.formfactorbox.GetItems() |
---|
1082 | ## set comboxbox to the selected item |
---|
1083 | for i in range(len(items)): |
---|
1084 | if items[i]== str(name): |
---|
1085 | self.formfactorbox.SetSelection(i) |
---|
1086 | break |
---|
1087 | break |
---|
1088 | |
---|
1089 | |
---|
1090 | def _draw_model(self): |
---|
1091 | """ |
---|
1092 | Method to draw or refresh a plotted model. |
---|
1093 | The method will use the data member from the model page |
---|
1094 | to build a call to the fitting perspective manager. |
---|
1095 | |
---|
1096 | [Note to coder: This way future changes will be done in only one place.] |
---|
1097 | """ |
---|
1098 | if self.model !=None: |
---|
1099 | temp_smear=None |
---|
1100 | if hasattr(self, "enable_smearer"): |
---|
1101 | if self.enable_smearer.GetValue(): |
---|
1102 | temp_smear= self.smearer |
---|
1103 | |
---|
1104 | self.manager.draw_model(self.model, data=self.data, |
---|
1105 | smearer= temp_smear, |
---|
1106 | qmin=float(self.qmin_x), qmax=float(self.qmax_x), |
---|
1107 | qstep= float(self.num_points), |
---|
1108 | enable2D=self.enable2D) |
---|
1109 | |
---|
1110 | |
---|
1111 | def _set_model_sizer(self, sizer, title="", object=None): |
---|
1112 | """ |
---|
1113 | Use lists to fill a sizer for model info |
---|
1114 | """ |
---|
1115 | |
---|
1116 | sizer.Clear(True) |
---|
1117 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1118 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1119 | #-------------------------------------------------------- |
---|
1120 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', style=wx.RB_GROUP) |
---|
1121 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
1122 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
1123 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
1124 | |
---|
1125 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1126 | id= self.shape_rbutton.GetId() ) |
---|
1127 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1128 | id= self.shape_indep_rbutton.GetId() ) |
---|
1129 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1130 | id= self.struct_rbutton.GetId() ) |
---|
1131 | self.Bind( wx.EVT_RADIOBUTTON, self._show_combox, |
---|
1132 | id= self.plugin_rbutton.GetId() ) |
---|
1133 | |
---|
1134 | |
---|
1135 | sizer_radiobutton = wx.GridSizer(2, 2,5, 5) |
---|
1136 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
1137 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
1138 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
1139 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
1140 | |
---|
1141 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
1142 | |
---|
1143 | self.text1 = wx.StaticText( self,-1,"" ) |
---|
1144 | self.text2 = wx.StaticText( self,-1,"P(Q)*S(Q)" ) |
---|
1145 | |
---|
1146 | |
---|
1147 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1148 | if self.model!=None: |
---|
1149 | self.formfactorbox.SetValue(self.model.name) |
---|
1150 | |
---|
1151 | |
---|
1152 | self.structurebox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
1153 | wx.EVT_COMBOBOX(self.formfactorbox,-1, self._on_select_model) |
---|
1154 | wx.EVT_COMBOBOX(self.structurebox,-1, self._on_select_model) |
---|
1155 | |
---|
1156 | |
---|
1157 | ## fill combox box |
---|
1158 | if len(self.model_list_box)>0: |
---|
1159 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1160 | |
---|
1161 | if len(self.model_list_box)>0: |
---|
1162 | self._populate_box( self.structurebox, |
---|
1163 | self.model_list_box["Structure Factors"]) |
---|
1164 | self.structurebox.Insert("None", 0,None) |
---|
1165 | self.structurebox.SetSelection(0) |
---|
1166 | self.structurebox.Hide() |
---|
1167 | self.text2.Hide() |
---|
1168 | self.structurebox.Disable() |
---|
1169 | self.text2.Disable() |
---|
1170 | |
---|
1171 | if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]: |
---|
1172 | self.structurebox.Show() |
---|
1173 | self.text2.Show() |
---|
1174 | self.structurebox.Enable() |
---|
1175 | self.text2.Enable() |
---|
1176 | |
---|
1177 | ## check model type to show sizer |
---|
1178 | if self.model !=None: |
---|
1179 | self._set_model_sizer_selection( self.model ) |
---|
1180 | |
---|
1181 | sizer_selection.Add(self.text1) |
---|
1182 | sizer_selection.Add((5,5)) |
---|
1183 | sizer_selection.Add(self.formfactorbox) |
---|
1184 | sizer_selection.Add((5,5)) |
---|
1185 | sizer_selection.Add(self.text2) |
---|
1186 | sizer_selection.Add((5,5)) |
---|
1187 | sizer_selection.Add(self.structurebox) |
---|
1188 | sizer_selection.Add((5,5)) |
---|
1189 | |
---|
1190 | boxsizer1.Add( sizer_radiobutton ) |
---|
1191 | boxsizer1.Add( (20,20)) |
---|
1192 | boxsizer1.Add( sizer_selection ) |
---|
1193 | if object !=None: |
---|
1194 | boxsizer1.Add( (-72,-72)) |
---|
1195 | boxsizer1.Add( object, 0, wx.ALIGN_RIGHT| wx.RIGHT, 10) |
---|
1196 | boxsizer1.Add( (60,60)) |
---|
1197 | #-------------------------------------------------------- |
---|
1198 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1199 | sizer.Layout() |
---|
1200 | self.SetScrollbars(20,20,200,100) |
---|
1201 | |
---|
1202 | |
---|
1203 | def _show_combox(self, event): |
---|
1204 | """ |
---|
1205 | Show combox box associate with type of model selected |
---|
1206 | """ |
---|
1207 | ## Don't want to populate combo box again if the event comes from check box |
---|
1208 | if self.shape_rbutton.GetValue()and\ |
---|
1209 | event.GetEventObject()==self.shape_rbutton: |
---|
1210 | ##fill the combobox with form factor list |
---|
1211 | self.structurebox.SetSelection(0) |
---|
1212 | self.structurebox.Disable() |
---|
1213 | self.formfactorbox.Clear() |
---|
1214 | self._populate_box( self.formfactorbox,self.model_list_box["Shapes"]) |
---|
1215 | |
---|
1216 | if self.shape_indep_rbutton.GetValue()and\ |
---|
1217 | event.GetEventObject()==self.shape_indep_rbutton: |
---|
1218 | ##fill the combobox with shape independent factor list |
---|
1219 | self.structurebox.SetSelection(0) |
---|
1220 | self.structurebox.Disable() |
---|
1221 | self.formfactorbox.Clear() |
---|
1222 | self._populate_box( self.formfactorbox, |
---|
1223 | self.model_list_box["Shape-Independent"]) |
---|
1224 | |
---|
1225 | if self.struct_rbutton.GetValue() and\ |
---|
1226 | event.GetEventObject()==self.struct_rbutton: |
---|
1227 | ##fill the combobox with structure factor list |
---|
1228 | self.structurebox.SetSelection(0) |
---|
1229 | self.structurebox.Disable() |
---|
1230 | self.formfactorbox.Clear() |
---|
1231 | self._populate_box( self.formfactorbox, |
---|
1232 | self.model_list_box["Structure Factors"]) |
---|
1233 | |
---|
1234 | if self.plugin_rbutton.GetValue()and\ |
---|
1235 | event.GetEventObject()==self.plugin_rbutton: |
---|
1236 | |
---|
1237 | ##fill the combobox with form factor list |
---|
1238 | self.structurebox.Disable() |
---|
1239 | self.formfactorbox.Clear() |
---|
1240 | self._populate_box( self.formfactorbox, |
---|
1241 | self.model_list_box["Customized Models"]) |
---|
1242 | |
---|
1243 | self._on_select_model(event=None) |
---|
1244 | self._save_typeOfmodel() |
---|
1245 | self.sizer4_4.Layout() |
---|
1246 | self.sizer4.Layout() |
---|
1247 | self.Layout() |
---|
1248 | self.Refresh() |
---|
1249 | self.SetScrollbars(20,20,200,100) |
---|
1250 | |
---|
1251 | def _populate_box(self, combobox, list): |
---|
1252 | """ |
---|
1253 | fill combox box with dict item |
---|
1254 | @param list: contains item to fill the combox |
---|
1255 | item must model class |
---|
1256 | """ |
---|
1257 | for models in list: |
---|
1258 | model= models() |
---|
1259 | name = model.__class__.__name__ |
---|
1260 | if models.__name__!="NoStructure": |
---|
1261 | if hasattr(model, "name"): |
---|
1262 | name = model.name |
---|
1263 | combobox.Append(name,models) |
---|
1264 | |
---|
1265 | return 0 |
---|
1266 | |
---|
1267 | |
---|
1268 | def _on_select_model_helper(self): |
---|
1269 | """ |
---|
1270 | call back for model selection |
---|
1271 | """ |
---|
1272 | ## reset dictionary containing reference to dispersion |
---|
1273 | self._disp_obj_dict = {} |
---|
1274 | self.disp_cb_dict ={} |
---|
1275 | f_id = self.formfactorbox.GetCurrentSelection() |
---|
1276 | form_factor = self.formfactorbox.GetClientData( f_id ) |
---|
1277 | if not form_factor in self.model_list_box["multiplication"]: |
---|
1278 | self.structurebox.Hide() |
---|
1279 | self.text2.Hide() |
---|
1280 | self.structurebox.Disable() |
---|
1281 | self.structurebox.SetSelection(0) |
---|
1282 | self.text2.Disable() |
---|
1283 | else: |
---|
1284 | self.structurebox.Show() |
---|
1285 | self.text2.Show() |
---|
1286 | self.structurebox.Enable() |
---|
1287 | self.text2.Enable() |
---|
1288 | |
---|
1289 | s_id = self.structurebox.GetCurrentSelection() |
---|
1290 | struct_factor = self.structurebox.GetClientData( s_id ) |
---|
1291 | |
---|
1292 | if struct_factor !=None: |
---|
1293 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
1294 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
1295 | |
---|
1296 | else: |
---|
1297 | if form_factor != None: |
---|
1298 | self.model= form_factor() |
---|
1299 | else: |
---|
1300 | self.model = None |
---|
1301 | return self.model |
---|
1302 | |
---|
1303 | ## post state to fit panel |
---|
1304 | self.state.parameters =[] |
---|
1305 | self.state.model =self.model |
---|
1306 | |
---|
1307 | self.disp_list =self.model.getDispParamList() |
---|
1308 | self.state.disp_list = self.disp_list |
---|
1309 | self.sizer4_4.Layout() |
---|
1310 | self.sizer4.Layout() |
---|
1311 | self.Layout() |
---|
1312 | self.SetScrollbars(20,20,200,100) |
---|
1313 | self.Refresh() |
---|
1314 | |
---|
1315 | |
---|
1316 | |
---|
1317 | def _onparamEnter(self,event): |
---|
1318 | """ |
---|
1319 | when enter value on panel redraw model according to changed |
---|
1320 | """ |
---|
1321 | tcrtl= event.GetEventObject() |
---|
1322 | ## save current state |
---|
1323 | self.save_current_state() |
---|
1324 | if event !=None: |
---|
1325 | self._undo.Enable(True) |
---|
1326 | event = PageInfoEvent(page = self) |
---|
1327 | wx.PostEvent(self.parent, event) |
---|
1328 | |
---|
1329 | if check_float(tcrtl): |
---|
1330 | |
---|
1331 | self._onparamEnter_helper() |
---|
1332 | else: |
---|
1333 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1334 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1335 | return |
---|
1336 | |
---|
1337 | |
---|
1338 | def _check_value_enter(self, list, modified): |
---|
1339 | """ |
---|
1340 | @param list: model parameter and panel info |
---|
1341 | each item of the list should be as follow: |
---|
1342 | item=[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
1343 | """ |
---|
1344 | is_modified = modified |
---|
1345 | if len(list)==0: |
---|
1346 | return is_modified |
---|
1347 | for item in list: |
---|
1348 | try: |
---|
1349 | name = str(item[1]) |
---|
1350 | if hasattr(self,"text2_3"): |
---|
1351 | self.text2_3.Hide() |
---|
1352 | ## check model parameters range |
---|
1353 | ## check minimun value |
---|
1354 | param_min= None |
---|
1355 | param_max= None |
---|
1356 | if item[5]!= None: |
---|
1357 | if item[5].GetValue().lstrip().rstrip()!="": |
---|
1358 | param_min = float(item[5].GetValue()) |
---|
1359 | |
---|
1360 | ## check maximum value |
---|
1361 | if item[6]!= None: |
---|
1362 | if item[6].GetValue().lstrip().rstrip()!="": |
---|
1363 | param_max = float(item[6].GetValue()) |
---|
1364 | |
---|
1365 | from sans.guiframe.utils import check_value |
---|
1366 | if param_min != None and param_max !=None: |
---|
1367 | if not check_value(item[5], item[6]): |
---|
1368 | msg= "Wrong Fit range entered for parameter " |
---|
1369 | msg+= "name %s of model %s "%(name, self.model.name) |
---|
1370 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1371 | if name in self.model.details.keys(): |
---|
1372 | self.model.details[name][1:]= param_min,param_max |
---|
1373 | |
---|
1374 | ## hide statictext +/- |
---|
1375 | if item[3]!=None: |
---|
1376 | item[3].Hide() |
---|
1377 | ## hide textcrtl for error after fit |
---|
1378 | if item[4]!=None: |
---|
1379 | item[4].Clear() |
---|
1380 | item[4].Hide() |
---|
1381 | |
---|
1382 | value= float(item[2].GetValue()) |
---|
1383 | |
---|
1384 | # If the value of the parameter has changed, |
---|
1385 | # +update the model and set the is_modified flag |
---|
1386 | if value != self.model.getParam(name): |
---|
1387 | self.model.setParam(name,value) |
---|
1388 | is_modified = True |
---|
1389 | |
---|
1390 | except: |
---|
1391 | msg= "Model Drawing Error:wrong value entered : %s"% sys.exc_value |
---|
1392 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1393 | return |
---|
1394 | |
---|
1395 | return is_modified |
---|
1396 | |
---|
1397 | |
---|
1398 | def _set_dipers_Param(self, event): |
---|
1399 | """ |
---|
1400 | respond to self.enable_disp and self.disable_disp radio box. |
---|
1401 | The dispersity object is reset inside the model into Gaussian. |
---|
1402 | When the user select yes , this method display a combo box for more selection |
---|
1403 | when the user selects No,the combo box disappears. |
---|
1404 | Redraw the model with the default dispersity (Gaussian) |
---|
1405 | """ |
---|
1406 | |
---|
1407 | self._reset_dispersity() |
---|
1408 | |
---|
1409 | if self.model ==None: |
---|
1410 | self.model_disp.Hide() |
---|
1411 | self.disp_box.Hide() |
---|
1412 | self.sizer4_4.Clear(True) |
---|
1413 | |
---|
1414 | return |
---|
1415 | |
---|
1416 | if self.enable_disp.GetValue(): |
---|
1417 | self.model_disp.Show(True) |
---|
1418 | self.disp_box.Show(True) |
---|
1419 | ## layout for model containing no dispersity parameters |
---|
1420 | |
---|
1421 | self.disp_list= self.model.getDispParamList() |
---|
1422 | |
---|
1423 | if len(self.disp_list)==0 and len(self.disp_cb_dict)==0: |
---|
1424 | self._layout_sizer_noDipers() |
---|
1425 | else: |
---|
1426 | ## set gaussian sizer |
---|
1427 | self._on_select_Disp(event=None) |
---|
1428 | else: |
---|
1429 | self.model_disp.Hide() |
---|
1430 | self.disp_box.Hide() |
---|
1431 | self.disp_box.SetSelection(0) |
---|
1432 | self.sizer4_4.Clear(True) |
---|
1433 | |
---|
1434 | ## post state to fit panel |
---|
1435 | self.save_current_state() |
---|
1436 | if event !=None: |
---|
1437 | self._undo.Enable(True) |
---|
1438 | event = PageInfoEvent(page = self) |
---|
1439 | wx.PostEvent(self.parent, event) |
---|
1440 | #draw the model with the current dispersity |
---|
1441 | self._draw_model() |
---|
1442 | self.sizer4_4.Layout() |
---|
1443 | self.sizer4.Layout() |
---|
1444 | self.Layout() |
---|
1445 | self.Refresh() |
---|
1446 | self.SetScrollbars(20,20,200,100) |
---|
1447 | |
---|
1448 | |
---|
1449 | def _layout_sizer_noDipers(self): |
---|
1450 | """ |
---|
1451 | Draw a sizer with no dispersity info |
---|
1452 | """ |
---|
1453 | ix=0 |
---|
1454 | iy=1 |
---|
1455 | self.fittable_param=[] |
---|
1456 | self.fixed_param=[] |
---|
1457 | self.orientation_params_disp=[] |
---|
1458 | |
---|
1459 | self.model_disp.Hide() |
---|
1460 | self.disp_box.Hide() |
---|
1461 | self.sizer4_4.Clear(True) |
---|
1462 | model_disp = wx.StaticText(self, -1, 'No PolyDispersity for this model') |
---|
1463 | self.sizer4_4.Add(model_disp,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1464 | self.sizer4_4.Layout() |
---|
1465 | self.sizer4.Layout() |
---|
1466 | self.SetScrollbars(20,20,200,100) |
---|
1467 | |
---|
1468 | |
---|
1469 | def _reset_dispersity(self): |
---|
1470 | """ |
---|
1471 | put gaussian dispersity into current model |
---|
1472 | """ |
---|
1473 | if len(self.param_toFit)>0: |
---|
1474 | for item in self.fittable_param: |
---|
1475 | if item in self.param_toFit: |
---|
1476 | self.param_toFit.remove(item) |
---|
1477 | for item in self.orientation_params_disp: |
---|
1478 | if item in self.param_toFit: |
---|
1479 | self.param_toFit.remove(item) |
---|
1480 | |
---|
1481 | self.fittable_param=[] |
---|
1482 | self.fixed_param=[] |
---|
1483 | self.orientation_params_disp=[] |
---|
1484 | self.values=[] |
---|
1485 | self.weights=[] |
---|
1486 | |
---|
1487 | |
---|
1488 | from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion |
---|
1489 | if len(self.disp_cb_dict)==0: |
---|
1490 | self.sizer4_4.Clear(True) |
---|
1491 | self.sizer4_4.Layout() |
---|
1492 | self.sizer4.Layout() |
---|
1493 | self.Layout() |
---|
1494 | self.Refresh() |
---|
1495 | self.SetScrollbars(20,20,200,100) |
---|
1496 | return |
---|
1497 | if (len(self.disp_cb_dict)>0) : |
---|
1498 | for p in self.disp_cb_dict: |
---|
1499 | # The parameter was un-selected. Go back to Gaussian model (with 0 pts) |
---|
1500 | disp_model = GaussianDispersion() |
---|
1501 | |
---|
1502 | self._disp_obj_dict[p] = disp_model |
---|
1503 | # Set the new model as the dispersion object for the selected parameter |
---|
1504 | try: |
---|
1505 | self.model.set_dispersion(p, disp_model) |
---|
1506 | except: |
---|
1507 | pass |
---|
1508 | # Redraw the model |
---|
1509 | #self._draw_model() |
---|
1510 | ## save state into |
---|
1511 | self.save_current_state() |
---|
1512 | self.sizer4_4.Layout() |
---|
1513 | self.sizer4.Layout() |
---|
1514 | self.Layout() |
---|
1515 | self.SetScrollbars(20,20,200,100) |
---|
1516 | self.Refresh() |
---|
1517 | |
---|
1518 | |
---|
1519 | |
---|
1520 | def _on_select_Disp(self,event): |
---|
1521 | """ |
---|
1522 | allow selecting different dispersion |
---|
1523 | self.disp_list should change type later .now only gaussian |
---|
1524 | """ |
---|
1525 | |
---|
1526 | n = self.disp_box.GetCurrentSelection() |
---|
1527 | name = self.disp_box.GetValue() |
---|
1528 | dispersity= self.disp_box.GetClientData(n) |
---|
1529 | |
---|
1530 | self.disp_name = name |
---|
1531 | |
---|
1532 | if name.lower() == "array": |
---|
1533 | self._set_sizer_arraydispersion() |
---|
1534 | else: |
---|
1535 | self._set_sizer_dispersion(dispersity= dispersity) |
---|
1536 | |
---|
1537 | |
---|
1538 | self.state.disp_box= n |
---|
1539 | |
---|
1540 | self._undo.Enable(True) |
---|
1541 | event = PageInfoEvent(page = self) |
---|
1542 | wx.PostEvent(self.parent, event) |
---|
1543 | |
---|
1544 | self.sizer4_4.Layout() |
---|
1545 | self.sizer4.Layout() |
---|
1546 | self.Layout() |
---|
1547 | self.SetScrollbars(20,20,200,100) |
---|
1548 | self.Refresh() |
---|
1549 | |
---|
1550 | def _set_sizer_arraydispersion(self): |
---|
1551 | """ |
---|
1552 | draw sizer with array dispersity parameters |
---|
1553 | """ |
---|
1554 | |
---|
1555 | if len(self.param_toFit)>0: |
---|
1556 | for item in self.fittable_param: |
---|
1557 | if item in self.param_toFit: |
---|
1558 | self.param_toFit.remove(item) |
---|
1559 | for item in self.orientation_params_disp: |
---|
1560 | if item in self.param_toFit: |
---|
1561 | self.param_toFit.remove(item) |
---|
1562 | |
---|
1563 | |
---|
1564 | self.fittable_param=[] |
---|
1565 | self.fixed_param=[] |
---|
1566 | self.orientation_params_disp=[] |
---|
1567 | self.sizer4_4.Clear(True) |
---|
1568 | self._reset_dispersity() |
---|
1569 | ix=0 |
---|
1570 | iy=1 |
---|
1571 | disp1 = wx.StaticText(self, -1, 'Array Dispersion') |
---|
1572 | self.sizer4_4.Add(disp1,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1573 | |
---|
1574 | # Look for model parameters to which we can apply an ArrayDispersion model |
---|
1575 | # Add a check box for each parameter. |
---|
1576 | self.disp_cb_dict = {} |
---|
1577 | |
---|
1578 | for p in self.model.dispersion.keys(): |
---|
1579 | if not p in self.model.orientation_params: |
---|
1580 | ix+=1 |
---|
1581 | self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) |
---|
1582 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1583 | |
---|
1584 | wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
1585 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1586 | for p in self.model.dispersion.keys(): |
---|
1587 | if p in self.model.orientation_params: |
---|
1588 | ix+=1 |
---|
1589 | self.disp_cb_dict[p] = wx.CheckBox(self, -1, p, (10, 10)) |
---|
1590 | self.state.disp_cb_dict[p]= self.disp_cb_dict[p].GetValue() |
---|
1591 | #print "self.enable2D",self.enable2D |
---|
1592 | if not (self.enable2D or self.data.__class__.__name__ =="Data2D"): |
---|
1593 | self.disp_cb_dict[p].Hide() |
---|
1594 | self.disp_cb_dict[p].Disable() |
---|
1595 | else: |
---|
1596 | self.disp_cb_dict[p].Show(True) |
---|
1597 | self.disp_cb_dict[p].Enable() |
---|
1598 | |
---|
1599 | wx.EVT_CHECKBOX(self, self.disp_cb_dict[p].GetId(), self.select_disp_angle) |
---|
1600 | self.sizer4_4.Add(self.disp_cb_dict[p], (iy, ix), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1601 | |
---|
1602 | |
---|
1603 | ix =0 |
---|
1604 | iy +=1 |
---|
1605 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1606 | self.sizer4_4.Layout() |
---|
1607 | self.sizer4.Layout() |
---|
1608 | self.SetScrollbars(20,20,200,100) |
---|
1609 | ## save state into |
---|
1610 | |
---|
1611 | self.state.orientation_params =[] |
---|
1612 | self.state.orientation_params_disp =[] |
---|
1613 | self.state.parameters =[] |
---|
1614 | self.state.fittable_param =[] |
---|
1615 | self.state.fixed_param =[] |
---|
1616 | |
---|
1617 | ## save checkbutton state and txtcrtl values |
---|
1618 | |
---|
1619 | self._copy_parameters_state(self.orientation_params, |
---|
1620 | self.state.orientation_params) |
---|
1621 | |
---|
1622 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1623 | self.state.orientation_params_disp) |
---|
1624 | |
---|
1625 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1626 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
1627 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1628 | |
---|
1629 | |
---|
1630 | ## post state to fit panel |
---|
1631 | event = PageInfoEvent(page = self) |
---|
1632 | wx.PostEvent(self.parent, event) |
---|
1633 | |
---|
1634 | |
---|
1635 | |
---|
1636 | |
---|
1637 | |
---|
1638 | def _set_range_sizer(self, title, object1=None,object=None): |
---|
1639 | """ |
---|
1640 | Fill the |
---|
1641 | """ |
---|
1642 | self.sizer5.Clear(True) |
---|
1643 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
1644 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1645 | #-------------------------------------------------------------- |
---|
1646 | self.qmin = BasicPage.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
1647 | self.qmin.SetValue(str(self.qmin_x)) |
---|
1648 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
1649 | #self.qmin.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1650 | #self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1651 | #self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
1652 | |
---|
1653 | self.qmax = BasicPage.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
1654 | self.qmax.SetValue(str(self.qmax_x)) |
---|
1655 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
1656 | #self.qmax.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
1657 | #self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
1658 | #self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
1659 | |
---|
1660 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
1661 | sizer= wx.GridSizer(3, 3,5, 5) |
---|
1662 | |
---|
1663 | sizer.Add((5,5)) |
---|
1664 | sizer.Add(wx.StaticText(self, -1, 'Min')) |
---|
1665 | sizer.Add(wx.StaticText(self, -1, 'Max')) |
---|
1666 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
1667 | |
---|
1668 | sizer.Add(self.qmin) |
---|
1669 | sizer.Add(self.qmax) |
---|
1670 | sizer_horizontal.Add(sizer) |
---|
1671 | if object!=None: |
---|
1672 | sizer_horizontal.Add(object) |
---|
1673 | |
---|
1674 | if object1!=None: |
---|
1675 | boxsizer1.Add(object1) |
---|
1676 | boxsizer1.Add((10,10)) |
---|
1677 | boxsizer1.Add(sizer_horizontal) |
---|
1678 | ## save state |
---|
1679 | self.save_current_state() |
---|
1680 | #---------------------------------------------------------------- |
---|
1681 | self.sizer5.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1682 | self.sizer5.Layout() |
---|
1683 | self.Layout() |
---|
1684 | self.SetScrollbars(20,20,200,100) |
---|
1685 | |
---|
1686 | |
---|
1687 | def _fill_save_sizer(self): |
---|
1688 | """ |
---|
1689 | Draw the layout for saving option |
---|
1690 | """ |
---|
1691 | # Skipping save state functionality for release 0.9.0 |
---|
1692 | #return |
---|
1693 | |
---|
1694 | self.sizer6.Clear(True) |
---|
1695 | box_description= wx.StaticBox(self, -1,"Save Status") |
---|
1696 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1697 | sizer_save = wx.BoxSizer(wx.HORIZONTAL) |
---|
1698 | |
---|
1699 | self.btSave_title = wx.StaticText(self, -1, 'Save the current panel status') |
---|
1700 | self.btSave = wx.Button(self,wx.NewId(),'Save') |
---|
1701 | self.btSave.Bind(wx.EVT_BUTTON, self.onSave,id= self.btSave.GetId()) |
---|
1702 | self.btSave.SetToolTipString("Save current panel status") |
---|
1703 | |
---|
1704 | sizer_save.Add(self.btSave_title) |
---|
1705 | sizer_save.Add((20,20),0, wx.LEFT|wx.RIGHT|wx.EXPAND,45) |
---|
1706 | |
---|
1707 | sizer_save.Add(self.btSave) |
---|
1708 | |
---|
1709 | boxsizer1.Add(sizer_save) |
---|
1710 | self.sizer6.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
1711 | self.sizer6.Layout() |
---|
1712 | self.SetScrollbars(20,20,200,100) |
---|
1713 | |
---|
1714 | def on_model_help_clicked(self,event): |
---|
1715 | """ |
---|
1716 | #On 'More details' button |
---|
1717 | """ |
---|
1718 | from helpPanel import HelpWindow |
---|
1719 | |
---|
1720 | if self.model == None: |
---|
1721 | name = 'FuncHelp' |
---|
1722 | else: |
---|
1723 | name = self.model.origin_name |
---|
1724 | |
---|
1725 | frame = HelpWindow(None, -1, pageToOpen="doc/model_functions.html") |
---|
1726 | frame.Show(True) |
---|
1727 | if frame.rhelp.HasAnchor(name): |
---|
1728 | frame.rhelp.ScrollToAnchor(name) |
---|
1729 | else: |
---|
1730 | msg= "Model does not contains an available description " |
---|
1731 | msg +="Please try searching in the Help window" |
---|
1732 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1733 | |
---|