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