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