~ubuntu-branches/ubuntu/trusty/python-traitsui/trusty

« back to all changes in this revision

Viewing changes to traitsui/editors/button_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
#  Date:   10/21/2004
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
 
 
18
""" Defines the button editor factory for all traits toolkit backends.
 
19
"""
 
20
 
 
21
#-------------------------------------------------------------------------------
 
22
#  Imports:
 
23
#-------------------------------------------------------------------------------
 
24
 
 
25
from __future__ import absolute_import
 
26
 
 
27
from traits.api import Str, Range, Enum, Property, Trait
 
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
 
 
33
from ..view import View
 
34
 
 
35
from ..ui_traits import AView, Image
 
36
 
 
37
from ..editor_factory import EditorFactory
 
38
 
 
39
#-------------------------------------------------------------------------------
 
40
#  'ToolkitEditorFactory' class:
 
41
#-------------------------------------------------------------------------------
 
42
 
 
43
class ToolkitEditorFactory ( EditorFactory ):
 
44
    """ Editor factory for buttons.
 
45
    """
 
46
 
 
47
    #---------------------------------------------------------------------------
 
48
    #  Trait definitions:
 
49
    #---------------------------------------------------------------------------
 
50
 
 
51
    # Value to set when the button is clicked
 
52
    value = Property
 
53
 
 
54
    # Optional label for the button
 
55
    label = Str
 
56
 
 
57
    # The name of the external object trait that the button label is synced to
 
58
    label_value = Str
 
59
 
 
60
    # The name of the trait on the object that contains the list of possible
 
61
    # values.  If this is set, then the value, label, and label_value traits
 
62
    # are ignored; instead, they will be set from this list.  When this button
 
63
    # is clicked, the value set will be the one selected from the drop-down.
 
64
    values_trait = Trait(None, None, Str)
 
65
 
 
66
    # (Optional) Image to display on the button
 
67
    image = Image
 
68
 
 
69
    # Extra padding to add to both the left and the right sides
 
70
    width_padding = Range( 0, 31, 7 )
 
71
 
 
72
    # Extra padding to add to both the top and the bottom sides
 
73
    height_padding = Range( 0, 31, 5 )
 
74
 
 
75
    # Presentation style
 
76
    style = Enum( 'button', 'radio', 'toolbar', 'checkbox' )
 
77
 
 
78
    # Orientation of the text relative to the image
 
79
    orientation = Enum( 'vertical', 'horizontal' )
 
80
 
 
81
    # The optional view to display when the button is clicked:
 
82
    view = AView
 
83
 
 
84
    #---------------------------------------------------------------------------
 
85
    #  Traits view definition:
 
86
    #---------------------------------------------------------------------------
 
87
 
 
88
    traits_view = View( [ 'label', 'value', '|[]' ] )
 
89
 
 
90
    #---------------------------------------------------------------------------
 
91
    #  Implementation of the 'value' property:
 
92
    #---------------------------------------------------------------------------
 
93
 
 
94
    def _get_value ( self ):
 
95
        return self._value
 
96
 
 
97
    def _set_value ( self, value ):
 
98
        self._value = value
 
99
        if isinstance(value, basestring):
 
100
            try:
 
101
                self._value = int( value )
 
102
            except:
 
103
                try:
 
104
                    self._value = float( value )
 
105
                except:
 
106
                    pass
 
107
 
 
108
    #---------------------------------------------------------------------------
 
109
    #  Initializes the object:
 
110
    #---------------------------------------------------------------------------
 
111
 
 
112
    def __init__ ( self, **traits ):
 
113
        self._value = 0
 
114
        super( ToolkitEditorFactory, self ).__init__( **traits )
 
115
 
 
116
 
 
117
# Define the ButtonEditor class
 
118
ButtonEditor = ToolkitEditorFactory
 
119
 
 
120
### EOF ---------------------------------------------------------------------