[d89f09b] | 1 | import wx |
---|
| 2 | import os |
---|
| 3 | import os.path |
---|
| 4 | |
---|
| 5 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
[aa92772] | 6 | def findModels(): |
---|
| 7 | print "looking for models" |
---|
| 8 | try: |
---|
| 9 | cwd= os.path.split(__file__)[0] |
---|
| 10 | except: |
---|
| 11 | cwd= os.getcwd() |
---|
| 12 | print "models cwd",cwd |
---|
| 13 | dir=os.path.join(cwd,'plugins') |
---|
| 14 | print "models: find plugins",dir |
---|
| 15 | if os.path.isdir(dir): |
---|
| 16 | return _findModels(dir) |
---|
| 17 | else: |
---|
| 18 | return [] |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
[1c66bc5] | 22 | def _findModels(dir): |
---|
| 23 | # List of plugin objects |
---|
| 24 | plugins = [] |
---|
| 25 | # Go through files in plug-in directory |
---|
| 26 | try: |
---|
| 27 | list = os.listdir(dir) |
---|
| 28 | for item in list: |
---|
[aa92772] | 29 | print "models: _findModels:",item |
---|
[1c66bc5] | 30 | toks = os.path.splitext(os.path.basename(item)) |
---|
[aa92772] | 31 | print "models: toks:",toks |
---|
[1c66bc5] | 32 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 33 | name = toks[0] |
---|
| 34 | |
---|
| 35 | path = [os.path.abspath(dir)] |
---|
| 36 | file = None |
---|
| 37 | try: |
---|
| 38 | (file, path, info) = imp.find_module(name, path) |
---|
| 39 | module = imp.load_module( name, file, item, info ) |
---|
| 40 | if hasattr(module, "Model"): |
---|
| 41 | try: |
---|
| 42 | plugins.append(module.Model) |
---|
| 43 | except: |
---|
| 44 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
| 45 | except: |
---|
| 46 | pass |
---|
| 47 | finally: |
---|
| 48 | if not file==None: |
---|
| 49 | file.close() |
---|
| 50 | except: |
---|
| 51 | pass |
---|
| 52 | return plugins |
---|
[aa92772] | 53 | |
---|
[d89f09b] | 54 | |
---|
[060b857] | 55 | |
---|
[d89f09b] | 56 | class ModelManager: |
---|
| 57 | |
---|
| 58 | ## Dictionary of models |
---|
| 59 | model_list = {} |
---|
| 60 | model_list_box = {} |
---|
| 61 | ## Event owner |
---|
| 62 | event_owner = None |
---|
| 63 | |
---|
| 64 | def _getModelList(self): |
---|
| 65 | """ |
---|
| 66 | List of models we want to make available by default |
---|
| 67 | for this application |
---|
| 68 | |
---|
| 69 | @param id: first event ID to register the menu events with |
---|
| 70 | @return: the next free event ID following the new menu events |
---|
| 71 | """ |
---|
| 72 | self.model_list = {} |
---|
| 73 | self.model_list_box = {} |
---|
| 74 | from sans.models.CylinderModel import CylinderModel |
---|
| 75 | self.model_list[str(wx.NewId())] = CylinderModel |
---|
| 76 | |
---|
| 77 | from sans.models.SphereModel import SphereModel |
---|
| 78 | self.model_list[str(wx.NewId())] = SphereModel |
---|
| 79 | |
---|
| 80 | from sans.guitools.LineModel import LineModel |
---|
| 81 | self.model_list[str(wx.NewId())] = LineModel |
---|
| 82 | |
---|
| 83 | return 0 |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | def populate_menu(self, modelmenu, event_owner): |
---|
| 87 | """ |
---|
| 88 | Populate a menu with our models |
---|
| 89 | |
---|
| 90 | @param id: first menu event ID to use when binding the menu events |
---|
| 91 | @param modelmenu: wx.Menu object to populate |
---|
| 92 | @param event_owner: wx object to bind the menu events to |
---|
| 93 | @return: the next free event ID following the new menu events |
---|
| 94 | """ |
---|
| 95 | |
---|
| 96 | self._getModelList() |
---|
| 97 | self.event_owner = event_owner |
---|
| 98 | |
---|
| 99 | for id_str,value in self.model_list.iteritems(): |
---|
| 100 | item = self.model_list[id_str] |
---|
| 101 | |
---|
| 102 | name = item.__name__ |
---|
| 103 | if hasattr(item, "name"): |
---|
| 104 | name = item.name |
---|
| 105 | |
---|
| 106 | self.model_list_box[name] =value |
---|
| 107 | |
---|
| 108 | modelmenu.Append(int(id_str), name, name) |
---|
| 109 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
[aa92772] | 110 | plugings=findModels() |
---|
| 111 | print "models: plugings",plugings |
---|
[d89f09b] | 112 | return 0 |
---|
| 113 | |
---|
| 114 | def _on_model(self, evt): |
---|
| 115 | """ |
---|
| 116 | React to a model menu event |
---|
| 117 | @param event: wx menu event |
---|
| 118 | """ |
---|
| 119 | if str(evt.GetId()) in self.model_list.keys(): |
---|
| 120 | # Notify the application manager that a new model has been set |
---|
| 121 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
| 122 | |
---|
| 123 | #TODO: post a model event to update all panels that need |
---|
| 124 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
| 125 | |
---|
| 126 | model = self.model_list[str(evt.GetId())]() |
---|
| 127 | evt = ModelEvent(model=model) |
---|
| 128 | wx.PostEvent(self.event_owner, evt) |
---|
| 129 | |
---|
| 130 | def get_model_list(self): |
---|
| 131 | """ @ return dictionary of models for fitpanel use """ |
---|
| 132 | return self.model_list_box |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|