File: traitsui_gradient_editor.py

package info (click to toggle)
mayavi2 4.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (97 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
""" Abstracts the Gradient editors provided for Qt and wxPython so these
can be used from TraitsUI.


Author: Prabhu Ramachandran
Copyright (c) 2012-2020  Enthought Inc., Mumbai, India.
"""

from traits.etsconfig.api import ETSConfig
from traitsui.api import CustomEditor
from tvtk.api import tvtk

##########################################################################
# Traits UI factory functions.
##########################################################################
def gradient_editor_factory(parent, trait_editor):
    """This is a factory function for `traitsui.CustomEditor` and allows us to
    use the `wxGradientEditorWidget` or `QGradientEditorWidget` as a traits
    UI editor.
    """
    tvtk_obj = getattr(trait_editor.object, trait_editor.name)
    if ETSConfig.toolkit == 'wx':
        from .wx_gradient_editor import wxGradientEditorWidget
        widget = wxGradientEditorWidget(parent, tvtk_obj)
    elif ETSConfig.toolkit == 'qt4':
        from .qt_gradient_editor import QGradientEditorWidget
        widget = QGradientEditorWidget(None, tvtk_obj)
    else:
        msg = 'Toolkit %s does not implement gradient_editors.'%ETSConfig.toolkit
        raise NotImplementedError(msg)
    return widget


##########################################################################
# Editor for VolumeProperty
##########################################################################
VolumePropertyEditor = CustomEditor(gradient_editor_factory)

##########################################################################
# Test case related code.
##########################################################################
def make_test_table(lut=False):
    from .ctf import ColorTransferFunction, PiecewiseFunction
    if lut:
        table = tvtk.LookupTable()
        table.table_range = (255, 355)
        return table, None, None
    else:
        table = tvtk.VolumeProperty()
        ctf = ColorTransferFunction()
        mins, maxs = 255, 355
        ds = (maxs-mins)/4.0
        try:
            ctf.range = (mins, maxs)
        except Exception:
            # VTK versions < 5.2 don't seem to need this.
            pass
        ctf.add_rgb_point(mins,      0.00, 0.0, 1.00)
        ctf.add_rgb_point(mins+ds,   0.25, 0.5, 0.75)
        ctf.add_rgb_point(mins+2*ds, 0.50, 1.0, 0.50)
        ctf.add_rgb_point(mins+3*ds, 0.75, 0.5, 0.25)
        ctf.add_rgb_point(maxs,      1.00, 0.0, 0.00)
        otf = PiecewiseFunction()
        otf.add_point(255, 0.0)
        otf.add_point(355, 0.2)
        table.set_color(ctf)
        table.set_scalar_opacity(otf)
        return table, ctf, otf

def test_trait_ui():
    from traits.api import HasTraits, Instance, Button
    from traitsui.api import View, Item, Group

    class Test(HasTraits):
        p = Instance(tvtk.VolumeProperty, ())
        b = Button('Click me')

        view = View(Group(
                        Item(name='p', style='custom',
                            resizable=True,
                            editor=VolumePropertyEditor),
                        Item('b'),
                        show_labels=False),
                    resizable=True
                    )

    table, otf, ctf = make_test_table(False)
    t = Test(p=table)
    # We need to hang on to these so these don't go out of scope.
    t.otf = otf
    t.ctf = ctf
    return t


if __name__ == '__main__':
    t = test_trait_ui()
    t.configure_traits()