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