~ubuntu-branches/ubuntu/precise/mayavi2/precise

« back to all changes in this revision

Viewing changes to enthought/mayavi/core/module.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 01:18:36 UTC
  • mfrom: (1.1.10 upstream) (2.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110709011836-fha21zirlgkqh92s
Tags: 4.0.0-1
* New upstream release
* debian/control:
  - Bump Standards-Version to 3.9.2
  - Set X-Python-Version: 2.6, fixes FTBFS (Closes: #625148)
  - Update Depends
* Update debian/watch file
* Cleanup debian/rules file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""The base class for all MayaVi modules.
2
 
 
3
 
"""
4
 
# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
5
 
# Copyright (c) 2005-2008, Enthought, Inc.
6
 
# License: BSD Style.
7
 
 
8
 
# Enthought library imports.
9
 
from enthought.traits.api import List, Instance, Str
10
 
 
11
 
# Local imports
12
 
from enthought.mayavi.core.pipeline_base import PipelineBase
13
 
from enthought.mayavi.core.pipeline_info import PipelineInfo
14
 
from enthought.mayavi.core.common import exception
15
 
 
16
 
 
17
 
######################################################################
18
 
# `Module` class.
19
 
######################################################################
20
 
class Module(PipelineBase):
21
 
    """ Base class for the Mayavi modules.
22
 
    """
23
 
 
24
 
    # The version of this class.  Used for persistence.
25
 
    __version__ = 0
26
 
 
27
 
    # The ModuleManager associated with this module.  A Module is
28
 
    # always a child of a ModuleManager.  When the module is added to
29
 
    # the mayavi pipeline (as a child of the module manager), the
30
 
    # module manager automatically sets this trait.
31
 
    module_manager = Instance('enthought.mayavi.core.module_manager.ModuleManager',
32
 
                               record=False)
33
 
 
34
 
    # The (optional) components used by this module.  NOTE: This is
35
 
    # not pickled.  It is the developers responsibility to setup the
36
 
    # components when the component traits are set in the handler.
37
 
    components = List(record=False)
38
 
 
39
 
    # The icon
40
 
    icon = Str('module.ico')
41
 
 
42
 
    # The human-readable type for this object
43
 
    type = Str(' module')
44
 
 
45
 
    # Information about what this object can consume.
46
 
    input_info = PipelineInfo(datasets=['any'])
47
 
 
48
 
    # Information about what this object can produce.
49
 
    output_info = PipelineInfo(datasets=['none'])
50
 
 
51
 
    ######################################################################
52
 
    # `object` interface.
53
 
    ######################################################################
54
 
    def __init__(self, **traits):
55
 
        super(Module, self).__init__(**traits)
56
 
 
57
 
        # Let the module setup its pipeline.
58
 
        self.setup_pipeline()
59
 
 
60
 
    def __get_pure_state__(self):
61
 
        d = super(Module, self).__get_pure_state__()
62
 
        for x in ('module_manager', 'components'):
63
 
            d.pop(x, None)
64
 
        return d
65
 
 
66
 
        
67
 
    ######################################################################
68
 
    # `Module` interface.
69
 
    ######################################################################
70
 
    def setup_pipeline(self):
71
 
        """Override this method so that it *creates* the tvtk
72
 
        pipeline.
73
 
 
74
 
        This method is invoked when the object is initialized via
75
 
        `__init__`.  Note that at the time this method is called, the
76
 
        tvtk data pipeline will *not* yet be setup.  So upstream data
77
 
        will not be available.  The idea is that you simply create the
78
 
        basic objects and setup those parts of the pipeline not
79
 
        dependent on upstream sources and filters.  You should also
80
 
        set the `actors` attribute up at this point.
81
 
        """
82
 
        pass
83
 
    
84
 
    def update_pipeline(self):
85
 
        """Override this method so that it *updates* the tvtk pipeline
86
 
        when data upstream is known to have changed.
87
 
 
88
 
        This method is invoked (automatically) when the input fires a
89
 
        `pipeline_changed` event.
90
 
        """
91
 
        raise NotImplementedError
92
 
 
93
 
    def update_data(self):
94
 
        """Override this method so that it flushes the vtk pipeline if
95
 
        that is necessary.
96
 
 
97
 
        This method is invoked (automatically) when any of the inputs
98
 
        sends a `data_changed` event.
99
 
        """
100
 
        # By default, just invoke render and set data_changed.
101
 
        self.data_changed = True
102
 
        self.render()
103
 
 
104
 
 
105
 
    ######################################################################
106
 
    # `Base` interface
107
 
    ######################################################################
108
 
    def start(self):
109
 
        """This is invoked when this object is added to the mayavi
110
 
        pipeline.  Note that when start is invoked, all the other
111
 
        information for the pipeline should be already set.
112
 
        """
113
 
        if self.running:
114
 
            return
115
 
 
116
 
        # Setup event handlers.
117
 
        self._setup_event_handlers()
118
 
 
119
 
        # Setup the pipeline.        
120
 
        self.update_pipeline()
121
 
 
122
 
        # Start the components.
123
 
        try:
124
 
            for component in self.components:
125
 
                component.start()
126
 
        except:
127
 
            exception()
128
 
 
129
 
        # Call parent method to set the running state.
130
 
        super(Module, self).start()
131
 
 
132
 
    def stop(self):
133
 
        """Invoked when this object is removed from the mayavi
134
 
        pipeline.
135
 
        """
136
 
        if not self.running:
137
 
            return
138
 
 
139
 
        # Teardown event handlers.
140
 
        self._teardown_event_handlers()
141
 
 
142
 
        # Stop the components.
143
 
        for component in self.components:
144
 
            component.stop()
145
 
 
146
 
        # Call parent method to set the running state.
147
 
        super(Module, self).stop()
148
 
 
149
 
    def add_child(self, child):
150
 
        """This method intelligently adds a child to this object in
151
 
        the MayaVi pipeline.        
152
 
        """
153
 
        # Pass on the buck to our module_manager.
154
 
        self.module_manager.add_child(child)
155
 
 
156
 
    ######################################################################
157
 
    # `TreeNodeObject` interface
158
 
    ######################################################################
159
 
    def tno_has_children(self, node):
160
 
        """ Returns whether or not the object has children.
161
 
        """
162
 
        return False
163
 
 
164
 
    def tno_allows_children(self, node):
165
 
        """ Returns whether chidren of this object are allowed or not.
166
 
        """
167
 
        return False
168
 
 
169
 
    def tno_get_children(self, node):
170
 
        """ Gets the object's children.
171
 
        """
172
 
        return None
173
 
 
174
 
    ######################################################################
175
 
    # Non-public interface
176
 
    ######################################################################
177
 
    def _change_components(self, old, new):
178
 
        """This method sets up the `components` trait and is typically
179
 
        called from a handler for a particular component.
180
 
 
181
 
        For example lets say you are using a `Actor` component and
182
 
        have a `actor` trait on the module.  The `_actor_changed`
183
 
        method should setup the pipeline.  Typically inside this
184
 
        handler, you also want to change the module's `components`
185
 
        trait.  This method merely does that by removing the older
186
 
        component and adding the new one and then updating the
187
 
        pipeline just in case.
188
 
        """
189
 
        comp = self.components
190
 
        if old is not None:
191
 
            comp.remove(old)
192
 
        comp.append(new)
193
 
        if old is not None:
194
 
            self.update_pipeline()            
195
 
            
196
 
    def _setup_event_handlers(self):
197
 
        mm = self.module_manager
198
 
        src = mm.source
199
 
        mm.on_trait_change(self.update_pipeline, 'source')
200
 
        src.on_trait_event(self.update_pipeline, 'pipeline_changed')
201
 
        src.on_trait_event(self.update_data, 'data_changed')
202
 
 
203
 
    def _teardown_event_handlers(self):
204
 
        mm = self.module_manager
205
 
        src = mm.source
206
 
        mm.on_trait_change(self.update_pipeline, 'source',
207
 
                           remove=True)
208
 
        src.on_trait_event(self.update_pipeline, 'pipeline_changed',
209
 
                           remove=True)
210
 
        src.on_trait_event(self.update_data, 'data_changed',
211
 
                           remove=True)
212
 
 
213
 
    def _scene_changed(self, old_scene, new_scene):
214
 
        for component in self.components:
215
 
            component.scene = new_scene
216
 
        super(Module, self)._scene_changed(old_scene, new_scene)
217
 
        
218
 
    def _components_changed(self, old, new):
219
 
        self._handle_components(old, new)
220
 
 
221
 
    def _components_items_changed(self, list_event):
222
 
        self._handle_components(list_event.removed, list_event.added)
223
 
 
224
 
    def _handle_components(self, removed, added):
225
 
        for component in removed:
226
 
            if self.running:
227
 
                component.stop()
228
 
        scene = self.scene
229
 
        for component in added:
230
 
            if scene is not None:
231
 
                component.scene = scene
232
 
            if self.running:
233
 
                component.start()
234
 
 
235
 
    def _visible_changed(self,value):              
236
 
        for c in self.components:                  
237
 
            c.visible = value                      
238
 
                                                   
239
 
        super(Module,self)._visible_changed(value)
240
 
 
241
 
    def _menu_helper_default(self):
242
 
        from enthought.mayavi.core.traits_menu import ModuleMenuHelper
243
 
        return ModuleMenuHelper(object=self.module_manager)