1 | |
---|
2 | import sys,re,string, wx |
---|
3 | from sans.guicomm.events import StatusEvent |
---|
4 | |
---|
5 | class SimultaneousFitPage(wx.ScrolledWindow): |
---|
6 | """ |
---|
7 | Simultaneous fitting panel |
---|
8 | All that needs to be defined are the |
---|
9 | two data members window_name and window_caption |
---|
10 | """ |
---|
11 | ## Internal name for the AUI manager |
---|
12 | window_name = "simultaneous Fit page" |
---|
13 | ## Title to appear on top of the window |
---|
14 | window_caption = "Simultaneous Fit Page" |
---|
15 | |
---|
16 | |
---|
17 | def __init__(self, parent, *args, **kwargs): |
---|
18 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
19 | """ |
---|
20 | Simultaneous page display |
---|
21 | """ |
---|
22 | self.parent = parent |
---|
23 | self.page_finder={} |
---|
24 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
25 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
26 | self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
27 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
28 | self.vbox.Add(self.sizer3) |
---|
29 | self.vbox.Add(self.sizer1) |
---|
30 | self.vbox.Add(self.sizer2) |
---|
31 | id = wx.NewId() |
---|
32 | self.btFit =wx.Button(self,id,'Constraint Fit') |
---|
33 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
34 | self.btFit.SetToolTipString("Perform fit.") |
---|
35 | ix = 0 |
---|
36 | iy = 1 |
---|
37 | self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) |
---|
38 | self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ |
---|
39 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
40 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) |
---|
41 | |
---|
42 | text=wx.StaticText(self, -1, 'Constraint') |
---|
43 | self.sizer2.Add(text,0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
44 | |
---|
45 | self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) |
---|
46 | |
---|
47 | self.sizer2.Add(self.ctl2, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
48 | |
---|
49 | self.sizer2.Add(self.btFit, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
50 | self.params=[] |
---|
51 | self.model_list=[] |
---|
52 | self.model_toFit=[] |
---|
53 | |
---|
54 | self.vbox.Layout() |
---|
55 | self.vbox.Fit(self) |
---|
56 | self.SetSizer(self.vbox) |
---|
57 | self.SetScrollbars(20,20,55,40) |
---|
58 | self.Centre() |
---|
59 | |
---|
60 | |
---|
61 | def onFit(self,event): |
---|
62 | """ signal for fitting""" |
---|
63 | for page in self.page_finder.iterkeys(): |
---|
64 | page.set_model_parameter() |
---|
65 | if len(self.model_toFit) >= 1 : |
---|
66 | self._onTextEnter() |
---|
67 | self.set_model() |
---|
68 | self.manager._on_simul_fit() |
---|
69 | else: |
---|
70 | wx.PostEvent(self.parent.Parent, StatusEvent(status=\ |
---|
71 | "Select at least one model to fit ")) |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | def set_manager(self, manager): |
---|
76 | """ |
---|
77 | set panel manager |
---|
78 | @param manager: instance of plugin fitting |
---|
79 | """ |
---|
80 | self.manager = manager |
---|
81 | self.add_model( self.manager.page_finder) |
---|
82 | |
---|
83 | |
---|
84 | def select_all_model_name(self,event): |
---|
85 | """ |
---|
86 | check all models names |
---|
87 | """ |
---|
88 | self.model_toFit=[] |
---|
89 | if self.cb1.GetValue()==True: |
---|
90 | for item in self.model_list: |
---|
91 | item[0].SetValue(True) |
---|
92 | self.manager.schedule_for_fit( value=1,fitproblem =item[1]) |
---|
93 | self.model_toFit.append(item) |
---|
94 | else: |
---|
95 | for item in self.model_list: |
---|
96 | item[0].SetValue(False) |
---|
97 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
98 | |
---|
99 | self.model_toFit=[] |
---|
100 | |
---|
101 | |
---|
102 | def add_model(self,page_finder): |
---|
103 | """ |
---|
104 | Receive a dictionary containing information to display model name |
---|
105 | @param page_finder: the dictionary containing models information |
---|
106 | """ |
---|
107 | if len(self.model_list)>0: |
---|
108 | for item in self.model_list: |
---|
109 | item[0].SetValue(False) |
---|
110 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
111 | self.model_list=[] |
---|
112 | self.model_toFit=[] |
---|
113 | self.sizer1.Clear(True) |
---|
114 | self.page_finder=page_finder |
---|
115 | self.cb1.SetValue(False) |
---|
116 | ix = 0 |
---|
117 | iy = 1 |
---|
118 | list=[] |
---|
119 | for page, value in page_finder.iteritems(): |
---|
120 | try: |
---|
121 | list = value.get_model() |
---|
122 | model = list[0] |
---|
123 | modelname = list[1] |
---|
124 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
125 | cb.SetValue(False) |
---|
126 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
127 | ix = 0 |
---|
128 | iy += 1 |
---|
129 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
130 | self.model_list.append([cb,value,page,modelname]) |
---|
131 | except: |
---|
132 | pass |
---|
133 | iy +=1 |
---|
134 | self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
135 | self.sizer1.Layout() |
---|
136 | self.vbox.Layout() |
---|
137 | |
---|
138 | |
---|
139 | def remove_model(self,delpage): |
---|
140 | """ |
---|
141 | Remove a checkbox and the name related to a model selected on page delpage |
---|
142 | @param delpage: the page removed |
---|
143 | """ |
---|
144 | self.model_list=[] |
---|
145 | self.model_toFit=[] |
---|
146 | self.sizer1.Clear(True) |
---|
147 | |
---|
148 | self.cb1.SetValue(False) |
---|
149 | ix = 0 |
---|
150 | iy = 1 |
---|
151 | list=[] |
---|
152 | for page, value in self.page_finder.iteritems(): |
---|
153 | try: |
---|
154 | if page!= delpage: |
---|
155 | list = value.get_model() |
---|
156 | model = list[0] |
---|
157 | modelname = list[1] |
---|
158 | cb = wx.CheckBox(self, -1, modelname, (10, 10)) |
---|
159 | cb.SetValue(False) |
---|
160 | self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
161 | ix = 0 |
---|
162 | iy += 1 |
---|
163 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) |
---|
164 | self.model_list.append([cb,value,page,modelname]) |
---|
165 | except: |
---|
166 | pass |
---|
167 | iy +=1 |
---|
168 | self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
169 | self.sizer1.Layout() |
---|
170 | self.vbox.Layout() |
---|
171 | |
---|
172 | |
---|
173 | def select_model_name(self,event): |
---|
174 | """ |
---|
175 | Save interformation related to checkbox and their states |
---|
176 | """ |
---|
177 | self.model_toFit=[] |
---|
178 | for item in self.model_list: |
---|
179 | if item[0].GetValue()==True: |
---|
180 | self.model_toFit.append(item) |
---|
181 | self.manager.schedule_for_fit( value=1,fitproblem =item[1]) |
---|
182 | else: |
---|
183 | self.manager.schedule_for_fit( value=0,fitproblem =item[1]) |
---|
184 | if item in self.model_toFit: |
---|
185 | self.model_toFit.remove(item) |
---|
186 | self.cb1.SetValue(False) |
---|
187 | |
---|
188 | if len(self.model_list)==len(self.model_toFit): |
---|
189 | self.cb1.SetValue(True) |
---|
190 | else: |
---|
191 | self.cb1.SetValue(False) |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | def set_model(self): |
---|
196 | """ |
---|
197 | set_model take values in self.params which are the values |
---|
198 | entered by the user and try to assign them into the model |
---|
199 | concerned in self.manager. page.finder |
---|
200 | """ |
---|
201 | if len(self.params) >0: |
---|
202 | for item in self.model_toFit: |
---|
203 | list=item[1].get_model() |
---|
204 | model=list[0] |
---|
205 | param_list=model.getParamList() |
---|
206 | if self.params !=[]: |
---|
207 | for element in self.params: |
---|
208 | if model.name == str(element[0]): |
---|
209 | for item in param_list: |
---|
210 | if item==str(element[1]): |
---|
211 | self.manager.set_page_finder(model.name,element[1], |
---|
212 | str(element[2])) |
---|
213 | |
---|
214 | |
---|
215 | def _onTextEnter(self): |
---|
216 | """ |
---|
217 | get values from the constrainst textcrtl ,parses them into model name |
---|
218 | parameter name and parameters values. |
---|
219 | store them in a list self.params .when when params is not empty set_model |
---|
220 | uses it to reset the appropriate model and its appropriates parameters |
---|
221 | """ |
---|
222 | value= self.ctl2.GetValue() |
---|
223 | if value: |
---|
224 | self.params=[] |
---|
225 | try: |
---|
226 | expression='[\s,;]' |
---|
227 | if re.search(expression,value) !=None: |
---|
228 | word=re.split(expression,value) |
---|
229 | for item in word: |
---|
230 | self.params.append(self.parser_helper(item)) |
---|
231 | else: |
---|
232 | self.params.append(self.parser_helper(value)) |
---|
233 | except: |
---|
234 | wx.PostEvent(self.parent.Parent, StatusEvent(status="Constraint Error: %s" % sys.exc_value)) |
---|
235 | |
---|
236 | |
---|
237 | def parser_helper(self,value): |
---|
238 | """ |
---|
239 | @return param:a list containing the name of a model ,its parameters |
---|
240 | value and name extracted from the constrainst controlbox |
---|
241 | """ |
---|
242 | if string.find(value, "=") !=-1: |
---|
243 | model_param= re.split("=",value) |
---|
244 | param_name=model_param[0] |
---|
245 | param_value=model_param[1] |
---|
246 | |
---|
247 | if string.find(param_name,".")!=-1: |
---|
248 | param_names= re.split("\.",param_name) |
---|
249 | model_name=param_names[0] |
---|
250 | param_name=param_names[1] |
---|
251 | param=[str(model_name),param_name,str(param_value)] |
---|
252 | return param |
---|
253 | else: |
---|
254 | wx.PostEvent(self.parent.Parent, |
---|
255 | StatusEvent(status="cannot evaluate this expression")) |
---|
256 | return |
---|
257 | else: |
---|
258 | wx.PostEvent(self.parent.Parent, StatusEvent(status="Missing '=' in expression")) |
---|
259 | |
---|
260 | |
---|
261 | |
---|