1
"""The base class for many filters.
3
# Author: Prabhu Ramachandran <prabhu_r at users dot sf dot net>
4
# Copyright (c) 2006, Enthought, Inc.
8
# Enthought library imports.
9
from traits.api import Instance
10
from traitsui.api import View, Group, Item
11
from tvtk.api import tvtk
14
from mayavi.core.filter import Filter
17
######################################################################
19
######################################################################
20
class FilterBase(Filter):
21
"""The base class for many of the filters.
24
# The version of this class. Used for persistence.
27
# The actual TVTK filter that this class manages.
28
filter = Instance(tvtk.Object, allow_none=False, record=True)
30
# The view of these filters.
32
view = View(Group(Item(name='filter', style='custom', resizable=True,
33
show_label=False), springy=True),
38
######################################################################
40
######################################################################
41
def setup_pipeline(self):
42
"""Override this method so that it *creates* its tvtk
45
This method is invoked when the object is initialized via
46
`__init__`. Note that at the time this method is called, the
47
tvtk data pipeline will *not* yet be setup. So upstream data
48
will not be available. The idea is that you simply create the
49
basic objects and setup those parts of the pipeline not
50
dependent on upstream sources and filters.
54
# Just hook up the filter so the update_data method is
55
# called when the traits change.
56
f.on_trait_change(self.update_data)
58
def update_pipeline(self):
59
"""Override this method so that it *updates* the tvtk pipeline
60
when data upstream is known to have changed.
62
This method is invoked (automatically) when the input fires a
63
`pipeline_changed` event.
65
# Do nothing if there is no input.
68
if len(inputs) == 0 or fil is None:
71
# By default we set the input to the first output of the first
73
fil.input = inputs[0].outputs[0]
75
self._set_outputs([fil.output])
77
def update_data(self):
78
"""Override this method to do what is necessary when upstream
81
This method is invoked (automatically) when any of the inputs
82
sends a `data_changed` event.
84
# Do nothing if there is no input and we aren't running.
85
if len(self.inputs) == 0 or not self.running:
89
# Propagate the data_changed event.
90
self.data_changed = True
92
def _filter_changed(self, old, new):
94
old.on_trait_change(self.update_data, remove=True)
96
new.on_trait_change(self.update_data)
99
self.update_pipeline()