1
#------------------------------------------------------------------------------
3
# Copyright (c) 2008, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: David C. Morrill
15
#------------------------------------------------------------------------------
16
""" Defines the tuple editor factory for all traits user interface toolkits.
19
#-------------------------------------------------------------------------------
21
#-------------------------------------------------------------------------------
23
from __future__ import absolute_import
25
from traits.trait_base import SequenceTypes, enumerate
27
from traits.api import Bool, HasTraits, List, Tuple, Unicode, Int, Any, TraitType
29
# CIRCULAR IMPORT FIXME: Importing from the source rather than traits.ui.api
30
# to avoid circular imports, as this EditorFactory will be part of
31
# traits.ui.api as well.
32
from ..view import View
34
from ..group import Group
36
from ..item import Item
38
from ..editor_factory import EditorFactory
40
from ..editor import Editor
42
#-------------------------------------------------------------------------------
43
# 'ToolkitEditorFactory' class:
44
#-------------------------------------------------------------------------------
46
class ToolkitEditorFactory ( EditorFactory ):
47
""" Editor factory for tuple editors.
49
#---------------------------------------------------------------------------
51
#---------------------------------------------------------------------------
53
# Trait definitions for each tuple field
56
# Labels for each of the tuple fields
57
labels = List( Unicode )
59
# Editors for each of the tuple fields:
60
editors = List( EditorFactory )
62
# Number of tuple fields or rows
65
# Is user input set on every keystroke? This is applied to every field
66
# of the tuple, provided the field does not already have an 'auto_set'
67
# metadata or an editor defined.
68
auto_set = Bool( True )
70
# Is user input set when the Enter key is pressed? This is applied to
71
# every field of the tuple, provided the field does not already have an
72
# 'enter_set' metadata or an editor defined.
73
enter_set = Bool( False )
75
#-------------------------------------------------------------------------------
76
# 'SimpleEditor' class:
77
#-------------------------------------------------------------------------------
79
class SimpleEditor ( Editor ):
80
""" Simple style of editor for tuples.
82
The editor displays an editor for each of the fields in the tuple, based on
83
the type of each field.
85
#---------------------------------------------------------------------------
86
# Finishes initializing the editor by creating the underlying toolkit
88
#---------------------------------------------------------------------------
90
def init ( self, parent ):
91
""" Finishes initializing the editor by creating the underlying toolkit
94
self._ts = ts = TupleStructure( self )
95
self._ui = ui = ts.view.ui( ts, parent, kind = 'subpanel' ).set(
97
self.control = ui.control
100
#---------------------------------------------------------------------------
101
# Updates the editor when the object trait changes external to the editor:
102
#---------------------------------------------------------------------------
104
def update_editor ( self ):
105
""" Updates the editor when the object trait changes external to the
109
for i, value in enumerate( self.value ):
110
setattr( ts, 'f%d' % i, value )
112
#---------------------------------------------------------------------------
113
# Returns the editor's control for indicating error status:
114
#---------------------------------------------------------------------------
116
def get_error_control ( self ):
117
""" Returns the editor's control for indicating error status.
119
return self._ui.get_error_controls()
121
#-------------------------------------------------------------------------------
122
# 'TupleStructure' class:
123
#-------------------------------------------------------------------------------
125
class TupleStructure ( HasTraits ):
126
""" Creates a view containing items for each field in a tuple.
128
#---------------------------------------------------------------------------
130
#---------------------------------------------------------------------------
132
# Editor this structure is linked to
135
# The constructed View for the tuple
138
# Number of tuple fields
141
#---------------------------------------------------------------------------
142
# Initializes the object:
143
#---------------------------------------------------------------------------
145
def __init__ ( self, editor ):
146
""" Initializes the object.
148
factory = editor.factory
149
types = factory.types
150
labels = factory.labels
151
editors = factory.editors
154
# Save the reference to the editor:
157
# Get the tuple we are mirroring:
158
object = editor.value
160
# For each tuple field, add a trait with the appropriate trait
161
# definition and default value:
163
self.fields = len( object )
164
len_labels = len( labels )
165
len_editors = len( editors )
168
type = editor.value_trait.handler
169
if isinstance( type, Tuple ):
172
if not isinstance( types, SequenceTypes ):
175
len_types = len( types )
180
for i, value in enumerate( object ):
181
type = types[ i % len_types ]
183
auto_set = enter_set = None
184
if isinstance(type, TraitType):
185
auto_set = type.auto_set
186
enter_set = type.enter_set
188
auto_set = editor.factory.auto_set
189
if enter_set is None:
190
enter_set = editor.factory.enter_set
198
field_editor = editors[i]
201
self.add_trait( name, type( value, event = 'field',
203
enter_set = enter_set ) )
204
item = Item( name = name, label = label, editor = field_editor )
206
content.append( item )
209
group = Group( orientation = 'horizontal' )
210
content.append( group )
212
group.content.append( item )
214
self.view = View( Group( show_labels = (len_labels != 0), *content ) )
216
#---------------------------------------------------------------------------
217
# Updates the underlying tuple when any field changes value:
218
#---------------------------------------------------------------------------
220
def _field_changed ( self ):
221
""" Updates the underlying tuple when any field changes value.
223
self.editor.value = tuple( [ getattr( self, 'f%d' % i )
224
for i in range( self.fields ) ] )
227
# Define the TupleEditor class.
228
TupleEditor = ToolkitEditorFactory
230
### EOF #######################################################################