1 | import wx |
---|
2 | import os |
---|
3 | import os.path |
---|
4 | |
---|
5 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
6 | |
---|
7 | |
---|
8 | class ModelManager: |
---|
9 | |
---|
10 | ## Dictionary of models |
---|
11 | model_list = {} |
---|
12 | model_list_box = {} |
---|
13 | ## Event owner |
---|
14 | event_owner = None |
---|
15 | |
---|
16 | def _getModelList(self): |
---|
17 | """ |
---|
18 | List of models we want to make available by default |
---|
19 | for this application |
---|
20 | |
---|
21 | @param id: first event ID to register the menu events with |
---|
22 | @return: the next free event ID following the new menu events |
---|
23 | """ |
---|
24 | self.model_list = {} |
---|
25 | self.model_list_box = {} |
---|
26 | from sans.models.CylinderModel import CylinderModel |
---|
27 | self.model_list[str(wx.NewId())] = CylinderModel |
---|
28 | |
---|
29 | from sans.models.SphereModel import SphereModel |
---|
30 | self.model_list[str(wx.NewId())] = SphereModel |
---|
31 | |
---|
32 | from sans.guitools.LineModel import LineModel |
---|
33 | self.model_list[str(wx.NewId())] = LineModel |
---|
34 | |
---|
35 | return 0 |
---|
36 | |
---|
37 | |
---|
38 | def populate_menu(self, modelmenu, event_owner): |
---|
39 | """ |
---|
40 | Populate a menu with our models |
---|
41 | |
---|
42 | @param id: first menu event ID to use when binding the menu events |
---|
43 | @param modelmenu: wx.Menu object to populate |
---|
44 | @param event_owner: wx object to bind the menu events to |
---|
45 | @return: the next free event ID following the new menu events |
---|
46 | """ |
---|
47 | |
---|
48 | self._getModelList() |
---|
49 | self.event_owner = event_owner |
---|
50 | |
---|
51 | for id_str,value in self.model_list.iteritems(): |
---|
52 | item = self.model_list[id_str] |
---|
53 | |
---|
54 | name = item.__name__ |
---|
55 | if hasattr(item, "name"): |
---|
56 | name = item.name |
---|
57 | |
---|
58 | self.model_list_box[name] =value |
---|
59 | |
---|
60 | modelmenu.Append(int(id_str), name, name) |
---|
61 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
62 | |
---|
63 | return 0 |
---|
64 | |
---|
65 | def _on_model(self, evt): |
---|
66 | """ |
---|
67 | React to a model menu event |
---|
68 | @param event: wx menu event |
---|
69 | """ |
---|
70 | if str(evt.GetId()) in self.model_list.keys(): |
---|
71 | # Notify the application manager that a new model has been set |
---|
72 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
73 | |
---|
74 | #TODO: post a model event to update all panels that need |
---|
75 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
76 | |
---|
77 | model = self.model_list[str(evt.GetId())]() |
---|
78 | evt = ModelEvent(model=model) |
---|
79 | wx.PostEvent(self.event_owner, evt) |
---|
80 | |
---|
81 | def get_model_list(self): |
---|
82 | """ @ return dictionary of models for fitpanel use """ |
---|
83 | return self.model_list_box |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | |
---|