~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to apptools/preferences/ui/widget_editor.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" An instance editor that allows total control over widget creation. """
 
2
 
 
3
 
 
4
# Enthought library imports.
 
5
from traits.etsconfig.api import ETSConfig
 
6
from traits.api import Any
 
7
from traitsui.api import EditorFactory
 
8
 
 
9
# fixme: We need to import the 'Editor' class from the appropriate toolkit.
 
10
exec('from traitsui.%s.editor import Editor' % ETSConfig.toolkit)
 
11
 
 
12
 
 
13
class _WidgetEditor(Editor):
 
14
    """ An instance editor that allows total control over widget creation. """
 
15
 
 
16
    #### '_WidgetEditor' interface ############################################
 
17
 
 
18
    # The toolkit-specific parent of the editor.
 
19
    parent = Any
 
20
 
 
21
    ###########################################################################
 
22
    # '_WidgetEditor' interface.
 
23
    ###########################################################################
 
24
 
 
25
    def init(self, parent):
 
26
        """ Initialize the editor. """
 
27
 
 
28
        self.parent = parent
 
29
 
 
30
        # fixme: What if there are no pages?!?
 
31
        page = self.object.pages[0]
 
32
 
 
33
        # Create the editor's control.
 
34
        self.control = page.create_control(parent)
 
35
 
 
36
        # Listen for the page being changed.
 
37
        self.object.on_trait_change(self._on_page_changed, 'selected_page')
 
38
 
 
39
        return
 
40
 
 
41
    def dispose(self):
 
42
        """ Dispose of the editor. """
 
43
 
 
44
        page = self.object.selected_page
 
45
        page.destroy_control()
 
46
 
 
47
        return
 
48
 
 
49
    def update_editor(self):
 
50
        """ Update the editor. """
 
51
 
 
52
        pass
 
53
 
 
54
    ###########################################################################
 
55
    # Private interface.
 
56
    ###########################################################################
 
57
 
 
58
    def _on_page_changed(self, obj, trait_name, old, new):
 
59
        """ Dynamic trait change handler. """
 
60
 
 
61
        if old is not None:
 
62
            old.destroy_control()
 
63
 
 
64
        if new is not None:
 
65
            self.control = new.create_control(self.parent)
 
66
 
 
67
        return
 
68
 
 
69
 
 
70
class WidgetEditor(EditorFactory):
 
71
    """ A factory widget editors. """
 
72
 
 
73
    ###########################################################################
 
74
    # 'object' interface.
 
75
    ###########################################################################
 
76
 
 
77
    def __call__ (self, *args, **traits):
 
78
        """ Call the object. """
 
79
 
 
80
        return self.set(**traits)
 
81
 
 
82
    ###########################################################################
 
83
    # 'EditorFactory' interface.
 
84
    ###########################################################################
 
85
 
 
86
    def simple_editor(self, ui, object, name, description, parent):
 
87
        """ Create a simple editor. """
 
88
 
 
89
        editor = _WidgetEditor(
 
90
            parent,
 
91
            factory     = self,
 
92
            ui          = ui,
 
93
            object      = object,
 
94
            name        = name,
 
95
            description = description
 
96
        )
 
97
 
 
98
        return editor
 
99
 
 
100
    custom_editor   = simple_editor
 
101
    text_editor     = simple_editor
 
102
    readonly_editor = simple_editor
 
103
 
 
104
#### EOF ######################################################################