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