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

« back to all changes in this revision

Viewing changes to mayavi/filters/filter_base.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 many filters.
 
2
"""
 
3
# Author: Prabhu Ramachandran <prabhu_r at users dot sf dot net>
 
4
# Copyright (c) 2006, Enthought, Inc.
 
5
# License: BSD Style.
 
6
 
 
7
 
 
8
# Enthought library imports.
 
9
from traits.api import Instance
 
10
from traitsui.api import View, Group, Item
 
11
from tvtk.api import tvtk
 
12
 
 
13
# Local imports
 
14
from mayavi.core.filter import Filter
 
15
 
 
16
 
 
17
######################################################################
 
18
# `FilterBase` class.
 
19
######################################################################
 
20
class FilterBase(Filter):
 
21
    """The base class for many of the filters.
 
22
    """
 
23
 
 
24
    # The version of this class.  Used for persistence.
 
25
    __version__ = 0
 
26
 
 
27
    # The actual TVTK filter that this class manages.
 
28
    filter = Instance(tvtk.Object, allow_none=False, record=True)
 
29
 
 
30
    # The view of these filters.
 
31
 
 
32
    view = View(Group(Item(name='filter', style='custom', resizable=True,
 
33
                      show_label=False), springy=True),
 
34
                scrollable=True,
 
35
                resizable=True
 
36
                )
 
37
 
 
38
    ######################################################################
 
39
    # `Filter` interface.
 
40
    ######################################################################
 
41
    def setup_pipeline(self):
 
42
        """Override this method so that it *creates* its tvtk
 
43
        pipeline.
 
44
 
 
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.
 
51
        """
 
52
        f = self.filter
 
53
        if f is not None:
 
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)
 
57
 
 
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.
 
61
 
 
62
        This method is invoked (automatically) when the input fires a
 
63
        `pipeline_changed` event.
 
64
        """
 
65
        # Do nothing if there is no input.
 
66
        inputs = self.inputs
 
67
        fil = self.filter
 
68
        if len(inputs) == 0 or fil is None:
 
69
            return
 
70
 
 
71
        # By default we set the input to the first output of the first
 
72
        # input.
 
73
        fil.input = inputs[0].outputs[0]
 
74
        fil.update()
 
75
        self._set_outputs([fil.output])
 
76
 
 
77
    def update_data(self):
 
78
        """Override this method to do what is necessary when upstream
 
79
        data changes.
 
80
 
 
81
        This method is invoked (automatically) when any of the inputs
 
82
        sends a `data_changed` event.
 
83
        """
 
84
        # Do nothing if there is no input and we aren't running.
 
85
        if len(self.inputs) == 0 or not self.running:
 
86
            return
 
87
 
 
88
        self.filter.update()
 
89
        # Propagate the data_changed event.
 
90
        self.data_changed = True
 
91
 
 
92
    def _filter_changed(self, old, new):
 
93
        if old is not None:
 
94
            old.on_trait_change(self.update_data, remove=True)
 
95
 
 
96
        new.on_trait_change(self.update_data)
 
97
 
 
98
        if old is not None:
 
99
            self.update_pipeline()
 
100