File: widget_editor.py

package info (click to toggle)
python-apptools 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • sloc: python: 16,657; makefile: 77
file content (104 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (4)
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
98
99
100
101
102
103
104
""" An instance editor that allows total control over widget creation. """


# Enthought library imports.
from traits.etsconfig.api import ETSConfig
from traits.api import Any
from traitsui.api import EditorFactory

# fixme: We need to import the 'Editor' class from the appropriate toolkit.
exec('from traitsui.%s.editor import Editor' % ETSConfig.toolkit)


class _WidgetEditor(Editor):
    """ An instance editor that allows total control over widget creation. """

    #### '_WidgetEditor' interface ############################################

    # The toolkit-specific parent of the editor.
    parent = Any

    ###########################################################################
    # '_WidgetEditor' interface.
    ###########################################################################

    def init(self, parent):
        """ Initialize the editor. """

        self.parent = parent

        # fixme: What if there are no pages?!?
        page = self.object.pages[0]

        # Create the editor's control.
        self.control = page.create_control(parent)

        # Listen for the page being changed.
        self.object.on_trait_change(self._on_page_changed, 'selected_page')

        return

    def dispose(self):
        """ Dispose of the editor. """

        page = self.object.selected_page
        page.destroy_control()

        return

    def update_editor(self):
        """ Update the editor. """

        pass

    ###########################################################################
    # Private interface.
    ###########################################################################

    def _on_page_changed(self, obj, trait_name, old, new):
        """ Dynamic trait change handler. """

        if old is not None:
            old.destroy_control()

        if new is not None:
            self.control = new.create_control(self.parent)

        return


class WidgetEditor(EditorFactory):
    """ A factory widget editors. """

    ###########################################################################
    # 'object' interface.
    ###########################################################################

    def __call__ (self, *args, **traits):
        """ Call the object. """

        return self.set(**traits)

    ###########################################################################
    # 'EditorFactory' interface.
    ###########################################################################

    def simple_editor(self, ui, object, name, description, parent):
        """ Create a simple editor. """

        editor = _WidgetEditor(
            parent,
            factory     = self,
            ui          = ui,
            object      = object,
            name        = name,
            description = description
        )

        return editor

    custom_editor   = simple_editor
    text_editor     = simple_editor
    readonly_editor = simple_editor

#### EOF ######################################################################