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