~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/editors/tuple_editor.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2008, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
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
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: David C. Morrill
 
14
#
 
15
#------------------------------------------------------------------------------
 
16
""" Defines the tuple editor factory for all traits user interface toolkits.
 
17
"""
 
18
 
 
19
#-------------------------------------------------------------------------------
 
20
#  Imports:
 
21
#-------------------------------------------------------------------------------
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
from traits.trait_base import SequenceTypes, enumerate
 
26
 
 
27
from traits.api import Bool, HasTraits, List, Tuple, Unicode, Int, Any, TraitType
 
28
 
 
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
 
33
 
 
34
from ..group import Group
 
35
 
 
36
from ..item import Item
 
37
 
 
38
from ..editor_factory import EditorFactory
 
39
 
 
40
from ..editor import Editor
 
41
 
 
42
#-------------------------------------------------------------------------------
 
43
#  'ToolkitEditorFactory' class:
 
44
#-------------------------------------------------------------------------------
 
45
 
 
46
class ToolkitEditorFactory ( EditorFactory ):
 
47
    """ Editor factory for tuple editors.
 
48
    """
 
49
    #---------------------------------------------------------------------------
 
50
    #  Trait definitions:
 
51
    #---------------------------------------------------------------------------
 
52
 
 
53
    # Trait definitions for each tuple field
 
54
    types = Any
 
55
 
 
56
    # Labels for each of the tuple fields
 
57
    labels = List( Unicode )
 
58
 
 
59
    # Editors for each of the tuple fields:
 
60
    editors = List( EditorFactory )
 
61
 
 
62
    # Number of tuple fields or rows
 
63
    cols   = Int( 1 )
 
64
 
 
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 )
 
69
 
 
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 )
 
74
 
 
75
#-------------------------------------------------------------------------------
 
76
#  'SimpleEditor' class:
 
77
#-------------------------------------------------------------------------------
 
78
 
 
79
class SimpleEditor ( Editor ):
 
80
    """ Simple style of editor for tuples.
 
81
 
 
82
    The editor displays an editor for each of the fields in the tuple, based on
 
83
    the type of each field.
 
84
    """
 
85
    #---------------------------------------------------------------------------
 
86
    #  Finishes initializing the editor by creating the underlying toolkit
 
87
    #  widget:
 
88
    #---------------------------------------------------------------------------
 
89
 
 
90
    def init ( self, parent ):
 
91
        """ Finishes initializing the editor by creating the underlying toolkit
 
92
            widget.
 
93
        """
 
94
        self._ts     = ts = TupleStructure( self )
 
95
        self._ui     = ui = ts.view.ui( ts, parent, kind = 'subpanel' ).set(
 
96
                                        parent = self.ui )
 
97
        self.control = ui.control
 
98
        self.set_tooltip()
 
99
 
 
100
    #---------------------------------------------------------------------------
 
101
    #  Updates the editor when the object trait changes external to the editor:
 
102
    #---------------------------------------------------------------------------
 
103
 
 
104
    def update_editor ( self ):
 
105
        """ Updates the editor when the object trait changes external to the
 
106
            editor.
 
107
        """
 
108
        ts = self._ts
 
109
        for i, value in enumerate( self.value ):
 
110
            setattr( ts, 'f%d' % i, value )
 
111
 
 
112
    #---------------------------------------------------------------------------
 
113
    #  Returns the editor's control for indicating error status:
 
114
    #---------------------------------------------------------------------------
 
115
 
 
116
    def get_error_control ( self ):
 
117
        """ Returns the editor's control for indicating error status.
 
118
        """
 
119
        return self._ui.get_error_controls()
 
120
 
 
121
#-------------------------------------------------------------------------------
 
122
#  'TupleStructure' class:
 
123
#-------------------------------------------------------------------------------
 
124
 
 
125
class TupleStructure ( HasTraits ):
 
126
    """ Creates a view containing items for each field in a tuple.
 
127
    """
 
128
    #---------------------------------------------------------------------------
 
129
    #  Trait definitions:
 
130
    #---------------------------------------------------------------------------
 
131
 
 
132
    # Editor this structure is linked to
 
133
    editor = Any
 
134
 
 
135
    # The constructed View for the tuple
 
136
    view = Any
 
137
 
 
138
    # Number of tuple fields
 
139
    fields = Int
 
140
 
 
141
    #---------------------------------------------------------------------------
 
142
    #  Initializes the object:
 
143
    #---------------------------------------------------------------------------
 
144
 
 
145
    def __init__ ( self, editor ):
 
146
        """ Initializes the object.
 
147
        """
 
148
        factory = editor.factory
 
149
        types   = factory.types
 
150
        labels  = factory.labels
 
151
        editors = factory.editors
 
152
        cols    = factory.cols
 
153
 
 
154
        # Save the reference to the editor:
 
155
        self.editor = editor
 
156
 
 
157
        # Get the tuple we are mirroring:
 
158
        object = editor.value
 
159
 
 
160
        # For each tuple field, add a trait with the appropriate trait
 
161
        # definition and default value:
 
162
        content     = []
 
163
        self.fields = len( object )
 
164
        len_labels  = len( labels )
 
165
        len_editors = len( editors )
 
166
 
 
167
        if types is None:
 
168
            type = editor.value_trait.handler
 
169
            if isinstance( type, Tuple ):
 
170
                types = type.types
 
171
 
 
172
        if not isinstance( types, SequenceTypes ):
 
173
            types = [ types ]
 
174
 
 
175
        len_types = len( types )
 
176
        if len_types == 0:
 
177
            types     = [ Any ]
 
178
            len_types = 1
 
179
 
 
180
        for i, value in enumerate( object ):
 
181
            type = types[ i % len_types ]
 
182
 
 
183
            auto_set = enter_set = None
 
184
            if isinstance(type, TraitType):
 
185
                auto_set = type.auto_set
 
186
                enter_set = type.enter_set
 
187
            if auto_set is None:
 
188
                auto_set = editor.factory.auto_set
 
189
            if enter_set is None:
 
190
                enter_set = editor.factory.enter_set
 
191
 
 
192
            label = ''
 
193
            if i < len_labels:
 
194
                label = labels[i]
 
195
 
 
196
            field_editor = None
 
197
            if i < len_editors:
 
198
                field_editor = editors[i]
 
199
 
 
200
            name = 'f%d' % i
 
201
            self.add_trait( name, type( value, event = 'field',
 
202
                                               auto_set = auto_set,
 
203
                                               enter_set = enter_set ) )
 
204
            item = Item( name = name, label = label, editor = field_editor )
 
205
            if cols <= 1:
 
206
                content.append( item )
 
207
            else:
 
208
                if (i % cols) == 0:
 
209
                    group = Group( orientation = 'horizontal' )
 
210
                    content.append( group )
 
211
 
 
212
                group.content.append( item )
 
213
 
 
214
        self.view = View( Group( show_labels = (len_labels != 0), *content ) )
 
215
 
 
216
    #---------------------------------------------------------------------------
 
217
    #  Updates the underlying tuple when any field changes value:
 
218
    #---------------------------------------------------------------------------
 
219
 
 
220
    def _field_changed ( self ):
 
221
        """ Updates the underlying tuple when any field changes value.
 
222
        """
 
223
        self.editor.value = tuple( [ getattr( self, 'f%d' % i )
 
224
                                     for i in range( self.fields ) ] )
 
225
 
 
226
 
 
227
# Define the TupleEditor class.
 
228
TupleEditor = ToolkitEditorFactory
 
229
 
 
230
### EOF #######################################################################
 
231
 
 
232
 
 
233