1 | |
---|
2 | import wx |
---|
3 | import wx.lib.newevent |
---|
4 | import imp |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import math |
---|
8 | import os.path |
---|
9 | # Time is needed by the log method |
---|
10 | import time |
---|
11 | |
---|
12 | from sans.guiframe.events import StatusEvent |
---|
13 | # Explicitly import from the pluginmodel module so that py2exe |
---|
14 | # places it in the distribution. The Model1DPlugin class is used |
---|
15 | # as the base class of plug-in models. |
---|
16 | from sans.models.pluginmodel import Model1DPlugin |
---|
17 | |
---|
18 | PLUGIN_DIR = 'plugins' |
---|
19 | |
---|
20 | def log(message): |
---|
21 | """ |
---|
22 | """ |
---|
23 | out = open("plugins.log", 'a') |
---|
24 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
25 | out.close() |
---|
26 | |
---|
27 | |
---|
28 | def _check_plugin(model, name): |
---|
29 | """ |
---|
30 | Do some checking before model adding plugins in the list |
---|
31 | |
---|
32 | :param model: class model to add into the plugin list |
---|
33 | :param name:name of the module plugin |
---|
34 | |
---|
35 | :return model: model if valid model or None if not valid |
---|
36 | |
---|
37 | """ |
---|
38 | #Check is the plugin is of type Model1DPlugin |
---|
39 | if not issubclass(model, Model1DPlugin): |
---|
40 | msg = "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
41 | log(msg) |
---|
42 | return None |
---|
43 | if model.__name__!="Model": |
---|
44 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
45 | log(msg) |
---|
46 | return None |
---|
47 | try: |
---|
48 | new_instance= model() |
---|
49 | except: |
---|
50 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
51 | str(sys.exc_type),sys.exc_value) |
---|
52 | log(msg) |
---|
53 | return None |
---|
54 | |
---|
55 | new_instance= model() |
---|
56 | if hasattr(new_instance,"function"): |
---|
57 | try: |
---|
58 | value=new_instance.function() |
---|
59 | except: |
---|
60 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
61 | str(sys.exc_type),sys.exc_value) |
---|
62 | log(msg) |
---|
63 | return None |
---|
64 | else: |
---|
65 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
66 | log(msg) |
---|
67 | return None |
---|
68 | return model |
---|
69 | |
---|
70 | |
---|
71 | def _findModels(dir): |
---|
72 | """ |
---|
73 | """ |
---|
74 | # List of plugin objects |
---|
75 | plugins = {} |
---|
76 | # Go through files in plug-in directory |
---|
77 | try: |
---|
78 | list = os.listdir(dir) |
---|
79 | for item in list: |
---|
80 | toks = os.path.splitext(os.path.basename(item)) |
---|
81 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
82 | name = toks[0] |
---|
83 | |
---|
84 | path = [os.path.abspath(dir)] |
---|
85 | file = None |
---|
86 | try: |
---|
87 | (file, path, info) = imp.find_module(name, path) |
---|
88 | module = imp.load_module( name, file, item, info ) |
---|
89 | if hasattr(module, "Model"): |
---|
90 | try: |
---|
91 | if _check_plugin(module.Model, name)!=None: |
---|
92 | plugins[name] = module.Model |
---|
93 | except: |
---|
94 | msg="Error accessing Model" |
---|
95 | msg+="in %s\n %s %s\n" % (name, |
---|
96 | str(sys.exc_type), sys.exc_value) |
---|
97 | log(msg) |
---|
98 | except: |
---|
99 | msg="Error accessing Model" |
---|
100 | msg +=" in %s\n %s %s \n" %(name, |
---|
101 | str(sys.exc_type), sys.exc_value) |
---|
102 | log(msg) |
---|
103 | finally: |
---|
104 | |
---|
105 | if not file==None: |
---|
106 | file.close() |
---|
107 | except: |
---|
108 | # Don't deal with bad plug-in imports. Just skip. |
---|
109 | pass |
---|
110 | return plugins |
---|
111 | |
---|
112 | class ModelList(object): |
---|
113 | """ |
---|
114 | Contains dictionary of model and their type |
---|
115 | """ |
---|
116 | def __init__(self): |
---|
117 | """ |
---|
118 | """ |
---|
119 | self.mydict = {} |
---|
120 | |
---|
121 | def set_list(self, name, mylist): |
---|
122 | """ |
---|
123 | :param name: the type of the list |
---|
124 | :param mylist: the list to add |
---|
125 | |
---|
126 | """ |
---|
127 | if name not in self.mydict.keys(): |
---|
128 | self.mydict[name] = mylist |
---|
129 | |
---|
130 | |
---|
131 | def get_list(self): |
---|
132 | """ |
---|
133 | return all the list stored in a dictionary object |
---|
134 | """ |
---|
135 | return self.mydict |
---|
136 | |
---|
137 | class ModelManagerBase: |
---|
138 | """ |
---|
139 | """ |
---|
140 | ## external dict for models |
---|
141 | model_combobox = ModelList() |
---|
142 | ## Dictionary of form models |
---|
143 | form_factor_dict = {} |
---|
144 | ## dictionary of other |
---|
145 | struct_factor_dict = {} |
---|
146 | ##list of form factors |
---|
147 | shape_list = [] |
---|
148 | ## independent shape model list |
---|
149 | shape_indep_list = [] |
---|
150 | ##list of structure factors |
---|
151 | struct_list = [] |
---|
152 | ##list of model allowing multiplication |
---|
153 | multiplication_factor = [] |
---|
154 | ##list of multifunctional shapes |
---|
155 | multi_func_list = [] |
---|
156 | ## list of added models |
---|
157 | plugins = [] |
---|
158 | ## Event owner (guiframe) |
---|
159 | event_owner = None |
---|
160 | last_time_dir_modified = 0 |
---|
161 | def __init__(self): |
---|
162 | """ |
---|
163 | """ |
---|
164 | |
---|
165 | self.stored_plugins = {} |
---|
166 | self._getModelList() |
---|
167 | |
---|
168 | |
---|
169 | def findModels(self): |
---|
170 | """ |
---|
171 | find plugin model in directory of plugin .recompile all file |
---|
172 | in the directory if file were modified |
---|
173 | """ |
---|
174 | if self.is_changed(): |
---|
175 | #always recompile the folder plugin |
---|
176 | import compileall |
---|
177 | compileall.compile_dir(dir=PLUGIN_DIR, force=1) |
---|
178 | dir = os.path.join(os.getcwd(), 'plugins') |
---|
179 | log("looking for models in: %s" % str(dir)) |
---|
180 | return _findModels(PLUGIN_DIR) |
---|
181 | return {} |
---|
182 | |
---|
183 | |
---|
184 | def _getModelList(self): |
---|
185 | """ |
---|
186 | List of models we want to make available by default |
---|
187 | for this application |
---|
188 | |
---|
189 | :return: the next free event ID following the new menu events |
---|
190 | |
---|
191 | """ |
---|
192 | from sans.models.SphereModel import SphereModel |
---|
193 | self.shape_list.append(SphereModel) |
---|
194 | self.multiplication_factor.append(SphereModel) |
---|
195 | |
---|
196 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
197 | self.shape_list.append(BinaryHSModel) |
---|
198 | |
---|
199 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
200 | self.shape_list.append(FuzzySphereModel) |
---|
201 | self.multiplication_factor.append(FuzzySphereModel) |
---|
202 | |
---|
203 | from sans.models.CoreShellModel import CoreShellModel |
---|
204 | self.shape_list.append(CoreShellModel) |
---|
205 | self.multiplication_factor.append(CoreShellModel) |
---|
206 | |
---|
207 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
208 | self.shape_list.append(CoreMultiShellModel) |
---|
209 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
210 | self.multi_func_list.append(CoreMultiShellModel) |
---|
211 | |
---|
212 | from sans.models.VesicleModel import VesicleModel |
---|
213 | self.shape_list.append(VesicleModel) |
---|
214 | self.multiplication_factor.append(VesicleModel) |
---|
215 | |
---|
216 | from sans.models.MultiShellModel import MultiShellModel |
---|
217 | self.shape_list.append(MultiShellModel) |
---|
218 | self.multiplication_factor.append(MultiShellModel) |
---|
219 | |
---|
220 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
221 | self.shape_list.append(OnionExpShellModel) |
---|
222 | self.multiplication_factor.append(OnionExpShellModel) |
---|
223 | self.multi_func_list.append(OnionExpShellModel) |
---|
224 | |
---|
225 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
226 | self.shape_list.append(SphericalSLDModel) |
---|
227 | self.multiplication_factor.append(SphericalSLDModel) |
---|
228 | self.multi_func_list.append(SphericalSLDModel) |
---|
229 | |
---|
230 | from sans.models.CylinderModel import CylinderModel |
---|
231 | self.shape_list.append(CylinderModel) |
---|
232 | self.multiplication_factor.append(CylinderModel) |
---|
233 | |
---|
234 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
235 | self.shape_list.append(CoreShellCylinderModel) |
---|
236 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
237 | |
---|
238 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
239 | self.shape_list.append(HollowCylinderModel) |
---|
240 | self.multiplication_factor.append(HollowCylinderModel) |
---|
241 | |
---|
242 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
243 | self.shape_list.append(FlexibleCylinderModel) |
---|
244 | |
---|
245 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
246 | self.shape_list.append(FlexCylEllipXModel) |
---|
247 | |
---|
248 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
249 | self.shape_list.append(StackedDisksModel) |
---|
250 | self.multiplication_factor.append(StackedDisksModel) |
---|
251 | |
---|
252 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
253 | self.shape_list.append(ParallelepipedModel) |
---|
254 | self.multiplication_factor.append(ParallelepipedModel) |
---|
255 | |
---|
256 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
257 | self.shape_list.append(CSParallelepipedModel) |
---|
258 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
259 | |
---|
260 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
261 | self.shape_list.append(EllipticalCylinderModel) |
---|
262 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
263 | |
---|
264 | from sans.models.BarBellModel import BarBellModel |
---|
265 | self.shape_list.append(BarBellModel) |
---|
266 | # not implemeted yet! |
---|
267 | #self.multiplication_factor.append(BarBellModel) |
---|
268 | |
---|
269 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
270 | self.shape_list.append(CappedCylinderModel) |
---|
271 | # not implemeted yet! |
---|
272 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
273 | |
---|
274 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
275 | self.shape_list.append(EllipsoidModel) |
---|
276 | self.multiplication_factor.append(EllipsoidModel) |
---|
277 | |
---|
278 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
279 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
280 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
281 | |
---|
282 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
283 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
284 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
285 | |
---|
286 | from sans.models.LamellarModel import LamellarModel |
---|
287 | self.shape_list.append(LamellarModel) |
---|
288 | |
---|
289 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
290 | self.shape_list.append(LamellarFFHGModel) |
---|
291 | |
---|
292 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
293 | self.shape_list.append(LamellarPSModel) |
---|
294 | |
---|
295 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
296 | self.shape_list.append(LamellarPSHGModel) |
---|
297 | |
---|
298 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
299 | self.shape_list.append(LamellarPCrystalModel) |
---|
300 | |
---|
301 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
302 | self.shape_list.append(SCCrystalModel) |
---|
303 | |
---|
304 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
305 | self.shape_list.append(FCCrystalModel) |
---|
306 | |
---|
307 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
308 | self.shape_list.append(BCCrystalModel) |
---|
309 | |
---|
310 | ## Structure factor |
---|
311 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
312 | self.struct_list.append(SquareWellStructure) |
---|
313 | |
---|
314 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
315 | self.struct_list.append(HardsphereStructure) |
---|
316 | |
---|
317 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
318 | self.struct_list.append(StickyHSStructure) |
---|
319 | |
---|
320 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
321 | self.struct_list.append(HayterMSAStructure) |
---|
322 | |
---|
323 | ##shape-independent models |
---|
324 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
325 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
326 | |
---|
327 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
328 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
329 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
330 | |
---|
331 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
332 | self.shape_indep_list.append(BroadPeakModel) |
---|
333 | |
---|
334 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
335 | self.shape_indep_list.append(CorrLengthModel) |
---|
336 | |
---|
337 | from sans.models.DABModel import DABModel |
---|
338 | self.shape_indep_list.append(DABModel ) |
---|
339 | |
---|
340 | from sans.models.DebyeModel import DebyeModel |
---|
341 | self.shape_indep_list.append(DebyeModel ) |
---|
342 | |
---|
343 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
344 | from sans.models.FractalModel import FractalModel |
---|
345 | self.shape_indep_list.append(FractalModel ) |
---|
346 | |
---|
347 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
348 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
349 | |
---|
350 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
351 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
352 | |
---|
353 | from sans.models.GuinierModel import GuinierModel |
---|
354 | self.shape_indep_list.append(GuinierModel ) |
---|
355 | |
---|
356 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
357 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
358 | |
---|
359 | from sans.models.LorentzModel import LorentzModel |
---|
360 | self.shape_indep_list.append( LorentzModel) |
---|
361 | |
---|
362 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
363 | self.shape_indep_list.append(PeakGaussModel) |
---|
364 | |
---|
365 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
366 | self.shape_indep_list.append(PeakLorentzModel) |
---|
367 | |
---|
368 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
369 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
370 | |
---|
371 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
372 | self.shape_indep_list.append(PolymerExclVolume) |
---|
373 | |
---|
374 | from sans.models.PorodModel import PorodModel |
---|
375 | self.shape_indep_list.append(PorodModel ) |
---|
376 | |
---|
377 | from sans.models.RPA10Model import RPA10Model |
---|
378 | self.shape_indep_list.append(RPA10Model) |
---|
379 | self.multi_func_list.append(RPA10Model) |
---|
380 | |
---|
381 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
382 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
383 | |
---|
384 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
385 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
386 | |
---|
387 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
388 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
389 | |
---|
390 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
391 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
392 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
393 | |
---|
394 | from sans.models.LineModel import LineModel |
---|
395 | self.shape_indep_list.append(LineModel) |
---|
396 | |
---|
397 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
398 | self.multi_func_list.append(ReflectivityModel) |
---|
399 | |
---|
400 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
401 | self.multi_func_list.append(ReflectivityIIModel) |
---|
402 | |
---|
403 | #Looking for plugins |
---|
404 | self.stored_plugins = self.findModels() |
---|
405 | self.plugins = self.stored_plugins.values() |
---|
406 | self.plugins.append(ReflectivityModel) |
---|
407 | self.plugins.append(ReflectivityIIModel) |
---|
408 | self._get_multifunc_models() |
---|
409 | |
---|
410 | return 0 |
---|
411 | |
---|
412 | def is_changed(self): |
---|
413 | """ |
---|
414 | check the last time the plugin dir has changed and return true |
---|
415 | is the directory was modified else return false |
---|
416 | """ |
---|
417 | is_modified = False |
---|
418 | if os.path.isdir(PLUGIN_DIR): |
---|
419 | temp = os.path.getmtime(PLUGIN_DIR) |
---|
420 | if self.last_time_dir_modified != temp: |
---|
421 | is_modified = True |
---|
422 | self.last_time_dir_modified = temp |
---|
423 | |
---|
424 | return is_modified |
---|
425 | |
---|
426 | def update(self): |
---|
427 | """ |
---|
428 | return a dictionary of model if |
---|
429 | new models were added else return empty dictionary |
---|
430 | """ |
---|
431 | new_plugins = self.findModels() |
---|
432 | if len(new_plugins) > 0: |
---|
433 | for name, plug in new_plugins.iteritems(): |
---|
434 | if name not in self.stored_plugins.keys(): |
---|
435 | self.stored_plugins[name] = plug |
---|
436 | self.plugins.append(plug) |
---|
437 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
438 | return self.model_combobox.get_list() |
---|
439 | else: |
---|
440 | return {} |
---|
441 | |
---|
442 | def populate_menu(self, modelmenu, event_owner): |
---|
443 | """ |
---|
444 | Populate a menu with our models |
---|
445 | |
---|
446 | :param id: first menu event ID to use when binding the menu events |
---|
447 | :param modelmenu: wx.Menu object to populate |
---|
448 | :param event_owner: wx object to bind the menu events to |
---|
449 | |
---|
450 | :return: the next free event ID following the new menu events |
---|
451 | |
---|
452 | """ |
---|
453 | ## Fill model lists |
---|
454 | self._getModelList() |
---|
455 | ## store reference to model menu of guiframe |
---|
456 | self.modelmenu = modelmenu |
---|
457 | ## guiframe reference |
---|
458 | self.event_owner = event_owner |
---|
459 | |
---|
460 | shape_submenu = wx.Menu() |
---|
461 | shape_indep_submenu = wx.Menu() |
---|
462 | structure_factor = wx.Menu() |
---|
463 | added_models = wx.Menu() |
---|
464 | multip_models = wx.Menu() |
---|
465 | ## create menu with shape |
---|
466 | self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], |
---|
467 | list1=self.shape_list) |
---|
468 | |
---|
469 | self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, |
---|
470 | "List of shape-independent models"], |
---|
471 | list1=self.shape_indep_list ) |
---|
472 | |
---|
473 | self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, |
---|
474 | "List of Structure factors models" ], |
---|
475 | list1=self.struct_list) |
---|
476 | |
---|
477 | self._fill_plugin_menu(menuinfo=["Customized Models", added_models, |
---|
478 | "List of additional models"], |
---|
479 | list1=self.plugins) |
---|
480 | |
---|
481 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
482 | "mulplication of 2 models"], |
---|
483 | list1=self.multiplication_factor , |
---|
484 | list2= self.struct_list) |
---|
485 | return 0 |
---|
486 | |
---|
487 | def _fill_plugin_menu(self, menuinfo, list1): |
---|
488 | """ |
---|
489 | fill the plugin menu with costumized models |
---|
490 | """ |
---|
491 | if len(list1)==0: |
---|
492 | id = wx.NewId() |
---|
493 | msg= "No model available check plugins.log for errors to fix problem" |
---|
494 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
495 | self._fill_simple_menu( menuinfo,list1) |
---|
496 | |
---|
497 | def _fill_simple_menu(self, menuinfo, list1): |
---|
498 | """ |
---|
499 | Fill the menu with list item |
---|
500 | |
---|
501 | :param modelmenu: the menu to fill |
---|
502 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
503 | with info.Should be a list : |
---|
504 | [name(string) , menu(wx.menu), help(string)] |
---|
505 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
506 | |
---|
507 | """ |
---|
508 | if len(list1)>0: |
---|
509 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
510 | |
---|
511 | for item in list1: |
---|
512 | try: |
---|
513 | id = wx.NewId() |
---|
514 | struct_factor=item() |
---|
515 | struct_name = struct_factor.__class__.__name__ |
---|
516 | if hasattr(struct_factor, "name"): |
---|
517 | struct_name = struct_factor.name |
---|
518 | |
---|
519 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
520 | if not item in self.struct_factor_dict.itervalues(): |
---|
521 | self.struct_factor_dict[str(id)]= item |
---|
522 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
523 | except: |
---|
524 | msg= "Error Occured: %s"%sys.exc_value |
---|
525 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
526 | |
---|
527 | id = wx.NewId() |
---|
528 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
529 | |
---|
530 | def _fill_menu(self, menuinfo, list1, list2): |
---|
531 | """ |
---|
532 | Fill the menu with list item |
---|
533 | |
---|
534 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
535 | with info.Should be a list : |
---|
536 | [name(string) , menu(wx.menu), help(string)] |
---|
537 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
538 | :param list2: contains item (Structure factor )to fill modelmenu |
---|
539 | third column |
---|
540 | |
---|
541 | """ |
---|
542 | if len(list1)>0: |
---|
543 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
544 | |
---|
545 | for item in list1: |
---|
546 | form_factor= item() |
---|
547 | form_name = form_factor.__class__.__name__ |
---|
548 | if hasattr(form_factor, "name"): |
---|
549 | form_name = form_factor.name |
---|
550 | ### store form factor to return to other users |
---|
551 | newmenu= wx.Menu() |
---|
552 | if len(list2)>0: |
---|
553 | for model in list2: |
---|
554 | id = wx.NewId() |
---|
555 | struct_factor = model() |
---|
556 | name = struct_factor.__class__.__name__ |
---|
557 | if hasattr(struct_factor, "name"): |
---|
558 | name = struct_factor.name |
---|
559 | newmenu.Append(id,name, name) |
---|
560 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
561 | ## save form_fact and struct_fact |
---|
562 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
563 | |
---|
564 | form_id= wx.NewId() |
---|
565 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
566 | id=wx.NewId() |
---|
567 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
568 | |
---|
569 | def _on_model(self, evt): |
---|
570 | """ |
---|
571 | React to a model menu event |
---|
572 | |
---|
573 | :param event: wx menu event |
---|
574 | |
---|
575 | """ |
---|
576 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
577 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
578 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
579 | model = MultiplicationModel(model1, model2) |
---|
580 | else: |
---|
581 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
582 | evt = ModelEvent(model=model) |
---|
583 | wx.PostEvent(self.event_owner, evt) |
---|
584 | |
---|
585 | def _get_multifunc_models(self): |
---|
586 | """ |
---|
587 | Get the multifunctional models |
---|
588 | """ |
---|
589 | for item in self.plugins: |
---|
590 | try: |
---|
591 | # check the multiplicity if any |
---|
592 | if item.multiplicity_info[0] > 1: |
---|
593 | self.multi_func_list.append(item) |
---|
594 | except: |
---|
595 | # pass to other items |
---|
596 | pass |
---|
597 | |
---|
598 | def get_model_list(self): |
---|
599 | """ |
---|
600 | return dictionary of models for fitpanel use |
---|
601 | |
---|
602 | """ |
---|
603 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
604 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
605 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
606 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
607 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
608 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
609 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
610 | return self.model_combobox.get_list() |
---|
611 | |
---|
612 | |
---|
613 | |
---|
614 | class ModelManager(object): |
---|
615 | """ |
---|
616 | implement model |
---|
617 | """ |
---|
618 | __modelmanager = ModelManagerBase() |
---|
619 | |
---|
620 | def findModels(self): |
---|
621 | return self.__modelmanager.findModels() |
---|
622 | |
---|
623 | def _getModelList(self): |
---|
624 | return self.__modelmanager._getModelList() |
---|
625 | |
---|
626 | def is_changed(self): |
---|
627 | return self.__modelmanager.is_changed() |
---|
628 | |
---|
629 | def update(self): |
---|
630 | return self.__modelmanager.update() |
---|
631 | |
---|
632 | def populate_menu(self, modelmenu, event_owner): |
---|
633 | return self.__modelmanager.populate_menu(modelmenu, event_owner) |
---|
634 | |
---|
635 | def _on_model(self, evt): |
---|
636 | return self.__modelmanager._on_model(evt) |
---|
637 | |
---|
638 | def _get_multifunc_models(self): |
---|
639 | return self.__modelmanager._get_multifunc_models() |
---|
640 | |
---|
641 | def get_model_list(self): |
---|
642 | return self.__modelmanager.get_model_list() |
---|
643 | |
---|
644 | |
---|
645 | |
---|