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