File: enable_traits.py

package info (click to toggle)
python-enable 3.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,392 kB
  • ctags: 17,135
  • sloc: cpp: 79,151; python: 29,601; makefile: 2,926; sh: 43
file content (162 lines) | stat: -rw-r--r-- 5,092 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
Define the base Enable object traits
"""

# Major library imports
from numpy import arange, array
from types import ListType, TupleType

# Enthought library imports
from enthought.kiva.traits.kiva_font_trait import KivaFont
from enthought.traits.api import Trait, Range, TraitPrefixList, TraitPrefixMap, \
    TraitHandler, Str, List, TraitFactory
from enthought.traits.ui.api import ImageEnumEditor, EnumEditor, FileEditor
# Try to get the CList trait; for traits 2 backwards compatibility, fall back
# to a normal List trait if we can't import it
try:
    from enthought.traits.api import CList
except ImportError:
    CList = List


# Relative imports
import base
from base import default_font_name, engraving_style, gc_image_for

#------------------------------------------------------------------------------
#  Constants:
#------------------------------------------------------------------------------

# numpy 'array' type:
ArrayType = type( arange( 1.0 ) )

# Basic sequence types:
basic_sequence_types = ( ListType, TupleType )

# Sequence types:
sequence_types = [ ArrayType, ListType, TupleType ]

# Valid pointer shape names:
pointer_shapes = [
   'arrow', 'right arrow', 'blank', 'bullseye', 'char', 'cross', 'hand',
   'ibeam', 'left button', 'magnifier', 'middle button', 'no entry',
   'paint brush', 'pencil', 'point left', 'point right', 'question arrow',
   'right button', 'size top', 'size bottom', 'size left', 'size right',
   'size top right', 'size bottom left', 'size top left', 'size bottom right',
   'sizing', 'spray can', 'wait', 'watch', 'arrow wait'
]

# Cursor styles:
CURSOR_X = 1
CURSOR_Y = 2

cursor_styles = {
    'default':    -1,
    'none':       0,
    'horizontal': CURSOR_Y,
    'vertical':   CURSOR_X,
    'both':       CURSOR_X | CURSOR_Y
}

class TraitImage(TraitHandler):

    def __init__(self, allow_none = True):
        self.allow_none = allow_none
        return

    def validate(self, object, name, value):
        if self.allow_none and ((value is None) or (value == '')):
            setattr( object, '_' + name, None )
            return None
        path   = ''
        image  = value
        prefix = image[:1]
        if prefix == '=':
            path  = object
            image = image[1:]
        elif prefix == '.':
            path  = None
            image = image[1:]
        image_ = gc_image_for( image, path )
        if image_ is not None:
            setattr( object, '_' + name, image_ )
            return value
        self.error( object, name, self.repr( value ) )

    def info(self):
        return 'the name of an image file (e.g a .png, .jpg, .gif file)'

border_size_editor = ImageEnumEditor(
                         values = [ x for x in range( 9 ) ],
                         suffix = '_weight',
                         cols   = 3,
                         module = base )


#-------------------------------------------------------------------------------
# LineStyle trait
#-------------------------------------------------------------------------------

# Privates used for specification of line style trait.
__line_style_trait_values = {
    'solid':     None,
    'dot dash':  array( [ 3.0, 5.0, 9.0, 5.0 ] ),
    'dash':      array( [ 6.0, 6.0 ] ),
    'dot':       array( [ 2.0, 2.0 ] ),
    'long dash': array( [ 9.0, 5.0 ] )
}
__line_style_trait_map_keys = __line_style_trait_values.keys()
LineStyleEditor = EnumEditor( values=__line_style_trait_map_keys)

def __line_style_trait( value='solid', **metadata ):
    return Trait( value, __line_style_trait_values,
                    editor=LineStyleEditor, **metadata)

# A mapped trait for use in specification of line style attributes.
LineStyle = TraitFactory( __line_style_trait )


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

# Font trait:
font_trait = KivaFont(default_font_name)

# Bounds trait
bounds_trait = CList( [0.0, 0.0] )      # (w,h)
coordinate_trait = CList( [0.0, 0.0] )  # (x,y)

#bounds_trait = Trait((0.0, 0.0, 20.0, 20.0), valid_bounds, editor=bounds_editor)

# Component minimum size trait
# PZW: Make these just floats, or maybe remove them altogether.
ComponentMinSize = Range(0.0, 99999.0)
ComponentMaxSize = ComponentMinSize(99999.0)

# Pointer shape trait:
Pointer = Trait('arrow', TraitPrefixList(pointer_shapes))

# Cursor style trait:
cursor_style_trait = Trait('default', TraitPrefixMap(cursor_styles))

# Text engraving style:
engraving_trait = Trait ('none', TraitPrefixMap(engraving_style), cols = 4)

spacing_trait = Range(0, 63, value = 4)
padding_trait = Range(0, 63, value = 4)
margin_trait = Range(0, 63)
border_size_trait = Range(0,  8, editor = border_size_editor)

# Simple image trait:
image_trait = Trait(None, TraitImage(), editor = FileEditor)
string_image_trait = Str(editor = FileEditor)

# Time interval trait:
TimeInterval = Trait(None, None, Range(0.0, 3600.0))

# Stretch traits:
Stretch = Range(0.0, 1.0, value = 1.0)
NoStretch = Stretch(0.0)

# EOF