1 | |
---|
2 | import sys,re,string, wx |
---|
3 | import wx.lib.newevent |
---|
4 | from sans.guiframe.events import StatusEvent |
---|
5 | from sans.guiframe.panel_base import PanelBase |
---|
6 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
7 | from sans.guiframe.events import PanelOnFocusEvent |
---|
8 | #Control panel width |
---|
9 | if sys.platform.count("darwin")==0: |
---|
10 | PANEL_WID = 420 |
---|
11 | FONT_VARIANT = 0 |
---|
12 | else: |
---|
13 | PANEL_WID = 490 |
---|
14 | FONT_VARIANT = 1 |
---|
15 | |
---|
16 | def get_fittableParam( model): |
---|
17 | """ |
---|
18 | return list of fittable parameters name of a model |
---|
19 | |
---|
20 | :param model: the model used |
---|
21 | |
---|
22 | """ |
---|
23 | fittable_param=[] |
---|
24 | for item in model.getParamList(): |
---|
25 | if not item in model.getDispParamList(): |
---|
26 | if not item in model.non_fittable: |
---|
27 | fittable_param.append(item) |
---|
28 | |
---|
29 | for item in model.fixed: |
---|
30 | fittable_param.append(item) |
---|
31 | |
---|
32 | return fittable_param |
---|
33 | |
---|
34 | class SimultaneousFitPage(ScrolledPanel, PanelBase): |
---|
35 | """ |
---|
36 | Simultaneous fitting panel |
---|
37 | All that needs to be defined are the |
---|
38 | two data members window_name and window_caption |
---|
39 | """ |
---|
40 | ## Internal name for the AUI manager |
---|
41 | window_name = "simultaneous Fit page" |
---|
42 | ## Title to appear on top of the window |
---|
43 | window_caption = "Simultaneous Fit Page" |
---|
44 | |
---|
45 | |
---|
46 | def __init__(self, parent,page_finder ={}, *args, **kwargs): |
---|
47 | ScrolledPanel.__init__(self, parent,style= wx.FULL_REPAINT_ON_RESIZE ) |
---|
48 | PanelBase.__init__(self, parent) |
---|
49 | """ |
---|
50 | Simultaneous page display |
---|
51 | """ |
---|
52 | self.SetupScrolling() |
---|
53 | ##Font size |
---|
54 | self.SetWindowVariant(variant = FONT_VARIANT) |
---|
55 | self.uid = wx.NewId() |
---|
56 | self.parent = parent |
---|
57 | ## store page_finder |
---|
58 | self.page_finder = page_finder |
---|
59 | ## list contaning info to set constraint |
---|
60 | ## look like self.constraint_dict[page_id]= page |
---|
61 | self.constraint_dict={} |
---|
62 | ## item list |
---|
63 | # self.constraints_list=[combobox1, combobox2,=,textcrtl, button ] |
---|
64 | self.constraints_list=[] |
---|
65 | ## list of current model |
---|
66 | self.model_list=[] |
---|
67 | ## selected mdoel to fit |
---|
68 | self.model_toFit=[] |
---|
69 | ## number of constraint |
---|
70 | self.nb_constraint= 0 |
---|
71 | self.model_cbox_left = None |
---|
72 | self.model_cbox_right = None |
---|
73 | self.uid = wx.NewId() |
---|
74 | ## draw page |
---|
75 | self.define_page_structure() |
---|
76 | self.draw_page() |
---|
77 | self.set_layout() |
---|
78 | self._set_save_flag(False) |
---|
79 | |
---|
80 | def define_page_structure(self): |
---|
81 | """ |
---|
82 | Create empty sizer for a panel |
---|
83 | """ |
---|
84 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
85 | self.sizer1 = wx.BoxSizer(wx.VERTICAL) |
---|
86 | self.sizer2 = wx.BoxSizer(wx.VERTICAL) |
---|
87 | self.sizer3 = wx.BoxSizer(wx.VERTICAL) |
---|
88 | |
---|
89 | self.sizer1.SetMinSize((PANEL_WID,-1)) |
---|
90 | self.sizer2.SetMinSize((PANEL_WID,-1)) |
---|
91 | self.sizer3.SetMinSize((PANEL_WID,-1)) |
---|
92 | self.vbox.Add(self.sizer1) |
---|
93 | self.vbox.Add(self.sizer2) |
---|
94 | self.vbox.Add(self.sizer3) |
---|
95 | |
---|
96 | def set_scroll(self): |
---|
97 | """ |
---|
98 | """ |
---|
99 | #self.SetScrollbars(20,20,25,65) |
---|
100 | self.Layout() |
---|
101 | |
---|
102 | def set_layout(self): |
---|
103 | """ |
---|
104 | layout |
---|
105 | """ |
---|
106 | self.vbox.Layout() |
---|
107 | self.vbox.Fit(self) |
---|
108 | self.SetSizer(self.vbox) |
---|
109 | self.set_scroll() |
---|
110 | self.Centre() |
---|
111 | |
---|
112 | def onRemove(self, event): |
---|
113 | """ |
---|
114 | Remove constraint fields |
---|
115 | """ |
---|
116 | if len(self.constraints_list)==1: |
---|
117 | self.hide_constraint.SetValue(True) |
---|
118 | self._hide_constraint() |
---|
119 | return |
---|
120 | if len(self.constraints_list)==0: |
---|
121 | return |
---|
122 | for item in self.constraints_list: |
---|
123 | length= len(item) |
---|
124 | if event.GetId()==item[length-2].GetId(): |
---|
125 | sizer= item[length-1] |
---|
126 | sizer.Clear(True) |
---|
127 | self.sizer_constraints.Remove(sizer) |
---|
128 | #self.SetScrollbars(20,20,25,65) |
---|
129 | self.constraints_list.remove(item) |
---|
130 | self.nb_constraint -= 1 |
---|
131 | self.sizer2.Layout() |
---|
132 | self.Layout() |
---|
133 | break |
---|
134 | self._onAdd_constraint(None) |
---|
135 | |
---|
136 | def onFit(self, event): |
---|
137 | """ |
---|
138 | signal for fitting |
---|
139 | |
---|
140 | """ |
---|
141 | ## making sure all parameters content a constraint |
---|
142 | ## validity of the constraint expression is own by fit engine |
---|
143 | if self.parent._manager._fit_engine != "park" and\ |
---|
144 | self._manager.sim_page.uid == self.uid: |
---|
145 | msg = "The FitEnging will be set to 'Park' fit engine\n" |
---|
146 | msg += " for the simultaneous fit..." |
---|
147 | #wx.MessageBox(msg, 'Info') |
---|
148 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
149 | "Fitting: %s"%msg, info="info")) |
---|
150 | if self.show_constraint.GetValue(): |
---|
151 | if not self._set_constraint(): |
---|
152 | return |
---|
153 | ## model was actually selected from this page to be fit |
---|
154 | if len(self.model_toFit) >= 1 : |
---|
155 | self.manager._reset_schedule_problem(value=0) |
---|
156 | for item in self.model_list: |
---|
157 | if item[0].GetValue(): |
---|
158 | self.manager.schedule_for_fit(value=1, uid=item[2]) |
---|
159 | try: |
---|
160 | if not self.manager.onFit(uid=self.uid): |
---|
161 | return |
---|
162 | except: |
---|
163 | msg= "Select at least one parameter to fit in the FitPages." |
---|
164 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
165 | else: |
---|
166 | msg= "Select at least one model check box to fit " |
---|
167 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
168 | |
---|
169 | def set_manager(self, manager): |
---|
170 | """ |
---|
171 | set panel manager |
---|
172 | |
---|
173 | :param manager: instance of plugin fitting |
---|
174 | |
---|
175 | """ |
---|
176 | self.manager = manager |
---|
177 | |
---|
178 | def check_all_model_name(self,event): |
---|
179 | """ |
---|
180 | check all models names |
---|
181 | """ |
---|
182 | self.model_toFit = [] |
---|
183 | if self.cb1.GetValue()== True: |
---|
184 | for item in self.model_list: |
---|
185 | if item[0].IsEnabled(): |
---|
186 | item[0].SetValue(True) |
---|
187 | self.model_toFit.append(item) |
---|
188 | |
---|
189 | ## constraint info |
---|
190 | self._store_model() |
---|
191 | ## display constraint fields |
---|
192 | if self.show_constraint.GetValue() and\ |
---|
193 | len(self.constraints_list)==0: |
---|
194 | self._show_all_constraint() |
---|
195 | self._show_constraint() |
---|
196 | else: |
---|
197 | for item in self.model_list: |
---|
198 | item[0].SetValue(False) |
---|
199 | |
---|
200 | self.model_toFit=[] |
---|
201 | ##constraint info |
---|
202 | self._hide_constraint() |
---|
203 | |
---|
204 | self._update_easy_setup_cb() |
---|
205 | self.Refresh() |
---|
206 | |
---|
207 | def check_model_name(self,event): |
---|
208 | """ |
---|
209 | Save information related to checkbox and their states |
---|
210 | """ |
---|
211 | self.model_toFit=[] |
---|
212 | cbox = event.GetEventObject() |
---|
213 | for item in self.model_list: |
---|
214 | if item[0].GetValue()==True: |
---|
215 | self.model_toFit.append(item) |
---|
216 | else: |
---|
217 | if item in self.model_toFit: |
---|
218 | self.model_toFit.remove(item) |
---|
219 | self.cb1.SetValue(False) |
---|
220 | |
---|
221 | ## display constraint fields |
---|
222 | if len(self.model_toFit)>=1: |
---|
223 | self._store_model() |
---|
224 | if self.show_constraint.GetValue() and\ |
---|
225 | len(self.constraints_list)==0: |
---|
226 | self._show_all_constraint() |
---|
227 | self._show_constraint() |
---|
228 | |
---|
229 | elif len(self.model_toFit)< 1: |
---|
230 | ##constraint info |
---|
231 | self._hide_constraint() |
---|
232 | |
---|
233 | self._update_easy_setup_cb() |
---|
234 | ## set the value of the main check button |
---|
235 | if len(self.model_list)==len(self.model_toFit): |
---|
236 | self.cb1.SetValue(True) |
---|
237 | self.Layout() |
---|
238 | return |
---|
239 | else: |
---|
240 | self.cb1.SetValue(False) |
---|
241 | self.Layout() |
---|
242 | |
---|
243 | def _update_easy_setup_cb(self): |
---|
244 | """ |
---|
245 | Update easy setup combobox on selecting a model |
---|
246 | """ |
---|
247 | if self.model_cbox_left != None and self.model_cbox_right != None: |
---|
248 | try: |
---|
249 | # when there is something |
---|
250 | self.model_cbox_left.Clear() |
---|
251 | self.model_cbox_right.Clear() |
---|
252 | self.model_cbox.Clear() |
---|
253 | except: |
---|
254 | # when there is nothing |
---|
255 | pass |
---|
256 | #for id, model in self.constraint_dict.iteritems(): |
---|
257 | for item in self.model_toFit: |
---|
258 | model = item[3] |
---|
259 | ## check if all parameters have been selected for constraint |
---|
260 | ## then do not allow add constraint on parameters |
---|
261 | if str(model.name) not in self.model_cbox_left.GetItems(): |
---|
262 | self.model_cbox_left.Append(str(model.name), model) |
---|
263 | if str(model.name) not in self.model_cbox_right.GetItems(): |
---|
264 | self.model_cbox_right.Append(str(model.name), model) |
---|
265 | if str(model.name) not in self.model_cbox.GetItems(): |
---|
266 | self.model_cbox.Append(str(model.name), model) |
---|
267 | self.model_cbox_left.SetSelection(0) |
---|
268 | self.sizer2.Layout() |
---|
269 | self.sizer3.Layout() |
---|
270 | |
---|
271 | def draw_page(self): |
---|
272 | """ |
---|
273 | Draw a sizer containing couples of data and model |
---|
274 | """ |
---|
275 | self.model_list=[] |
---|
276 | self.model_toFit=[] |
---|
277 | self.constraints_list=[] |
---|
278 | self.constraint_dict={} |
---|
279 | self.nb_constraint= 0 |
---|
280 | |
---|
281 | if len(self.model_list)>0: |
---|
282 | for item in self.model_list: |
---|
283 | item[0].SetValue(False) |
---|
284 | self.manager.schedule_for_fit(value=0, uid=item[2]) |
---|
285 | |
---|
286 | self.sizer1.Clear(True) |
---|
287 | box_description= wx.StaticBox(self, -1,"Fit Combinations") |
---|
288 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
289 | sizer_title = wx.BoxSizer(wx.HORIZONTAL) |
---|
290 | sizer_couples = wx.GridBagSizer(5,5) |
---|
291 | #------------------------------------------------------ |
---|
292 | if len(self.page_finder)==0: |
---|
293 | msg = " No fit combinations are found! \n\n" |
---|
294 | msg += " Please load data and set up " |
---|
295 | msg += "at least two fit panels first..." |
---|
296 | sizer_title.Add(wx.StaticText(self, -1, msg)) |
---|
297 | else: |
---|
298 | ## store model |
---|
299 | self._store_model() |
---|
300 | |
---|
301 | self.cb1 = wx.CheckBox(self, -1,'Select all') |
---|
302 | self.cb1.SetValue(False) |
---|
303 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name) |
---|
304 | |
---|
305 | sizer_title.Add((10,10),0, |
---|
306 | wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
307 | sizer_title.Add(self.cb1,0, |
---|
308 | wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5) |
---|
309 | |
---|
310 | ## draw list of model and data name |
---|
311 | self._fill_sizer_model_list(sizer_couples) |
---|
312 | ## draw the sizer containing constraint info |
---|
313 | self._fill_sizer_constraint() |
---|
314 | ## draw fit button |
---|
315 | self._fill_sizer_fit() |
---|
316 | #-------------------------------------------------------- |
---|
317 | boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=5) |
---|
318 | boxsizer1.Add(sizer_couples, 1, flag= wx.TOP|wx.BOTTOM,border=5) |
---|
319 | |
---|
320 | self.sizer1.Add(boxsizer1, 1, wx.EXPAND | wx.ALL, 10) |
---|
321 | self.sizer1.Layout() |
---|
322 | #self.SetScrollbars(20,20,25,65) |
---|
323 | self.AdjustScrollbars() |
---|
324 | self.Layout() |
---|
325 | |
---|
326 | def _store_model(self): |
---|
327 | """ |
---|
328 | Store selected model |
---|
329 | """ |
---|
330 | if len(self.model_toFit) < 1: |
---|
331 | return |
---|
332 | for item in self.model_toFit: |
---|
333 | model = item[3] |
---|
334 | page_id= item[2] |
---|
335 | self.constraint_dict[page_id] = model |
---|
336 | |
---|
337 | def _display_constraint(self, event): |
---|
338 | """ |
---|
339 | Show fields to add constraint |
---|
340 | """ |
---|
341 | if len(self.model_toFit)< 1: |
---|
342 | msg= "Select at least 1 model to add constraint " |
---|
343 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
344 | ## hide button |
---|
345 | self._hide_constraint() |
---|
346 | return |
---|
347 | if self.show_constraint.GetValue(): |
---|
348 | self._show_all_constraint() |
---|
349 | self._show_constraint() |
---|
350 | self.Layout() |
---|
351 | return |
---|
352 | else: |
---|
353 | self._hide_constraint() |
---|
354 | self.Layout() |
---|
355 | return |
---|
356 | |
---|
357 | def _show_all_constraint(self): |
---|
358 | """ |
---|
359 | Show constraint fields |
---|
360 | """ |
---|
361 | #for id, model in self.constraint_dict.iteritems(): |
---|
362 | box_description= wx.StaticBox(self, -1,"Easy Setup ") |
---|
363 | boxsizer = wx.StaticBoxSizer(box_description, wx.HORIZONTAL) |
---|
364 | sizer_constraint = wx.BoxSizer(wx.HORIZONTAL|wx.LEFT|wx.RIGHT|wx.EXPAND) |
---|
365 | self.model_cbox_left = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
366 | self.model_cbox_left.Clear() |
---|
367 | self.model_cbox_right = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
368 | self.model_cbox_right.Clear() |
---|
369 | wx.EVT_COMBOBOX(self.model_cbox_left, -1, self._on_select_modelcb) |
---|
370 | wx.EVT_COMBOBOX(self.model_cbox_right, -1, self._on_select_modelcb) |
---|
371 | egal_txt= wx.StaticText(self,-1," = ") |
---|
372 | self.set_button =wx.Button(self, wx.NewId(),'Set All') |
---|
373 | self.set_button.Bind(wx.EVT_BUTTON, self._on_set_all_equal, |
---|
374 | id=self.set_button.GetId()) |
---|
375 | set_tip = "Add constraints for all the adjustable parameters " |
---|
376 | set_tip += "(checked in FitPages) if exist." |
---|
377 | self.set_button.SetToolTipString(set_tip) |
---|
378 | self.set_button.Disable() |
---|
379 | |
---|
380 | for id, model in self.constraint_dict.iteritems(): |
---|
381 | ## check if all parameters have been selected for constraint |
---|
382 | ## then do not allow add constraint on parameters |
---|
383 | self.model_cbox_left.Append( str(model.name), model) |
---|
384 | self.model_cbox_left.Select(0) |
---|
385 | for id, model in self.constraint_dict.iteritems(): |
---|
386 | ## check if all parameters have been selected for constraint |
---|
387 | ## then do not allow add constraint on parameters |
---|
388 | self.model_cbox_right.Append( str(model.name), model) |
---|
389 | boxsizer.Add(self.model_cbox_left, |
---|
390 | flag= wx.RIGHT|wx.EXPAND, border=10) |
---|
391 | boxsizer.Add(wx.StaticText(self, -1,".parameters"), |
---|
392 | flag= wx.RIGHT|wx.EXPAND, border=5) |
---|
393 | boxsizer.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND, border=5) |
---|
394 | boxsizer.Add(self.model_cbox_right, |
---|
395 | flag= wx.RIGHT|wx.EXPAND, border=10) |
---|
396 | boxsizer.Add(wx.StaticText(self, -1,".parameters"), |
---|
397 | flag= wx.RIGHT|wx.EXPAND,border=5) |
---|
398 | boxsizer.Add((20,-1)) |
---|
399 | boxsizer.Add(self.set_button, flag= wx.RIGHT|wx.EXPAND, border=5) |
---|
400 | sizer_constraint.Add(boxsizer, flag= wx.RIGHT|wx.EXPAND, border=5) |
---|
401 | self.sizer_all_constraints.Insert(before=0, |
---|
402 | item=sizer_constraint, |
---|
403 | flag= wx.TOP|wx.BOTTOM|wx.EXPAND, border=5) |
---|
404 | |
---|
405 | self.sizer_all_constraints.Layout() |
---|
406 | self.sizer2.Layout() |
---|
407 | #self.SetScrollbars(20,20,25,65) |
---|
408 | |
---|
409 | def _on_select_modelcb(self, event): |
---|
410 | """ |
---|
411 | On select model left or right combobox |
---|
412 | """ |
---|
413 | event.Skip() |
---|
414 | flag = True |
---|
415 | if self.model_cbox_left.GetValue().strip() == '': |
---|
416 | flag = False |
---|
417 | if self.model_cbox_right.GetValue().strip() == '': |
---|
418 | flag = False |
---|
419 | if self.model_cbox_left.GetValue() ==\ |
---|
420 | self.model_cbox_right.GetValue(): |
---|
421 | flag = False |
---|
422 | self.set_button.Enable(flag) |
---|
423 | |
---|
424 | def _on_set_all_equal(self, event): |
---|
425 | """ |
---|
426 | On set button |
---|
427 | """ |
---|
428 | event.Skip() |
---|
429 | length = len(self.constraints_list) |
---|
430 | if length < 1: |
---|
431 | return |
---|
432 | param_list = [] |
---|
433 | param_listB = [] |
---|
434 | selection = self.model_cbox_left.GetCurrentSelection() |
---|
435 | model_left = self.model_cbox_left.GetValue() |
---|
436 | model = self.model_cbox_left.GetClientData(selection) |
---|
437 | selectionB = self.model_cbox_right.GetCurrentSelection() |
---|
438 | model_right = self.model_cbox_right.GetValue() |
---|
439 | modelB = self.model_cbox_right.GetClientData(selectionB) |
---|
440 | for id, dic_model in self.constraint_dict.iteritems(): |
---|
441 | if model == dic_model: |
---|
442 | param_list = self.page_finder[id].get_param2fit() |
---|
443 | if modelB == dic_model: |
---|
444 | param_listB = self.page_finder[id].get_param2fit() |
---|
445 | if len(param_list) > 0 and len(param_listB) > 0: |
---|
446 | break |
---|
447 | #param_list= get_fittableParam(model) |
---|
448 | #param_listB= get_fittableParam(modelB) |
---|
449 | num_cbox = 0 |
---|
450 | has_param = False |
---|
451 | for param in param_list: |
---|
452 | num_cbox += 1 |
---|
453 | if param in param_listB: |
---|
454 | self.model_cbox.SetStringSelection(model_left) |
---|
455 | self._on_select_model(None) |
---|
456 | self.param_cbox.Clear() |
---|
457 | self.param_cbox.Append( str(param), model) |
---|
458 | self.param_cbox.SetStringSelection( str(param)) |
---|
459 | self.ctl2.SetValue(str(model_right + "." + str(param))) |
---|
460 | has_param = True |
---|
461 | if num_cbox == (len(param_list) + 1): |
---|
462 | break |
---|
463 | self._show_constraint() |
---|
464 | |
---|
465 | self.sizer_constraints.Layout() |
---|
466 | self.sizer2.Layout() |
---|
467 | self.SetScrollbars(20,20,25,65) |
---|
468 | self.Layout() |
---|
469 | if not has_param: |
---|
470 | msg= " There is no adjustable parameter (checked to fit)" |
---|
471 | msg += " either one of the models." |
---|
472 | wx.PostEvent(self.parent.parent, StatusEvent(info="warning", |
---|
473 | status= msg )) |
---|
474 | else: |
---|
475 | msg= " The constraints are added." |
---|
476 | wx.PostEvent(self.parent.parent, StatusEvent(info="info", |
---|
477 | status= msg )) |
---|
478 | def _show_constraint(self): |
---|
479 | """ |
---|
480 | Show constraint fields |
---|
481 | """ |
---|
482 | self.btAdd.Show(True) |
---|
483 | if len(self.constraints_list)!= 0: |
---|
484 | nb_fit_param = 0 |
---|
485 | for id, model in self.constraint_dict.iteritems(): |
---|
486 | nb_fit_param += len(self.page_finder[id].get_param2fit()) |
---|
487 | ##Don't add anymore |
---|
488 | if len(self.constraints_list) == nb_fit_param: |
---|
489 | msg= "Cannot add another constraint .Maximum of number " |
---|
490 | msg += "Parameters name reached %s"%str(nb_fit_param) |
---|
491 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
492 | self.sizer_constraints.Layout() |
---|
493 | self.sizer2.Layout() |
---|
494 | #self.SetScrollbars(20,20,25,65) |
---|
495 | return |
---|
496 | if len(self.model_toFit) < 1 : |
---|
497 | msg= "Select at least 1 model to add constraint " |
---|
498 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
499 | self.sizer_constraints.Layout() |
---|
500 | self.sizer2.Layout() |
---|
501 | #self.SetScrollbars(20,20,25,65) |
---|
502 | return |
---|
503 | |
---|
504 | sizer_constraint = wx.BoxSizer(wx.HORIZONTAL) |
---|
505 | model_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
506 | model_cbox.Clear() |
---|
507 | param_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY, size=(100,-1),) |
---|
508 | param_cbox.Hide() |
---|
509 | |
---|
510 | #This is for GetCLientData() _on_select_param: Was None return on MAC. |
---|
511 | self.param_cbox = param_cbox |
---|
512 | |
---|
513 | wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param) |
---|
514 | self.ctl2 = wx.TextCtrl(self, -1) |
---|
515 | egal_txt= wx.StaticText(self,-1, " = ") |
---|
516 | self.btRemove = wx.Button(self,wx.NewId(),'Remove') |
---|
517 | self.btRemove.Bind(wx.EVT_BUTTON, self.onRemove, |
---|
518 | id=self.btRemove.GetId()) |
---|
519 | self.btRemove.SetToolTipString("Remove constraint.") |
---|
520 | self.btRemove.Hide() |
---|
521 | if hasattr(self,"btAdd"): |
---|
522 | self.btAdd.Hide() |
---|
523 | for id, model in self.constraint_dict.iteritems(): |
---|
524 | ## check if all parameters have been selected for constraint |
---|
525 | ## then do not allow add constraint on parameters |
---|
526 | model_cbox.Append( str(model.name), model) |
---|
527 | |
---|
528 | #This is for GetCLientData() passing to self._on_select_param: Was None return on MAC. |
---|
529 | self.model_cbox = model_cbox |
---|
530 | |
---|
531 | wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model) |
---|
532 | sizer_constraint.Add((5,-1)) |
---|
533 | sizer_constraint.Add(model_cbox, flag= wx.RIGHT|wx.EXPAND,border=10) |
---|
534 | sizer_constraint.Add(param_cbox, flag= wx.RIGHT|wx.EXPAND,border=5) |
---|
535 | sizer_constraint.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND,border=5) |
---|
536 | sizer_constraint.Add(self.ctl2, flag= wx.RIGHT|wx.EXPAND,border=10) |
---|
537 | sizer_constraint.Add(self.btRemove, flag= wx.RIGHT|wx.EXPAND,border=10) |
---|
538 | |
---|
539 | self.sizer_constraints.Insert(before=self.nb_constraint, |
---|
540 | item=sizer_constraint, flag= wx.TOP|wx.BOTTOM|wx.EXPAND, |
---|
541 | border=5) |
---|
542 | ##[combobox1, combobox2,=,textcrtl, remove button ] |
---|
543 | self.constraints_list.append([model_cbox, param_cbox, egal_txt, |
---|
544 | self.ctl2,self.btRemove,sizer_constraint]) |
---|
545 | |
---|
546 | self.nb_constraint += 1 |
---|
547 | self.sizer_constraints.Layout() |
---|
548 | self.sizer2.Layout() |
---|
549 | #self.SetScrollbars(20,20,25,65) |
---|
550 | |
---|
551 | def _hide_constraint(self): |
---|
552 | """ |
---|
553 | hide buttons related constraint |
---|
554 | """ |
---|
555 | for id in self.page_finder.iterkeys(): |
---|
556 | self.page_finder[id].clear_model_param() |
---|
557 | |
---|
558 | self.nb_constraint = 0 |
---|
559 | self.constraint_dict = {} |
---|
560 | if hasattr(self,"btAdd"): |
---|
561 | self.btAdd.Hide() |
---|
562 | self._store_model() |
---|
563 | if self.model_cbox_left != None: |
---|
564 | self.model_cbox_left.Clear() |
---|
565 | self.model_cbox_left = None |
---|
566 | if self.model_cbox_right != None: |
---|
567 | self.model_cbox_right.Clear() |
---|
568 | self.model_cbox_right = None |
---|
569 | self.constraints_list = [] |
---|
570 | self.sizer_all_constraints.Clear(True) |
---|
571 | self.sizer_all_constraints.Layout() |
---|
572 | self.sizer_constraints.Clear(True) |
---|
573 | self.sizer_constraints.Layout() |
---|
574 | self.sizer2.Layout() |
---|
575 | #self.SetScrollbars(20,20,25,65) |
---|
576 | #self.AdjustScrollbars() |
---|
577 | |
---|
578 | def _on_select_model(self, event): |
---|
579 | """ |
---|
580 | fill combox box with list of parameters |
---|
581 | """ |
---|
582 | param_list = [] |
---|
583 | ##This way PC/MAC both work, instead of using event.GetClientData(). |
---|
584 | n = self.model_cbox.GetCurrentSelection() |
---|
585 | model = self.model_cbox.GetClientData(n) |
---|
586 | for id, dic_model in self.constraint_dict.iteritems(): |
---|
587 | if model == dic_model: |
---|
588 | param_list = self.page_finder[id].get_param2fit() |
---|
589 | #break |
---|
590 | #param_list= get_fittableParam(model) |
---|
591 | length = len(self.constraints_list) |
---|
592 | if length < 1: |
---|
593 | return |
---|
594 | param_cbox = self.constraints_list[length-1][1] |
---|
595 | param_cbox.Clear() |
---|
596 | ## insert only fittable paramaters |
---|
597 | for param in param_list: |
---|
598 | param_cbox.Append( str(param), model) |
---|
599 | |
---|
600 | param_cbox.Show(True) |
---|
601 | self.btRemove.Show(True) |
---|
602 | self.btAdd.Show(True) |
---|
603 | self.sizer2.Layout() |
---|
604 | #self.SetScrollbars(20,20,25,65) |
---|
605 | |
---|
606 | def _on_select_param(self, event): |
---|
607 | """ |
---|
608 | Store the appropriate constraint in the page_finder |
---|
609 | """ |
---|
610 | ##This way PC/MAC both work, instead of using event.GetClientData(). |
---|
611 | n = self.param_cbox.GetCurrentSelection() |
---|
612 | model = self.param_cbox.GetClientData(n) |
---|
613 | param = event.GetString() |
---|
614 | |
---|
615 | length = len(self.constraints_list) |
---|
616 | if length < 1: |
---|
617 | return |
---|
618 | egal_txt = self.constraints_list[length-1][2] |
---|
619 | egal_txt.Show(True) |
---|
620 | |
---|
621 | ctl2 = self.constraints_list[length-1][3] |
---|
622 | ctl2.Show(True) |
---|
623 | #self.sizer2.Layout() |
---|
624 | #self.SetScrollbars(20,20,25,65) |
---|
625 | |
---|
626 | def _onAdd_constraint(self, event): |
---|
627 | """ |
---|
628 | Add another line for constraint |
---|
629 | """ |
---|
630 | if not self.show_constraint.GetValue(): |
---|
631 | msg= " Select Yes to add Constraint " |
---|
632 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
633 | return |
---|
634 | ## check that a constraint is added |
---|
635 | # before allow to add another constraint |
---|
636 | for item in self.constraints_list: |
---|
637 | model_cbox = item[0] |
---|
638 | if model_cbox.GetString(0)=="": |
---|
639 | msg= " Select a model Name! " |
---|
640 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
641 | return |
---|
642 | param_cbox = item[1] |
---|
643 | if param_cbox.GetString(0)=="": |
---|
644 | msg= " Select a parameter Name! " |
---|
645 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
646 | return |
---|
647 | ctl2 = item[3] |
---|
648 | if ctl2.GetValue().lstrip().rstrip()=="": |
---|
649 | model= param_cbox.GetClientData(param_cbox.GetCurrentSelection()) |
---|
650 | msg= " Enter a constraint for %s.%s! "%(model.name, |
---|
651 | param_cbox.GetString(0)) |
---|
652 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
653 | return |
---|
654 | ## some model or parameters can be constrained |
---|
655 | self._show_constraint() |
---|
656 | |
---|
657 | def _fill_sizer_fit(self): |
---|
658 | """ |
---|
659 | Draw fit button |
---|
660 | """ |
---|
661 | self.sizer3.Clear(True) |
---|
662 | box_description= wx.StaticBox(self, -1,"Fit ") |
---|
663 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
664 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
665 | |
---|
666 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=wx.DefaultSize) |
---|
667 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId()) |
---|
668 | self.btFit.SetToolTipString("Perform fit.") |
---|
669 | text = " Note: Park fitting engine will be used automatically. \n" |
---|
670 | text += " This page requires at least one FitPage with a data \n" |
---|
671 | text += " and a model set for fitting." |
---|
672 | #text+= "automatically for more than 2 combinations checked" |
---|
673 | text_hint = wx.StaticText(self,-1,text) |
---|
674 | |
---|
675 | sizer_button.Add(text_hint, wx.RIGHT|wx.EXPAND, 10) |
---|
676 | sizer_button.Add(self.btFit, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
677 | |
---|
678 | boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM,border=10) |
---|
679 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
680 | self.sizer3.Layout() |
---|
681 | #self.SetScrollbars(20,20,25,65) |
---|
682 | |
---|
683 | def _fill_sizer_constraint(self): |
---|
684 | """ |
---|
685 | Fill sizer containing constraint info |
---|
686 | """ |
---|
687 | msg = "Select at least 2 model to add constraint " |
---|
688 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
689 | |
---|
690 | self.sizer2.Clear(True) |
---|
691 | box_description= wx.StaticBox(self, -1,"Fit Constraints") |
---|
692 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
693 | sizer_title = wx.BoxSizer(wx.HORIZONTAL) |
---|
694 | self.sizer_all_constraints = wx.BoxSizer(wx.HORIZONTAL) |
---|
695 | self.sizer_constraints = wx.BoxSizer(wx.VERTICAL) |
---|
696 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
697 | |
---|
698 | self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), |
---|
699 | style=wx.RB_GROUP) |
---|
700 | self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
701 | self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint, |
---|
702 | id= self.hide_constraint.GetId() ) |
---|
703 | self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint, |
---|
704 | id= self.show_constraint.GetId() ) |
---|
705 | self.hide_constraint.SetValue(True) |
---|
706 | sizer_title.Add( wx.StaticText(self,-1," Model") ) |
---|
707 | sizer_title.Add(( 10,10) ) |
---|
708 | sizer_title.Add( wx.StaticText(self,-1," Parameter") ) |
---|
709 | sizer_title.Add(( 10,10) ) |
---|
710 | sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") ) |
---|
711 | sizer_title.Add(( 10,10) ) |
---|
712 | sizer_title.Add( self.show_constraint ) |
---|
713 | sizer_title.Add( self.hide_constraint ) |
---|
714 | sizer_title.Add(( 10,10) ) |
---|
715 | |
---|
716 | self.btAdd =wx.Button(self,wx.NewId(),'Add') |
---|
717 | self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint, |
---|
718 | id=self.btAdd.GetId()) |
---|
719 | self.btAdd.SetToolTipString("Add another constraint?") |
---|
720 | self.btAdd.Hide() |
---|
721 | |
---|
722 | text_hint = wx.StaticText(self, -1, |
---|
723 | "Example: [M0][paramter] = M1.parameter") |
---|
724 | sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
725 | sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) |
---|
726 | |
---|
727 | boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=10) |
---|
728 | boxsizer1.Add(self.sizer_all_constraints, flag= wx.TOP|wx.BOTTOM, |
---|
729 | border=10) |
---|
730 | boxsizer1.Add(self.sizer_constraints, flag= wx.TOP|wx.BOTTOM, |
---|
731 | border=10) |
---|
732 | boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM, border=10) |
---|
733 | |
---|
734 | self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
735 | self.sizer2.Layout() |
---|
736 | #self.SetScrollbars(20,20,25,65) |
---|
737 | |
---|
738 | def _set_constraint(self): |
---|
739 | """ |
---|
740 | get values from the constrainst textcrtl ,parses them into model name |
---|
741 | parameter name and parameters values. |
---|
742 | store them in a list self.params .when when params is not empty |
---|
743 | set_model uses it to reset the appropriate model |
---|
744 | and its appropriates parameters |
---|
745 | """ |
---|
746 | for item in self.constraints_list: |
---|
747 | model = item[0].GetClientData(item[0].GetCurrentSelection()) |
---|
748 | param = item[1].GetString(item[1].GetCurrentSelection()) |
---|
749 | constraint = item[3].GetValue().lstrip().rstrip() |
---|
750 | if param.lstrip().rstrip()=="": |
---|
751 | param= None |
---|
752 | msg= " Constraint will be ignored!. missing parameters" |
---|
753 | msg+= " in combobox to set constraint! " |
---|
754 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
755 | for id, value in self.constraint_dict.iteritems(): |
---|
756 | if model == value: |
---|
757 | if constraint == "": |
---|
758 | msg= " Constraint will be ignored!. missing value" |
---|
759 | msg+= " in textcrtl to set constraint! " |
---|
760 | wx.PostEvent(self.parent.parent, |
---|
761 | StatusEvent(status= msg )) |
---|
762 | constraint = None |
---|
763 | if str(param) in self.page_finder[id].get_param2fit(): |
---|
764 | msg = " Checking constraint for parameter: %s ", param |
---|
765 | wx.PostEvent(self.parent.parent, |
---|
766 | StatusEvent(info="info", status= msg )) |
---|
767 | else: |
---|
768 | model_name = item[0].GetLabel() |
---|
769 | fitpage = self.page_finder[id].get_fit_tab_caption() |
---|
770 | msg = "All constrainted parameters must be set " |
---|
771 | msg += " adjustable: '%s.%s' "% (model_name, param) |
---|
772 | msg += "is NOT checked in '%s'. "% fitpage |
---|
773 | msg += " Please check it to fit or" |
---|
774 | msg += " remove the line of the constraint." |
---|
775 | wx.PostEvent(self.parent.parent, |
---|
776 | StatusEvent(info="error", status= msg )) |
---|
777 | return False |
---|
778 | |
---|
779 | for fid in self.page_finder[id].iterkeys(): |
---|
780 | self.page_finder[id].set_model_param(param, |
---|
781 | constraint, fid=fid) |
---|
782 | break |
---|
783 | return True |
---|
784 | |
---|
785 | def _fill_sizer_model_list(self,sizer): |
---|
786 | """ |
---|
787 | Receive a dictionary containing information to display model name |
---|
788 | |
---|
789 | :param page_finder: the dictionary containing models information |
---|
790 | |
---|
791 | """ |
---|
792 | ix = 0 |
---|
793 | iy = 0 |
---|
794 | list=[] |
---|
795 | sizer.Clear(True) |
---|
796 | |
---|
797 | new_name = wx.StaticText(self, -1, ' Model Title ', |
---|
798 | style=wx.ALIGN_CENTER) |
---|
799 | new_name.SetBackgroundColour('orange') |
---|
800 | new_name.SetForegroundColour(wx.WHITE) |
---|
801 | sizer.Add(new_name,(iy, ix),(1,1), |
---|
802 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
803 | ix += 2 |
---|
804 | model_type = wx.StaticText(self, -1, ' Model ') |
---|
805 | model_type.SetBackgroundColour('grey') |
---|
806 | model_type.SetForegroundColour(wx.WHITE) |
---|
807 | sizer.Add(model_type,(iy, ix),(1,1), |
---|
808 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
809 | ix += 1 |
---|
810 | data_used = wx.StaticText(self, -1, ' Data ') |
---|
811 | data_used.SetBackgroundColour('grey') |
---|
812 | data_used.SetForegroundColour(wx.WHITE) |
---|
813 | sizer.Add(data_used,(iy, ix),(1,1), |
---|
814 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
815 | ix += 1 |
---|
816 | tab_used = wx.StaticText(self, -1, ' FitPage ') |
---|
817 | tab_used.SetBackgroundColour('grey') |
---|
818 | tab_used.SetForegroundColour(wx.WHITE) |
---|
819 | sizer.Add(tab_used,(iy, ix),(1,1), |
---|
820 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
821 | for id, value in self.page_finder.iteritems(): |
---|
822 | if id not in self.parent.opened_pages: |
---|
823 | continue |
---|
824 | try: |
---|
825 | ix = 0 |
---|
826 | for fitproblem in value.get_fit_problem(): |
---|
827 | if self.parent.get_page_by_id(id).batch_on: |
---|
828 | continue |
---|
829 | data = fitproblem.get_fit_data() |
---|
830 | if not data.is_data: |
---|
831 | continue |
---|
832 | model = fitproblem.get_model() |
---|
833 | if model == None: |
---|
834 | continue |
---|
835 | iy += 1 |
---|
836 | name = '_' |
---|
837 | if model is not None: |
---|
838 | name = str(model.name) |
---|
839 | cb = wx.CheckBox(self, -1, name) |
---|
840 | cb.SetValue(False) |
---|
841 | cb.Enable(model is not None and data.is_data) |
---|
842 | sizer.Add(cb, (iy, ix), (1, 1), |
---|
843 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
844 | wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name) |
---|
845 | ix += 2 |
---|
846 | type = model.__class__.__name__ |
---|
847 | model_type = wx.StaticText(self, -1, str(type)) |
---|
848 | sizer.Add(model_type, (iy, ix), (1, 1), |
---|
849 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
850 | name = '-' |
---|
851 | if data is not None and data.is_data: |
---|
852 | name = str(data.name) |
---|
853 | data_used = wx.StaticText(self, -1, name) |
---|
854 | ix += 1 |
---|
855 | sizer.Add(data_used, (iy, ix), (1, 1), |
---|
856 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
857 | ix += 1 |
---|
858 | caption = value.get_fit_tab_caption() |
---|
859 | tab_caption_used= wx.StaticText(self, -1, str(caption)) |
---|
860 | sizer.Add(tab_caption_used, (iy, ix), (1, 1), |
---|
861 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
862 | |
---|
863 | self.model_list.append([cb,value,id,model]) |
---|
864 | except: |
---|
865 | raise |
---|
866 | #pass |
---|
867 | iy += 1 |
---|
868 | sizer.Add((20, 20), (iy, ix), (1, 1), |
---|
869 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
870 | sizer.Layout() |
---|
871 | |
---|
872 | |
---|
873 | def on_set_focus(self, event=None): |
---|
874 | """ |
---|
875 | The derivative class is on focus if implemented |
---|
876 | """ |
---|
877 | if self.parent is not None: |
---|
878 | if self.parent.parent is not None: |
---|
879 | wx.PostEvent(self.parent.parent, PanelOnFocusEvent(panel=self)) |
---|
880 | self.page_finder = self.parent._manager.get_page_finder() |
---|
881 | |
---|