File: component_editor.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (114 lines) | stat: -rw-r--r-- 3,333 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
""" Defines a Traits editor for displaying an Enable component.
"""
#-------------------------------------------------------------------------------
#  Written by: David C. Morrill
#  Date: 01/26/2007
#  (c) Copyright 2007 by Enthought, Inc.
#----------------------------------------------------------------------------

from enable.colors import ColorTrait
from enable.window import Window

from traits.etsconfig.api import ETSConfig

from traits.api import Property, Tuple
from traitsui.api import BasicEditorFactory

if ETSConfig.toolkit == 'wx':
    from traitsui.wx.editor import Editor
elif ETSConfig.toolkit == 'qt4':
    from traitsui.qt4.editor import Editor
else:
    Editor = object


class _ComponentEditor( Editor ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    # The plot editor is scrollable (overrides Traits UI Editor).
    scrollable = True

    def init( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
        widget.
        """

        size = self._get_initial_size()

        self._window = Window(parent,
                              size=size,
                              component=self.value)

        self.control = self._window.control
        self._window.bgcolor = self.factory.bgcolor
        self._parent = parent

    def dispose(self):
        """ Disposes of the contents of an editor.
        """
        self._window.cleanup()
        self._window = None
        self._parent = None
        super(_ComponentEditor, self).dispose()

    def update_editor( self ):
        """ Updates the editor when the object trait changes externally to the
        editor.
        """
        self._window.component = self.value
        return

    def _get_initial_size(self):
        """ Compute the initial size of the component.

        Use the item size to set the size of the component;
        if not specified, use the default size given in ComponentEditor.size
        """

        width = self.item.width
        if width < 0:
            width = self.factory.size[0]

        height = self.item.height
        if height < 0:
            height = self.factory.size[1]

        return width, height


class ComponentEditor( BasicEditorFactory ):
    """ wxPython editor factory for Enable components.
    """
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    # The class used to create all editor styles (overrides BasicEditorFactory).
    klass = _ComponentEditor

    # The background color for the window
    bgcolor = ColorTrait('sys_window')

    # The default size of the Window wrapping this Enable component
    size = Tuple((400,400))

    # Convenience function for accessing the width
    width = Property

    # Convenience function for accessing the width
    height = Property

    def _get_width(self):
        return self.size[0]

    def _set_width(self, width):
        self.size = (width, self.size[1])

    def _get_height(self):
        return self.size[1]

    def _set_height(self, height):
        self.size = (self.size[0], height)